MPI_Sendrecv 使用二维矩阵的问题
Issue with MPI_Sendrecv using 2D Matrix
据我所知,MPI_Sendrecv
需要两个不同的缓冲区用于发送和接收。我的以下代码将 (N/P) 行块发送到 P-1
处理器,但它不起作用并给我一个冻结的屏幕。我试图确保一切正确,但我看不出问题在哪里(我省略了变量声明以使其简短)
int **M, **FinalM, **M0;
M = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
M[i] = malloc(N * sizeof (int));
}
FinalM = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
FinalM[i] = malloc(n * sizeof (int));
}
M0 = malloc(N/P * sizeof (int *));
for (i = 0; i < N/P; i++) {
M0[i] = malloc(N * sizeof (int));
}
c = N/P; // P is Number of Processors and N rows
if (rank == 0) {
for (i = 0; i < P; i++) {
k = i*c;
k1 = (i + 1) * c;
for (j = k; j < k1; j++) {
MPI_Sendrecv(M[j], N, MPI_INT, i, TAG, FinalM[j], N, MPI_INT, i, TAG, MPI_COMM_WORLD, &status[d]);
}
}
} else {
for (i = 0; i < (N / P); i++) {
MPI_Recv(M0[i], N, MPI_INT, 0, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
请问有人可以给我一个解决这个问题的提示吗?
谢谢。
来自 https://www.open-mpi.org/doc/v1.8/man3/MPI_Sendrecv.3.php#toc7 的文档:
MPI_Sendrecv executes a blocking send and receive operation.
在您的例子中,主循环将矩阵的一部分发送给第一个工作人员并阻塞,等待工作人员响应。没有回应,所以它永远挂起。您需要在主循环中仅使用 MPI_Send
,然后再使用 MPI_Recv
的另一个循环。工作人员必须使用 MPI_Send
.
发回一些东西
据我所知,MPI_Sendrecv
需要两个不同的缓冲区用于发送和接收。我的以下代码将 (N/P) 行块发送到 P-1
处理器,但它不起作用并给我一个冻结的屏幕。我试图确保一切正确,但我看不出问题在哪里(我省略了变量声明以使其简短)
int **M, **FinalM, **M0;
M = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
M[i] = malloc(N * sizeof (int));
}
FinalM = malloc(N * sizeof (int *));
for (i = 0; i < N; i++) {
FinalM[i] = malloc(n * sizeof (int));
}
M0 = malloc(N/P * sizeof (int *));
for (i = 0; i < N/P; i++) {
M0[i] = malloc(N * sizeof (int));
}
c = N/P; // P is Number of Processors and N rows
if (rank == 0) {
for (i = 0; i < P; i++) {
k = i*c;
k1 = (i + 1) * c;
for (j = k; j < k1; j++) {
MPI_Sendrecv(M[j], N, MPI_INT, i, TAG, FinalM[j], N, MPI_INT, i, TAG, MPI_COMM_WORLD, &status[d]);
}
}
} else {
for (i = 0; i < (N / P); i++) {
MPI_Recv(M0[i], N, MPI_INT, 0, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
请问有人可以给我一个解决这个问题的提示吗? 谢谢。
来自 https://www.open-mpi.org/doc/v1.8/man3/MPI_Sendrecv.3.php#toc7 的文档:
MPI_Sendrecv executes a blocking send and receive operation.
在您的例子中,主循环将矩阵的一部分发送给第一个工作人员并阻塞,等待工作人员响应。没有回应,所以它永远挂起。您需要在主循环中仅使用 MPI_Send
,然后再使用 MPI_Recv
的另一个循环。工作人员必须使用 MPI_Send
.