-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygpu_binary_ufunc_test.py
48 lines (41 loc) · 1.35 KB
/
pygpu_binary_ufunc_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 15 00:20:55 2017
@author: hkohr
"""
import numpy as np
import pygpu
ctx = pygpu.init('cuda')
pygpu.set_default_context(ctx)
ufunc = 'logaddexp2'
dtype1 = 'float32'
dtype2 = 'float32'
def random_array(dtype):
dtype = np.dtype(dtype)
if dtype == bool:
return np.random.randint(low=0, high=2, size=10).astype(bool)
elif np.issubsctype(dtype, np.unsignedinteger):
return np.random.randint(low=0, high=10, size=10).astype(dtype)
elif np.issubsctype(dtype, np.signedinteger):
return np.random.randint(low=0, high=10, size=10).astype(dtype)
elif np.issubsctype(dtype, np.floating):
return np.random.uniform(low=-4, high=4, size=10).astype(dtype)
else:
raise ValueError('unable to handle dtype {}'.format(dtype))
x_npy = random_array(dtype1)
y_npy = random_array(dtype2)
x_pygpu = pygpu.array(x_npy, dtype=dtype1)
y_pygpu = pygpu.array(y_npy, dtype=dtype2)
ufunc_npy = getattr(np, ufunc)
ufunc_pygpu = getattr(pygpu.ufuncs, ufunc)
res_npy = ufunc_npy(x_npy, y_npy)
res_pygpu = ufunc_pygpu(x_pygpu, y_pygpu)
print('=== testing ufunc {} for dtypes {}, {} ==='
''.format(ufunc, dtype1, dtype2))
print('x =', x_npy)
print('y =', y_npy)
print('npy: {}(x, y) ='.format(ufunc))
print(res_npy)
print('pygpu: {}(x, y) ='.format(ufunc))
print(res_pygpu)