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

Garry Willgoose Garry.Willgoose@newcastle.edu...
Mon Feb 11 23:52:51 CST 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.

module fm1
   subroutine modify(myvalue)
   use fm3
   real :: myvalue
   end subroutine modify

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

module fm2
   subroutine modify(myvalue)
   use fm3
   real, intent(in) :: myvalue
   end subroutine modify

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

module fm3
real :: value=3.0
end module fm3

The result of running the following script is

import fm1,fm2
print 'original',result1,result2
print 'fm1 allocate',result1,result2
print 'fm2 allocate',result1,result2


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

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@newcastle.edu.au;  
email-for-life: garry.willgoose@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