如何在MPI中的struct中发送指针 [英] How to send pointer in struct in MPI

查看:61
本文介绍了如何在MPI中的struct中发送指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:

typedef struct
{
    int x;
    double *y;
    int **z;
}
ind;

如何通过 MPI 将 *y 和 **z 之类的指针发送到其他进程?我知道很多答案都说从不通过 MPI 发送指针.但是如果我不能将 *y 更改为数组,因为它在主程序的其他部分使用,我应该怎么做才能通过 MPI 通过进程传输它们?特别是**z,我该怎么办?提前致谢!

how could I send pointer like *y and **z via MPI to other processes? I know that many answers said that never send pointers by MPI. But if I cannot change *y to an array because it is used in other part of the main program, what should I do to transfer them through processes via MPI? Especially for **z, how should I do ? Thanks in advance!

推荐答案

只需遵循第二个示例中的代码 这里,我做了以下事情.我相信这就是你想要的.

Just following the code from the second example here, I did the following. I believe this is what you want.

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char *argv[]) {
  int *send, *recv;
  int rank, i;
  MPI_Status status;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  send = malloc(sizeof(int)*10);
  recv = malloc(sizeof(int)*10);

  for (i=0;i<10;i++)
    send[i] = 5*i;

  if (rank == 0)
    MPI_Send(send, 10, MPI_INT, 1, 0, MPI_COMM_WORLD);
  else {
    MPI_Recv(recv, 10, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);

    for (i=0;i<10;i++)
      printf("%d\n", recv[i]);
  }

  MPI_Finalize();
  return 0;
}

运行它输出,

$ mpiexec -n 2 mpi_test
0
5
10
15
20
25
30
35
40
45

现在你只需要根据你自己的问题来调整它.

Now you just have to adapt it to your own problem.

这篇关于如何在MPI中的struct中发送指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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