1
2
3
4 from unittest import TestCase
5
6 from ctypes import Structure, c_int, c_float, c_double, c_void_p
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
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
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
37 scu = self.scu
38 self.assertTrue(scu.devprop.major >= 2)
39
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
49 arra = arange(nelm, dtype='float32')
50 arrb = -arra
51 arrc = empty(nelm, dtype='float32')
52 arrc.fill(2)
53
54 gmema = scu.alloc(arra.nbytes)
55 gmemb = scu.alloc(arrb.nbytes)
56 gmemc = scu.alloc(arrc.nbytes)
57
58 scu.memcpy(gmema, arra)
59 scu.memcpy(gmemb, arrb)
60
61 lib.vecadd_float(gmema.gptr, gmemb.gptr, gmemc.gptr, nelm)
62
63 self.assertTrue((arrc == 2.0).all())
64 scu.memcpy(arrc, gmemc)
65 self.assertTrue((arrc == 0.0).all())
66
67 scu.free(gmemc)
68 scu.free(gmemb)
69 scu.free(gmema)
70
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
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
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
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
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
108 scu.free(garra)
109 scu.free(garrb)
110 scu.free(garrc)
111 scu.free(gp)
112
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
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
131 from numpy import empty, arange
132 scu = self.scu
133 lib = self.lib
134 nelm = 1024
135
136 arra = arange(nelm, dtype='float32')
137 arrb = -arra
138 arrc = empty(nelm, dtype='float32')
139 arrc.fill(2)
140
141 gmema = scu.alloc(arra.nbytes)
142 gmemb = scu.alloc(arrb.nbytes)
143 gmemc = scu.alloc(arrc.nbytes)
144
145 scu.memcpy(gmema, arra)
146 scu.memcpy(gmemb, arrb)
147
148 lib.vecadd_float(gmema.gptr, gmemb.gptr, gmemc.gptr, nelm)
149
150 self.assertTrue((arrc == 2.0).all())
151 scu.memcpy(arrc, gmemc)
152 self.assertTrue((arrc == 0.0).all())
153
154 scu.free(gmemc)
155 scu.free(gmemb)
156 scu.free(gmema)
157
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
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
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
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
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
195 scu.free(garra)
196 scu.free(garrb)
197 scu.free(garrc)
198 scu.free(gp)
199