Main program is in python invoking MPI by m4py. Then we call fortran subroutines from the main program.
This example call subroutines in fmath.f90 from the program hello.py. That is, we combine hello.py and fortran subroutines. We have two files setcomm.py and m_comm.py, which are keys to combine them.
To run example, we generate mklloc.txt in advance;
mpif90 dummy.f90 -lmkl_rt;ldd a.out|grep mkl>mklloc.txt
Step 1. Generate ecaljF.so from fortran source codes.
$ mpif90 -shared -fPIC -o ecaljF.so *.f90
or
$ mpif90 -c -fPIC m_comm.f90 -o m_comm.o
$ mpif90 -c -fPIC fmath.f90 -o fmath.o
$ mpif90 -shared ecaljF.so m_comm.o fmath.o
Step 2. Call hellop.py in the usual manner.
$ mpiexec -n 4 python3 ./hello.py
- We can run fortran subrouines successively in hello.py. We can supply only logical,integer, and real(8) when we call a fortran subrouine. No return values from fortran.
- All the fortran codes can know rank,size, and comm in m_comm module. Let fortran main programs to be subroutines without arguments but with bind(C). See fmath.f90.
- See our main program hello.py. We init MPI by mpi4py in setcomm.py.
In the techniques in this sample, we can easily use a python code as the main routine. We call fortran programs (subroutines now) successively. Look into hello.py. Since fortran subroutines are conneced python code, we can keep data in modules even when we call nexr fortran subroutine in python. This is as if we keep data in module even when we have finished fortran main program!
- compilar option -fpic - non-position-dependent code.
- Evvensubroutine foobar bind(C) is Global, even when you define foobar bind(C) in a module, it is global.
- subroutines in module is global from python. Thus we can call subroutine hello2, without specifing use foobar.
This code is inspired by discussion with Atsushi Togo and NAKANO Kosuke in NIMS. And I read https://mnakao.net/data/2018/HPFPC.pdf.