Skip to content

Commit

Permalink
Support for numpy2 (#1012)
Browse files Browse the repository at this point in the history
Compile DLite against numpy 2.x. The compiled ABI should be backward compatible with numpy-1.x.
* Updated dlite to numpy2. The ABI should be backward compatible with numpy-1.x
* Update requirements for temdata example
* Display numpy version in cmake configure
* Removed explicit installation of numpy-1.x in cd_release.yml and pyproject.toml
* Build manylinux i686 from Python 3.10+

---------

Co-authored-by: Francesca L. Bleken <[email protected]>
  • Loading branch information
jesper-friis and francescalb authored Dec 4, 2024
1 parent b62c039 commit 9097409
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 232 deletions.
87 changes: 42 additions & 45 deletions .github/workflows/cd_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,61 +76,61 @@ jobs:
include:
# See ci_build_wheel.yml for a tested matrix of working combinations

# 32-bit linux
# 64-bit manylinux
- os: ubuntu-20.04
system_type: ["manylinux", "2010"]
arch: i686
py_minors: 8,9
- os: ubuntu-20.04
system_type: ["manylinux", "2014"]
arch: i686
py_minors: 8,9,10,11
system_type: ["manylinux", "_2_34"]
arch: x86_64
py_minors: 8,9,10,11,12
#- os: ubuntu-20.04
# system_type: ["manylinux", "2014"]
# arch: x86_64
# py_minors: 8,9,10,11,12
#- os: ubuntu-20.04
# system_type: ["manylinux", "2010"]
# arch: x86_64
# py_minors: 8,9
#- os: ubuntu-20.04
# system_type: ["manylinux", "_2_28"]
# arch: x86_64
# py_minors: 8,9,10,11,12

# 64-bit musllinux
- os: ubuntu-20.04
system_type: ["musllinux", "_1_1"]
arch: i686
arch: x86_64
py_minors: 8,9,10,11
#- os: ubuntu-20.04
# system_type: ["musllinux", "_1_1"]
# arch: x86_64
# py_minors: 8,9,10,11

# 64-bit linux
- os: ubuntu-20.04
system_type: ["manylinux", "2010"]
arch: x86_64
py_minors: 8,9
# 32-bit manylinux
- os: ubuntu-20.04
system_type: ["manylinux", "2014"]
arch: x86_64
py_minors: 8,9,10,11,12
- os: ubuntu-20.04
system_type: ["manylinux", "_2_28"]
arch: x86_64
py_minors: 8,9,10,11,12
arch: i686
py_minors: 10,11
#- os: ubuntu-20.04
# system_type: ["manylinux", "2014"]
# arch: i686
# py_minors: 8,9,10,11
#- os: ubuntu-20.04
# system_type: ["manylinux", "2010"]
# arch: i686
# py_minors: 8,9

# 32-bit musllinux
- os: ubuntu-20.04
system_type: ["musllinux", "_1_1"]
arch: x86_64
arch: i686
py_minors: 8,9,10,11

# 32-bit Windows

# 64-bit Windows
- os: windows-2019
system_type: ["win", ""]
arch: amd64
py_minors: '8'
- os: windows-2019
system_type: ["win", ""]
arch: amd64
py_minors: '9'
- os: windows-2019
system_type: ["win", ""]
arch: amd64
py_minors: '10'
- os: windows-2019
system_type: ["win", ""]
arch: amd64
py_minors: '11'
- os: windows-2019
system_type: ["win", ""]
arch: amd64
py_minors: '12'
py_minors: '8,9,10,11,12'

# 32-bit Windows

steps:
- name: Checkout repository
Expand Down Expand Up @@ -208,13 +208,10 @@ jobs:
- name: Install Python dependencies
run: |
python -m pip install -U pip
pip install -U setuptools wheel
# Build package/tool
pip install -U build
pip install -U setuptools wheel build
# Build requirements
pip install -U oldest-supported-numpy cmake
pip install -U -r requirements.txt -r requirements_dev.txt
- name: Build source distribution
run: python -m build --no-isolation --sdist python
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci_build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- os: ubuntu-20.04
system_type: ["manylinux", "2014"]
arch: i686
py_minors: 8,12
py_minors: 10,12
# Python 3.12 fails since cibuildwheel still depends on distutils
# for musllinux
#- os: ubuntu-20.04
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/container_builds_weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
type: "2014"
arch: "i686"
#py_minors: "8 9 10 11 12 13"
py_minors: "8 9 10 11 12"
py_minors: "10 11 12"
#- system: "manylinux"
# type: "2010"
# arch: "i686"
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ if(WITH_PYTHON)
message(STATUS "Python3_LIBRARIES = ${Python3_LIBRARIES}")
message(STATUS "Python3_EXECUTABLE = ${Python3_EXECUTABLE}")
message(STATUS "Python3_INCLUDE_DIRS = ${Python3_INCLUDE_DIRS}")
message(STATUS "Python3_NumPy_VERSION=${Python3_NumPy_VERSION}")
message(STATUS "Python3_NumPy_INCLUDE_DIRS = ${Python3_NumPy_INCLUDE_DIRS}")
message(STATUS "Python3_STATIC_LIBS = ${Python3_STATIC_LIBS}")

Expand Down
15 changes: 9 additions & 6 deletions bindings/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,15 @@ add_custom_command(
"$<TARGET_FILE:dlite-env>"
"$<TARGET_FILE:dlite-getuuid>"
${pkgdir}/share/dlite/bin
DEPENDS
python_package
dlite-codegen
dlite-env
dlite-getuuid
)
)
add_dependencies(
dlitepy
python_package
dlite-codegen
dlite-env
dlite-getuuid
)


#
# Install
Expand Down
38 changes: 26 additions & 12 deletions bindings/python/dlite-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

#define SWIG_FILE_WITH_INIT /* tell numpy that we initialize it in %init */

/* Set this to an exception object (e.g. PyExc_StopIteration) to raise
another exceptions than DLiteError. No need to increase the reference
count. */
PyObject *dlite_swig_exception = NULL;

/* forward declarations */
char *strndup(const char *s, size_t n);
/* Set this to an exception object (e.g. PyExc_StopIteration) to raise
another exceptions than DLiteError. No need to increase the reference
count. */
PyObject *dlite_swig_exception = NULL;

/* forward declarations */
char *strndup(const char *s, size_t n);
%}

/* Some cross-target language typedef's and definitions */
Expand Down Expand Up @@ -56,7 +57,20 @@ int dlite_swig_set_scalar(void *ptr, DLiteType type, size_t size, obj_t *obj);
* https://github.com/numpy/numpy/blame/master/tools/swig/numpy.i
*/

%include "numpy.i" // slightly changed to fit out needs, search for "XXX"
%include "numpy.i"

%{
/* Macros for compatibility with numpy<2.0.0 */
#if NPY_ABI_VERSION < 0x02000000
typedef int elsize_t;
# define PyDataType_ELSIZE(descr) ((descr)->elsize)
# define PyDataType_SET_ELSIZE(descr, size) ((descr)->elsize = (elsize_t)size)

#else
typedef npy_intp elsize_t;
#endif
%}


%init %{
dlite_init(); /* make sure that dlite is initialised */
Expand Down Expand Up @@ -162,22 +176,22 @@ PyArray_Descr *npy_dtype(DLiteType type, size_t size)
switch (type) {
case dliteBlob:
case dliteBool:
dtype->elsize = (int)size;
PyDataType_SET_ELSIZE(dtype, size);
break;
case dliteInt:
case dliteUInt:
case dliteFloat:
assert(dtype->elsize == (int)size);
assert(PyDataType_ELSIZE(dtype) == (elsize_t)size);
break;
case dliteFixString:
dtype->elsize = (int)size;
PyDataType_SET_ELSIZE(dtype, size);
break;
case dliteStringPtr:
case dliteRef:
case dliteDimension:
case dliteProperty:
case dliteRelation:
assert(dtype->elsize == 0 || sizeof(void *));
assert(PyDataType_ELSIZE(dtype) == 0 || sizeof(void *));
break;
}
return dtype;
Expand Down Expand Up @@ -510,7 +524,7 @@ void *dlite_swig_copy_array(int ndims, int *shape, DLiteType type,
case dliteFixString:
for (i=0; i<PyArray_SIZE(arr); i++)
strncpy((char *)ptr + i*size,
(char *)(PyArray_DATA(arr)) + i*dtype->elsize,
(char *)(PyArray_DATA(arr)) + i*PyDataType_ELSIZE(dtype),
size);
break;
case dliteStringPtr:
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _dlite_infer_dimensions(
if prop.ndims:
value = getter(prop.name)
array = (
np.array(value, copy=False)
np.asarray(value)
if value is not None
else np.zeros([0] * prop.ndims)
)
Expand Down
Loading

0 comments on commit 9097409

Please sign in to comment.