modulefortranmpisubroutine

Updated: Should I put MPI in a module or a module's subroutine?


Updated: I have a problem, and I don't know what it is. I have a test program with MPI_INIT and MPI_FINALIZE in its body. I have a module that contains 5 subroutines: 3 subroutines are dependent, and independent from 2 other subroutines. I want to put the MPI code in the test program into this module. I put MPI_INIT in the module where the variables are declared and before the subroutine. I obtain a series of errors with the same error message:

This statement must not appear in the specification part of a module

How does "MPI_INIT and MPI_FINALIZE should be called only once" affect Fortran program, modules, and subroutines? Where should I put MPI functions and variables if there are multiple, independent programs, each calling this module's subroutines multiple number of times?

~~~~~~~~~ I have a module that contains a series of subroutines, which contain do loops that I wish to parallelize. The subroutines are public that other programs use. Should I define MPI outside the subroutines:

module ...
call MPI_INIT
subroutine 1
... (MPI code)
subroutine 2
subroutine 3
MPI_GATHERV
call MPI_FINALIZE
module

or inside each subroutine?

module ...
subroutine 1
call MPI_INIT
... (MPI code)
MPI_GATHERV
call MPI_FINALIZE
subroutine 2
call MPI_INIT
... (MPI code)
MPI_GATHERV
call MPI_FINALIZE
subroutine 3
call MPI_INIT
... (MPI code)
MPI_GATHERV
call MPI_FINALIZE
module

I see the advantage of following the coarse grain principle for solution 1. If a program calls subroutine 1, will it also execute MPI codes outside the subroutine?


Solution

  • You should initialize and finalize MPI exactly once in your program. After calling MPI_Finalize you are not allowed to do further MPI actions. The standard says:

    Once MPI_FINALIZE returns, no MPI routine (not even MPI_INIT) may be called, except for MPI_GET_VERSION, MPI_GET_LIBRARY_VERSION, MPI_INITIALIZED, MPI_FINALIZED, and any function with the prefix MPI_T_ (within the constraints for functions with this prefix listed in Section 14.3.4).

    (MPI3, p361, l25) MPI3 PDF

    Reply to the update: You are not allowed to put executable statements into the declaration part of your code. The point, that there should be just one call to MPI_Init and MPI_Finalize in your execution means exactly that. Your application could read something like that:

    program mini
      use mpi
      implicit none
      integer :: iError
      call mpi_init(iError)
      call do_some_stuff()
      call mpi_finalize(iError)
    end program mini
    

    If you have various initialization stuff you want to do in the beginning of the program, you can of course combine it in module subroutine and call mpi_init in there. If you use a test program for your module, use mpi_init and mpi_finalize there. An example for the call of mpi_init and mpi_finalize in some subroutines can be found for example in the env_module of the treelm library which we use to set up very general stuff.

    Where should I put MPI functions and variables if there are multiple, independent programs, each calling this module's subroutines multiple number of times?

    Could you rephrase that? I don't get it. MPI functions and variables are supposed to be in the mpi module, if you have multiple independent programs calling them, they all have to "use" the mpi module. Independent programs are also fine to have the MPI_Init and MPI_Finalize each in itself. Maybe you could post a short code example, what you want to achieve and what your problem is.