MPI_Gatherv 的使用 发送接收不同大小的数据块

MPI_Gatherv可以从不同进程收集大小不同的数据块,但是在网上看到的都是发送固定大小,偏移量不同的例子,而官网给出的例子,虽然大小可变,但是需要重新定义数据类型,对于连续数据比较麻烦。

下面的代码按照如下思路

1.每个进程根据进程号初始化一块数据,发送数据大小与进程号有关(myid+1)

2.首先使用Gather将每个进程要发送数据大小发给root进程,以方便偏移量的计算

3.在root进程里,计算偏移量

4.调用MPI_Gatherv,(其中发送数据是变量,每个进程不一样,否则会出现错误)

int main(int argc, char *argv[]) {

    int myid;       //process id number
    int p;       //number of process
    MPI_Init(&argc, &argv); //parallel init
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    const int n = 100;
    int sendBuf[100], *recvBuf;
    int *displs, *recvCount;
    int root = 0;
    //数据初始化,每个进程发送进程号+1的数据
    int senddatanum;
    for (int i = 0; i < n; i++) {
        sendBuf[i] = myid;
        senddatanum= myid+1;
    }
    displs = (int *) malloc(sizeof(int) * p); // displs
    recvCount = (int *) malloc(sizeof(int) * p); // send num
    //首先将不同进程发送数据块大小信息获得,以便后续偏移量的计算
    MPI_Gather(&senddatanum, 1, MPI_INT, recvCount, 1, MPI_INT,root, MPI_COMM_WORLD);
    if(!myid)
    {
        for(int i=0;i<p;i++)
            printf("%d \n",recvCount[i]);
        //计算偏移
        displs[0] = 0;
        displs[1] = displs[0]+recvCount[0];
        displs[2] = displs[1] + recvCount[1];
        displs[3] = displs[2] + recvCount[2];
        displs[4] = displs[3] + recvCount[3];
    }


    recvBuf = (int *) malloc(p * p * sizeof(int));
    memset(recvBuf, 0, sizeof(int) * p * p);

    //senddatanum需要与每个进程发送的数据量相等

    MPI_Gatherv(sendBuf, senddatanum, MPI_INT, recvBuf, recvCount, displs, MPI_INT,root, MPI_COMM_WORLD);


    if (myid == 0) {
        for (int i = 0; i < p*p; i++)
            printf("%d ", recvBuf[i]);
    }
    MPI_Finalize();

    return 0;

}

 

Output:

使用5个进程执行之后输出如下:

1
2
3
4
5
0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 0 0 0 0 0 0 0 0 0 0

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用MPI_Scatterv和MPI_Gatherv计算矩阵乘法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <mpi.h> #define MATRIX_SIZE 4 int main(int argc, char* argv[]) { int rank, size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); if (size != MATRIX_SIZE) { printf("Error: the number of processes must be %d\n", MATRIX_SIZE); MPI_Abort(MPI_COMM_WORLD, 1); } int matrix_a[MATRIX_SIZE][MATRIX_SIZE] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; int matrix_b[MATRIX_SIZE][MATRIX_SIZE] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; int local_matrix_a[MATRIX_SIZE][MATRIX_SIZE/MATRIX_SIZE]; int local_matrix_b[MATRIX_SIZE/MATRIX_SIZE][MATRIX_SIZE]; int local_matrix_c[MATRIX_SIZE/MATRIX_SIZE][MATRIX_SIZE/MATRIX_SIZE]; int sendcounts[MATRIX_SIZE]; int displs[MATRIX_SIZE]; // scatter matrix_a for (int i = 0; i < MATRIX_SIZE; i++) { int local_cols = MATRIX_SIZE / size; sendcounts[i] = local_cols; displs[i] = i * local_cols; } MPI_Scatterv(matrix_a, sendcounts, displs, MPI_INT, local_matrix_a, MATRIX_SIZE * MATRIX_SIZE / size, MPI_INT, 0, MPI_COMM_WORLD); // scatter matrix_b for (int i = 0; i < MATRIX_SIZE; i++) { int local_rows = MATRIX_SIZE / size; sendcounts[i] = local_rows; displs[i] = i * local_rows; } MPI_Scatterv(matrix_b, sendcounts, displs, MPI_INT, local_matrix_b, MATRIX_SIZE * MATRIX_SIZE / size, MPI_INT, 0, MPI_COMM_WORLD); // calculate local_matrix_c for (int i = 0; i < MATRIX_SIZE/MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE/MATRIX_SIZE; j++) { local_matrix_c[i][j] = 0; for (int k = 0; k < MATRIX_SIZE; k++) { local_matrix_c[i][j] += local_matrix_a[i][k] * local_matrix_b[k][j]; } } } // gather matrix_c MPI_Gatherv(local_matrix_c, MATRIX_SIZE * MATRIX_SIZE / size, MPI_INT, matrix_c, sendcounts, displs, MPI_INT, 0, MPI_COMM_WORLD); if (rank == 0) { printf("Matrix C:\n"); for (int i = 0; i < MATRIX_SIZE; i++) { for (int j = 0; j < MATRIX_SIZE; j++) { printf("%d ", matrix_c[i][j]); } printf("\n"); } } MPI_Finalize(); return 0; } ``` 在这个示例中,矩阵A和矩阵B都是4x4的矩阵。我们将使用4个进程来计算矩阵乘积,每个进程计算结果的一部分。使用MPI_Scatterv将矩阵A和矩阵B分发到各个进程中,并使用MPI_Gatherv将结果收集回主进程。在每个进程中,我们使用双重循环计算局部矩阵的乘积,并将结果存储在局部矩阵C中。最后,我们在主进程中输出完整的矩阵C。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值