两个变量的mpi_gather

时间:2012-12-03 17:38:02

标签: fortran mpi

我是Fortran的MPI编程新手。我想绘制一个2D图。我试图让每个处理器计算一个图形点并将其发送到root以将其写入文件。有人可以告诉我如何发送两个变量:xf(x) mpi_gather。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

正如Hristo所说的那样和“Is there anything wrong with passing an unallocated array to a routine without an explicit interface?”的例子,这就是你如何做到这一点

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