Skip to content

Commit e37d6ff

Browse files
authored
Merge pull request #2391 from devitocodes/numpy2
deps: support numpy 2.0
2 parents 16746b6 + c56da1e commit e37d6ff

7 files changed

Lines changed: 50 additions & 11 deletions

File tree

‎.github/workflows/pytest-core-nompi.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ jobs:
163163
if: "!contains(matrix.name, 'docker')"
164164
run: |
165165
pip install ${{ env.PIPFLAGS }} --upgrade pip
166-
pip install ${{ env.PIPFLAGS }} -e .[tests]
167166
pip install ${{ env.PIPFLAGS }} sympy==${{matrix.sympy}}
167+
pip install ${{ env.PIPFLAGS }} -e .[tests]
168168
169169
- name: Check configuration
170170
run: |

‎devito/__init__.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import atexit
22
from itertools import product
3+
import os
34
from . import _version
45

6+
import numpy as np
7+
58
# Import the global `configuration` dict
69
from devito.parameters import * # noqa
710

@@ -160,6 +163,13 @@ def mode_performance():
160163
configuration['opt-options']['blockinner'] = True
161164

162165

166+
if "PYTEST_VERSION" in os.environ and np.version.full_version.startswith('2'):
167+
# Avoid change in repr break docstring tests
168+
# Only sets it here for testing
169+
# https://numpy.org/devdocs/release/2.0.0-notes.html#representation-of-numpy-scalars-changed # noqa
170+
np.set_printoptions(legacy="1.25")
171+
172+
163173
# Ensure the SymPy caches are purged at exit
164174
# For whatever reason, if we don't do this the garbage collector won't its
165175
# job properly and thus we may end up missing some custom __del__'s

‎devito/arch/compiler.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def sniff_compiler_version(cc, allow_fail=False):
8181
ver = '.'.join(ver.strip().split('.')[:3])
8282
if not ver:
8383
return Version("0")
84+
# Sanitize bad icx formatting
85+
ver = ver.replace("+git", "").replace("git", "")
8486
ver = Version(ver)
8587
except UnicodeDecodeError:
8688
pass

‎requirements.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pip>=9.0.1
2-
numpy>1.16,<2.0
2+
numpy>1.16,<2.1
33
sympy>=1.9,<1.13
44
psutil>=5.1.0,<7.0
55
py-cpuinfo<10

‎setup.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
import versioneer
22

33
import os
4+
import sys
5+
import pkg_resources
46
from setuptools import setup, find_packages
57

8+
9+
def numpy_compat(required):
10+
new_reqs = [r for r in required if "numpy" not in r and "sympy" not in r]
11+
if sys.version_info < (3, 9):
12+
# Numpy 2.0 requires python > 3.8
13+
new_reqs.extend(["sympy>=1.9,<1.13", "numpy>1.16,<2.0"])
14+
return new_reqs
15+
16+
# Due to api changes in numpy 2.0, it requires sympy 1.12.1 at the minimum
17+
# Check if sympy is installed and enforce numpy version accordingly.
18+
# If sympy isn't installed, endforce sympy>=1.12.1 and numpy>=2.0
19+
try:
20+
sympy_version = pkg_resources.get_distribution("sympy").version
21+
min_ver2 = pkg_resources.parse_version("1.12.1")
22+
if pkg_resources.parse_version(sympy_version) < min_ver2:
23+
new_reqs.append("numpy>1.16,<2.0")
24+
else:
25+
new_reqs.append("numpy>=2.0")
26+
except pkg_resources.DistributionNotFound:
27+
new_reqs.extend(["sympy>=1.12.1", "numpy>=2.0"])
28+
29+
return new_reqs
30+
31+
632
with open('requirements.txt') as f:
733
required = f.read().splitlines()
34+
required = numpy_compat(required)
835

936
with open('requirements-optional.txt') as f:
1037
optionals = f.read().splitlines()

‎tests/test_interpolation.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from math import sin, floor
2-
31
import numpy as np
2+
from numpy import sin, floor
43
import pytest
54
from sympy import Float
65

@@ -93,8 +92,8 @@ def precompute_linear_interpolation(points, grid, origin, r=2):
9392
9493
Allow larger radius with zero weights for testing.
9594
"""
96-
gridpoints = [tuple(floor((point[i]-origin[i])/grid.spacing[i])
97-
for i in range(len(point))) for point in points]
95+
gridpoints = np.array([tuple(floor((point[i]-origin[i])/grid.spacing[i])
96+
for i in range(len(point))) for point in points])
9897

9998
interpolation_coeffs = np.zeros((len(points), grid.dim, r))
10099
rs = r // 2 - 1
@@ -114,13 +113,14 @@ def test_precomputed_interpolation(r):
114113
precomputed values for interpolation coefficients
115114
"""
116115
shape = (101, 101)
117-
points = [(.05, .9), (.01, .8), (0.07, 0.84)]
116+
points = np.array([(.05, .9), (.01, .8), (0.07, 0.84)])
118117
origin = (0, 0)
119118

120119
grid = Grid(shape=shape, origin=origin)
121120

122121
def init(data):
123122
# This is data with halo so need to shift to match the m.data expectations
123+
print(grid.spacing)
124124
for i in range(data.shape[0]):
125125
for j in range(data.shape[1]):
126126
data[i, j] = sin(grid.spacing[0]*(i-r)) + sin(grid.spacing[1]*(j-r))
@@ -638,7 +638,7 @@ def test_msf_interpolate():
638638
with a TimeFunction
639639
"""
640640
shape = (101, 101)
641-
points = [(.05, .9), (.01, .8), (0.07, 0.84)]
641+
points = np.array([(.05, .9), (.01, .8), (0.07, 0.84)])
642642
origin = (0, 0)
643643

644644
grid = Grid(shape=shape, origin=origin)

‎tests/test_sparse.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ def _precompute_linear_interpolation(self, points, grid, origin):
2121
precomputes gridpoints and coefficients according to a linear
2222
scheme to be used in PrecomputedSparseFunction.
2323
"""
24-
gridpoints = [
24+
gridpoints = np.array([
2525
tuple(
2626
floor((point[i] - origin[i]) / grid.spacing[i]) for i in range(len(point))
2727
)
2828
for point in points
29-
]
29+
])
3030

3131
coefficients = np.zeros((len(points), 2, 2))
3232
for i, point in enumerate(points):
@@ -41,7 +41,7 @@ def _precompute_linear_interpolation(self, points, grid, origin):
4141

4242
def test_precomputed_interpolation(self):
4343
shape = (101, 101)
44-
points = [(0.05, 0.9), (0.01, 0.8), (0.07, 0.84)]
44+
points = np.array([(0.05, 0.9), (0.01, 0.8), (0.07, 0.84)])
4545
origin = (0, 0)
4646

4747
grid = Grid(shape=shape, origin=origin)

0 commit comments

Comments
 (0)