Skip to content

Commit 1a8015a

Browse files
authored
Merge pull request #94 from SYMSCAE/array-modernization
enh: Modernize array handling with nanobind
2 parents 0a748c1 + 2355f21 commit 1a8015a

19 files changed

Lines changed: 485 additions & 384 deletions

‎CMakeLists.txt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if(DEFINED ENV{CONDA_PREFIX})
1111
list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}" "$ENV{CONDA_PREFIX}/Library")
1212
endif()
1313

14-
find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module NumPy)
14+
find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module)
1515
execute_process(
1616
COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
1717
OUTPUT_VARIABLE nanobind_DIR
@@ -52,10 +52,9 @@ target_compile_definitions(diffpy_core PRIVATE DIFFPYRUNTIMERELPATH=data)
5252
target_link_libraries(diffpy_core PUBLIC Boost::serialization GSL::gsl GSL::gslcblas ${CMAKE_DL_LIBS})
5353

5454
file(GLOB SRREAL_SOURCES CONFIGURE_DEPENDS "src/extensions/*.cpp")
55-
# Keep the CPython-specific ABI while the bindings use the NumPy C API and
56-
# CPython macros. Do not label these wheels abi3 without porting those calls.
55+
# Retain CPython-specific wheels; stable-ABI packaging is a separate change.
5756
nanobind_add_module(srreal_ext NB_STATIC ${SRREAL_SOURCES})
58-
target_link_libraries(srreal_ext PRIVATE diffpy_core Python::NumPy)
57+
target_link_libraries(srreal_ext PRIVATE diffpy_core)
5958
set_target_properties(srreal_ext PROPERTIES INSTALL_RPATH_USE_LINK_PATH ON)
6059
install(TARGETS srreal_ext LIBRARY DESTINATION diffpy/srreal)
6160
install(DIRECTORY "${LIBDIFFPY_SOURCE_DIR}/src/runtime/"

‎news/nanobind-arrays.rst‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
**Changed:**
2+
3+
* Use typed nanobind arrays for numeric input, copied results, and writable
4+
Atom views. Keep NumPy output types, shapes, native dtypes, and the existing
5+
scalar-versus-array return conventions.
6+
* Remove the NumPy C API and build-time NumPy dependency. NumPy remains a
7+
runtime dependency; wheel Python ABI requirements are unchanged.
8+
* Retain NumPy-owned writable storage for copied results and explicit storage
9+
owners for Atom views, including slices retained after structural edits.
10+
* Preserve endpoint-specific conversion rules, NumPy scalar casting warnings,
11+
and coordinate/matrix broadcasting. Invalid coordinate/matrix assignments
12+
no longer partially modify an Atom.
13+
14+
**Fixed:**
15+
16+
* Read strided and reversed integer index arrays in logical order instead of
17+
treating their storage as contiguous.
18+
* Interpret byte-swapped FFT inputs and integer index arrays in their actual
19+
byte order, and safely handle unaligned buffers.
20+
* Reject integer-array indices outside the C++ ``int`` range with
21+
``OverflowError`` instead of silently wrapping. These three corrections
22+
intentionally change erroneous behavior inherited from Boost.Python.

‎pyproject.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
requires = [
33
"scikit-build-core>=0.10",
44
"setuptools-scm>=8",
5-
"numpy",
65
"nanobind>=3.1,<4",
76
]
87
build-backend = "scikit_build_core.build"

‎src/extensions/srreal_atom.hpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,17 @@ struct AtomViewPin
135135
}
136136
};
137137

138-
inline nb::object pin_atom_view(PythonAtom& atom, nb::object view)
138+
inline nb::object atom_view_owner(PythonAtom& atom)
139139
{
140+
nb::object python_atom = nb::cast(&atom, nb::rv_policy::reference);
140141
if (atom.references())
141142
{
142143
nb::capsule owner(new AtomViewPin(atom.references()), [](void* p) noexcept {
143144
delete static_cast<AtomViewPin*>(p);
144145
});
145-
setNumPyArrayBase(view, std::move(owner));
146+
return nb::make_tuple(python_atom, owner);
146147
}
147-
return view;
148+
return python_atom;
148149
}
149150

150151
inline void preserve_atom_views(const std::shared_ptr<AtomReferences>& refs)

0 commit comments

Comments
 (0)