问题
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 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).
[...]
The F2PY interface to Fortran 90 module data is similar to Fortran 77 common blocks.
Link to Documentation
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.
回答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:
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
Compile:
f2py -c -m mytest mytest.f90
Test:
$ /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) 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
来源:https://stackoverflow.com/questions/52950218/share-fortran-90-module-data-with-f2py-between-many-extension-modules