Newer
Older
#! /usr/bin/env python
import numpy
import numpy.linalg as la
import sys
import pytools.test
def have_cl():
try:
import pyopencl
return True
except:
return False
if have_cl():
import pyopencl.array as cl_array
import pyopencl as cl
from pyopencl.tools import pytest_generate_tests_for_pyopencl \
as pytest_generate_tests
from pyopencl.tools import has_double_support
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
@pytools.test.mark_test.opencl
def test_pow_array(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
result = pow(a_gpu,a_gpu).get()
assert (numpy.abs(a**a - result) < 1e-3).all()
result = (a_gpu**a_gpu).get()
assert (numpy.abs(pow(a, a) - result) < 1e-3).all()
@pytools.test.mark_test.opencl
def test_pow_number(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
result = pow(a_gpu, 2).get()
assert (numpy.abs(a**2 - result) < 1e-3).all()
@pytools.test.mark_test.opencl
def test_abs(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
a = -cl_array.arange(context, queue, 111, dtype=numpy.float32)
res = a.get()
for i in range(111):
assert res[i] <= 0
a = abs(a)
res = a.get()
for i in range (111):
assert abs(res[i]) >= 0
assert res[i] == i
@pytools.test.mark_test.opencl
def test_len(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_cpu = cl_array.to_device(context, queue, a)
assert len(a_cpu) == 10
@pytools.test.mark_test.opencl
def test_multiply(ctx_getter):
"""Test the muliplication of an array with a scalar. """
context = ctx_getter()
queue = cl.CommandQueue(context)
for sz in [10, 50000]:
for dtype, scalars in [
(numpy.float32, [2]),
#(numpy.complex64, [2, 2j])
]:
for scalar in scalars:
a = numpy.arange(sz).astype(dtype)
a_gpu = cl_array.to_device(context, queue, a)
a_doubled = (scalar * a_gpu).get()
assert (a * scalar == a_doubled).all()
@pytools.test.mark_test.opencl
def test_multiply_array(ctx_getter):
"""Test the multiplication of two arrays."""
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
b_gpu = cl_array.to_device(context, queue, a)
a_squared = (b_gpu*a_gpu).get()
assert (a*a == a_squared).all()
@pytools.test.mark_test.opencl
def test_addition_array(ctx_getter):
"""Test the addition of two arrays."""
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
a_added = (a_gpu+a_gpu).get()
assert (a+a == a_added).all()
@pytools.test.mark_test.opencl
def test_addition_scalar(ctx_getter):
"""Test the addition of an array and a scalar."""
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
a_added = (7+a_gpu).get()
assert (7+a == a_added).all()
@pytools.test.mark_test.opencl
def test_substract_array(ctx_getter):
"""Test the substraction of two arrays."""
#test data
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
b = numpy.array([10,20,30,40,50,60,70,80,90,100]).astype(numpy.float32)
context = ctx_getter()
queue = cl.CommandQueue(context)
a_gpu = cl_array.to_device(context, queue, a)
b_gpu = cl_array.to_device(context, queue, b)
result = (a_gpu-b_gpu).get()
assert (a-b == result).all()
result = (b_gpu-a_gpu).get()
assert (b-a == result).all()
@pytools.test.mark_test.opencl
def test_substract_scalar(ctx_getter):
"""Test the substraction of an array and a scalar."""
context = ctx_getter()
queue = cl.CommandQueue(context)
#test data
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
#convert a to a gpu object
a_gpu = cl_array.to_device(context, queue, a)
result = (a_gpu-7).get()
assert (a-7 == result).all()
result = (7-a_gpu).get()
assert (7-a == result).all()
@pytools.test.mark_test.opencl
def test_divide_scalar(ctx_getter):
"""Test the division of an array and a scalar."""
context = ctx_getter()
queue = cl.CommandQueue(context)
a = numpy.array([1,2,3,4,5,6,7,8,9,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
result = (a_gpu/2).get()
assert (a/2 == result).all()
result = (2/a_gpu).get()
assert (2/a == result).all()
@pytools.test.mark_test.opencl
def test_divide_array(ctx_getter):
"""Test the division of an array and a scalar. """
context = ctx_getter()
queue = cl.CommandQueue(context)
#test data
a = numpy.array([10,20,30,40,50,60,70,80,90,100]).astype(numpy.float32)
b = numpy.array([10,10,10,10,10,10,10,10,10,10]).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
b_gpu = cl_array.to_device(context, queue, b)
a_divide = (a_gpu/b_gpu).get()
assert (numpy.abs(a/b - a_divide) < 1e-3).all()
a_divide = (b_gpu/a_gpu).get()
assert (numpy.abs(b/a - a_divide) < 1e-3).all()
@pytools.test.mark_test.opencl
def test_random(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
from pyopencl.clrandom import rand as clrand
if has_double_support(context.devices[0]):
dtypes = [numpy.float32, numpy.float64]
else:
dtypes = [numpy.float32]
for dtype in dtypes:
a = clrand(context, queue, (10, 100), dtype=dtype).get()
assert (0 <= a).all()
assert (a < 1).all()
@pytools.test.mark_test.opencl
def test_nan_arithmetic(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
def make_nan_contaminated_vector(size):
shape = (size,)
a = numpy.random.randn(*shape).astype(numpy.float32)
#for i in range(0, shape[0], 3):
#a[i] = float('nan')
from random import randrange
for i in range(size//10):
a[randrange(0, size)] = float('nan')
return a
size = 1 << 20
a = make_nan_contaminated_vector(size)
a_gpu = cl_array.to_device(context, queue, a)
b = make_nan_contaminated_vector(size)
b_gpu = cl_array.to_device(context, queue, b)
ab = a*b
ab_gpu = (a_gpu*b_gpu).get()
for i in range(size):
assert numpy.isnan(ab[i]) == numpy.isnan(ab_gpu[i])
@pytools.test.mark_test.opencl
def test_elwise_kernel(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
from pyopencl.clrandom import rand as clrand
a_gpu = clrand(context, queue, (50,), numpy.float32)
b_gpu = clrand(context, queue, (50,), numpy.float32)
from pyopencl.elementwise import ElementwiseKernel
lin_comb = ElementwiseKernel(context,
"float a, float *x, float b, float *y, float *z",
"z[i] = a*x[i] + b*y[i]",
"linear_combination")
c_gpu = cl_array.empty_like(a_gpu)
lin_comb(5, a_gpu, 6, b_gpu, c_gpu)
assert la.norm((c_gpu - (5*a_gpu+6*b_gpu)).get()) < 1e-5
@pytools.test.mark_test.opencl
def test_take(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
idx = cl_array.arange(context, queue, 0, 200000, 2, dtype=numpy.uint32)
a = cl_array.arange(context, queue, 0, 600000, 3, dtype=numpy.float32)
result = cl_array.take(a, idx)
assert ((3*idx).get() == result.get()).all()
@pytools.test.mark_test.opencl
def test_arange(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
n = 5000
a = cl_array.arange(context, queue, n, dtype=numpy.float32)
assert (numpy.arange(n, dtype=numpy.float32) == a.get()).all()
@pytools.test.mark_test.opencl
def test_reverse(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
n = 5000
a = numpy.arange(n).astype(numpy.float32)
a_gpu = cl_array.to_device(context, queue, a)
a_gpu = a_gpu.reverse()
assert (a[::-1] == a_gpu.get()).all()
@pytools.test.mark_test.opencl
def test_sum(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
a_gpu = clrand(context, queue, (200000,), numpy.float32)
a = a_gpu.get()
sum_a = numpy.sum(a)
sum_a_gpu = cl_array.sum(a_gpu).get()
@pytools.test.mark_test.opencl
def test_minmax(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
if has_double_support(context.devices[0]):
dtypes = [numpy.float64, numpy.float32, numpy.int32]
else:
dtypes = [numpy.float32, numpy.int32]
for what in ["min", "max"]:
for dtype in dtypes:
a_gpu = clrand(context, queue, (200000,), dtype)
a = a_gpu.get()
op_a = getattr(numpy, what)(a)
op_a_gpu = getattr(cl_array, what)(a_gpu).get()
assert op_a_gpu == op_a, (op_a_gpu, op_a, dtype, what)
@pytools.test.mark_test.opencl
def test_subset_minmax(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
l_a = 200000
gran = 5
l_m = l_a - l_a // gran + 1
if has_double_support(context.devices[0]):
dtypes = [numpy.float64, numpy.float32, numpy.int32]
else:
dtypes = [numpy.float32, numpy.int32]
for dtype in dtypes:
a_gpu = clrand(context, queue, (l_a,), dtype)
meaningful_indices_gpu = cl_array.zeros(
context, queue, l_m, dtype=numpy.int32)
meaningful_indices = meaningful_indices_gpu.get()
j = 0
for i in range(len(meaningful_indices)):
meaningful_indices[i] = j
j = j + 1
if j % gran == 0:
j = j + 1
meaningful_indices_gpu = cl_array.to_device(
context, queue, meaningful_indices)
b = a[meaningful_indices]
min_a = numpy.min(b)
min_a_gpu = cl_array.subset_min(meaningful_indices_gpu, a_gpu).get()
assert min_a_gpu == min_a
@pytools.test.mark_test.opencl
def test_dot(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
from pyopencl.clrandom import rand as clrand
a_gpu = clrand(context, queue, (200000,), numpy.float32)
a = a_gpu.get()
b_gpu = clrand(context, queue, (200000,), numpy.float32)
b = b_gpu.get()
dot_ab = numpy.dot(a, b)
dot_ab_gpu = cl_array.dot(a_gpu, b_gpu).get()
assert abs(dot_ab_gpu-dot_ab)/abs(dot_ab) < 1e-4
if False:
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
@pytools.test.mark_test.opencl
def test_slice(ctx_getter):
from pyopencl.clrandom import rand as clrand
l = 20000
a_gpu = clrand(context, queue, (l,))
a = a_gpu.get()
from random import randrange
for i in range(200):
start = randrange(l)
end = randrange(start, l)
a_gpu_slice = a_gpu[start:end]
a_slice = a[start:end]
assert la.norm(a_gpu_slice.get()-a_slice) == 0
@pytools.test.mark_test.opencl
def test_if_positive(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
from pyopencl.clrandom import rand as clrand
l = 20000
a_gpu = clrand(context, queue, (l,), numpy.float32)
b_gpu = clrand(context, queue, (l,), numpy.float32)
a = a_gpu.get()
b = b_gpu.get()
max_a_b_gpu = cl_array.maximum(a_gpu, b_gpu)
min_a_b_gpu = cl_array.minimum(a_gpu, b_gpu)
print(max_a_b_gpu)
print(numpy.maximum(a, b))
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
assert la.norm(max_a_b_gpu.get()- numpy.maximum(a, b)) == 0
assert la.norm(min_a_b_gpu.get()- numpy.minimum(a, b)) == 0
@pytools.test.mark_test.opencl
def test_take_put(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
for n in [5, 17, 333]:
one_field_size = 8
buf_gpu = cl_array.zeros(context, queue,
n*one_field_size, dtype=numpy.float32)
dest_indices = cl_array.to_device(context, queue,
numpy.array([ 0, 1, 2, 3, 32, 33, 34, 35], dtype=numpy.uint32))
read_map = cl_array.to_device(context, queue,
numpy.array([7, 6, 5, 4, 3, 2, 1, 0], dtype=numpy.uint32))
cl_array.multi_take_put(
arrays=[buf_gpu for i in range(n)],
dest_indices=dest_indices,
src_indices=read_map,
src_offsets=[i*one_field_size for i in range(n)],
dest_shape=(96,))
@pytools.test.mark_test.opencl
def test_astype(ctx_getter):
context = ctx_getter()
queue = cl.CommandQueue(context)
from pyopencl.clrandom import rand as clrand
if not has_double_support(context.devices[0]):
return
a_gpu = clrand(context, queue, (2000,), dtype=numpy.float32)
a = a_gpu.get().astype(numpy.float64)
a2 = a_gpu.astype(numpy.float64).get()
assert a2.dtype == numpy.float64
assert la.norm(a - a2) == 0, (a, a2)
a_gpu = clrand(context, queue, (2000,), dtype=numpy.float64)
a = a_gpu.get().astype(numpy.float32)
a2 = a_gpu.astype(numpy.float32).get()
assert a2.dtype == numpy.float32
assert la.norm(a - a2)/la.norm(a) < 1e-7
if __name__ == "__main__":
# make sure that import failures get reported, instead of skipping the tests.
import pyopencl as cl
import sys
if len(sys.argv) > 1:
else:
from py.test.cmdline import main
main([__file__])