[SciPy-User] Examples how to call NumPy from C++

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Examples how to call NumPy from C++

Alexander Kalinin
Dear SciPy users,

I have some software in C++. A want to call NumPy python procedures from C++, for example some matrix multiplication procedures.

Does anybody know some examples how to pass arrays from C++ to NumPy and call procedure like matrix multiplication?

Sincerely,
Alexander

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Examples how to call NumPy from C++

Chris Barker - NOAA Federal
On Tue, Aug 11, 2015 at 3:54 AM, Alexander Kalinin <[hidden email]> wrote:
Dear SciPy users,

I have some software in C++. A want to call NumPy python procedures from C++, for example some matrix multiplication procedures.

Does anybody know some examples how to pass arrays from C++ to NumPy and call procedure like matrix multiplication?

Step one -- figure out how to call Python from C++ -- look for "embedding" -- though you may be better off  hosting the process with Python, and and callig teh C++ code from there, it's generally easier.

Step two-- figure out how to get C+= and Python (numpy) to "talk" to each other.

This can be done with the C API and numpy's C API -- and it is not too hard, but most of us find it easier and less error-prone to use a tool for the interface.

I think Cython is the best option, but as C++ person, you may prefer boost-python

Have fun!

-CHB





--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Examples how to call NumPy from C++

Sturla Molden-3
In reply to this post by Alexander Kalinin
Alexander Kalinin <[hidden email]> wrote:
 
> I have some software in C++. A want to call NumPy python procedures from
> C++, for example some matrix multiplication procedures.

If you want matrix multiplication in C++ you are better off using CBLAS
(e.g. Intel MKL, OpenBLAS, Apple Accelerate Framework), Eigen or BLISS. If
you need more linear algebra there is LAPACKE.

That said, to call NumPy from C++ you must either embed the Python
interpreter in your C++ program or extend Python with C++. The most
relevant options are Python C API, PyCXX, Boost.Python, SIP, Swig, or
Cython. Personally I prefer PyCXX or Cython, but it is a matter of taste.

Sturla

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Examples how to call NumPy from C++

Alexander Kalinin
Thank you very much for answers!

I have a little bit more complicated algorithm in NumPy than simple matrix multiplication. I have a big software in C++ and want to integrate some new math. First idea was to prototype algorithm in NumPy and next rewrite it on C++. But in NumPy implementation suddenly becomes very clear and it works pretty fast! So my idea now to embed this algorithm in C++ directly.

Yes, I known Swig, Cython and other tools. But all of them mostly do the reverse task: embed C++ into Python. I did now found any detailed examples how to solve my task: embed Python into C++.

So I found only two candidates: Python C API and boost.python with pretty new addon boot.numpy. But also there is a lack of examples how to pass and return complex numpy structures to C++.

But I believe I am not a first one who what to call NumPy from C++. May be somebody already played with such tasks.

Sincerely,
Alexander

On Thu, Aug 13, 2015 at 1:29 AM Sturla Molden <[hidden email]> wrote:
Alexander Kalinin <[hidden email]> wrote:

> I have some software in C++. A want to call NumPy python procedures from
> C++, for example some matrix multiplication procedures.

If you want matrix multiplication in C++ you are better off using CBLAS
(e.g. Intel MKL, OpenBLAS, Apple Accelerate Framework), Eigen or BLISS. If
you need more linear algebra there is LAPACKE.

That said, to call NumPy from C++ you must either embed the Python
interpreter in your C++ program or extend Python with C++. The most
relevant options are Python C API, PyCXX, Boost.Python, SIP, Swig, or
Cython. Personally I prefer PyCXX or Cython, but it is a matter of taste.

Sturla

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Examples how to call NumPy from C++

Stefan Seefeld-2
On 13/08/15 04:40 AM, Alexander Kalinin wrote:

> Thank you very much for answers!
>
> I have a little bit more complicated algorithm in NumPy than simple
> matrix multiplication. I have a big software in C++ and want to
> integrate some new math. First idea was to prototype algorithm in
> NumPy and next rewrite it on C++. But in NumPy implementation suddenly
> becomes very clear and it works pretty fast! So my idea now to embed
> this algorithm in C++ directly.
>
> Yes, I known Swig, Cython and other tools. But all of them mostly do
> the reverse task: embed C++ into Python. I did now found any detailed
> examples how to solve my task: embed Python into C++.
>
> So I found only two candidates: Python C API and boost.python with
> pretty new addon boot.numpy. But also there is a lack of examples how
> to pass and return complex numpy structures to C++.
>
> But I believe I am not a first one who what to call NumPy from C++.
> May be somebody already played with such tasks.

Alexander,

I have been using Boost.Python to bridge C++ and Python successfully.
(I'm actually the maintainer of Boost.Python, and I have mentored some
GSoC work on Boost.NumPy in the past.) I do know from my own experience
that it's possible to share arrays between the C++ and Python runtimes
(in both directions) without copies, so creating a C++ array and then
expose that as a NumPy array to the Python runtime definitely works.

For a more in-depth technical discussion I suggest the Boost.Python
mailing list.

Regards,
        Stefan

--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Examples how to call NumPy from C++

Nathaniel Smith
In reply to this post by Alexander Kalinin

On Aug 13, 2015 1:44 AM, "Alexander Kalinin" <[hidden email]> wrote:
>
> Thank you very much for answers!
>
> I have a little bit more complicated algorithm in NumPy than simple matrix multiplication. I have a big software in C++ and want to integrate some new math. First idea was to prototype algorithm in NumPy and next rewrite it on C++. But in NumPy implementation suddenly becomes very clear and it works pretty fast! So my idea now to embed this algorithm in C++ directly.
>
> Yes, I known Swig, Cython and other tools. But all of them mostly do the reverse task: embed C++ into Python. I did now found any detailed examples how to solve my task: embed Python into C++.

In general, embedding python in c/c++ is very similar to going the other way. Basically you just have to call a few functions to set up the python interpreter and then it's just the usual problems of calling python from C or vice versa, and you can use all the usual tools like cython or boost.python or whatever you like:
  https://docs.python.org/2/extending/embedding.html

You might also want to seriously think turning things around and embedding your C++ library in python instead. It's a bit more work of you're starting from an existing C++ program, but there are a lot of advantages as well. This essay is very opinionated but does do a good job of explaining why this is worth considering:
  https://twistedmatrix.com/users/glyph/rant/extendit.html

-n


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user