|
| 1 | +# This file includes tests from scipy.fft module: |
| 2 | +# https://github.com/scipy/scipy/blob/main/scipy/fft/tests/test_multithreading.py.py |
| 3 | + |
| 4 | +import multiprocessing |
| 5 | +import os |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | +from numpy.testing import assert_allclose |
| 10 | + |
| 11 | +import mkl_fft.interfaces.scipy_fft as fft |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module") |
| 15 | +def x(): |
| 16 | + return np.random.randn(512, 128) # Must be large enough to qualify for mt |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "func", |
| 21 | + [ |
| 22 | + fft.fft, |
| 23 | + fft.ifft, |
| 24 | + fft.fft2, |
| 25 | + fft.ifft2, |
| 26 | + fft.fftn, |
| 27 | + fft.ifftn, |
| 28 | + fft.rfft, |
| 29 | + fft.irfft, |
| 30 | + fft.rfft2, |
| 31 | + fft.irfft2, |
| 32 | + fft.rfftn, |
| 33 | + fft.irfftn, |
| 34 | + # TODO: fft.hfft, fft.ihfft, fft.hfft2, fft.ihfft2, fft.hfftn, fft.ihfftn, |
| 35 | + # TODO: fft.dct, fft.idct, fft.dctn, fft.idctn, |
| 36 | + # TODO: fft.dst, fft.idst, fft.dstn, fft.idstn, |
| 37 | + ], |
| 38 | +) |
| 39 | +@pytest.mark.parametrize("workers", [2, -1]) |
| 40 | +def test_threaded_same(x, func, workers): |
| 41 | + expected = func(x, workers=1) |
| 42 | + actual = func(x, workers=workers) |
| 43 | + assert_allclose(actual, expected) |
| 44 | + |
| 45 | + |
| 46 | +def _mt_fft(x): |
| 47 | + return fft.fft(x, workers=2) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.slow |
| 51 | +def test_mixed_threads_processes(x): |
| 52 | + # Test that the fft threadpool is safe to use before & after fork |
| 53 | + |
| 54 | + expect = fft.fft(x, workers=2) |
| 55 | + |
| 56 | + with multiprocessing.Pool(2) as p: |
| 57 | + res = p.map(_mt_fft, [x for _ in range(4)]) |
| 58 | + |
| 59 | + for r in res: |
| 60 | + assert_allclose(r, expect) |
| 61 | + |
| 62 | + fft.fft(x, workers=2) |
| 63 | + |
| 64 | + |
| 65 | +def test_invalid_workers(x): |
| 66 | + cpus = os.cpu_count() |
| 67 | + |
| 68 | + fft.ifft([1], workers=-cpus) |
| 69 | + |
| 70 | + with pytest.raises(ValueError, match="workers must not be zero"): |
| 71 | + fft.fft(x, workers=0) |
| 72 | + |
| 73 | + with pytest.raises(ValueError, match="workers value out of range"): |
| 74 | + fft.ifft(x, workers=-cpus - 1) |
| 75 | + |
| 76 | + |
| 77 | +def test_set_get_workers(): |
| 78 | + cpus = os.cpu_count() |
| 79 | + # scipy default is 1 but mkl_fft default is max number of threads |
| 80 | + assert fft.get_workers() == cpus |
| 81 | + with fft.set_workers(4): |
| 82 | + assert fft.get_workers() == 4 |
| 83 | + |
| 84 | + with fft.set_workers(-1): |
| 85 | + assert fft.get_workers() == cpus |
| 86 | + |
| 87 | + assert fft.get_workers() == 4 |
| 88 | + |
| 89 | + # scipy default is 1 but mkl_fft default is max number of threads |
| 90 | + assert fft.get_workers() == cpus |
| 91 | + |
| 92 | + with fft.set_workers(-cpus): |
| 93 | + assert fft.get_workers() == 1 |
| 94 | + |
| 95 | + |
| 96 | +def test_set_workers_invalid(): |
| 97 | + |
| 98 | + with pytest.raises(ValueError): # , match='workers must not be zero'): |
| 99 | + with fft.set_workers(0): |
| 100 | + pass |
| 101 | + |
| 102 | + with pytest.raises(ValueError): # , match='workers value out of range'): |
| 103 | + with fft.set_workers(-os.cpu_count() - 1): |
| 104 | + pass |
0 commit comments