Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 使用mpi中的可分配数组发送派生类型数据时出现seg错误_Fortran_Mpi_Allocatable Array - Fatal编程技术网

Fortran 使用mpi中的可分配数组发送派生类型数据时出现seg错误

Fortran 使用mpi中的可分配数组发送派生类型数据时出现seg错误,fortran,mpi,allocatable-array,Fortran,Mpi,Allocatable Array,我正在尝试发送mpi中带有可分配数组的派生类型数据。ad出现seg错误 program test_type use mpi implicit none type mytype real,allocatable::x(:) integer::a end type mytype type(mytype),allocatable::y(:) type(mytype)::z integer::n,i,ierr,myid,ntasks,status,request in

我正在尝试发送mpi中带有可分配数组的派生类型数据。ad出现seg错误

program test_type  

 use mpi

 implicit none

 type mytype
  real,allocatable::x(:)
  integer::a
 end type mytype

 type(mytype),allocatable::y(:)
 type(mytype)::z
 integer::n,i,ierr,myid,ntasks,status,request
 integer :: datatype, oldtypes(2), blockcounts(2) 
 integer(KIND=MPI_ADDRESS_KIND) :: offsets(2)

 call mpi_init(ierr)
 call mpi_comm_rank(mpi_comm_world,myid,ierr)
 call mpi_comm_size(mpi_comm_world,ntasks,ierr)

 n=2

 allocate(z%x(n))

 if(myid==0)then
  allocate(y(ntasks-1))
  do i=1,ntasks-1
   allocate(y(i)%x(n))
  enddo
 else
  call random_number(z%x)
  z%a=myid
  write(0,*) "z in process", myid, z%x, z%a
 endif

 call mpi_get_address(z%x,offsets(1),ierr)
 call mpi_get_address(z%a,offsets(2),ierr)
 offsets=offsets-offsets(1)

 oldtypes=(/ mpi_real,mpi_integer /)
 blockcounts=(/ n,1 /)

 write(0,*) "before commit",myid,offsets,blockcounts,oldtypes
 call mpi_type_create_struct(2,blockcounts,offsets,oldtypes,datatype,ierr) 
 call mpi_type_commit(datatype, ierr)
 write(0,*) "after commit",myid,datatype, ierr

 if(myid==0) then   
  do i=1,ntasks-1 
   call mpi_irecv(y(i),1,datatype,1,0,mpi_comm_world,request,ierr) 
   write(0,*) "received", y(i)%x,y(i)%a
  enddo
 else
  call mpi_isend(z,1,datatype,0,0,mpi_comm_world,request,ierr) 
  write(0,*) "sent"
  write(0,*) myid, z%x, z%a
 end if

 call mpi_finalize(ierr)

end program
这是我通过两个过程打印出来的:

before commit           0                     0             -14898056
           2           1          13           7
 after commit           0          73           0
 z in process           1  3.9208680E-07  2.5480442E-02           1
 before commit           1                     0            -491689432
           2           1          13           7
 after commit           1          73           0
 received  0.0000000E+00  0.0000000E+00           0
forrtl: severe (174): SIGSEGV, segmentation fault occurred
它似乎得到了负地址偏移量。请帮忙。
谢谢。

此代码存在多个问题

大多数Fortran编译器中的可分配数组类似于C/C++中的指针:数组名称后面的真实对象包含指向已分配数据的指针。该数据通常分配在堆上,并且可以在进程的虚拟地址空间中的任何位置,这解释了负偏移量。顺便说一句,负偏移量在MPI数据类型中是完全可以接受的(这就是为什么
MPI\u ADDRESS\u KIND
指定有符号整数类型),所以这里没有大问题

更大的问题是,动态分配的对象之间的偏移量通常随每次分配而变化。您可以检查:

ADDR(y(1)%x) - ADDR(y(1)%a)
完全不同于

ADDR(y(i)%x) - ADDR(y(i)%a), for i = 2..ntasks-1
ADDR
这里只是由
MPI\u GET\u address
返回的对象地址的shorhand符号)

即使偏移量与
i
的某些值匹配,这与其说是规则,不如说是巧合

这会导致以下情况:使用
z
变量的偏移量构造的类型不能用于发送
y
数组的元素。要解决此问题,只需删除
mytype%x的可分配属性(如果
n
事先已知),即可

对于较小的
ntasks
值,另一个很好的选择是定义与
y
数组元素数量相同的MPI数据类型。然后使用基于偏移量
y(i)%x
y(i)%a
datatype(i)
,发送
y(i)

更严重的问题是,您使用的是非阻塞MPI操作,并且在访问数据缓冲区之前从不等待它们完成。这段代码根本不起作用:

do i=1,ntasks-1 
 call mpi_irecv(y(i),1,datatype,1,0,mpi_comm_world,request,ierr) 
 write(0,*) "received", y(i)%x,y(i)%a
enddo
调用
MPI\u IRECV
启动异步接收操作。在执行
WRITE
操作符时,操作可能仍在进行中,因此访问的是完全随机的数据(某些内存分配器可能在调试模式下实际将数据归零)。在
MPI ISEND
WRITE
调用之间插入对
MPI\u WAIT
的调用,或者使用阻塞接收
MPI\u RECV

使用非阻塞发送调用时也存在类似的问题。由于您从不等待请求或测试的完成,因此允许MPI库无限期地推迟操作的实际进程,并且发送可能永远不会实际发生。同样,由于在您的情况下绝对没有理由使用非阻塞发送,请将
MPI\u ISEND
替换为
MPI\u send

最后但并非最不重要的一点是,秩0仅从秩1接收消息:

call mpi_irecv(y(i),1,datatype,1,0,mpi_comm_world,request,ierr)
                              ^^^
同时,所有其他进程都将发送到秩0。因此,您的程序只有在使用两个MPI进程运行时才能工作。您可能需要将接收呼叫中带下划线的
1
替换为
i