在许多扩展模块之间通过F2PY共享Fortran 90模块数据 [英] Share Fortran 90 module data with F2PY between many extension modules

查看:77
本文介绍了在许多扩展模块之间通过F2PY共享Fortran 90模块数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在许多自编译的F2PY扩展模块之间共享位于Fortran 90模块中的数据. F2PY的文档说,这不可能,因为Python通常是如何导入共享库的.

I want to share data which is located in a Fortran 90 module between many self compiled F2PY extension modules. The documentation of F2PY says that this is not possible due to to how Python imports shared libraries in general.

F2PY生成包装程序中定义的通用块 签名块.链接的所有Fortran代码都可以看到通用块 使用当前扩展模块,但不适用于其他扩展模块 (此限制是由于Python如何导入共享库).

F2PY generates wrappers to common blocks defined in a routine signature block. Common blocks are visible by all Fortran codes linked with the current extension module, but not to other extension modules (this restriction is due to how Python imports shared libraries).

[...]

Fortran 90模块数据的F2PY接口类似于Fortran 77 常见块.

The F2PY interface to Fortran 90 module data is similar to Fortran 77 common blocks.

链接到文档

由于事实,我必须使用大约100个嵌套的Fortran 90子例程,因此我需要在它们之间共享数据.有什么建议可以实现这一目标吗?

Due to the fact, that I have to use about 100 nested Fortran 90 subroutines, I need to share data between them. Any suggestions how I can achieve that?

我考虑过将每个变量作为参数传递给每个子例程,然后再返回这些变量,但这听起来有点不对.

I thought about passing every variable as parameter to each subroutine and return the variables afterwards, but this sounds somehow wrong.

推荐答案

尽管只是一种反复试验的方法,但是如何将变量模块和所有子例程放入一个文件中并使用f2py(* 1)进行编译呢? ?例如...

Though just a trial-and-error approach, how about putting the variable module and all the subroutines into a single file and compile it with f2py (*1)? For example...

mytest.f90:

mytest.f90:

include "vars.f90"
include "sub1.f90"
include "sub2.f90"

vars.f90:

module vars
    integer :: n = 100
end

sub1.f90:

subroutine sub1
    use vars, only: n
    implicit none
    print *, "sub1: n = ", n
end

sub2.f90:

subroutine sub2
    use vars, only: n
    implicit none
    print *, "sub2: n = ", n
    print *, "adding 1 to n"
    n = n + 1
    print *, "n = ", n
end

编译:

f2py -c -m mytest mytest.f90

测试:

$ /usr/local/bin/python3
>>> import mytest
>>> mytest.vars.n
array(100, dtype=int32)
>>> mytest.sub1()
 sub1: n =          100
>>> mytest.sub2()
 sub2: n =          100
 adding 1 to n
 n =          101
>>> mytest.sub2()
 sub2: n =          101
 adding 1 to n
 n =          102
>>> mytest.vars.n = 777
>>> mytest.sub2()
 sub2: n =          777
 adding 1 to n
 n =          778

(* 1)在上述情况下,只需将所有文件名赋予f2py就足够了,例如

(*1) In the above case, simply giving all the file names to f2py seems sufficient, for example,

$ f2py -c -m mytest vars.f90 sub1.f90 sub2.f90

这篇关于在许多扩展模块之间通过F2PY共享Fortran 90模块数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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