|
core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
|
NumPy arrays in and out of a header-only C++ function, with the GIL released: the names and the template. More...
#include <nanobind/nanobind.h>#include <nanobind/ndarray.h>#include <cstddef>#include <memory>#include <span>#include <stdexcept>#include <utility>#include <vector>Go to the source code of this file.
Typedefs | |
| template<class T > | |
| using | snicholls::python::vector_in = ::nanobind::ndarray< const T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
A read-only, C-contiguous, one-dimensional CPU array of T, as a function argument. | |
| template<class T > | |
| using | snicholls::python::matrix_in = ::nanobind::ndarray< const T, ::nanobind::ndim< 2 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
A read-only, C-contiguous (row-major), two-dimensional CPU array of T, as a function argument. | |
| template<class T > | |
| using | snicholls::python::vector_inout = ::nanobind::ndarray< T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > |
A writable, C-contiguous, one-dimensional CPU array of T, for functions that work in place. | |
| template<class T > | |
| using | snicholls::python::numpy_vector = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 1 > > |
A one-dimensional NumPy array of T, as a result. | |
| template<class T > | |
| using | snicholls::python::numpy_matrix = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 2 >, ::nanobind::c_contig > |
A two-dimensional, row-major NumPy array of T, as a result. | |
Functions | |
| template<class... Args> | |
| auto | snicholls::python::as_span (const ::nanobind::ndarray< Args... > &a) noexcept |
| A span over a C-contiguous array's elements, all dimensions flattened in row-major order. | |
| template<class T > | |
| numpy_matrix< T > | snicholls::python::to_numpy (std::vector< T > &&values, std::size_t rows, std::size_t columns) |
A NumPy array of shape (rows, columns) owning values' buffer, without copying it. | |
| template<class T > | |
| numpy_vector< T > | snicholls::python::to_numpy (std::vector< T > &&values) |
A one-dimensional NumPy array owning values' buffer, without copying it. | |
NumPy arrays in and out of a header-only C++ function, with the GIL released: the names and the template.
std::span of plain data and return plain data, so binding one is always the same four steps. This header names the pieces, and the demo holds the template that maintainers copy:vector_in<T> / matrix_in<T> - a read-only, C-contiguous, CPU nanobind::ndarray. NumPy arrays, and anything else exporting DLPack or the buffer protocol, convert; a non-contiguous array or one of another dtype is copied by nanobind first (pass .noconvert() on the argument to refuse instead).as_span(a) - a std::span over the array's memory, no copy.without_gil(...) (gil.hpp) around the core call; check preconditions with require(...) (errors.hpp) before, because Python passes anything.to_numpy(std::move(vector)) (or with rows and columns) - the vector's buffer handed to a NumPy array, no copy, freed when the array is.ndarray; gil.hpp (without_gil, release_gil); errors.hpp (require).as_span is O(1), noexcept, and valid while the ndarray argument lives (the whole call).to_numpy is O(1) after one allocation for the owner; the NumPy array owns the data from then on.TypeError naming the expected signature. to_numpy throws std::bad_alloc (MemoryError), and std::length_error (ValueError) when rows times columns is not the vector's size.BENCHMARKS.md (tabular Shapley, Python against C++).scripts/demos/ndarray_demo.sh.snicholls::core_nanobind; <nanobind/ndarray.h>. NumPy is needed at run time only by functions that return NumPy arrays.uint64 coalition table or index array must be asked for as such; NumPy's default integer is int64.Definition in file ndarray.hpp.