Fortran 两个变量的mpi_聚集

Fortran 两个变量的mpi_聚集,fortran,mpi,Fortran,Mpi,我不熟悉用Fortran进行MPI编程。我想画一个二维图。我试图让每个处理器计算一个图形点,并将其发送给root用户,以便将其写入文件。谁能告诉我如何发送两个变量,即:x和f(x)与mpi\u聚集。谢谢你的帮助 作为赫里斯托所说的话和“这是你可能会怎么做”的一个例子 Program gather Use mpi Implicit None Integer, Dimension( :, : ), Allocatable :: result Integer, Dimensio

我不熟悉用Fortran进行MPI编程。我想画一个二维图。我试图让每个处理器计算一个图形点,并将其发送给root用户,以便将其写入文件。谁能告诉我如何发送两个变量,即:
x
f(x)
mpi\u聚集
。谢谢你的帮助

作为赫里斯托所说的话和“这是你可能会怎么做”的一个例子

Program gather

  Use mpi

  Implicit None

  Integer, Dimension( :, : ), Allocatable :: result

  Integer, Dimension( 1:2 ) :: buffer

  Integer :: me, nprocs, error
  Integer :: x, fx

  Call mpi_init( error )

  Call mpi_comm_rank( mpi_comm_world, me    , error )
  Call mpi_comm_size( mpi_comm_world, nprocs, error )

  If( me == 0 ) Then
     Allocate( result( 1:2, 1:nprocs ) ) !Naughty - should check stat
  Else
     Allocate( result( 1:0, 1:0 ) )      !Naughty - should check stat
  End If

  x  = me
  fx = x * x

  buffer( 1 ) = x
  buffer( 2 ) = fx

  Call mpi_gather( buffer, 2, mpi_integer, &
                   result, 2, mpi_integer, &
                   0, mpi_comm_world, error )

  If( me == 0 ) Then
     Write( *, '( 99999( i3, 1x ) )' ) result( 1, : ) 
     Write( *, '( 99999( i3, 1x ) )' ) result( 2, : ) 
  End If

  Call mpi_finalize( error )

End Program gather
Wot now? mpif90 gather.f90
Wot now? mpirun -np 7 ./a.out
  0   1   2   3   4   5   6
  0   1   4   9  16  25  36

MPI\u聚集
适用于阵列。只需将两个值放在一个包含2个元素的数组中。更优雅的方法是定义一个Fortran类型来保存这两个值,然后使用
MPI\u type\u create\u struct
创建一个结构化的MPI类型。