Skip to content

Commit

Permalink
Add tests from user cases for parfors tuple handling
Browse files Browse the repository at this point in the history
As title.
  • Loading branch information
stuartarchibald committed Mar 26, 2020
1 parent 7ef19c9 commit 1cc1a7d
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions numba/tests/test_parfors.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def copy_args(*args):
new_args.append(x.copy())
elif isinstance(x, numbers.Number):
new_args.append(x)
elif isinstance(x, tuple):
new_args.append(x)
elif isinstance(x, list):
new_args.append(x[:])
else:
Expand Down Expand Up @@ -1620,6 +1622,7 @@ def test_impl(a):
x = np.arange(10)
self.check(test_impl, x)


class TestParforsLeaks(MemoryLeakMixin, TestParforsBase):
def check(self, pyfunc, *args, **kwargs):
cfunc, cpfunc = self.compile_all(pyfunc, *args)
Expand Down Expand Up @@ -3290,7 +3293,9 @@ def test1(d):
# original state
numba.parfors.parfor.sequential_parfor_lowering = save_state

@skip_parfors_unsupported
def test_oversized_tuple_as_arg_to_kernel(self):

@njit(parallel=True)
def oversize_tuple():
big_tup = (1,2,3,4)
Expand All @@ -3307,6 +3312,124 @@ def oversize_tuple():
self.assertIn("Use of a tuple", errstr)
self.assertIn("in a parallel region", errstr)

@skip_parfors_unsupported
def test_issue5167(self):

def ndvi_njit(img_nir, img_red):
fillvalue = 0
out_img = np.full(img_nir.shape, fillvalue, dtype=img_nir.dtype)
dims = img_nir.shape
for y in prange(dims[0]):
for x in prange(dims[1]):
out_img[y, x] = ((img_nir[y, x] - img_red[y, x]) /
(img_nir[y, x] + img_red[y, x]))
return out_img

tile_shape = (4, 4)
array1 = np.random.default_rng().uniform(low=0.0, high=10000.0,
size=tile_shape)
array2 = np.random.default_rng().uniform(low=0.0, high=10000.0,
size=tile_shape)
self.check(ndvi_njit, array1, array2)

@skip_parfors_unsupported
def test_issue5065(self):

def reproducer(a, dist, dist_args):
result = np.zeros((a.shape[0], a.shape[0]), dtype=np.float32)
for i in prange(a.shape[0]):
for j in range(i + 1, a.shape[0]):
d = dist(a[i], a[j], *dist_args)
result[i, j] = d
result[j, i] = d
return result

@njit
def euclidean(x, y):
result = 0.0
for i in range(x.shape[0]):
result += (x[i] - y[i]) ** 2
return np.sqrt(result)

a = np.random.random(size=(5, 2))

got = njit(parallel=True)(reproducer)(a.copy(), euclidean,())
expected = reproducer(a.copy(), euclidean,())

np.testing.assert_allclose(got, expected)

@skip_parfors_unsupported
def test_issue5001(self):

def test_numba_parallel(myarray):
result = [0] * len(myarray)
for i in prange(len(myarray)):
result[i] = len(myarray[i])
return result

myarray = (np.empty(100),np.empty(50))
self.check(test_numba_parallel, myarray)

@skip_parfors_unsupported
def test_issue3169(self):

@njit
def foo(grids):
pass

@njit(parallel=True)
def bar(grids):
for x in prange(1):
foo(grids)

# returns nothing, just check it compiles
bar(([1],) * 2)

@skip_parfors_unsupported
def test_issue4846(self):

mytype = namedtuple("mytype", ("a", "b"))

def outer(mydata):
for k in prange(3):
inner(k, mydata)
return mydata.a

@njit(nogil=True)
def inner(k, mydata):
f = (k, mydata.a)
g = (k, mydata.b)

mydata = mytype(a="a", b="b")

self.check(outer, mydata)

@skip_parfors_unsupported
def test_issue3748(self):

def test1b():
x = (1, 2, 3, 4, 5)
a = 0
for i in prange(len(x)):
a += x[i]
return a

self.check(test1b,)

@skip_parfors_unsupported
def test_issue5277(self):

def parallel_test(size, arr):
for x in prange(size[0]):
for y in prange(size[1]):
arr[y][x] = x * 4.5 + y
return arr

size = (10, 10)
arr = np.zeros(size, dtype=int)

self.check(parallel_test, size, arr)


@skip_parfors_unsupported
class TestParforsDiagnostics(TestParforsBase):
Expand Down

0 comments on commit 1cc1a7d

Please sign in to comment.