gpt4 book ai didi

c++ - 如何使用 MPI_Sendrecv 进行矩阵转置?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:12 25 4
gpt4 key购买 nike

我想使用 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 是一个阻塞操作,只有总大小 p 中的 rank o 调用它,因此阻塞了应用程序。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com