Splitting of MPI communicators

有时候为了创建2D的拓扑,需要对MPI_COMM_WORLD进行划分。所使用的MPI函数是:

MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm *newcomm) 

例子程序(出自MPI Groups, Communicators and Topologies):

#include <stdio.h> 
#include <mpi.h>

main (int argc, char **argv)
{
    MPI_Comm row_comm, col_comm;
    int myrank, size, P=4, Q=3, p, q;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    p = myrank / Q;
    q = myrank % Q;

    /* Split comm intor row and column comms */
    MPI_Comm_split(MPI_COMM_WORLD, p, q, &row_comm);
    MPI_Comm_split(MPI_COMM_WORLD, q, p, &col_comm);

    int sum, rowsum, colsum;
    sum = myrank;
    rowsum = myrank;
    colsum = myrank;
    MPI_Allreduce(MPI_IN_PLACE, &sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
    MPI_Allreduce(MPI_IN_PLACE, &rowsum, 1, MPI_INT, MPI_SUM, row_comm);
    MPI_Allreduce(MPI_IN_PLACE, &colsum, 1, MPI_INT, MPI_SUM, col_comm);
    if (myrank == 0)
        printf("The sum of all rank is %d\n", sum);

    printf("[%d]: My coordinates are (%d, %d), row sum is %d, col sum is %d\n",myrank,p,q,
rowsum,colsum);
    MPI_Finalize();
}


参考资料:

1. http://www.cs.utexas.edu/~pingali/CS378/2011sp/lectures/MPIGroups.ppt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值