[Numpy-discussion] f2py: sharing F90 module data between modules

Garry Willgoose Garry.Willgoose at newcastle.edu.au
Tue Feb 12 00:52:51 EST 2008


I have a suite of fortran modules that I want to wrap with f2py  
independently (so they appear to python as seperate imports) but  
where each module has access to another fortran module (which  
contains global data that is shared between the suite of fortran  
modules). I currently compile all the fortran modules, including the  
global data, in a single f2py statement so all the fortran code gets  
imported in a single statement

import tsim

where tsim is the python name of the aggregated modules. What I would  
like to do is as below, where fm1, fm2 are two example modules that  
will be called from python, and fm3 has the global data shared  
between fm1, fm2.

<fm1.f90>
module fm1
contains
   subroutine modify(myvalue)
   use fm3
   real :: myvalue
   value=myvalue
   end subroutine modify

   real function result()
   use fm3
   result=value
   end function result
end module fm1

<fm2.f90>
module fm2
contains
   subroutine modify(myvalue)
   use fm3
   real, intent(in) :: myvalue
   value=myvalue
   end subroutine modify

   real function result()
   use fm3
   result=value
   end function result
end module fm2

<fm3.f90>
module fm3
real :: value=3.0
end module fm3

The result of running the following script is

import fm1,fm2
result1=fm1.fm1.result()
result2=fm2.fm2.result()
print 'original',result1,result2
x=10.0
fm1.fm1.modify(x)
result1=fm1.fm1.result()
result2=fm2.fm2.result()
print 'fm1 allocate',result1,result2
x=20.0
fm2.fm2.modify(x)
result1=fm1.fm1.result()
result2=fm2.fm2.result()
print 'fm2 allocate',result1,result2


is

original 3.0 3.0
fm1 allocate 10.0 3.0
fm2 allocate 10.0 20.0


which clearly shows that fm1, and fm2 have independent images of fm3  
(I vaguely recall some time back finding docs that said that f2py/ 
python loaded each imported into seperate memory and common/modules  
couldn't be shared between python modules). As I mentioned if  
fm1,fm2,fm3 are all compiled in a single f2py statement (so they are  
in a single python module) then this works fine.

I guess the question is there any way that I can get fm3 to be shared  
between fm1 and fm2? The reasons for wanting to do this are because  
I'm developing a plug-in like architecture for environmental  
modelling where the user can develop new fortran modules (suitably  
f2py'ed) that can just be dropped into the module search path but  
still have access to the global data (subject to fortran module  
interfaces, etc).


====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c2im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au;  
g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson
====================================================================








More information about the NumPy-Discussion mailing list