Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions numba/cuda/tests/cudapy/test_gufunc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import numpy.core.umath_tests as ut

from collections import namedtuple
from numba import void, float32, float64
from numba import guvectorize
from numba import cuda
Expand Down Expand Up @@ -225,6 +226,41 @@ def foo(inp, out):
msg = "cannot specify 'out' as both a positional and keyword argument"
self.assertEqual(str(raises.exception), msg)

def check_tuple_arg(self, a, b):
@guvectorize([(float64[:], float64[:], float64[:])], '(n),(n)->()',
>
def gu_reduce(x, y, r):
s = 0
for i in range(len(x)):
s += x[i] * y[i]
r[0] = s

r = gu_reduce(a, b)
expected = np.sum(np.asarray(a) * np.asarray(b), axis=1)
np.testing.assert_equal(expected, r)

def test_tuple_of_tuple_arg(self):
a = ((1.0, 2.0, 3.0),
(4.0, 5.0, 6.0))
b = ((1.5, 2.5, 3.5),
(4.5, 5.5, 6.5))
self.check_tuple_arg(a, b)

def test_tuple_of_namedtuple_arg(self):
Point = namedtuple('Point', ('x', 'y', 'z'))
a = (Point(x=1.0, y=2.0, z=3.0),
Point(x=4.0, y=5.0, z=6.0))
b = (Point(x=1.5, y=2.5, z=3.5),
Point(x=4.5, y=5.5, z=6.5))
self.check_tuple_arg(a, b)

def test_tuple_of_array_arg(self):
a = (np.asarray((1.0, 2.0, 3.0)),
np.asarray((4.0, 5.0, 6.0)))
b = (np.asarray((1.5, 2.5, 3.5)),
np.asarray((4.5, 5.5, 6.5)))
self.check_tuple_arg(a, b)


if __name__ == '__main__':
unittest.main()
42 changes: 42 additions & 0 deletions numba/cuda/tests/cudapy/test_vectorize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

from collections import namedtuple
from numba import vectorize
from numba import cuda, int32, float32, float64
from numba.cuda.testing import skip_on_cudasim
Expand Down Expand Up @@ -179,6 +180,47 @@ def vector_add(a, b):
expect = x + x
np.testing.assert_equal(got, expect)

def check_tuple_arg(self, a, b):
@vectorize(sig,>
def vector_add(a, b):
return a + b

r = vector_add(a, b)
np.testing.assert_equal(np.asarray(a) + np.asarray(b), r)

def test_tuple_arg(self):
a = (1.0, 2.0, 3.0)
b = (4.0, 5.0, 6.0)
self.check_tuple_arg(a, b)

def test_namedtuple_arg(self):
Point = namedtuple('Point', ('x', 'y', 'z'))
a = Point(x=1.0, y=2.0, z=3.0)
b = Point(x=4.0, y=5.0, z=6.0)
self.check_tuple_arg(a, b)

def test_tuple_of_array_arg(self):
arr = np.arange(10, dtype=np.int32)
a = (arr, arr + 1)
b = (arr + 2, arr + 2)
self.check_tuple_arg(a, b)

def test_tuple_of_namedtuple_arg(self):
Point = namedtuple('Point', ('x', 'y', 'z'))
a = (Point(x=1.0, y=2.0, z=3.0), Point(x=1.5, y=2.5, z=3.5))
b = (Point(x=4.0, y=5.0, z=6.0), Point(x=4.5, y=5.5, z=6.5))
self.check_tuple_arg(a, b)

def test_namedtuple_of_array_arg(self):
xs1 = np.arange(10, dtype=np.int32)
ys1 = xs1 + 2
xs2 = np.arange(10, dtype=np.int32) * 2
ys2 = xs2 + 1
Points = namedtuple('Points', ('xs', 'ys'))
a = Points(xs=xs1, ys=ys1)
b = Points(xs=xs2, ys=ys2)
self.check_tuple_arg(a, b)


if __name__ == '__main__':
unittest.main()
13 changes: 7 additions & 6 deletions numba/np/ufunc/deviceufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,25 @@ def _fill_arrays(self):
Get all arguments in array form
"""
for i, arg in enumerate(self.args):
if isinstance(arg, np.ndarray):
self.arrays[i] = arg
elif self.is_device_array(arg):
if self.is_device_array(arg):
self.arrays[i] = self.as_device_array(arg)
elif isinstance(arg, (int, float, complex, np.number)):
# Is scalar
self.scalarpos.append(i)
else:
raise TypeError("argument #%d has invalid type of %s" \
% (i + 1, type(arg) ))
array = np.asarray(arg)
if array.dtype == np.object:
fmt = "argument #%d has invalid type of %s"
raise TypeError(fmt % (i + 1, type(arg) ))
Comment thread
stuartarchibald marked this conversation as resolved.
Resolved
self.arrays[i] = array

def _fill_argtypes(self):
"""
Get dtypes
"""
for i, ary in enumerate(self.arrays):
if ary is not None:
self.argtypes[i] = ary.dtype
self.argtypes[i] = np.asarray(ary).dtype

def _resolve_signature(self):
"""Resolve signature.
Expand Down