F2PY - parameter of the access module from the subroutine

I can not get f2py to refer to the parameter from the module in a separate routine, where it is used to determine the size of the input array. That is, the parameter is damaged in the module:

! File: testmod.f90 MODULE testmod INTEGER, PARAMETER :: dimsize = 20 END MODULE testmod 

and the dimsize parameter should be specified in the subroutine (NOT contained in the module) in another file, which will be the entry point for my python module:

 ! File testsub.f90 SUBROUTINE testsub(arg) USE testmod REAL, INTENT(IN) :: arg(dimsize) END SUBROUTINE testsub 

I compile like this:

 f2py -m testmod -h testmod.pyf testsub.f90 pgf90 -g -Mbounds -Mchkptr -c -fPIC testmod.f90 -o testmod.o pgf90 -g -Mbounds -Mchkptr -c -fPIC testsub.f90 -o testsub.o f2py -c testmod.pyf testmod.o testsub.o 

but get this error:

 testmodmodule.c: In function 'f2py_rout_testmod_testsub': testmodmodule.c:180: error: 'dimsize' undeclared (first use in this function) 

I tried modifying testub.g90 to include the following directive as suggested by ni other posts:

 SUBROUTINE testsub(arg) USE testmod !f2py integer, parameter :: dimsize REAL, INTENT(IN) :: arg(dimsize) END SUBROUTINE testsub 

but to no avail. I need to leave the subroutine separate from the module.

How can I get f2py to correctly enable the dimsize variable?

TIA

+7
python fortran f2py
source share
2 answers

It has been a long time since this question got any activity, but I solved the problem, so I decided to publish it for everyone who has this problem. The problem is that although Fortran code is completely believable, the C shell generated by F2PY does not know what dimsize is when it checks the correct size of the array (in the C shell it is encapsulated in some module structure).

Just change this code:

 SUBROUTINE testsub(arg) USE testmod !f2py integer, parameter :: dimsize REAL, INTENT(IN) :: arg(dimsize) END SUBROUTINE testsub 

:

 SUBROUTINE testsub(arg) USE testmod !f2py integer, intent(aux) :: dimsize REAL, INTENT(IN) :: arg(dimsize) END SUBROUTINE testsub 

This will "Define the helper variable C in the F2PY generated wrapper." As Scipy docs say, it’s β€œuseful to store parameter values ​​so that they can be accessed when initializing the expression of other variables.”

I did not test this with your code, but I tested it with a similar situation that I had.

+2
source share

Although I have not tested this, I think you almost have it with the source code. We do something similar for some of our codes, but with gfortran.

You do not need the f2py file testmod.f90. You just need to compile it into an object file just as if it were a regular Fortran:

 pgf90 -g -Mbounds -Mchkptr -c -fPIC testmod.f90 -o testmod.o 

Then you should compile your testub.f90 into a module suitable for use in python using

 f2py --fcompiler=pgf90 --f90flags="-g -Mbounds -Mchkptr" -c testsub.f90 -m testsub testmod.o 

This should build testub.so or the equivalent, allowing you to import testsub and then testsub.testsub(my_arg) in python.

+1
source share

All Articles