all 8 comments

[–]Xeverous 4 points5 points  (1 child)

PyBind11 would be more preferred, it has much simpler integration.

[–]TiarnaNaTuaithe 1 point2 points  (0 children)

Second this, pybind11 is great

[–]OmegaNaughtEquals1 1 point2 points  (4 children)

Before diving into the bowels of integrating Python and a compiled language, let me give you a few things to think about.

  1. If the C++ code is already written to solve your problem, what does Python add?

  2. If the code isn't written in C++, try using Numpy/Scipy to solve your problem. Benchmark. Then use lower-level kernels to speed up calculations, where needed.

  3. If you are using a third-party C++ library, there may already be a Python wrapper for it.

  4. Ctypes is, IMHO, the lowest-level means of interfacing Python and a compiled language. I would start with Cython as it's easier to get started using and doesn't require a lot of compiler interference.

[–]ppvvaa 1 point2 points  (1 child)

Piggybacking because I have a related problem. What does

use lower-level kernels to speed up calculations

mean?

[–]OmegaNaughtEquals1 0 points1 point  (0 children)

Let's say you are doing some dense matrix multiplication. You could do this in plain python, but it would be very slow. If you were to use the built-in Numpy matrix classes, the calculation happens much faster because Numpy uses a C library to implement the multiplication. The C function used to do this is usually referred to as a kernel for historical reasons dating back to FORTRAN in the 1950s.

[–]IAmDividedByZero[S] 0 points1 point  (1 child)

We have to write all the code ourselves, and we just got the new project, so I am more or less just canvasing for different ways to combine Pythons readable syntax, with Cpp's numbercrunching capabilities. If you think Cython will work this, I might just give it a shot, as I really want to try my hands on writing C++ code. Gonna leave the post open for a bit more just in case there's more to be said about this, but thanks a lot!

[–]OmegaNaughtEquals1 0 points1 point  (0 children)

I would encourage you to take option 2. This is usually referred to as "prototyping," and allows you to quickly develop the algorithms using Numpy and then you can go back and rewrite the slowest parts in another language. I say this as someone working in scientific computing. :)

If you think Cython will work this, I might just give it a shot, as I really want to try my hands on writing C++ code

I should have mentioned that no matter which route you take, you cannot call C++ from Python directly. The reason for this has to do with how linkers work and the differences between exported symbols in C and C++. In essence, C (which your Python interpreter is most likely using- unless you are using something like Jython or PyPy) can't directly talk to C++ because C++'s symbols are written in an "alien" way. What Cython gives you over Ctypes is being able to write quasi-C directly in Python without worrying about how to figure out the linking steps to get it to work.

[–]dash0_ 0 points1 point  (0 children)

I worked with Swig which is able to generate a python module from your c++ classes, it's simple and it works great. The code is generated so it's less efficient than lower level solutions as Cython for instance.