我想使用MPI_Sendrecv执行矩阵转置,但是在运行时,它会等待并且程序不会结束。这是我的代码:

...

void sendrecvelem(int **A, int **R, int p, int elements, int part) {
  int i,j,c=0;
  for(i=1;i<p;i++) {
    for(j=0;j<part;j++) {
      MPI_Sendrecv(A[c*part], j, MPI_INT, i, j, &R[elements][c*part], j, MPI_INT, i, j, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    }
    c++;
  }
}

int main(int argc, char ** argv) {
  MPI_Init(&argc, &argv);
  int p,id;
  MPI_Comm_size(MPI_COMM_WORLD, &p);
  MPI_Comm_rank(MPI_COMM_WORLD, &id);
  int ** data=NULL;
  int elements=10,i,j;
  int **A=NULL;
  int part=elements/(p-1);
  int **R=NULL;

  if(id == 0) {
    A = allocMatrix(elements,elements);
    R = allocMatrix(elements, elements);
    init(A,elements,elements);
    printelements(A, elements, elements);
    sendrecvelem(A, R, p, elements, part);
    printelements(R, elements, elements);
  }

  MPI_Finalize();
  return 0;
}

最佳答案

MPI_Sendrecv是一项阻止操作,只有总大小rank o中的p对其进行调用,因此阻止了该应用程序。

MPI_Comm_rank(MPI_COMM_WORLD, &id);
if(id == 0) {
    A = allocMatrix(elements,elements);
    R = allocMatrix(elements, elements);
    init(A,elements,elements);
    printelements(A, elements, elements);

    sendrecvelem(A, R, p, elements, part);

    /* MPI_Sendrecv is called only by rank 0. Rank 0 sends part of array to next rank
    and since MPI_Sendrecv is blocking, it waits for other process to receive data.
    Since this entire function is only called by rank 0 (if(id == 0)),
    the application will wait which is an expected behaviour in this case. */


    printelements(R, elements, elements);
  } else {
   /* one solution is to put an else block (which will be called by all prcess other than 0)
   and call MPI_Sendrecv */
   sendrecvelem(A, R, p, elements, part);

  }

关于c++ - 如何使用MPI_Sendrecv进行矩阵转置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59339474/

10-11 00:35