From 46e5e746842b59954178463ceac560da728453d2 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 8 Aug 2019 20:48:49 -0500 Subject: [PATCH 1/2] Fix numpy initialization. x-ref: https://github.com/inducer/pycuda/issues/214 --- src/numpy_init.hpp | 61 ------------------------------------------ src/tools.hpp | 3 +-- src/wrap_cl.cpp | 11 ++++++++ src/wrap_cl.hpp | 2 +- src/wrap_cl_part_1.cpp | 3 +++ src/wrap_cl_part_2.cpp | 3 +++ src/wrap_constants.cpp | 3 +++ src/wrap_mempool.cpp | 3 +++ 8 files changed, 25 insertions(+), 64 deletions(-) delete mode 100644 src/numpy_init.hpp diff --git a/src/numpy_init.hpp b/src/numpy_init.hpp deleted file mode 100644 index 2cf7fe07..00000000 --- a/src/numpy_init.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// Numpy import/init -// -// Copyright (C) 2009 Andreas Kloeckner -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - - -#ifndef _FAYHVVAAA_PYOPENCL_HEADER_SEEN_NUMPY_INIT_HPP -#define _FAYHVVAAA_PYOPENCL_HEADER_SEEN_NUMPY_INIT_HPP - - -// #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION -#include -#include - - -namespace -{ - static struct pyublas_array_importer - { - static bool do_import_array() - { -#ifdef PYPY_VERSION - import_array(); -#else - import_array1(false); -#endif - return true; - } - - pyublas_array_importer() - { - if (!do_import_array()) - throw std::runtime_error("numpy failed to initialize"); - } - } _array_importer; -} - - - - -#endif diff --git a/src/tools.hpp b/src/tools.hpp index 7fd906ef..f64f443f 100644 --- a/src/tools.hpp +++ b/src/tools.hpp @@ -31,8 +31,7 @@ #include #include -#include "numpy_init.hpp" - +#include diff --git a/src/wrap_cl.cpp b/src/wrap_cl.cpp index 2ea3d591..50a48201 100644 --- a/src/wrap_cl.cpp +++ b/src/wrap_cl.cpp @@ -24,6 +24,8 @@ // OTHER DEALINGS IN THE SOFTWARE. +#define PY_ARRAY_UNIQUE_SYMBOL pyopencl_ARRAY_API + #include "wrap_cl.hpp" @@ -39,8 +41,17 @@ extern void pyopencl_expose_part_1(py::module &m); extern void pyopencl_expose_part_2(py::module &m); extern void pyopencl_expose_mempool(py::module &m); +static bool import_numpy_helper() +{ + import_array1(false); + return true; +} + PYBIND11_MODULE(_cl, m) { + if (!import_numpy_helper()) + throw py::error_already_set(); + pyopencl_expose_constants(m); pyopencl_expose_part_1(m); pyopencl_expose_part_2(m); diff --git a/src/wrap_cl.hpp b/src/wrap_cl.hpp index ae59fa25..71e3d067 100644 --- a/src/wrap_cl.hpp +++ b/src/wrap_cl.hpp @@ -84,7 +84,7 @@ #include #include #include "wrap_helpers.hpp" -#include "numpy_init.hpp" +#include #include "tools.hpp" #ifdef PYOPENCL_PRETEND_CL_VERSION diff --git a/src/wrap_cl_part_1.cpp b/src/wrap_cl_part_1.cpp index a87158f9..541201e3 100644 --- a/src/wrap_cl_part_1.cpp +++ b/src/wrap_cl_part_1.cpp @@ -24,6 +24,9 @@ // OTHER DEALINGS IN THE SOFTWARE. +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pyopencl_ARRAY_API + #include "wrap_cl.hpp" diff --git a/src/wrap_cl_part_2.cpp b/src/wrap_cl_part_2.cpp index 4ef6afca..5ca5efcf 100644 --- a/src/wrap_cl_part_2.cpp +++ b/src/wrap_cl_part_2.cpp @@ -24,6 +24,9 @@ // OTHER DEALINGS IN THE SOFTWARE. +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pyopencl_ARRAY_API + #include "wrap_cl.hpp" diff --git a/src/wrap_constants.cpp b/src/wrap_constants.cpp index 48a165c0..258df278 100644 --- a/src/wrap_constants.cpp +++ b/src/wrap_constants.cpp @@ -24,6 +24,9 @@ // OTHER DEALINGS IN THE SOFTWARE. +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pyopencl_ARRAY_API + #include "wrap_cl.hpp" diff --git a/src/wrap_mempool.cpp b/src/wrap_mempool.cpp index 31cae81e..e29110ec 100644 --- a/src/wrap_mempool.cpp +++ b/src/wrap_mempool.cpp @@ -28,6 +28,9 @@ // first to prevent OS X from overriding a bunch of macros. (e.g. isspace) #include +#define NO_IMPORT_ARRAY +#define PY_ARRAY_UNIQUE_SYMBOL pyopencl_ARRAY_API + #include #include #include "wrap_helpers.hpp" -- GitLab From 6bf6bda82b4c9545960cf0abdc8f125fe828c8ce Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Thu, 8 Aug 2019 21:04:07 -0500 Subject: [PATCH 2/2] Fix numpy import for pypy --- src/wrap_cl.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wrap_cl.cpp b/src/wrap_cl.cpp index 50a48201..b9393ed0 100644 --- a/src/wrap_cl.cpp +++ b/src/wrap_cl.cpp @@ -43,7 +43,11 @@ extern void pyopencl_expose_mempool(py::module &m); static bool import_numpy_helper() { - import_array1(false); +#ifdef PYPY_VERSION + import_array(); +#else + import_array1(false); +#endif return true; } -- GitLab