Pythran is a Python-to-C++ compiler for a subset of Python that includes partial numpy support. It acts a little like Numba and Cython—you annotate a function’s arguments, and then it takes over with further type annotation and code specialization. It takes advantage of vectorization possibilities and of OpenMP-based parallelization possibilities.
In some examples I show how to use it from inside python to optimize it, but i am wondering if it is possible to use it for translating python code to c++...
Can it do so? What if the functions I want to use depend on another one? What if the other functions are imported from a separate module? Is there an example / tutorial of such a process?
There's a blog post from Jean Laroche achieving just what you want to do: https://serge-sans-paille.github.io/pythran-stories/pythran-as-a-bridge-between-fast-prototyping-and-code-deployment.html
If you can get the code to compile with Pythran decorators (not always easy), then getting the C++ code is very simple:
pythran -e my-module.py
The article linked below somehow the author says something about not having to decorate anything. Not sure here.
But yes it will give you the massively templated C++ code likely with a NumPy DLL dependency and probably a Python DLL one too (apparently when NOT making a Python module, you can skip this). Maybe an OpenBLAS or MKL lib to link to, maybe an OpenMP one. Sure you could somehow avoid all these DLLs if you code it properly, although you wouldn't get massive accelerations. Good luck reading it though, it's not very legible. But hey, it is a .cpp file dependent on tons of Pythran templates...
I wouldn't use Pythran for generating C++ code that you use in other projects; it's main purpose is for making fast Python modules. But I suppose since it's easy enough to generate .cpp files you could. But auto-generated code is always a bit hard to follow because you'll get typedef variables like type __type0 to type __type200 or something of that nature, and one declaration may refer to 5 of these variables. That's not to discount the beauty and functionality of Pythran; it's a wonderful project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With