Skip to content

Commit

Permalink
TST: add multithreaded ufunc execution test
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Apr 25, 2024
1 parent 777c0da commit 68df7e4
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions numpy/_core/tests/test_multithreading.py
Expand Up @@ -9,13 +9,31 @@
pytest.skip(allow_module_level=True, reason="no threading support in wasm")


def test_parallel_errstate_creation():
def run_threaded(func, iters, pass_count=False):
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as tpe:
if pass_count:
futures = [tpe.submit(func, i) for i in range(iters)]
else:
futures = [tpe.submit(func) for _ in range(iters)]
for f in futures:
f.result()


def test_parallel_randomstate_creation():
# if the coercion cache is enabled and not thread-safe, creating
# RandomState instances simultaneously leads to a data race
def func(seed):
np.random.RandomState(seed)

with concurrent.futures.ThreadPoolExecutor(max_workers=8) as tpe:
futures = [tpe.submit(func, i) for i in range(500)]
for f in futures:
f.result()
run_threaded(func, 500, pass_count=True)


def test_parallel_ufunc_execution():
# if the loop data cache or dispatch cache are not thread-safe
# computing ufuncs simultaneously in multiple threads leads
# to a data race
def func():
arr = np.random.random((25,))
np.unique(arr)

run_threaded(func, 500)

0 comments on commit 68df7e4

Please sign in to comment.