使用MPI接收数组

时间:2017-11-19 01:20:35

标签: c arrays parallel-processing mpi

我正在尝试创建一个并行程序,合并对从不同进程发送给彼此的两个数组进行排序。在这个简化版本中,我试图让通信工作,我希望简单地从进程0向进程1发送一个数组(长度为4个无符号整数),然后在进程1中打印本地和接收数组。是这个的代码。 (Load_and_distribute只是填充数组,我已经检查过以确保两个进程确实有四个无符号整数)。

int
main(int argc, char ** argv)
{
  int ret;
  unsigned int ln, tn;
  unsigned int * lvals;
  int rank, size;

  ret = MPI_Init(&argc, &argv);
  assert(MPI_SUCCESS == ret);

/* get information about MPI environment */
  ret = MPI_Comm_size(MPI_COMM_WORLD, &size);
  assert(MPI_SUCCESS == ret);
  ret = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  assert(MPI_SUCCESS == ret);

  load_and_distribute(argv[1], &ln, &lvals);

  unsigned int rn;
  unsigned int * rvals;

  rvals = malloc(4*sizeof(*rvals));

 if(rank == 0){
    MPI_Send(&lvals, 4, MPI_UNSIGNED, 1, 0, MPI_COMM_WORLD);
  }
  else if (rank == 1){
    rvals[0] = 4;
    MPI_Recv(&rvals, 4, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    printf("lvals = %d %d %d %d\n",lvals[0],lvals[1],lvals[2],lvals[3]);
    printf("rvals = %d %d %d %d\n",rvals[0],rvals[1],rvals[2],rvals[3]);
  } 

  ret = MPI_Finalize();
  assert(MPI_SUCCESS == ret);

  return EXIT_SUCCESS;
}

发送和接收似乎没有合适,但是当它试图打印rval值时,我到达了这个输出,我不确定为什么。

[hpc5:04355] *** Process received signal ***
[hpc5:04355] Signal: Segmentation fault (11)
[hpc5:04355] Signal code: Address not mapped (1)
[hpc5:04355] Failing at address: 0xe0c4ac
[hpc5:04355] [ 0] /lib64/libpthread.so.0(+0xf370)[0x7f2a8d23c370]
[hpc5:04355] [ 1] ./hms_mpi[0x40165d]
[hpc5:04355] [ 2] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7f2a8ce8db35]
[hpc5:04355] [ 3] ./hms_mpi[0x400c29]
[hpc5:04355] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 4355 on node hpc5 exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

MPI_Send()MPI_Recv()的正确缓冲区为lvalsrvals(例如使用&关键字)

答案 1 :(得分:0)

删除&在你的MPI_Send和MPI_Recv中:

MPI_Send(lvals, 4, MPI_UNSIGNED, 1, 0, MPI_COMM_WORLD);

MPI_Recv(rvals, 4, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

它的工作原理如下:

int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)

buf:发送缓冲区的初始地址(选择)*