Skip to content

Commit

Permalink
numpy 2.0 support (atcollab#785)
Browse files Browse the repository at this point in the history
* Switch to numpy 2.0, correct Segmentation fault
* updated build action
* upgrade upload-artifact
* update matlab testing
* full Matlab tests on linux
  • Loading branch information
lfarv authored Jun 21, 2024
1 parent ad67355 commit 5028aaf
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 52 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/build-python-wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04, macos-12, windows-2022]
os: [ubuntu-latest, windows-latest, macos-13, macos-14]

steps:
- uses: actions/checkout@v4
Expand All @@ -22,18 +22,13 @@ jobs:
# see: https://github.com/pypa/setuptools_scm/issues/480
fetch-depth: 0

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.9'

- name: Build wheels
uses: pypa/cibuildwheel@v2.16.2
uses: pypa/cibuildwheel@v2.19.1

- name: Upload wheels
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: wheels
name: wheels-${{ matrix.os }}
path: ./wheelhouse/*.whl
if-no-files-found: error

Expand All @@ -59,7 +54,7 @@ jobs:
run: python -m build --sdist

- name: Upload sdist
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: tar.gz
path: ./dist/*.tar.gz
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/matlab-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ jobs:

- uses: actions/checkout@v4

- name: Set up python 3.9
- name: Set up python
uses: actions/setup-python@v5
with:
python-version: '3.9'
python-version: '3.10'
cache: pip

- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1
uses: matlab-actions/setup-matlab@v2
with:
release: R2022a
release: R2023a

- name: Atmexall
uses: matlab-actions/run-command@v1
uses: matlab-actions/run-command@v2
with:
command: run('atmat/atpath');githubsetup();

Expand Down
11 changes: 7 additions & 4 deletions atmat/attests/githubrun.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
% normally no reason to run it in a user workflow.

tsuite=fullfile(atroot,'attests');
if ispc || ismac
v=assertSuccess(run(testsuite(tsuite)));
else
v=assertSuccess(run(testsuite(tsuite,'Tag','GitHub')));
if ~(ispc || ismac)
% Solution for
% "Intel MKL ERROR: Parameter 11 was incorrect on entry to DSBEVD."
% found there:
% https://fr.mathworks.com/matlabcentral/answers/1983899-intel-mkl-error-when-callying-scipy-from-matlab
py.sys.setdlopenflags(int32(bitor(int64(py.os.RTLD_LAZY), int64(py.os.RTLD_DEEPBIND))));
end
v=assertSuccess(run(testsuite(tsuite)));
disp(table(v));
7 changes: 1 addition & 6 deletions atmat/attests/githubsetup.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ function githubsetup()
atmexall -fail
if ispc
execfile=fullfile(getenv('pythonLocation'),'pythonw.exe');
execmode='InProcess';
elseif ismac
execfile=fullfile(getenv('pythonLocation'),'bin','python');
execmode='InProcess';
else
execfile=fullfile(getenv('pythonLocation'),'bin','python');
execmode='OutOfProcess';
end
pyenv("Version", execfile,'ExecutionMode', execmode);
pyenv("Version", execfile,'ExecutionMode', 'InProcess');
disp(pyenv);
end
18 changes: 10 additions & 8 deletions pyat/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
#include <float.h>
#include <atrandom.c>

#define atPrintf(...) PySys_WriteStdout(__VA_ARGS__)

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/ndarrayobject.h>

#define NUMPY_IMPORT_ARRAY_RETVAL NULL
#define NUMPY_IMPORT_ARRAY_TYPE void *
#if NPY_ABI_VERSION < 0x02000000
#define NPY_RAVEL_AXIS 32
#endif

typedef PyObject atElem;

Expand Down Expand Up @@ -306,11 +308,11 @@ void set_energy_particle(PyObject *lattice, PyObject *energy,
void set_current_fillpattern(PyArrayObject *bspos, PyArrayObject *bcurrents,
struct parameters *param){
if(bcurrents != NULL){
PyObject *bcurrentsum = PyArray_Sum(bcurrents, NPY_MAXDIMS,
PyObject *bcurrentsum = PyArray_Sum(bcurrents, NPY_RAVEL_AXIS,
PyArray_DESCR(bcurrents)->type_num,
NULL);
NULL);
param->beam_current = PyFloat_AsDouble(bcurrentsum);
Py_DECREF(bcurrentsum);
Py_DECREF(bcurrentsum);
param->nbunch = PyArray_SIZE(bspos);
param->bunch_spos = PyArray_DATA(bspos);
param->bunch_currents = PyArray_DATA(bcurrents);
Expand All @@ -319,7 +321,7 @@ void set_current_fillpattern(PyArrayObject *bspos, PyArrayObject *bcurrents,
param->nbunch=1;
param->bunch_spos = (double[1]){0.0};
param->bunch_currents = (double[1]){0.0};
}
}
}

/*
Expand Down Expand Up @@ -415,7 +417,7 @@ static PyObject *at_atpass(PyObject *self, PyObject *args, PyObject *kwargs) {
else
param.nturn = counter;

set_energy_particle(lattice, energy, particle, &param);
set_energy_particle(lattice, energy, particle, &param);
set_current_fillpattern(bspos, bcurrents, &param);

num_particles = (PyArray_SIZE(rin)/6);
Expand Down
2 changes: 1 addition & 1 deletion pyat/at/load/matfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import scipy.io

# imports necessary in 'globals()' for 'eval'
from numpy import array, uint8, NaN # noqa: F401
from numpy import array, uint8, nan as NaN # noqa: F401

from .allfiles import register_format
from .utils import split_ignoring_parentheses, RingParam, keep_elements
Expand Down
2 changes: 1 addition & 1 deletion pyat/at/load/reprfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np

# imports necessary in 'globals()' for 'eval'
from numpy import array, uint8, NaN # noqa: F401
from numpy import array, uint8, nan as NaN # noqa: F401

from at.lattice import Lattice, Element

Expand Down
43 changes: 31 additions & 12 deletions pyat/at/load/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"keep_attributes",
"split_ignoring_parentheses",
"RingParam",
"protect",
"restore",
]

import collections
Expand All @@ -32,6 +34,7 @@
from at.lattice import idtable_element

_ext_suffix = sysconfig.get_config_var("EXT_SUFFIX")
_placeholder = "placeholder"


def _no_encoder(v):
Expand Down Expand Up @@ -379,25 +382,41 @@ def element_to_dict(elem: Element, encoder: Callable[[Any], Any] = _no_encoder)
return dct


def split_ignoring_parentheses(string: str, delimiter: str = ",") -> list[str]:
"""Split a string while keeping parenthesized expressions intact
def split_ignoring_parentheses(
string: str,
delimiter: str = ",",
fence: tuple[str, str] = ('\\(', '\\)'),
maxsplit: int = -1,
) -> list[str]:
"""Split a string while keeping protected expressions intact
Example: "l=0,hom(4,0.0,0)" -> ["l=0", "hom(4,0.0,0)"]
"""
placeholder = "placeholder"
substituted, matches = protect(string, fence=fence)
parts = substituted.split(delimiter, maxsplit=maxsplit)
return restore(matches, *parts)


def protect(string: str, fence: tuple[str, str] = ('"', '"')):
inf, outf = fence
pattern = f"{inf}.*?{outf}"
substituted = string[:]
matches = collections.deque(re.finditer("\\(.*?\\)", string))
matches = collections.deque(re.finditer(pattern, string))
for match in matches:
substituted = substituted.replace(match.group(), placeholder, 1)
parts = substituted.split(delimiter)
replaced_parts = []
for part in parts:
if placeholder in part:
substituted = substituted.replace(match.group(), _placeholder, 1)
return substituted, matches


def restore(matches, *parts):

def rep(part):
while _placeholder in part:
next_match = matches.popleft()
part = part.replace(placeholder, next_match.group(), 1)
replaced_parts.append(part)
assert not matches
part = part.replace(_placeholder, next_match.group(), 1)
return part

replaced_parts = [rep(part) for part in parts]
assert not matches
return replaced_parts


Expand Down
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
requires = [
"setuptools >= 64",
"setuptools_scm >= 7",
"oldest-supported-numpy",
"oldest-supported-numpy; python_version <= '3.8'",
"numpy >= 2.0; python_version >= '3.9'",
"wheel",
]
# build-backend = "setuptools.build_meta"
Expand Down Expand Up @@ -36,7 +37,8 @@ classifiers = [
requires-python = ">=3.7"
dependencies = [
"importlib-resources;python_version<'3.9'",
"numpy >=1.16.6, <2.0",
"numpy >=1.16.6, <2.0; python_version <= '3.8'",
"numpy >= 1.23.5; python_version >= '3.9'",
"scipy>=1.4.0"
]

Expand Down Expand Up @@ -73,9 +75,6 @@ build-verbosity = "1"
# "build" frontend fails on windows
# build-frontend = "build"

[tool.cibuildwheel.macos]
archs = ["x86_64", "arm64"]

#[tool.cibuildwheel.linux]
## Pass the detected PyAT version to the linux docker containers
#environment-pass = ["SETUPTOOLS_SCM_PRETEND_VERSION"]
Expand Down

0 comments on commit 5028aaf

Please sign in to comment.