我正在编码并行算法,并且在无阻塞通信方面存在问题。我通过以下代码对问题进行建模:
int main( int argc, char* argv[] ) {
MPI_Init(&argc, &argv);
int rank, p;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &p );
int a,b, i, j;
int maxNumber = 8192;
int(*tab)[maxNumber] = malloc(sizeof(int[maxNumber + 1][maxNumber + 1]));
MPI_Request* r = malloc(sizeof * r);
if(rank == 0){
for(i = 0; i < maxNumber + 1; i++){
for(j = 0; j < maxNumber + 1; j++){
tab[i][j] = 2*i+i*j;
}
for(a = 1; a < p; a++){
MPI_Isend(&tab[i], maxNumber + 1, MPI_INT, a, i, MPI_COMM_WORLD, r);
printf("Process 0 send the block %d to process %d\n", i, a);
}
}
}
else{
for(i = 1; i < p; i++){
if(rank == i){
for(j = 0; j < maxNumber + 1; j++){
printf("Process %d wait the block %d to process 0\n", i, j);
MPI_Recv(&tab[j], maxNumber + 1, MPI_INT, 0, j, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d receive the block %d to process 0\n", i, j);
}
}
}
}
MPI_Finalize();
return 0;
}
处理器0经过一些计算后,将大小为8192 * 8192的矩阵的每一行发送给其他处理器。问题是处理器0在其他处理器接收到数据之前完成了发送8192行的操作。
这是输出的一部分:
...
...
Process 0 send the block 8187 to process 1
Process 0 send the block 8188 to process 1
Process 0 send the block 8189 to process 1
Process 0 send the block 8190 to process 1
Process 0 send the block 8191 to process 1
Process 0 send the block 8192 to process 1
Process 1 receive the block 5 to process 0
Process 1 wait the block 6 to process 0
Process 1 receive the block 6 to process 0
Process 1 wait the block 7 to process 0
Process 1 receive the block 7 to process 0
Process 1 wait the block 8 to process 0
Process 1 receive the block 8 to process 0
Process 1 wait the block 9 to process 0
Process 1 receive the block 9 to process 0
...
...
PS:对于发送,通信必须是非阻塞的,因为在我的问题中,进程0在每次迭代中以O(n²/p²)进行计算,然后再将其发送给其他处理器,以便它们尽快开始计算。尽可能。
请您知道我该怎么做才能解决此问题?
感谢@Gilles的回答。它使我能够解决我的问题。我需要使用MPI_Ibsend分配所需数量的缓冲区空间,直到将数据交付为止,该副本空间可以复制到其中。
int main( int argc, char* argv[] ) {
MPI_Init(&argc, &argv);
int rank, p;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &p );
int a, i, j;
int maxNumber = atoi(argv[1]);
int(*tab)[maxNumber] = malloc(sizeof(int[maxNumber + 1][maxNumber + 1]));
MPI_Request* tabReq = malloc(maxNumber * sizeof * tabReq);
int bufsize = maxNumber * maxNumber;
char *buf = malloc( bufsize );
if(rank == 0){
for(i = 0; i < maxNumber + 1; i++){
for(j = 0; j < maxNumber + 1; j++){
tab[i][j] = 2*i+i*j;
}
for(a = 1; a < p; a++){
MPI_Buffer_attach( buf, bufsize );
MPI_Ibsend(&tab[i], maxNumber + 1, MPI_INT, a, i, MPI_COMM_WORLD, &tabReq[i]);
MPI_Buffer_detach( &buf, &bufsize );
printf("Process 0 send the block %d to process %d\n", i, a);
}
}
}
else{
for(j = 0; j < maxNumber + 1; j++){
printf("Process %d wait the block %d to process 0\n", rank, j);
MPI_Recv(&tab[j], maxNumber + 1, MPI_INT, 0, j, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d receive the block %d to process 0\n", rank, j);
}
}
MPI_Finalize();
return 0;
}
这是输出的一部分:
...
...
Process 1 wait the block 8186 to process 0
Process 0 send the block 8185 to process 1
Process 1 receive the block 8186 to process 0
Process 1 wait the block 8187 to process 0
Process 0 send the block 8186 to process 1
Process 1 receive the block 8187 to process 0
Process 1 wait the block 8188 to process 0
Process 0 send the block 8187 to process 1
Process 1 receive the block 8188 to process 0
Process 1 wait the block 8189 to process 0
Process 0 send the block 8188 to process 1
Process 1 receive the block 8189 to process 0
Process 1 wait the block 8190 to process 0
Process 0 send the block 8189 to process 1
Process 1 receive the block 8190 to process 0
Process 1 wait the block 8191 to process 0
Process 0 send the block 8190 to process 1
Process 1 receive the block 8191 to process 0
Process 1 wait the block 8192 to process 0
Process 0 send the block 8191 to process 1
Process 1 receive the block 8192 to process 0
Process 0 send the block 8192 to process 1