编译多个模块时,import_array()出现numpy/CAPI错误 [英] Numpy/CAPI error with import_array() when compiling multiple modules

查看:723
本文介绍了编译多个模块时,import_array()出现numpy/CAPI错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译在scipy.weave中使用的C ++模块,该模块由几个头文件和源C ++文件组成.这些文件包含广泛使用Numpy/C-API接口的类和方法.但是我无法弄清楚如何成功包含import_array().在过去的一周里,我一直在为此苦苦挣扎,而且我疯了.希望您能为我提供帮助,因为weave

I am trying to compile a C++ module to use in scipy.weave that is composed of several headers and source C++ files. These files contain classes and methods that extensively use the Numpy/C-API interface. But I am failing to figure out how to include import_array() successfully. I have been struggling on this for the past week and I am going nuts. I hope you could help me with it because the weave help is not very explanatory.

在实践中,我首先有一个名为pycapi_utils的模块,其中包含一些例程,用于将C对象与Python对象进行接口连接.它由头文件pycapi_utils.h和源文件pycapi_utils.cpp组成,例如:

In practice I have first a module called pycapi_utils that contains some routines to interface C objects with Python objects. It consists of a header file pycapi_utils.h and a source file pycapi_utils.cpp such as:

//pycapi_utils.h
#if ! defined _PYCAPI_UTILS_H
#define _PYCAPI_UTILS_H 1

#include <stdlib.h>
#include <Python.h>
#include <numpy/arrayobject.h>
#include <tuple>
#include <list>

typedef std::tuple<const char*,PyObject*> pykeyval; //Tuple type (string,Pyobj*) as dictionary entry (key,val)
typedef std::list<pykeyval> kvlist;                    

//Declaration of methods
PyObject* array_double_to_pyobj(double* v_c, long int NUMEL); //Convert from array to Python list (double)
...
...
#endif

//pycapi_utils.cpp

#include "pycapi_utils.h"

PyObject* array_double_to_pyobj(double* v_c, long int NUMEL){
    //Convert a double array to a Numpy array
    PyObject* out_array = PyArray_SimpleNew(1, &NUMEL, NPY_DOUBLE);
    double* v_b = (double*) ((PyArrayObject*) out_array)->data;
    for (int i=0;i<NUMEL;i++) v_b[i] = v_c[i];
    free(v_c);
    return out_array;
}

然后我还有另一个模块model,其中包含处理某些数学模型的类和例程.同样,它由标题和源文件组成,例如:

Then I have a further module model that contains classes and routines dealing with some mathematical model. Again it consists of a header and source file like:

//model.h
#if ! defined _MODEL_H
#define _MODEL_H 1

//model class
class my_model{
    int i,j;
    public:
        my_model();
        ~my_model();
        double* update(double*); 
}

//Simulator   
PyObject* simulate(double* input);
#endif  

//model.cpp

#include "pycapi_utils.h"
#include "model.h"

//Define class and methods
model::model{
...
...
}

...
...

double* model::update(double* input){
    double* x = (double*)calloc(N,sizeof(double));
    ...
    ...

    // Do something
    ...
    ...

    return x;
}

PyObject* simulate(double* input){
    //Initialize Python interface 
    Py_Initialize;
    import_array();

    model random_network;
    double* output;

    output = random_network.update(input);
    return array_double_to_pyobj(output);  // from pycapi_utils.h     
}

上面的代码包含在Python的scipy.weave模块中,

The above code is included in a scipy.weave module in Python with

def model_py(input):
    support_code="""
                 #include "model.h"
                 """
    code = """
           return_val = simulate(input.data());
           """
    libs=['gsl','gslcblas','m']
    vars = ['input']
    out = weave.inline(code,
                       vars,
                       support_code=support_code,
                       sources = source_files,
                       libraries=libs
                       type_converters=converters.blitz,
                       compiler='gcc',
                       extra_compile_args=['-std=c++11'],
                       force=1) 

它无法编译给出的内容:

It fails to compile giving:

error: int _import_array() was not declared in this scope

值得注意的是,如果我同时将pycapi_utils.h和源文件pycapi_utils.cpp都塞进了文件,那么一切都很好.但是我不想使用此解决方案,因为实际上我的模块需要包含在其他几个也使用PyObjects并需要调用import_array()的模块中.

Noteworthy is that if I lump into pycapi_utils.h also the source pycapi_utils.cpp, everything works fine. But I don't want to use this solution, as in practice my modules here need to be included in several other modules that also use PyObjects and need call import_array().

我一直希望在这篇文章上堆栈交换,但是我无法弄清楚在我的情况下是否以及如何正确定义#define指令.同样,该帖子中的示例并不完全符合我的情况,在main()的全局范围内调用import_array(),而在我的情况下,在simulate例程中调用import_array(),该例程由main() build调用通过scipy.weave.

I was looking to this post on stack exchange, but I cannot figure out if and how to properly define the #define directives in my case. Also the example in that post is not exactly my case as there, import_array() is called within the global scope of main() whereas in my case import_array() is called within my simulate routine which is invoked by main() build by scipy.weave.

推荐答案

我遇到了类似的问题,正如您所发布的链接所指出的那样,万恶的根源在于PyArray_API被定义为静态的,这意味着每个翻译单元都有自己的PyArray_API,默认情况下会用PyArray_API = NULL对其进行初始化.因此,必须为每个*.cpp文件调用一次import_array().在您的情况下,在pycapi_utils.cpp中调用它,并在model.cpp中调用一次就足够了.您还可以在实际使用以下命令调用前,测试是否需要array_import:

I had a similar problem, as the link you've posted points out, the root of all evil is that the PyArray_API is defined static, which means that each translation unit has it's own PyArray_API which is initialized with PyArray_API = NULL by default. Thus import_array() must be called once for every *.cpp file. In your case it should be sufficient to call it in pycapi_utils.cpp and also once in model.cpp. You can also test if array_import is necessary before actualy calling it with:

if(PyArray_API == NULL)
{
    import_array(); 
}

这篇关于编译多个模块时,import_array()出现numpy/CAPI错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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