作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
特别是在 Fortran 中的 MPI 中,仅在根进程上定义数组是否可能并且是一个不错的选择?例如这样的事情:
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 中,声明需要放在最前面。但我希望有办法解决这个问题?
这样,数组也不会占用我的“虚拟”内存,对吗?还是我误解了什么?
最佳答案
正如您所指出的,您不能在 IF
block 中或过程声明 block 之后的任何位置使用声明语句。但是,在所有进程上将数组声明为 ALLOCATABLE
,并且仅在某些进程上分配是允许的,在我看来这是一个不错的选择。
! 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
这样,程序就不会消耗其他进程的任何额外内存。
关于arrays - 仅在根进程上声明的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18744667/
我是一名优秀的程序员,十分优秀!