Warm tip: This article is reproduced from serverfault.com, please click

Does MPI_finalize release memory?

发布于 2020-12-01 13:37:56

In the following code, I have an array b which is being used in MPI. As far as I understand, each processor gets a copy of b even before the call to MPI_INIT. But what happens after we call MPI_FINALIZE? Is that piece of memory still available to each processor?

In a similar manner, what would happen if b is instead declared as a pointer, it is allocated inside MPI_INIT-MPI_FINALIZE but it is not deallocated? Is that memory still available after finalizing MPI?

program main
use mpi
implicit none

integer myr, numpr, ier
integer b(1000)

call MPI_INIT(ier)
call MPI_COMM_RANK(MPI_COMM_WORLD, myr, ier)
call MPI_COMM_SIZE(MPI_COMM_WORLD, numpr, ier)

if (myr .eq. 0) then
   !initialize b array
endif 

call MPI_BCAST(b, 100, MPI_INTEGER, 0, MPI_COMM_WORLD, ier)

call MPI_FINALIZE(ier)

!do more calculations with b

end
Questioner
armando
Viewed
0
Joe Todd 2020-12-01 21:56:52

If you imagine the code that you've written without any of the MPI stuff, you'll see that each processor starts with a B array of size 1000, because you declare it as such:

integer b(1000)

Neither MPI_Init nor MPI_Finalise are involved in allocating or deallocating any of this memory.

Likewise, you can allocate an array at run time (C) and it will stick around until you explicitly deallocate it:

PROGRAM main
use mpi
implicit none

integer myr, numpr, ier
integer b(1000)
INTEGER, ALLOCATABLE :: C(:)



call MPI_INIT(ier)
call MPI_COMM_RANK(MPI_COMM_WORLD, myr, ier)
call MPI_COMM_SIZE(MPI_COMM_WORLD, numpr, ier)

ALLOCATE(C(1000))

if (myr .eq. 0) then
  b = 100  ! Set all values to 100
  c = 99   ! Ditto 99
endif 

call MPI_BCAST(b, 1000, MPI_INTEGER, 0, MPI_COMM_WORLD, ier)
call MPI_BCAST(c, 1000, MPI_INTEGER, 0, MPI_COMM_WORLD, ier)

call MPI_FINALIZE(ier)

PRINT *, myr, B(200)
PRINT *, myr, C(200)


DEALLOCATE(C)

END PROGRAM main

produces output:

           1         100
           1          99
           0         100
           0          99

Also, note that you had a typo (I think) in your initial code. You only send the first 100 members of B (which has size 1000).