【问题标题】:MPI_IN_PLACE+MPI_Allgather instead of MPI_Gather+MPI_BcastMPI_IN_PLACE+MPI_Allgather 代替 MPI_Gather+MPI_Bcast
【发布时间】:2023-01-13 00:55:31
【问题描述】:

我如何使用MPI_AllgatherMPI_IN_PLACE执行MPI_GatherMPI_BCast的以下操作(我想避免制作大型数组的副本,因此首选就地修改)?

上述操作的代码如下:

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

#define MAX_ELES 100
#define CUR_ELES 6

void print_arr(int arr[], int rank) {
    printf("RANK %d: ", rank+1);
    for (int i = 0; i < BODYCT; i++)
        printf("%d ", arr[i]);
    printf("\n"); 
}

int main(int argc, char *argv){
    MPI_Init(&argc, &argv);
    MPI_Comm comm = MPI_COMM_WORLD;
    int rank, size;
    MPI_Comm_rank(comm, &rank);
    MPI_Comm_size(comm, &size);

    // init all elements to 0
    int positions[MAX_ELES];
    for (int position = 0; position < CUR_ELES; position++)
        positions[position] = 0;

    // Determine indices for data parallelism
    int lower_bound, upper_bound;
    int N = CUR_ELES;
    int P = size;
    lower_bound = rank*N/P;
    upper_bound = rank == P-1 ? (rank+1)*N/P + N%P : (rank+1)*N/P; 

    // Initialize the values of the array based on the process rank
    for (int b = lower_bound; b < upper_bound; b++)
        positions[b] = rank+1;

    // inspect array on process
    print_arr(positions, rank);

    // GATHER
    int gathered_positions[MAX_ELES];
    MPI_Gather(
        &positions[lower_bound], upper_bound-lower_bound, MPI_INT,
        gathered_positions, upper_bound-lower_bound, MPI_INT, 0, comm);

    // inspect gathered array
    if (rank == 0) {
        printf("---------\n");
        printf("RANK %d: Gathered\n", rank+1);
        print_arr(gathered_positions, rank);
        printf("---------\n");
    }

    // BCAST
    MPI_Bcast(gathered_positions, CUR_ELES, MPI_INT, 0, comm);

    MPI_Finalize();
}

【问题讨论】:

    标签: c mpi


    【解决方案1】:

    使用MPI_IN_PLACE作为发送缓冲区;发送计数和类型被忽略。每个进程都需要将它们的贡献放在如果您进行非就地全收集时它会结束的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2019-05-30
      • 2015-04-01
      • 2015-06-07
      • 2012-11-20
      • 2013-11-30
      相关资源
      最近更新 更多