| 1 |
# |
| 2 |
# Copyright 2017 Knowledge Economy Developments Ltd |
| 3 |
# |
| 4 |
# Henry Gomersall |
| 5 |
# [email protected] |
| 6 |
# |
| 7 |
# All rights reserved. |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without |
| 10 |
# modification, are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# * Redistributions of source code must retain the above copyright notice, this |
| 13 |
# list of conditions and the following disclaimer. |
| 14 |
# |
| 15 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 16 |
# this list of conditions and the following disclaimer in the documentation |
| 17 |
# and/or other materials provided with the distribution. |
| 18 |
# |
| 19 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 20 |
# may be used to endorse or promote products derived from this software without |
| 21 |
# specific prior written permission. |
| 22 |
# |
| 23 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 27 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 |
# POSSIBILITY OF SUCH DAMAGE. |
| 34 |
# |
| 35 |
|
| 36 |
import copy |
| 37 |
import sys |
| 38 |
|
| 39 |
from pyfftw import interfaces, builders |
| 40 |
import pyfftw |
| 41 |
import numpy |
| 42 |
import numpy as np |
| 43 |
|
| 44 |
import unittest |
| 45 |
from .test_pyfftw_base import run_test_suites, miss |
| 46 |
from .test_pyfftw_numpy_interface import InterfacesNumpyFFTTestFFT |
| 47 |
|
| 48 |
import threading |
| 49 |
import time |
| 50 |
|
| 51 |
import os |
| 52 |
import hashlib |
| 53 |
|
| 54 |
'''Test the caching functionality of the interfaces package. |
| 55 |
''' |
| 56 |
|
| 57 |
def _check_n_cache_threads_running(): |
| 58 |
'''Return how many threads have the name 'PyFFTWCacheThread. |
| 59 |
|
| 60 |
Obviously, this isn't production quality, but it should suffice for |
| 61 |
the tests here. |
| 62 |
''' |
| 63 |
|
| 64 |
cache_threads = 0 |
| 65 |
for each_thread in threading.enumerate(): |
| 66 |
if each_thread.name == 'PyFFTWCacheThread': |
| 67 |
cache_threads += 1 |
| 68 |
|
| 69 |
return cache_threads |
| 70 |
|
| 71 |
@unittest.skipIf(*miss('64')) |
| 72 |
class InterfacesNumpyFFTCacheTestFFT(InterfacesNumpyFFTTestFFT): |
| 73 |
test_shapes = ( |
| 74 |
((100,), {}), |
| 75 |
((128, 64), {'axis': 0}), |
| 76 |
((128, 32), {'axis': -1}), |
| 77 |
((32, 64), {}), |
| 78 |
) |
| 79 |
|
| 80 |
def validate(self, array_type, test_shape, dtype, |
| 81 |
s, kwargs, copy_func=copy.copy): |
| 82 |
|
| 83 |
# Do it with the cache |
| 84 |
interfaces.cache.enable() |
| 85 |
output = self._validate(array_type, test_shape, dtype, s, kwargs, |
| 86 |
copy_func=copy_func) |
| 87 |
output2 = self._validate(array_type, test_shape, dtype, s, kwargs, |
| 88 |
copy_func=copy_func) |
| 89 |
|
| 90 |
self.assertIsNot(output, output2) |
| 91 |
|
| 92 |
# Turn it off to finish |
| 93 |
interfaces.cache.disable() |
| 94 |
|
| 95 |
@unittest.skipIf(*miss('64')) |
| 96 |
class CacheSpecificInterfacesUtils(unittest.TestCase): |
| 97 |
|
| 98 |
def test_slow_lookup_no_race_condition(self): |
| 99 |
'''Checks that lookups in _utils longer than the keepalive time are ok. |
| 100 |
''' |
| 101 |
# Any old size, it doesn't matter |
| 102 |
data_shape = (128,) |
| 103 |
|
| 104 |
# Monkey patch the module with a custom _Cache object |
| 105 |
_Cache_class = interfaces.cache._Cache |
| 106 |
class _SlowLookupCache(_Cache_class): |
| 107 |
|
| 108 |
def _lookup(self, key): |
| 109 |
return _Cache_class.lookup(self, key) |
| 110 |
|
| 111 |
def lookup(self, key): |
| 112 |
time.sleep(0.1) |
| 113 |
return self._lookup(key) |
| 114 |
|
| 115 |
try: |
| 116 |
interfaces.cache._Cache = _SlowLookupCache |
| 117 |
|
| 118 |
interfaces.cache.enable() |
| 119 |
|
| 120 |
# something shortish |
| 121 |
interfaces.cache.set_keepalive_time(0.001) |
| 122 |
|
| 123 |
ar, ai = numpy.random.randn(*(2,) + data_shape) |
| 124 |
a = ar + 1j*ai |
| 125 |
|
| 126 |
# Both the following should work without exception |
| 127 |
# (even if it fails to get from the cache) |
| 128 |
interfaces.numpy_fft.fft(a) |
| 129 |
interfaces.numpy_fft.fft(a) |
| 130 |
|
| 131 |
interfaces.cache.disable() |
| 132 |
|
| 133 |
finally: |
| 134 |
# Revert the monkey patching |
| 135 |
interfaces.cache._Cache = _Cache_class |
| 136 |
|
| 137 |
|
| 138 |
class InterfacesCacheTest(unittest.TestCase): |
| 139 |
|
| 140 |
def test_missing_threading(self): |
| 141 |
self.assertIs(interfaces.cache._fftw_cache, None) |
| 142 |
|
| 143 |
mod_threading = interfaces.cache._threading |
| 144 |
interfaces.cache._threading = None |
| 145 |
|
| 146 |
with self.assertRaises(ImportError): |
| 147 |
interfaces.cache.enable() |
| 148 |
|
| 149 |
interfaces.cache._threading = mod_threading |
| 150 |
|
| 151 |
def test_is_enabled(self): |
| 152 |
self.assertIs(interfaces.cache._fftw_cache, None) |
| 153 |
|
| 154 |
interfaces.cache.enable() |
| 155 |
self.assertTrue(interfaces.cache.is_enabled()) |
| 156 |
|
| 157 |
interfaces.cache.disable() |
| 158 |
self.assertFalse(interfaces.cache.is_enabled()) |
| 159 |
|
| 160 |
def test_cache_enable_disable(self): |
| 161 |
|
| 162 |
self.assertIs(interfaces.cache._fftw_cache, None) |
| 163 |
|
| 164 |
interfaces.cache.enable() |
| 165 |
self.assertIsInstance( |
| 166 |
interfaces.cache._fftw_cache, interfaces.cache._Cache) |
| 167 |
|
| 168 |
interfaces.cache.disable() |
| 169 |
self.assertIs(interfaces.cache._fftw_cache, None) |
| 170 |
|
| 171 |
def test_set_keepalive_time(self): |
| 172 |
with self.assertRaises(interfaces.cache.CacheError): |
| 173 |
interfaces.cache.set_keepalive_time(10) |
| 174 |
|
| 175 |
interfaces.cache.enable() |
| 176 |
interfaces.cache.set_keepalive_time(10) |
| 177 |
|
| 178 |
self.assertTrue( |
| 179 |
interfaces.cache._fftw_cache.keepalive_time == 10.0) |
| 180 |
|
| 181 |
interfaces.cache.disable() |
| 182 |
|
| 183 |
|
| 184 |
class CacheTest(unittest.TestCase): |
| 185 |
|
| 186 |
def test_cache_parent_thread_ended(self): |
| 187 |
'''Test ending cache parent thread ends cache thread. |
| 188 |
''' |
| 189 |
# Firstly make sure we've exited any lingering threads from other |
| 190 |
# tests. |
| 191 |
time.sleep(0.25) |
| 192 |
|
| 193 |
self.assertTrue(_check_n_cache_threads_running() == 0) |
| 194 |
|
| 195 |
def cache_parent_thread(): |
| 196 |
cache = interfaces.cache._Cache() |
| 197 |
time.sleep(0.5) |
| 198 |
|
| 199 |
# We give the parent thread the same name as a Cache thread so |
| 200 |
# it is picked up by the cache_threads_running function |
| 201 |
parent_t = threading.Thread( |
| 202 |
target=cache_parent_thread, name='PyFFTWCacheThread') |
| 203 |
parent_t.start() |
| 204 |
|
| 205 |
time.sleep(0.25) |
| 206 |
# Check it's running |
| 207 |
self.assertTrue(_check_n_cache_threads_running() == 2) |
| 208 |
|
| 209 |
parent_t.join() |
| 210 |
time.sleep(0.25) |
| 211 |
# Check both threads have exited properly |
| 212 |
self.assertTrue(_check_n_cache_threads_running() == 0) |
| 213 |
|
| 214 |
def test_delete_cache_object(self): |
| 215 |
'''Test deleting a cache object ends cache thread. |
| 216 |
''' |
| 217 |
# Firstly make sure we've exited any lingering threads from other |
| 218 |
# tests. |
| 219 |
time.sleep(0.25) |
| 220 |
self.assertTrue(_check_n_cache_threads_running() == 0) |
| 221 |
|
| 222 |
_cache = interfaces.cache._Cache() |
| 223 |
time.sleep(0.25) |
| 224 |
self.assertTrue(_check_n_cache_threads_running() == 1) |
| 225 |
|
| 226 |
del _cache |
| 227 |
time.sleep(0.25) |
| 228 |
self.assertTrue(_check_n_cache_threads_running() == 0) |
| 229 |
|
| 230 |
@unittest.skipIf(*miss('64')) |
| 231 |
def test_insert_and_lookup_item(self): |
| 232 |
_cache = interfaces.cache._Cache() |
| 233 |
|
| 234 |
key = 'the key' |
| 235 |
|
| 236 |
test_array = numpy.random.randn(16) |
| 237 |
obj = builders.fft(test_array) |
| 238 |
_cache.insert(obj, key) |
| 239 |
|
| 240 |
self.assertIs(_cache.lookup(key), obj) |
| 241 |
|
| 242 |
@unittest.skipIf(*miss('64')) |
| 243 |
def test_invalid_lookup(self): |
| 244 |
_cache = interfaces.cache._Cache() |
| 245 |
|
| 246 |
key = 'the key' |
| 247 |
|
| 248 |
test_array = numpy.random.randn(16) |
| 249 |
obj = builders.fft(test_array) |
| 250 |
_cache.insert(obj, key) |
| 251 |
|
| 252 |
self.assertRaises(KeyError, _cache.lookup, 'wrong_key') |
| 253 |
|
| 254 |
def test_keepalive_time_update(self): |
| 255 |
_cache = interfaces.cache._Cache() |
| 256 |
|
| 257 |
# The default |
| 258 |
self.assertEqual(_cache.keepalive_time, 0.1) |
| 259 |
|
| 260 |
_cache.set_keepalive_time(0.3) |
| 261 |
self.assertEqual(_cache.keepalive_time, 0.3) |
| 262 |
|
| 263 |
_cache.set_keepalive_time(10.0) |
| 264 |
self.assertEqual(_cache.keepalive_time, 10.0) |
| 265 |
|
| 266 |
_cache.set_keepalive_time('0.2') |
| 267 |
self.assertEqual(_cache.keepalive_time, 0.2) |
| 268 |
|
| 269 |
with self.assertRaises(ValueError): |
| 270 |
_cache.set_keepalive_time('foo') |
| 271 |
|
| 272 |
with self.assertRaises(TypeError): |
| 273 |
_cache.set_keepalive_time([]) |
| 274 |
|
| 275 |
@unittest.skipIf(*miss('64')) |
| 276 |
def test_contains(self): |
| 277 |
_cache = interfaces.cache._Cache() |
| 278 |
|
| 279 |
key = 'the key' |
| 280 |
|
| 281 |
test_array = numpy.random.randn(16) |
| 282 |
obj = builders.fft(test_array) |
| 283 |
_cache.insert(obj, key) |
| 284 |
|
| 285 |
self.assertTrue(key in _cache) |
| 286 |
self.assertFalse('Not a key' in _cache) |
| 287 |
|
| 288 |
@unittest.skipIf(*miss('64')) |
| 289 |
def test_objects_removed_after_keepalive(self): |
| 290 |
_cache = interfaces.cache._Cache() |
| 291 |
|
| 292 |
key = 'the key' |
| 293 |
|
| 294 |
test_array = numpy.random.randn(16) |
| 295 |
obj = builders.fft(test_array) |
| 296 |
_cache.insert(obj, key) |
| 297 |
|
| 298 |
self.assertIs(_cache.lookup(key), obj) |
| 299 |
|
| 300 |
keepalive_time = _cache.keepalive_time |
| 301 |
|
| 302 |
if os.name == 'nt' or sys.platform == 'darwin': |
| 303 |
# increase sleep time to address random test failures on CI |
| 304 |
time.sleep(keepalive_time * 8) |
| 305 |
else: |
| 306 |
# Relax a bit more otherwise |
| 307 |
time.sleep(keepalive_time * 4) |
| 308 |
|
| 309 |
self.assertRaises(KeyError, _cache.lookup, key) |
| 310 |
|
| 311 |
_cache.insert(obj, key) |
| 312 |
old_keepalive_time = _cache.keepalive_time |
| 313 |
_cache.set_keepalive_time(old_keepalive_time * 4) |
| 314 |
|
| 315 |
self.assertIs(_cache.lookup(key), obj) |
| 316 |
|
| 317 |
time.sleep(old_keepalive_time * 3.5) |
| 318 |
# still should be there |
| 319 |
self.assertIs(_cache.lookup(key), obj) |
| 320 |
|
| 321 |
time.sleep(old_keepalive_time * 16) |
| 322 |
|
| 323 |
self.assertRaises(KeyError, _cache.lookup, key) |
| 324 |
|
| 325 |
def test_misaligned_data_doesnt_clobber_cache(self): |
| 326 |
'''A bug was highlighted in #197 in which misaligned data causes |
| 327 |
an overwrite of an FFTW internal array which is also the same as |
| 328 |
an output array. The correct behaviour is for the cache to have |
| 329 |
alignment as a key to stop this happening. |
| 330 |
''' |
| 331 |
interfaces.cache.enable() |
| 332 |
|
| 333 |
N = 64 |
| 334 |
pyfftw.interfaces.cache.enable() |
| 335 |
np.random.seed(12345) |
| 336 |
|
| 337 |
Um = pyfftw.empty_aligned((N, N+1), dtype=np.float32, order='C') |
| 338 |
Vm = pyfftw.empty_aligned((N, N+1), dtype=np.float32, order='C') |
| 339 |
U = np.ndarray((N, N), dtype=Um.dtype, buffer=Um.data, offset=0) |
| 340 |
V = np.ndarray( |
| 341 |
(N, N), dtype=Vm.dtype, buffer=Vm.data, offset=Vm.itemsize) |
| 342 |
|
| 343 |
U[:] = np.random.randn(N, N).astype(np.float32) |
| 344 |
V[:] = np.random.randn(N, N).astype(np.float32) |
| 345 |
|
| 346 |
uh = hashlib.md5(U).hexdigest() |
| 347 |
vh = hashlib.md5(V).hexdigest() |
| 348 |
x = interfaces.numpy_fft.rfftn( |
| 349 |
U, None, axes=(0, 1), overwrite_input=False) |
| 350 |
y = interfaces.numpy_fft.rfftn( |
| 351 |
V, None, axes=(0, 1), overwrite_input=False) |
| 352 |
|
| 353 |
self.assertTrue(uh == hashlib.md5(U).hexdigest()) |
| 354 |
self.assertTrue(vh == hashlib.md5(V).hexdigest()) |
| 355 |
|
| 356 |
interfaces.cache.disable() |
| 357 |
|
| 358 |
|
| 359 |
class InterfacesNumpyFFTCacheTestIFFT(InterfacesNumpyFFTCacheTestFFT): |
| 360 |
func = 'ifft' |
| 361 |
|
| 362 |
class InterfacesNumpyFFTCacheTestRFFT(InterfacesNumpyFFTCacheTestFFT): |
| 363 |
func = 'rfft' |
| 364 |
|
| 365 |
class InterfacesNumpyFFTCacheTestIRFFT(InterfacesNumpyFFTCacheTestFFT): |
| 366 |
func = 'irfft' |
| 367 |
realinv = True |
| 368 |
|
| 369 |
class InterfacesNumpyFFTCacheTestFFT2(InterfacesNumpyFFTCacheTestFFT): |
| 370 |
axes_kw = 'axes' |
| 371 |
func = 'ifft2' |
| 372 |
test_shapes = ( |
| 373 |
((128, 64), {'axes': None}), |
| 374 |
((128, 32), {'axes': None}), |
| 375 |
((32, 64), {'axes': (-2, -1)}), |
| 376 |
((4, 6, 8, 4), {'axes': (0, 3)}), |
| 377 |
) |
| 378 |
|
| 379 |
invalid_args = ( |
| 380 |
((100,), ((100, 200),), ValueError, 'Shape error'), |
| 381 |
((100, 200), ((100, 200, 100),), ValueError, 'Shape error'), |
| 382 |
((100,), ((100, 200), (-3, -2, -1)), ValueError, 'Shape error'), |
| 383 |
((100, 200), (100, -1), TypeError, ''), |
| 384 |
((100, 200), ((100, 200), (-3, -2)), IndexError, 'Invalid axes'), |
| 385 |
((100, 200), ((100,), (-3,)), IndexError, 'Invalid axes')) |
| 386 |
|
| 387 |
|
| 388 |
class InterfacesNumpyFFTCacheTestIFFT2(InterfacesNumpyFFTCacheTestFFT2): |
| 389 |
func = 'ifft2' |
| 390 |
|
| 391 |
class InterfacesNumpyFFTCacheTestRFFT2(InterfacesNumpyFFTCacheTestFFT2): |
| 392 |
func = 'rfft2' |
| 393 |
|
| 394 |
class InterfacesNumpyFFTCacheTestIRFFT2(InterfacesNumpyFFTCacheTestFFT2): |
| 395 |
func = 'irfft2' |
| 396 |
realinv = True |
| 397 |
|
| 398 |
class InterfacesNumpyFFTCacheTestFFTN(InterfacesNumpyFFTCacheTestFFT2): |
| 399 |
func = 'ifftn' |
| 400 |
test_shapes = ( |
| 401 |
((128, 32, 4), {'axes': None}), |
| 402 |
((64, 128, 16), {'axes': (0, 1, 2)}), |
| 403 |
((4, 6, 8, 4), {'axes': (0, 3, 1)}), |
| 404 |
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}), |
| 405 |
) |
| 406 |
|
| 407 |
class InterfacesNumpyFFTCacheTestIFFTN(InterfacesNumpyFFTCacheTestFFTN): |
| 408 |
func = 'ifftn' |
| 409 |
|
| 410 |
class InterfacesNumpyFFTCacheTestRFFTN(InterfacesNumpyFFTCacheTestFFTN): |
| 411 |
func = 'rfftn' |
| 412 |
|
| 413 |
class InterfacesNumpyFFTCacheTestIRFFTN(InterfacesNumpyFFTCacheTestFFTN): |
| 414 |
func = 'irfftn' |
| 415 |
realinv = True |
| 416 |
|
| 417 |
test_cases = ( |
| 418 |
CacheTest, |
| 419 |
InterfacesCacheTest, |
| 420 |
CacheSpecificInterfacesUtils, |
| 421 |
InterfacesNumpyFFTCacheTestFFT, |
| 422 |
InterfacesNumpyFFTCacheTestIFFT, |
| 423 |
InterfacesNumpyFFTCacheTestRFFT, |
| 424 |
InterfacesNumpyFFTCacheTestIRFFT, |
| 425 |
InterfacesNumpyFFTCacheTestFFT2, |
| 426 |
InterfacesNumpyFFTCacheTestIFFT2, |
| 427 |
InterfacesNumpyFFTCacheTestRFFT2, |
| 428 |
InterfacesNumpyFFTCacheTestIRFFT2, |
| 429 |
InterfacesNumpyFFTCacheTestFFTN, |
| 430 |
InterfacesNumpyFFTCacheTestIFFTN, |
| 431 |
InterfacesNumpyFFTCacheTestRFFTN, |
| 432 |
InterfacesNumpyFFTCacheTestIRFFTN,) |
| 433 |
|
| 434 |
test_set = None |
| 435 |
|
| 436 |
if __name__ == '__main__': |
| 437 |
|
| 438 |
run_test_suites(test_cases, test_set) |