我需要一些MPI传播者的帮助,这是我相对较新的主题。
我有一个MPI代码,它将从几个输入文件中读取输入。每个进程都将从至少一个文件中读取,大多数将从多个文件中读取。每个文件都将被阅读。
我需要为每个文件创建一个通信器。让我们举例说,从文件" A.dat"中读取的进程0,1和2,从文件" B.dat"中读取的进程2,3和4; ,过程4,5和6从" C.dat"中读取。 (实际上会有更多的进程和文件。)所以我需要三个传播者。第一个应该包含procs 0,1和2;第二,第三和第四;第三个4,5和6.我对如何做到这一点感到茫然。谁知道怎么做?
答案 0 :(得分: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();
}