简体   繁体   English

Python NumPy数组可增强::: multi_array numpy_boost设置以在C ++和NumPy之间建立接口

[英]Python NumPy arrays to boost::multi_array numpy_boost setup to interface between C++ and NumPy

I have a C/C++ DLL with libraries I need to call from NumPy, where the format inside the DLL is all boost::multi_array format. 我有一个C / C ++ DLL,其中包含需要从NumPy调用的库,其中DLL内的格式均为boost :: multi_array格式。 While I have seen some posts involving this project: https://github.com/mdboom/numpy-boost there really is very little documentation and examples from users out there on the steps required to wrap the boost::multi_array to NumPy interface with this library. 虽然我看到了一些与此项目有关的帖子: https : //github.com/mdboom/numpy-boost,但实际上很少有用户提供的文档和示例,这些示例和示例涉及将boost :: multi_array与NumPy接口包装在一起所需的步骤。这个图书馆。 My questions: while the examples are enough to guess the C++ part of the interface (other than strings, how is this handled? Q1), what do you have to do on the Python side to use the compiled DLL (Q2)? 我的问题是:尽管这些示例足以猜测接口的C ++部分(除了字符串,这是如何处理的?Q1),但是在Python端您必须做什么才能使用编译的DLL(Q2)? It seems Ctypes wouldn't work (correct me if I'm wrong) so does this have to be rewritten in Cython? 看来Ctypes不起作用(如果我错了,请纠正我),所以是否必须在Cython中重写它?

For reference, this is the C part with all the datatypes I'm trying to wrap: 作为参考,这是C部分,其中包含我要包装的所有数据类型:

extern "C"
{
    DECLDIR void Cfunction(
        boost::multi_array<double, 2>& p_result,
        const vector<string>&          p_calcType,
        const string&                  p_optionType,
        unsigned long                  p_nTimeStep = 50,
        const vector<double>&          p_premium = vector<double>());  

With ctypes you can load DLL dynamically and call functions from it. 使用ctypes可以动态加载DLL并从中调用函数。 From my point of view, it's easiest way for using compiled code within Python code. 从我的角度来看,这是在Python代码中使用已编译代码的最简单方法。 Here is how: How can I use a DLL file from Python? 方法如下: 如何使用Python中的DLL文件?

numpy allows you to get pointer to memory location. numpy允许您获取指向内存位置的指针。 Look at that: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.ndarray.ctypes.html 看一下: http : //docs.scipy.org/doc/numpy-1.10.1/reference/genic/numpy.ndarray.ctypes.html

If you don't have source code of your DLL and cannot change its interface, you create C++ wrapper (your own separate DLL) that accepts raw pointer from Python instead of boost::multi_array . 如果您没有DLL的源代码并且无法更改其接口,则可以创建C ++包装器(您自己的单独的DLL),该包装器接受来自Python的原始指针而不是boost::multi_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM