MPI_Gather gives seg fault in the most basic code

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

Leave a Comment