Array declared only on root process

2019-02-15 10:15发布

问题:

In MPI in Fortran specifically, is it possible and a good choice to have an array defined only on the root process? For example something like this:

program test
implicit none

include 'mpif.h'

all mpi_init(ierr)

call mpi_comm_rank(mpi_comm_world,myid,ierr)
call mpi_comm_size(mpi_comm_world,numproc,ierr) 

if (myid .eq. 0) then
    complex(8), dimension(:,:), allocatable :: array
end if

   ...

if (myid .eq. 0) then
    allocate(array(2,2))
end if

   ...

end program

As you can guess, I already tried this and it does not work, as in Fortran declarations need to be on top. But I was hoping for a way around this?

This way, the array would also not eat into my "virtual" memory right? Or am I misunderstanding something?

回答1:

As you pointed out, you cannot have declaration statements in IF-blocks or anywhere after the declaration block of the procedure. However, declaring the array as ALLOCATABLE on all processes, and allocating only on some is allowed, and in my opinion a good choice.

! Declare as allocatable on all processes
complex(8), dimension(:,:), allocatable :: array

   ...

! Allocate only on some
if (myid .eq. 0) then
    allocate(array(2,2))
end if

This way, the program will not consume any additional memory on other processes.



回答2:

Do you need the (full) array on each process? If not, try to allocate it in chunks: Proc0: 1--10, Proc1: 11--20, etc. Then, each process only needs to allocate its chunk. You might need a way of mapping between global indices (1--20 in this example) and local indices (1--10 for each process).

Of course, you need to collect all chunks from all processes to the root process at some point (e.g. I/O), but you might be able to do this sequentially without any further memory.