如何在C ++标头和源文件中使用NumPy C-API? [英] How can I use the NumPy C-API in both C++ header and source file?

查看:366
本文介绍了如何在C ++标头和源文件中使用NumPy C-API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost :: Python将Python代码提供给C ++库.我有一个将C ++类型转换为Python类型的模板函数:

I am making Python code available to a C++ library using Boost::Python. I have a template function that converts C++ types to Python types:

template <typename T> bp::object convert(T v);

专门用于各种原始类型以及一些模板化类.一个这样的类是N维数组,对此我有一个函数可以转换为NumPy数组.我想在相应的convert专业化中使用此功能,例如:

that is specialized for various primitive types as well as some templated classes. One such class is an N-dimensional Array, for which I have a function to convert to a NumPy Array. I'd like to use this function in a corresponding convert specialization, e.g.:

template <typename Y> bp::object convert(NDArray<Y> v);

我的主要问题是,由于此转换函数是模板化的,因此需要保留在标头中,但是它使用NumPy的PyArray_函数,该函数要求在使用前先调用import_array().当前在构造函数中为单例对象调用import_array(),其目的是提供对Python函数的访问.看来这是行不通的,因为默认情况下,#include <numpy/arrayobject.h>仅使PyArray_函数可用于当前编译单元.我尝试为标头定义PY_ARRAY_UNIQUE_SYMBOL并定义NO_IMPORT_ARRAY,但这不会阻止PyArray_函数进行段错误.

My primary issue is that this conversion function then needs to live in a header since it's templated, but it uses NumPy's PyArray_ functions, which require import_array() to be called prior to usage. import_array() is currently called in the constructor for a singleton object whose purpose is to provide access to Python functions. It appears this won't work because, by default, #include <numpy/arrayobject.h> only makes the PyArray_ functions available to the current compilation unit. I've tried defining a PY_ARRAY_UNIQUE_SYMBOL and defining NO_IMPORT_ARRAY for the header, but this doesn't prevent PyArray_ functions from segfaulting.

这是我的代码的简化表示,它在"conversions.h"标头中使用PyArray_函数时会出现段错误:

Here's a simplified representation of my code, which segfaults upon using a PyArray_ function in the "conversions.h" header:

"conversions.h":

"conversions.h":

#include <boost/python.hpp>
#include <numpy/numpyconfig.h>
#include <numpy/arrayobject.h>

namespace bp = boost::python;

template <typename T> bp::object convert(T v);
template <> bp::object convert<int>(int v) { return bp::long_(v); }
...
template <typename Y> bp::object convert(NDArray<Y> v)
{
... use PyArray_ functions to create and return a NumPy array
... segfaults here!
}

"Bridge.h":

"Bridge.h":

#include "conversions.h"

class Bridge {
public:
    static Bridge* instance();

    // c++11 variadic template (parameter pack)
    template <typename... Args> void exec(Args... args)
    {
        ...
        fn(convert(args)...); // fn is a Python function
        ...
    }
    void foo();

private:
    Bridge();
    Bridge(const Bridge&);
    void operator=(const Bridge&);
    static Bridge* instance_;
}

"Bridge.cpp":

"Bridge.cpp":

#include "Bridge.h"
#include <numpy/numpyconfig.h>
#include <numpy/arrayobject.h>

Bridge* Bridge::instance_ = nullptr;
Bridge* Bridge::instance() {
    if (!instance_) { instance_ = new Bridge(); }
    return instance_;
}
Bridge::Bridge() {
    Py_Initialize();
    _import_array();
    ...
}
void Bridge::foo() {
    ... // other stuff using PyArray functions
}

"main.cpp":

"main.cpp":

#include "Bridge.h"

int main(void)
{
    NDArray my_array(...);
    Bridge::instance()->exec(42, "hello", my_array); 
    return 0;
}

推荐答案

自那以后,我了解到一个问题是,对PyArray函数的调用应与对import_array的调用在同一编译单元中进行(NumPy初始化函数).

I've since learned that one problem is that the calls to the PyArray functions should happen in the same compilation unit as the call to import_array (the NumPy initialization function).

解决此问题的一种方法是在内部包装" PyArray_*函数并使用它们,而不是直接使用NumPy API.

One way to solve this problem is to "wrap" the PyArray_* functions internally and use them rather than the NumPy API directly.

可能找到了另一种解决方法此处.

There may be another solution found here.

我的解决方案:

创建一个"numpy_wrappers.h"文件:

Create a "numpy_wrappers.h" file:

...
#include "numpy/ndarraytypes.h"

int NumPyArray_NDIM(PyObject* obj);
npy_intp NumPyArray_DIM(PyObject* obj, int i);
void *NumPyArray_DATA(PyObject* obj);
...

然后通过在与您调用的源文件相同的文件中包装"原始功能来实现这些功能 import_array(NumPy初始化函数):

Then implement these by "wrapping" the original functions in the same source file as your call to import_array (the NumPy initialization function):

...
Bridge::Bridge() {
    Py_Initialize();
    _import_array();
    ...
}
...
/// Wraps PyArray_NDIM
int NumPyArray_NDIM(PyObject* obj)
{
    return PyArray_NDIM((PyArrayObject*)obj);
}

/// Wraps PyArray_DIM
npy_intp NumPyArray_DIM(PyObject* obj, int i)
{
    return PyArray_DIM((PyArrayObject*)obj, i);
}

/// Wraps PyArray_DATA
void* NumPyArray_DATA(PyObject* obj)
{
    return PyArray_DATA((PyArrayObject*)obj);
}
...

然后可以在模板标题中使用它们,如下所示:

Then they can be used in template headers, like so:

...
template <typename Y> bp::object convert(NDArray<Y> v)
{
... use NumPyArray_ functions to create and return a NumPy array
... No more segfaults!
}
...

您可以在此处上看到深入的实现,一个用于在某些C ++ STL类型和Python标准类型之间无缝转换的工具箱.

You can see an in-depth implementation of this here, a toolbox for seamlessly converting between some C++ STL types and Python standard types.

这篇关于如何在C ++标头和源文件中使用NumPy C-API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆