Skip to content

Commit

Permalink
Merge pull request #2391 from devitocodes/numpy2
Browse files Browse the repository at this point in the history
deps: support numpy 2.0
  • Loading branch information
mloubout authored Jun 26, 2024
2 parents 16746b6 + c56da1e commit e37d6ff
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pytest-core-nompi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ jobs:
if: "!contains(matrix.name, 'docker')"
run: |
pip install ${{ env.PIPFLAGS }} --upgrade pip
pip install ${{ env.PIPFLAGS }} -e .[tests]
pip install ${{ env.PIPFLAGS }} sympy==${{matrix.sympy}}
pip install ${{ env.PIPFLAGS }} -e .[tests]
- name: Check configuration
run: |
Expand Down
10 changes: 10 additions & 0 deletions devito/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import atexit
from itertools import product
import os
from . import _version

import numpy as np

# Import the global `configuration` dict
from devito.parameters import * # noqa

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


if "PYTEST_VERSION" in os.environ and np.version.full_version.startswith('2'):
# Avoid change in repr break docstring tests
# Only sets it here for testing
# https://numpy.org/devdocs/release/2.0.0-notes.html#representation-of-numpy-scalars-changed # noqa
np.set_printoptions(legacy="1.25")


# Ensure the SymPy caches are purged at exit
# For whatever reason, if we don't do this the garbage collector won't its
# job properly and thus we may end up missing some custom __del__'s
Expand Down
2 changes: 2 additions & 0 deletions devito/arch/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def sniff_compiler_version(cc, allow_fail=False):
ver = '.'.join(ver.strip().split('.')[:3])
if not ver:
return Version("0")
# Sanitize bad icx formatting
ver = ver.replace("+git", "").replace("git", "")
ver = Version(ver)
except UnicodeDecodeError:
pass
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pip>=9.0.1
numpy>1.16,<2.0
numpy>1.16,<2.1
sympy>=1.9,<1.13
psutil>=5.1.0,<7.0
py-cpuinfo<10
Expand Down
27 changes: 27 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
import versioneer

import os
import sys
import pkg_resources
from setuptools import setup, find_packages


def numpy_compat(required):
new_reqs = [r for r in required if "numpy" not in r and "sympy" not in r]
if sys.version_info < (3, 9):
# Numpy 2.0 requires python > 3.8
new_reqs.extend(["sympy>=1.9,<1.13", "numpy>1.16,<2.0"])
return new_reqs

# Due to api changes in numpy 2.0, it requires sympy 1.12.1 at the minimum
# Check if sympy is installed and enforce numpy version accordingly.
# If sympy isn't installed, endforce sympy>=1.12.1 and numpy>=2.0
try:
sympy_version = pkg_resources.get_distribution("sympy").version
min_ver2 = pkg_resources.parse_version("1.12.1")
if pkg_resources.parse_version(sympy_version) < min_ver2:
new_reqs.append("numpy>1.16,<2.0")
else:
new_reqs.append("numpy>=2.0")
except pkg_resources.DistributionNotFound:
new_reqs.extend(["sympy>=1.12.1", "numpy>=2.0"])

return new_reqs


with open('requirements.txt') as f:
required = f.read().splitlines()
required = numpy_compat(required)

with open('requirements-optional.txt') as f:
optionals = f.read().splitlines()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from math import sin, floor

import numpy as np
from numpy import sin, floor
import pytest
from sympy import Float

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

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

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

def init(data):
# This is data with halo so need to shift to match the m.data expectations
print(grid.spacing)
for i in range(data.shape[0]):
for j in range(data.shape[1]):
data[i, j] = sin(grid.spacing[0]*(i-r)) + sin(grid.spacing[1]*(j-r))
Expand Down Expand Up @@ -638,7 +638,7 @@ def test_msf_interpolate():
with a TimeFunction
"""
shape = (101, 101)
points = [(.05, .9), (.01, .8), (0.07, 0.84)]
points = np.array([(.05, .9), (.01, .8), (0.07, 0.84)])
origin = (0, 0)

grid = Grid(shape=shape, origin=origin)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def _precompute_linear_interpolation(self, points, grid, origin):
precomputes gridpoints and coefficients according to a linear
scheme to be used in PrecomputedSparseFunction.
"""
gridpoints = [
gridpoints = np.array([
tuple(
floor((point[i] - origin[i]) / grid.spacing[i]) for i in range(len(point))
)
for point in points
]
])

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

def test_precomputed_interpolation(self):
shape = (101, 101)
points = [(0.05, 0.9), (0.01, 0.8), (0.07, 0.84)]
points = np.array([(0.05, 0.9), (0.01, 0.8), (0.07, 0.84)])
origin = (0, 0)

grid = Grid(shape=shape, origin=origin)
Expand Down

0 comments on commit e37d6ff

Please sign in to comment.