2016-08-08 14 views
0

私は比較的新しいテーマであるMPIコミュニケータに助けが必要です。ファイルを読むためのMPIコミュニケータの作成

私はいくつかの入力ファイルから入力を読み取るMPIコードを持っています。すべてのプロセスは少なくとも1つのファイルから読み込み、ほとんどは複数のファイルから読み込みます。すべてのファイルが読み込まれます。

ファイルごとにコミュニケータを作成する必要があります。例えば、ファイル "A.dat"から読み取られたプロセス0,1,2、ファイル "B.dat"から読み取られたプロセス2,3,4、およびプロセス4,5,6が "C"から読み取られたとする。 dat "と呼ぶ。 (実際には、さらに多くのプロセスとファイルが存在します)。私は3人のコミュニケーターが必要です。最初にprocs 0,1、および2を含める必要があります。第2、第3、第4、 3番目の4,5,6です。私はむしろこれをどうやって行うのかについて迷っています。誰でも知っている?

答えて

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(); 
} 

REFERENCE

+0

ありがとう! 2番目の例は私が必要なものです。 MPI_Comm_splitは私のためには機能しません。私はMPI_Group_inclとMPI_Comm_createへの呼び出しについて不思議です。すべてのprocsはこれらの呼び出しを行う必要がありますか?私はいくつかのコミュニケーターを持つつもりです。特定のコミュニケータのために、そのコミュニケータのプロキシだけがそのコミュニケータを呼び出すようにコードを書くことはできますか?再度、感謝します。 –

+0

MAPのような構造を条件と組み合わせて使用​​すると、選択した一連のプロセスで必要な呼び出しを実行できます。何かが好きです:if(can_read_file_A [world_rank] == true){...特定のグループ、コミュニケータを作成し、コードを実行してメモリを解放します} – Felipe

関連する問題