Package solvcon :: Package tests :: Module test_scuda
[hide private]
[frames] | no frames]

Source Code for Module solvcon.tests.test_scuda

  1  # -*- coding: UTF-8 -*- 
  2  # Copyright (C) 2008-2011 by Yung-Yu Chen.  See LICENSE.txt for terms of usage. 
  3   
  4  from unittest import TestCase 
  5   
  6  from ctypes import Structure, c_int, c_float, c_double, c_void_p 
7 -class Custom(Structure):
8 _fields_ = [ 9 ('nelm', c_int), 10 ('dval', c_double), 11 ('arra', c_void_p), 12 ('arrb', c_void_p), 13 ('arrc', c_void_p), 14 ]
15
16 -class TestScuda(TestCase):
17 - def setUp(self):
18 import sys, os 19 from nose.plugins.skip import SkipTest 20 from ..conf import env 21 if sys.platform.startswith('win'): raise SkipTest 22 if env.scu is None: raise SkipTest 23 self.scu = env.scu
24
25 -class TestScuda20(TestScuda):
26 - def setUp(self):
27 super(TestScuda20, self).setUp() 28 import os 29 from ctypes import CDLL 30 from nose.plugins.skip import SkipTest 31 from ..conf import env 32 if not self.scu.devprop.has_compute_capability('2.0'): raise SkipTest 33 libpath = os.path.join(env.libdir, 'libsc_cuda20test.so') 34 self.lib = CDLL(libpath)
35
36 - def test_properties(self):
37 scu = self.scu 38 self.assertTrue(scu.devprop.major >= 2)
39
40 - def test_vecadd_float(self):
41 from ctypes import (byref, c_size_t, c_void_p, c_int, 42 sizeof, create_string_buffer, memmove) 43 from numpy import empty, arange 44 from solvcon.scuda import CudaDim3 45 scu = self.scu 46 lib = self.lib 47 nelm = 1024 48 # allocate on CPU. 49 arra = arange(nelm, dtype='float32') 50 arrb = -arra 51 arrc = empty(nelm, dtype='float32') 52 arrc.fill(2) 53 # allocate on GPU. 54 gmema = scu.alloc(arra.nbytes) 55 gmemb = scu.alloc(arrb.nbytes) 56 gmemc = scu.alloc(arrc.nbytes) 57 # copy from host to device. 58 scu.memcpy(gmema, arra) 59 scu.memcpy(gmemb, arrb) 60 # invoke kernel. 61 lib.vecadd_float(gmema.gptr, gmemb.gptr, gmemc.gptr, nelm) 62 # copy from device to host. 63 self.assertTrue((arrc == 2.0).all()) 64 scu.memcpy(arrc, gmemc) 65 self.assertTrue((arrc == 0.0).all()) 66 # deallocate on GPU. 67 scu.free(gmemc) 68 scu.free(gmemb) 69 scu.free(gmema)
70
71 - def test_structop(self):
72 from ctypes import c_void_p, sizeof, byref 73 from numpy import empty, arange 74 scu = self.scu 75 lib = self.lib 76 nelm = 337 77 nthread = 32 78 # CPU data. 79 ctm = Custom(nelm=nelm, dval=4.0) 80 arra = arange(ctm.nelm, dtype='float64') 81 ctm.arra = arra.ctypes._as_parameter_ 82 arrb = arange(ctm.nelm, dtype='float64') 83 ctm.arrb = arrb.ctypes._as_parameter_ 84 arrc = empty(ctm.nelm, dtype='float64') 85 ctm.arrc = arrc.ctypes._as_parameter_ 86 # GPU data. 87 gtm = Custom(nelm=ctm.nelm, dval=ctm.dval) 88 garra = scu.alloc(arra.nbytes) 89 scu.memcpy(garra, arra) 90 gtm.arra = garra.gptr 91 garrb = scu.alloc(arrb.nbytes) 92 scu.memcpy(garrb, arrb) 93 gtm.arrb = garrb.gptr 94 garrc = scu.alloc(arrc.nbytes) 95 scu.memcpy(garrc, arrc) 96 gtm.arrc = garrc.gptr 97 # operate. 98 gp = scu.alloc(sizeof(gtm)) 99 scu.cudaMemcpy(gp.gptr, byref(gtm), sizeof(gtm), 100 scu.cudaMemcpyHostToDevice) 101 lib.structop(nthread, byref(ctm), gp.gptr) 102 # get back and compare. 103 rarrc = empty(ctm.nelm, dtype='float64') 104 rarrc.fill(1000) 105 scu.memcpy(rarrc, garrc) 106 self.assertTrue((rarrc == arra+arrb+ctm.dval).all()) 107 # free. 108 scu.free(garra) 109 scu.free(garrb) 110 scu.free(garrc) 111 scu.free(gp)
112
113 -class TestScuda13(TestScuda):
114 - def setUp(self):
115 super(TestScuda13, self).setUp() 116 import os 117 from ctypes import CDLL 118 from nose.plugins.skip import SkipTest 119 from ..conf import env 120 if not self.scu.devprop.has_compute_capability('1.3'): raise SkipTest 121 libpath = os.path.join(env.libdir, 'libsc_cuda13test.so') 122 self.lib = CDLL(libpath)
123
124 - def test_properties(self):
125 scu = self.scu 126 self.assertTrue(scu.devprop.major >= 1) 127 if scu.devprop.major == 1: 128 self.assertTrue(scu.devprop.minor >= 3)
129
130 - def test_vecadd_float(self):
131 from numpy import empty, arange 132 scu = self.scu 133 lib = self.lib 134 nelm = 1024 135 # allocate on CPU. 136 arra = arange(nelm, dtype='float32') 137 arrb = -arra 138 arrc = empty(nelm, dtype='float32') 139 arrc.fill(2) 140 # allocate on GPU. 141 gmema = scu.alloc(arra.nbytes) 142 gmemb = scu.alloc(arrb.nbytes) 143 gmemc = scu.alloc(arrc.nbytes) 144 # copy from host to device. 145 scu.memcpy(gmema, arra) 146 scu.memcpy(gmemb, arrb) 147 # invoke kernel. 148 lib.vecadd_float(gmema.gptr, gmemb.gptr, gmemc.gptr, nelm) 149 # copy from device to host. 150 self.assertTrue((arrc == 2.0).all()) 151 scu.memcpy(arrc, gmemc) 152 self.assertTrue((arrc == 0.0).all()) 153 # deallocate on GPU. 154 scu.free(gmemc) 155 scu.free(gmemb) 156 scu.free(gmema)
157
158 - def test_structop(self):
159 from ctypes import c_void_p, sizeof, byref 160 from numpy import empty, arange 161 scu = self.scu 162 lib = self.lib 163 nelm = 337 164 nthread = 32 165 # CPU data. 166 ctm = Custom(nelm=nelm, dval=4.0) 167 arra = arange(ctm.nelm, dtype='float64') 168 ctm.arra = arra.ctypes._as_parameter_ 169 arrb = arange(ctm.nelm, dtype='float64') 170 ctm.arrb = arrb.ctypes._as_parameter_ 171 arrc = empty(ctm.nelm, dtype='float64') 172 ctm.arrc = arrc.ctypes._as_parameter_ 173 # GPU data. 174 gtm = Custom(nelm=ctm.nelm, dval=ctm.dval) 175 garra = scu.alloc(arra.nbytes) 176 scu.memcpy(garra, arra) 177 gtm.arra = garra.gptr 178 garrb = scu.alloc(arrb.nbytes) 179 scu.memcpy(garrb, arrb) 180 gtm.arrb = garrb.gptr 181 garrc = scu.alloc(arrc.nbytes) 182 scu.memcpy(garrc, arrc) 183 gtm.arrc = garrc.gptr 184 # operate. 185 gp = scu.alloc(sizeof(gtm)) 186 scu.cudaMemcpy(gp.gptr, byref(gtm), sizeof(gtm), 187 scu.cudaMemcpyHostToDevice) 188 lib.structop(nthread, byref(ctm), gp.gptr) 189 # get back and compare. 190 rarrc = empty(ctm.nelm, dtype='float64') 191 rarrc.fill(1000) 192 scu.memcpy(rarrc, garrc) 193 self.assertTrue((rarrc == arra+arrb+ctm.dval).all()) 194 # free. 195 scu.free(garra) 196 scu.free(garrb) 197 scu.free(garrc) 198 scu.free(gp)
199