1

まず、インターネットを検索しましたが、答えが見つかりませんでした。たぶんその理由は私がMPIの初心者だからです。ここに質問があります:

まず、マスタープロセッサにtxtファイルを読み取らせたい。次に、十分な情報を取得します。しかし、この間、私は他の人に読書プロセスを待ってもらいたいです。

これが私のコードです:

int processorID;  
int numberOfProcessors;  


int main(int argc, char* argv[]){
MPI_Init(&argc, &argv);  
MPI_Comm_size(MPI_COMM_WORLD ,&numberOfProcessors);  
MPI_Comm_rank(MPI_COMM_WORLD ,&processorID);

int a, b, c;

if(MASTER){
    FILE *myFile;           
    myFile=fopen("input.txt", "r");

    fscanf(myFile, "%d", &a);
    fscanf(myFile, "%d", &b);
    fscanf(myFile, "%d", &c);

}
    MPI_Barrier(MPI_COMM_WORLD);

    if(SLAVE){
            printf("%d\n" ,a);
            printf("%d\n" ,processorID);
    }
MPI_Finalize();  
return 0;  

}

MPI_Barrierを使用するべきではありませんか?たとえば、私は5つのプロセッサを持っており、0はマスターです。MPI_Barrierのおかげで、他の1-2-3-4は読み取りが完了するまで0を待つ必要はありませんか?しかし、これは機能していません。

4

1 に答える 1

0

a他のプロセスは、プロセスローカルであるため、の更新された値を確認できません。を使用して他の人に送信しMPI_Bcastます。このようにして、すべてのプロセスはで待機しMPI_Bcastます。

コードを次のように変更しました。

int processorID;  
int numberOfProcessors;  


int main(int argc, char* argv[]){
MPI_Init(&argc, &argv);  
MPI_Comm_size(MPI_COMM_WORLD ,&numberOfProcessors);  
MPI_Comm_rank(MPI_COMM_WORLD ,&processorID);

int a, b, c;

if(!processorID){ // master
    FILE *myFile;           
    myFile=fopen("input.txt", "r");

    fscanf(myFile, "%d", &a);
    fscanf(myFile, "%d", &b);
    fscanf(myFile, "%d", &c);
    MPI_Bcast(&a, 1, MPI_INT, 0, MPI_COMM_WORLD);

}
    //MPI_Barrier(MPI_COMM_WORLD);
else {
    //if(SLAVE){
            // blocks at Bcast waiting for a. Prints value read by MASTER
            MPI_Bcast(&a, 1, MPI_INT, 0, MPI_COMM_WORLD); 
            printf("%d\n" ,a);
            printf("%d\n" ,processorID);
    }
MPI_Finalize();  
return 0;  

}
于 2013-01-11T15:23:32.770 に答える