0

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. 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. 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)? It seems Ctypes wouldn't work (correct me if I'm wrong) so does this have to be rewritten in Cython?

For reference, this is the C part with all the datatypes I'm trying to wrap:

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>());  
Matt
  • 2,602
  • 13
  • 36

1 Answers1

0

With ctypes you can load DLL dynamically and call functions from it. From my point of view, it's easiest way for using compiled code within Python code. Here is how: How can I use a DLL file from Python?

numpy allows you to get pointer to memory location. Look at that: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/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.

Community
  • 1
  • 1
George Sovetov
  • 4,942
  • 5
  • 36
  • 57
  • Yes thanks I did see those posts earlier, and you're right I can't access the source code of the DLL - on your 3rd point, that's basically where I'm heading. Are you saying I create a new C++ DLL which links to the boost::multi_array DLL, using the NumPy pointer to get past the data format required by ctypes? – Matt Mar 28 '16 at 21:03
  • @Matt Yes, if it's possible to interpret (or convert) raw pointer with known shape (dimensions) as `boost::multi_array`, you can create your own DLL that has similar function but accepts pointer and dimensions as simple C types and then calls function from 3rd-party DLL. Creating this DLL has nothing common with Python, other libraries or other boost functionality. – George Sovetov Mar 28 '16 at 21:08
  • I believe that may be easy for you but I'm trying to simply use the template library which already maps NumPy to boost::multi_array directly without having to write another wrapper for ctypes. But I think you've given me an idea on how to possibly approach this... – Matt Mar 28 '16 at 21:30
  • And it sounds like you suggest doing something like this to interface it with ctypes: http://stackoverflow.com/a/5868051/6037118 but I'm thinking Cython in this instance may be easier since my C skills-> boost format aren't great. – Matt Mar 28 '16 at 21:58
  • @Matt That's great that you get something useful from my answer. And yes, first and second part of what I propose is same as in that answer. – George Sovetov Mar 28 '16 at 22:33
  • 1
    Yes thank you didn't even think of creating a DLL to access the DLL with a wrapper, just need to collect the right headers for the DLL I have no source for then will try to use the template library for NumPy -> Boost to compile that library and access through Cython or Ctypes depending on the work involved. Appreciate the help! I do wonder if others would benefit from a Ctypes wrapper that also wraps external DLLs in C++ where one imports the Ctypes C++ wrapper, then the specific DLL, and if that would actually work for multiple projects that would be a nice Python extension. – Matt Mar 29 '16 at 00:42