Fortran+mpi ; initialization of some variables before mpi_ini()

Hello,

I have a question about initializing some variables before the
mpi_init() call.

Parameters can be initialized before mpi_init() , however,
does the standard allow initiailze variables that are not parameters be initialized before mpi_init ().

This is exemplified in the code below:

   module arr_m

! parameters can be initialized before mpi_init
integer,parameter :: MX = 4
integer,parameter :: ip(MX)=(/10,11,12,14/)
! can below vars be initialized before MPI_INIT ???
integer :: iv(MX) = (/100,101,102,103/)
integer :: NN = MX
integer :: iv3(MX) = ip + (/3,3,3,3/)
end module arr_m

   PROGRAM main
   use mpi
   use arr_m
   integer rank, nprocs, ierr,

   call MPI_init(ierr)
   call MPI_comm_size(MPI_COMM_WORLD, nprocs, ierr)
   call MPI_comm_rank(MPI_COMM_WORLD, rank, ierr)

    print*,rank, ' MX=',MX
    print*,rank, ' ip=',ip
    call mpi_barrier(MPI_COMM_WORLD,ierr)

    print*,rank, ' NN=',NN
    print*,rank, ' iv =',iv
    print*,rank, ' iv3=',iv3

   call MPI_finalize(ierr)
   END PROGRAM

Specifically, MX and ip, which are parameters, are initialized before mpi_init ,

Basically, my question is if the variables NN and iv
can also be initialized inside module (before mpi_init)

or they should be initialized after mpi_init

Basically, my question is if the variables NN and iv can also be initialized inside module (before mpi_init) or they should be initialized after mpi_init

MPI is a self-contained library with no dependencies on how the Fortran code initializes its data, either static data initialization or initialization at runtime. In other words, sure, the variables can be statically initialized.

Are you encountering any issues?

For example, assuming a real variable a,

in order to get a=4.0 on all mpi threads, can variable a be initialized before call mpi_init() line below:

a=4.0
call MPI_init( mpi_comm_world, ierr)

or it Has to be always after mpi_init, after :

call MPI_init ( MPI_comm_world, ierr)
a=4.0


i though both are the OK, and achieve the same thing: a=4 on all mpi threads.

The question seems to suggest that you’re under the impression that “a” is a single variable shared by all ranks?

Each MPI rank is a separate process and has it’s own distinct address space. Hence it doesn’t matter if “a” is set before or after MPI_init since each rank will be applying it to it’s own “a”, not a common “a”.

You do use the incorrect term “MPI thread”, which may be the basis of your misunderstanding. A process (aka “heavy-weight process”) has it’s own address space, while threads (aka “light-weight processes”) run within a process and shares the address space. A MPI rank is a specific process, not a thread.