| 1 |
# Copyright 2014 Knowledge Economy Developments Ltd |
| 2 |
# |
| 3 |
# Henry Gomersall |
| 4 |
# [email protected] |
| 5 |
# |
| 6 |
# All rights reserved. |
| 7 |
# |
| 8 |
# Redistribution and use in source and binary forms, with or without |
| 9 |
# modification, are permitted provided that the following conditions are met: |
| 10 |
# |
| 11 |
# * Redistributions of source code must retain the above copyright notice, this |
| 12 |
# list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
# this list of conditions and the following disclaimer in the documentation |
| 16 |
# and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 19 |
# may be used to endorse or promote products derived from this software without |
| 20 |
# specific prior written permission. |
| 21 |
# |
| 22 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 26 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 |
# POSSIBILITY OF SUCH DAMAGE. |
| 33 |
# |
| 34 |
|
| 35 |
|
| 36 |
from .test_pyfftw_base import run_test_suites |
| 37 |
|
| 38 |
import unittest |
| 39 |
import pyfftw |
| 40 |
import platform |
| 41 |
import os |
| 42 |
from numpy.testing import assert_, assert_equal |
| 43 |
|
| 44 |
def get_cpus_info(): |
| 45 |
|
| 46 |
if 'Linux' in platform.system(): |
| 47 |
# A simple /proc/cpuinfo parser |
| 48 |
with open(os.path.join('/', 'proc','cpuinfo'), 'r') as f: |
| 49 |
|
| 50 |
cpus_info = [] |
| 51 |
idx = 0 |
| 52 |
for line in f.readlines(): |
| 53 |
if line.find(':') < 0: |
| 54 |
idx += 1 |
| 55 |
continue |
| 56 |
|
| 57 |
key, values = [each.strip() for each in line.split(':')] |
| 58 |
|
| 59 |
try: |
| 60 |
cpus_info[idx][key] = values |
| 61 |
except IndexError: |
| 62 |
cpus_info.append({key: values}) |
| 63 |
|
| 64 |
else: |
| 65 |
cpus_info = None |
| 66 |
|
| 67 |
return cpus_info |
| 68 |
|
| 69 |
class UtilsTest(unittest.TestCase): |
| 70 |
|
| 71 |
def setUp(self): |
| 72 |
|
| 73 |
return |
| 74 |
|
| 75 |
def tearDown(self): |
| 76 |
|
| 77 |
return |
| 78 |
|
| 79 |
@unittest.skipIf('Linux' not in platform.system(), |
| 80 |
'Skipping as we only have it set up for Linux at present.') |
| 81 |
def test_get_alignment(self): |
| 82 |
cpus_info = get_cpus_info() |
| 83 |
|
| 84 |
for each_cpu in cpus_info: |
| 85 |
if 'avx' in each_cpu['flags']: |
| 86 |
self.assertTrue(pyfftw.simd_alignment == 32) |
| 87 |
elif 'sse' in each_cpu['flags']: |
| 88 |
self.assertTrue(pyfftw.simd_alignment == 16) |
| 89 |
else: |
| 90 |
self.assertTrue(pyfftw.simd_alignment == 1) |
| 91 |
|
| 92 |
|
| 93 |
class NextFastLenTest(unittest.TestCase): |
| 94 |
|
| 95 |
def __init__(self, *args, **kwargs): |
| 96 |
|
| 97 |
super(NextFastLenTest, self).__init__(*args, **kwargs) |
| 98 |
|
| 99 |
if not hasattr(self, 'assertRaisesRegex'): |
| 100 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 101 |
|
| 102 |
def test_next_fast_len(self): |
| 103 |
|
| 104 |
def nums(): |
| 105 |
for j in range(1, 1000): |
| 106 |
yield j |
| 107 |
yield 2**5 * 3**5 * 4**5 + 1 |
| 108 |
|
| 109 |
for n in nums(): |
| 110 |
m = pyfftw.next_fast_len(n) |
| 111 |
msg = "n=%d, m=%d" % (n, m) |
| 112 |
|
| 113 |
assert_(m >= n, msg) |
| 114 |
|
| 115 |
# check regularity |
| 116 |
k = m |
| 117 |
num11 = num13 = 0 |
| 118 |
# These factors come from the description in the FFTW3 docs: |
| 119 |
# http://fftw.org/fftw3_doc/Complex-DFTs.html#Complex-DFTs |
| 120 |
for d in [2, 3, 5, 7, 11, 13]: |
| 121 |
while True: |
| 122 |
a, b = divmod(k, d) |
| 123 |
if b == 0: |
| 124 |
k = a |
| 125 |
if d in [11, 13]: |
| 126 |
# only allowed to match 11 or 13 once |
| 127 |
if num11 > 0 or num13 > 0: |
| 128 |
break |
| 129 |
if d == 11: |
| 130 |
num11 += 1 |
| 131 |
else: |
| 132 |
num13 += 1 |
| 133 |
else: |
| 134 |
break |
| 135 |
assert_equal(k, 1, err_msg=msg) |
| 136 |
|
| 137 |
def test_next_fast_len_strict(self): |
| 138 |
strict_test_cases = { |
| 139 |
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 11: 11, 13: 13, |
| 140 |
14: 14, 15: 15, 16: 16, 17: 18, 1021: 1024, |
| 141 |
|
| 142 |
# 2 * 3 * 5 * 7 * 11 |
| 143 |
2310: 2310, |
| 144 |
2310 - 1: 2310, |
| 145 |
# 2 * 3 * 5 * 7 * 13 |
| 146 |
2730: 2730, |
| 147 |
2730 - 1: 2730, |
| 148 |
# 2**2 * 3**2 * 5**2 * 7**2 * 11 |
| 149 |
485100: 485100, |
| 150 |
485100-1: 485100, |
| 151 |
# 2**2 * 3**2 * 5**2 * 7**2 * 13 |
| 152 |
573300: 573300, |
| 153 |
573300-1: 573300, |
| 154 |
|
| 155 |
# more than one multiple of 11 or 13 is not accepted |
| 156 |
# 2 * 3 * 5 * 7 * 11**2 |
| 157 |
25410: 25872, |
| 158 |
# 2 * 3 * 5 * 7 * 13**2 |
| 159 |
35490: 35672, |
| 160 |
# 2 * 3 * 5 * 7 * 11 * 13 |
| 161 |
30030: 30576, |
| 162 |
|
| 163 |
} |
| 164 |
for x, y in strict_test_cases.items(): |
| 165 |
assert_equal(pyfftw.next_fast_len(x), y) |
| 166 |
|
| 167 |
test_cases = ( |
| 168 |
UtilsTest, |
| 169 |
NextFastLenTest) |
| 170 |
|
| 171 |
test_set = None |
| 172 |
|
| 173 |
if __name__ == '__main__': |
| 174 |
|
| 175 |
run_test_suites(test_cases, test_set) |