Skip to content

Commit 6261524

Browse files
committed
TST: add multithreaded ufunc execution test
1 parent 2fd11c0 commit 6261524

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

numpy/_core/tests/test_multithreading.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99
pytest.skip(allow_module_level=True, reason="no threading support in wasm")
1010

1111

12-
def test_parallel_errstate_creation():
12+
def run_threaded(func, iters, pass_count=False):
13+
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as tpe:
14+
if pass_count:
15+
futures = [tpe.submit(func, i) for i in range(iters)]
16+
else:
17+
futures = [tpe.submit(func) for _ in range(iters)]
18+
for f in futures:
19+
f.result()
20+
21+
22+
def test_parallel_randomstate_creation():
1323
# if the coercion cache is enabled and not thread-safe, creating
1424
# RandomState instances simultaneously leads to a data race
1525
def func(seed):
1626
np.random.RandomState(seed)
1727

18-
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as tpe:
19-
futures = [tpe.submit(func, i) for i in range(500)]
20-
for f in futures:
21-
f.result()
28+
run_threaded(func, 500, pass_count=True)
29+
30+
def test_parallel_ufunc_execution():
31+
# if the loop data cache or dispatch cache are not thread-safe
32+
# computing ufuncs simultaneously in multiple threads leads
33+
# to a data race
34+
def func():
35+
arr = np.random.random((25,))
36+
np.isnan(arr)
37+
38+
run_threaded(func, 500)

0 commit comments

Comments
 (0)