首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用MPI_Isend发送多条非阻塞消息,用C语言MPI_Recv接收的问题

用MPI_Isend发送多条非阻塞消息,用C语言MPI_Recv接收的问题
EN

Stack Overflow用户
提问于 2020-01-17 09:02:02
回答 1查看 493关注 0票数 0

我正在编写一个并行算法,并且我对非阻塞通信有一个问题.我通过以下代码对问题进行建模:

代码语言:javascript
复制
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行。

这是输出的一部分:

代码语言:javascript
复制
...
...
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)的方式进行计算,然后再发送给其他处理器,以便他们能够尽快开始计算。

你知道我能为解决这个问题做些什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-19 04:06:29

谢谢你的回答。它能让我解决我的问题。我需要使用MPI_Ibsend来分配所需的缓冲区空间,以便将数据复制到其中,直到数据交付为止。

代码语言:javascript
复制
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;
}

这是输出的一部分:

代码语言:javascript
复制
...
...
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59784093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档