MPI_Gather 在最基本的代码中给出了段错误 [英] MPI_Gather gives seg fault in the most basic code

查看:22
本文介绍了MPI_Gather 在最基本的代码中给出了段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个更大的程序,我在其中遇到了 MPI_Gather.

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

当(有 2 个节点)我得到以下错误输出.

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 ***

我做错了什么?MPI 肯定已在我使用的机器上安装并运行.

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

推荐答案

最大的问题是您没有在对 MPI_Gather 的调用中包含最后一个 arg ierr.医生说

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

Fortran 中的所有 MPI 例程(MPI_WTIME 和 MPI_WTICK 除外)在参数列表的末尾都有一个额外的参数 ierr.

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

除此之外,我的建议是始终坚持良好的做法:不要为变量使用内部函数名称,例如 size.

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

这篇关于MPI_Gather 在最基本的代码中给出了段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆