Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

fortran - MPI_Gather gives seg fault in the most basic code

I am working on a much bigger program where I struggle with MPI_Gather.

I wrote a minimal example code, see below.

 program test
  use MPI
  integer :: ierr, rank, size
  double precision, allocatable, dimension(:) :: send, recv

  call MPI_Init(ierr)

  call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
  if (ierr /= 0) print *, 'Error in MPI_Comm_size'
  call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
  if (ierr /= 0) print *, 'Error in MPI_Comm_size'

  allocate(send(1), recv(size))

  send(1) = rank

  call MPI_Gather(send, 1, MPI_DOUBLE_PRECISION, &
                  recv, 1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD)
  print *, recv
  call MPI_Finalize(ierr)
end program test

When (with 2 nodes) I get the following error output.

[jorvik:13887] *** Process received signal ***
[jorvik:13887] Signal: Segmentation fault (11)
[jorvik:13887] Signal code: Address not mapped (1)
[jorvik:13887] Failing at address: (nil)
[jorvik:13888] *** Process received signal ***
[jorvik:13888] Signal: Segmentation fault (11)
[jorvik:13888] Signal code: Address not mapped (1)
[jorvik:13888] Failing at address: (nil)
[jorvik:13887] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x36150) [0x7f6ab77f8150]
[jorvik:13887] [ 1] /usr/lib/libmpi_f77.so.0(PMPI_GATHER+0x12d) [0x7f6ab7ebca9d]
[jorvik:13887] [ 2] ./test() [0x4011a3]
[jorvik:13887] [ 3] ./test(main+0x34) [0x401283]
[jorvik:13887] [ 4] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f6ab77e376d]
[jorvik:13887] [ 5] ./test() [0x400d59]
[jorvik:13887] *** End of error message ***
[jorvik:13888] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x36150) [0x7f0ca067d150]
[jorvik:13888] [ 1] /usr/lib/libmpi_f77.so.0(PMPI_GATHER+0x12d) [0x7f0ca0d41a9d]
[jorvik:13888] [ 2] ./test() [0x4011a3]
[jorvik:13888] [ 3] ./test(main+0x34) [0x401283]
[jorvik:13888] [ 4] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f0ca066876d]
[jorvik:13888] [ 5] ./test() [0x400d59]
[jorvik:13888] *** End of error message ***

What am I doing wrong? MPI is definitely installed and running on the machine I am using.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The big problem is that you did not include the last arg ierr in the call to MPI_Gather. The doc said

All MPI routines in Fortran (except for MPI_WTIME and MPI_WTICK) have an additional argument ierr at the end of the argument list.

In addition to that, my advise is to always stick to good practice: Do not use intrinsic funtion names for your variable, example of size.

program test
    use MPI
    integer :: ierr, rank, nProc
    double precision, allocatable, dimension(:) :: send, recv

    call MPI_Init(ierr)
    if (ierr /= 0) print *, 'Error in MPI_Init'

    call MPI_Comm_size(MPI_COMM_WORLD, nProc, ierr)
    if (ierr /= 0) print *, 'Error in MPI_Comm_size'
    call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
    if (ierr /= 0) print *, 'Error in MPI_Comm_size'

    allocate(send(1), recv(nProc))

    send(1) = rank

    call MPI_Gather(send, 1, MPI_DOUBLE_PRECISION, &
                    recv, 1, MPI_DOUBLE_PRECISION, 0, MPI_COMM_WORLD, ierr)
    if (ierr /= 0) print *, 'Error in MPI_Gather'
    print *, recv
    call MPI_Finalize(ierr)
end program test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...