如何编译用于FORTRAN的Python脚本? [英] How to compile Python scripts for use in FORTRAN?

查看:1665
本文介绍了如何编译用于FORTRAN的Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我发现了许多关于这个问题的答案和讨论,但我无法找到特定于我的情况的解决方案。这是:



我有一个主程序用FORTRAN编写。
我已经给了一组非常有用的python脚本。
我的目标是从我的主FORTRAN程序访问这些python脚本。目前,我只需从FORTRAN调用脚本:



CALL SYSTEM('python pyexample.py')



数据从.dat文件读取并写入.dat文件。这是python脚本和主FORTRAN程序如何相互通信。



我目前正在我的本地机器上运行我的代码。我有python安装numpy,scipy等。



我的问题:
代码需要在远程服务器上运行。对于严格的FORTRAN代码,我在本地编译代码并将可执行文件发送到服务器,它在队列中等待。但是,服务器没有安装python。服务器被用作大学和工业之间的数字处理站。在服务器上安装python和必要的模块不是一个选项。这意味着我的CALL SYSTEM('python pyexample.py')策略不再有效。



解决方案:
我发现一些信息线程中的事情将Python编译为机器代码是否可行?



Shedskin,Psyco,Cython,Pypy,Cpython API



不知道这是什么称为他们)似乎编译python脚本到C代码或C ++。显然不是所有的python特性可以转换为C.同样,其中一些似乎是实验性的。是否可以用我的FORTRAN代码编译我的Python脚本?有f2py将FORTRAN代码转换为python,但是它不工作。



任何帮助将非常感谢。非常感谢您的时间。



Vincent



PS:我在Ubuntu上使用python 2.6

解决方案

无论如何,你需要在服务器上获取Python运行时,否则将无法执行Python字节码。 Ignacio在正确的轨道上,建议直接调用libpython,但是由于Fortran的参数传递约定,你将更容易编写一个C包装器来处理Fortran和CPython嵌入API之间的接口。

不幸的是,你这样做是困难的 - 编写一个Python程序可以调用Fortran子程序比其他方式更容易。


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

解决方案

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.

这篇关于如何编译用于FORTRAN的Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆