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

2

5

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?

Hoopes answered 5/8, 2019 at 9:47 Comment(0)
C
5

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

Contingence answered 19/8, 2019 at 13:8 Comment(0)
C
1

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.

Countable answered 6/3, 2022 at 15:54 Comment(1)
And getting rid of the templates is probably tricky? There's an old post by Serge showing an example where what you get out is "pure" C++, but attempting to redo that example with Pythran of today gives a completely different result.Isochronize

© 2022 - 2024 — McMap. All rights reserved.