From ea7d1fa9dcf97f9a148fe02209f6ef836a5cb5be Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 8 Aug 2019 22:55:06 -0500 Subject: [PATCH] Fix numpy initialization. Closes gh-214 --- src/cpp/cuda.cpp | 3 +++ src/wrapper/_pvt_struct_v2.cpp | 6 +++++- src/wrapper/_pvt_struct_v3.cpp | 6 +++++- src/wrapper/mempool.cpp | 3 +++ src/wrapper/numpy_init.hpp | 33 --------------------------------- src/wrapper/tools.hpp | 2 +- src/wrapper/wrap_cudadrv.cpp | 12 ++++++++++++ 7 files changed, 29 insertions(+), 36 deletions(-) delete mode 100644 src/wrapper/numpy_init.hpp diff --git a/src/cpp/cuda.cpp b/src/cpp/cuda.cpp index 4bceb78f..00c7072c 100644 --- a/src/cpp/cuda.cpp +++ b/src/cpp/cuda.cpp @@ -1,3 +1,6 @@ +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pycuda_ARRAY_API + #include "cuda.hpp" boost::thread_specific_ptr pycuda::context_stack_ptr; diff --git a/src/wrapper/_pvt_struct_v2.cpp b/src/wrapper/_pvt_struct_v2.cpp index dac34846..aab88150 100644 --- a/src/wrapper/_pvt_struct_v2.cpp +++ b/src/wrapper/_pvt_struct_v2.cpp @@ -7,13 +7,15 @@ * for packing complex values and only supports native packing. * (the minimum that's needed for PyOpenCL/PyCUDA.) */ +#define PY_ARRAY_UNIQUE_SYMBOL pycuda_pvt_struct_v2_ARRAY_API + #define PY_SSIZE_T_CLEAN #include "Python.h" +#include #include "structseq.h" #include "structmember.h" #include -#include "numpy_init.hpp" // static PyTypeObject PyStructType; @@ -1570,6 +1572,8 @@ The variable struct.error is an exception raised on errors.\n"); PyMODINIT_FUNC init_pvt_struct(void) { + import_array(); + PyObject *ver, *m; ver = PyString_FromString("0.2"); diff --git a/src/wrapper/_pvt_struct_v3.cpp b/src/wrapper/_pvt_struct_v3.cpp index 316062f1..15f7b18e 100644 --- a/src/wrapper/_pvt_struct_v3.cpp +++ b/src/wrapper/_pvt_struct_v3.cpp @@ -3,12 +3,14 @@ /* New version supporting byte order, alignment and size options, character strings, and unsigned numbers */ +#define PY_ARRAY_UNIQUE_SYMBOL pycuda_pvt_struct_v2_ARRAY_API + #define PY_SSIZE_T_CLEAN #include "Python.h" +#include #include "structmember.h" #include -#include "numpy_init.hpp" namespace { extern PyTypeObject PyStructType; @@ -1720,6 +1722,8 @@ extern "C" PyMODINIT_FUNC PyInit__pvt_struct(void) { + import_array(); + PyObject *m; m = PyModule_Create(&_structmodule); diff --git a/src/wrapper/mempool.cpp b/src/wrapper/mempool.cpp index 7734a6a2..918d3d0d 100644 --- a/src/wrapper/mempool.cpp +++ b/src/wrapper/mempool.cpp @@ -1,3 +1,6 @@ +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pycuda_ARRAY_API + #include #include "tools.hpp" #include "wrap_helpers.hpp" diff --git a/src/wrapper/numpy_init.hpp b/src/wrapper/numpy_init.hpp deleted file mode 100644 index a0719859..00000000 --- a/src/wrapper/numpy_init.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef _FAYHVVAAA_PYCUDA_HEADER_SEEN_NUMPY_INIT_HPP - - - - -#include -#include - - - - -namespace -{ - static struct pyublas_array_importer - { - static bool do_import_array() - { - import_array1(false); - return true; - } - - pyublas_array_importer() - { - if (!do_import_array()) - throw std::runtime_error("numpy failed to initialize"); - } - } _array_importer; -} - - - - -#endif diff --git a/src/wrapper/tools.hpp b/src/wrapper/tools.hpp index f0fd5718..98a7d8c3 100644 --- a/src/wrapper/tools.hpp +++ b/src/wrapper/tools.hpp @@ -7,7 +7,7 @@ #include #include #include -#include "numpy_init.hpp" +#include diff --git a/src/wrapper/wrap_cudadrv.cpp b/src/wrapper/wrap_cudadrv.cpp index f50f12b4..dfa3d1cd 100644 --- a/src/wrapper/wrap_cudadrv.cpp +++ b/src/wrapper/wrap_cudadrv.cpp @@ -1,3 +1,5 @@ +#define PY_ARRAY_UNIQUE_SYMBOL pycuda_ARRAY_API + #include #include @@ -656,8 +658,18 @@ void pycuda_expose_curand(); +static bool import_numpy_helper() +{ + import_array1(false); + return true; +} + + BOOST_PYTHON_MODULE(_driver) { + if (!import_numpy_helper()) + throw py::error_already_set(); + py::def("get_version", cuda_version); #if CUDAPP_CUDA_VERSION >= 2020 py::def("get_driver_version", pycuda::get_driver_version); -- GitLab