test_multithreading.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from scipy import fft
  2. import numpy as np
  3. import pytest
  4. from numpy.testing import assert_allclose
  5. import multiprocessing
  6. import os
  7. @pytest.fixture(scope='module')
  8. def x():
  9. return np.random.randn(512, 128) # Must be large enough to qualify for mt
  10. @pytest.mark.parametrize("func", [
  11. fft.fft, fft.ifft, fft.fft2, fft.ifft2, fft.fftn, fft.ifftn,
  12. fft.rfft, fft.irfft, fft.rfft2, fft.irfft2, fft.rfftn, fft.irfftn,
  13. fft.hfft, fft.ihfft, fft.hfft2, fft.ihfft2, fft.hfftn, fft.ihfftn,
  14. fft.dct, fft.idct, fft.dctn, fft.idctn,
  15. fft.dst, fft.idst, fft.dstn, fft.idstn,
  16. ])
  17. @pytest.mark.parametrize("workers", [2, -1])
  18. def test_threaded_same(x, func, workers):
  19. expected = func(x, workers=1)
  20. actual = func(x, workers=workers)
  21. assert_allclose(actual, expected)
  22. def _mt_fft(x):
  23. return fft.fft(x, workers=2)
  24. def test_mixed_threads_processes(x):
  25. # Test that the fft threadpool is safe to use before & after fork
  26. expect = fft.fft(x, workers=2)
  27. with multiprocessing.Pool(2) as p:
  28. res = p.map(_mt_fft, [x for _ in range(4)])
  29. for r in res:
  30. assert_allclose(r, expect)
  31. fft.fft(x, workers=2)
  32. def test_invalid_workers(x):
  33. cpus = os.cpu_count()
  34. fft.ifft([1], workers=-cpus)
  35. with pytest.raises(ValueError, match='workers must not be zero'):
  36. fft.fft(x, workers=0)
  37. with pytest.raises(ValueError, match='workers value out of range'):
  38. fft.ifft(x, workers=-cpus-1)
  39. def test_set_get_workers():
  40. cpus = os.cpu_count()
  41. assert fft.get_workers() == 1
  42. with fft.set_workers(4):
  43. assert fft.get_workers() == 4
  44. with fft.set_workers(-1):
  45. assert fft.get_workers() == cpus
  46. assert fft.get_workers() == 4
  47. assert fft.get_workers() == 1
  48. with fft.set_workers(-cpus):
  49. assert fft.get_workers() == 1
  50. def test_set_workers_invalid():
  51. with pytest.raises(ValueError, match='workers must not be zero'):
  52. with fft.set_workers(0):
  53. pass
  54. with pytest.raises(ValueError, match='workers value out of range'):
  55. with fft.set_workers(-os.cpu_count()-1):
  56. pass