How to compile Python scripts for use in FORTRAN?

旧街凉风 提交于 2019-12-19 02:03:30

问题


Although I found many answers and discussions about this question, I am unable to find a solution particular to my situation. Here it is:

I have a main program written in FORTRAN. I have been given a set of python scripts that are very useful. My goal is to access these python scripts from my main FORTRAN program. Currently, I simply call the scripts from FORTRAN as such:

CALL SYSTEM ('python pyexample.py')

Data is read from .dat files and written to .dat files. This is how the python scripts and the main FORTRAN program communicate to each other.

I am currently running my code on my local machine. I have python installed with numpy, scipy, etc.

My problem: The code needs to run on a remote server. For strictly FORTRAN code, I compile the code locally and send the executable to the server where it waits in a queue. However, the server does not have python installed. The server is being used as a number crunching station between universities and industry. Installing python along with the necessary modules on the server is not an option. This means that my “CALL SYSTEM ('python pyexample.py')” strategy no longer works.

Solution?: I found some information on a couple of things in thread Is it feasible to compile Python to machine code?

Shedskin, Psyco, Cython, Pypy, Cpython API

These “modules”(? Not sure if that's what to call them) seem to compile python script to C code or C++. Apparently not all python features can be translated to C. As well, some of these appear to be experimental. Is it possible to compile my python scripts with my FORTRAN code? There exists f2py which converts FORTRAN code to python, but it doesn't work the other way around.

Any help would be greatly appreciated. Thank you for your time.

Vincent

PS: I'm using python 2.6 on Ubuntu


回答1:


One way or another, you'll need to get the Python runtime on your server, otherwise it won't be possible to execute Python bytecode. Ignacio is on the right track with suggesting invoking libpython directly, but due to Fortran's parameter-passing conventions, it will be a lot easier for you to write a C wrapper to handle the interface between Fortran and the CPython embedding API.

Unfortunately, you're doing this the hard way -- it's a lot easier to write a Python program that can call Fortran subroutines than the other way around.




回答2:


You don't want any of those. What you should do is use FORTRAN's FFI to talk with libpython and frob its API.



来源:https://stackoverflow.com/questions/2805244/how-to-compile-python-scripts-for-use-in-fortran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!