问题
I have struct like this:
typedef struct
{
    int x;
    double *y;
    int **z;
}
ind;
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!
回答1:
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;
}
Running it outputs,
$ 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.
来源:https://stackoverflow.com/questions/32217553/how-to-send-pointer-in-struct-in-mpi