只阵宣布根进程 [英] Array declared only on root process
问题描述
在MPI使用Fortran具体而言,是有可能的,并且只有在根流程定义的一个不错的选择数组?例如这样的事情:
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
正如您可以猜到,我已经尝试过这一点,这是行不通的,如Fortran的声明必须在最前面。但我希望解决的办法?
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?
推荐答案
正如你指出,你不能在声明语句IF
-blocks或之后的任何地方声明块的过程。不过,声明上的所有进程的数组 ALLOCATABLE
,并且仅适用于某些分配是允许的,在我看来一个不错的选择。
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.
这篇关于只阵宣布根进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!