0

我需要一些关于 MPI 传播者的帮助,这是一个我相对较新的主题。

我有一个 MPI 代码,它将从多个输入文件中读取输入。每个进程都会从至少一个文件中读取,大多数会从多个文件中读取。每个文件都会被读取。

我需要为每个文件创建一个通信器。例如,进程 0、1 和 2 从文件“A.dat”中读取,进程 2、3 和 4 从文件“B.dat”中读取,进程 4、5 和 6 从“C.dat”中读取。达”。(实际上会有更多的进程和文件。)所以我需要三个通信器。第一个应包含 procs 0、1 和 2;第二个 2、3 和 4;第三个 4、5 和 6。我不知道如何做到这一点。有谁知道怎么做?

4

1 回答 1

1

可以将较大的通信器拆分为具有一定大小的较小通信器:

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

int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    // Get the rank and size in the MPI_COMM_WORLD communicator
    int world_rank, world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    int color = world_rank / 4; //4 process (colors) for each communicator

    MPI_Comm row_comm;

    /*
     The first argument is the communicator that will be used as the basis for the new communicators.
     The second argument determines to which new communicator each processes will belong.
     The third argument determines the ordering (rank) within each new communicator...
     The process which passes in the smallest value for the third argument will be rank 0, the next smallest will be rank 1, and so on.
     The final argument returns the new communicator back to the user.
     */
    MPI_Comm_split(MPI_COMM_WORLD, color, world_rank, &row_comm);

    int row_rank, row_size;
    MPI_Comm_rank(row_comm, &row_rank);
    MPI_Comm_size(row_comm, &row_size);

    printf("WORLD RANK/SIZE: %d/%d \t ROW RANK/SIZE: %d/%d\n",
           world_rank, world_size, row_rank, row_size);

    MPI_Comm_free(&row_comm);

    // Finalize the MPI environment.
    MPI_Finalize();
}

或者您可以创建组(更灵活的方式)

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

int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    // Get the rank and size in the original communicator
    int world_rank, world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // Get the group of processes in MPI_COMM_WORLD
    MPI_Group world_group;
    MPI_Comm_group(MPI_COMM_WORLD, &world_group);

    int prime_group_size = 6;
    const int ranks[6] = {2, 3, 5, 7, 11, 13};

    // Construct a group containing all of the prime ranks in world_group
    MPI_Group prime_group;
    MPI_Group_incl(world_group, prime_group_size, ranks, &prime_group);

    // Create a new communicator based on the group
    MPI_Comm prime_comm;
    MPI_Comm_create_group(MPI_COMM_WORLD, prime_group, 0, &prime_comm);

    int prime_rank = -1, prime_size = -1;
    // If this rank isn't in the new communicator, it will be
    // MPI_COMM_NULL. Using MPI_COMM_NULL for MPI_Comm_rank or
    // MPI_Comm_size is erroneous
    if (MPI_COMM_NULL != prime_comm) {
        MPI_Comm_rank(prime_comm, &prime_rank);
        MPI_Comm_size(prime_comm, &prime_size);

        printf("WORLD RANK/SIZE: %d/%d \t PRIME RANK/SIZE: %d/%d\n",
               world_rank, world_size, prime_rank, prime_size);

        MPI_Group_free(&prime_group);
        MPI_Comm_free(&prime_comm);
    }


    MPI_Group_free(&world_group);

    // Finalize the MPI environment.
    MPI_Finalize();
}

参考

于 2016-08-08T17:35:24.013 回答