Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to / How to get the c++ code generated from running pythran on python

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?

like image 746
ntg Avatar asked Aug 05 '19 09:08

ntg


2 Answers

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

like image 155
serge-sans-paille Avatar answered Oct 03 '22 07:10

serge-sans-paille


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.

like image 32
Matt Avatar answered Oct 03 '22 07:10

Matt