2016-09-24 6 views
0

私は1000までのすべての数値の合計を計算するプログラムを書いています。たとえば、1 + 2 + 3 + 4 + 5 .... + 100です。まず、10のプロセッサに合計ジョブを割り当てます。プロセッサ0は1〜100、プロセッサ1は101〜200などを取得します。合計は配列に格納されます。MPI(Summation)

すべての集計が並列処理された後、プロセッサはプロセッサ0に値を送信します(プロセッサ0は非ブロック送信/ recvを使用して値を受け取ります)。プロセッサ0はすべての値を合計し、結果を表示します。

#include <mpi.h> 
#include <iostream> 

using namespace std; 

int summation(int, int); 

int main(int argc, char ** argv) 
{ 
    int * array; 
    int total_proc; 
    int curr_proc; 
    int limit = 0; 
    int partial_sum = 0; 
    int upperlimit = 0, lowerlimit = 0; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_size(MPI_COMM_WORLD, &total_proc); 
    MPI_Comm_rank(MPI_COMM_WORLD, &curr_proc); 
    MPI_Request send_request, recv_request; 

    /* checking if 1000 is divisible by number of procs, else quit */ 
    if(1000 % total_proc != 0) 
    { 
     MPI_Finalize(); 
     if(curr_proc == 0) 
      cout << "**** 1000 is not divisible by " << total_proc << " ...quitting..."<< endl; 
     return 0; 
    } 

    /* number of partial summations */ 
    limit = 1000/total_proc; 

    array = new int [total_proc]; 

    /* assigning jobs to processors */ 
    for(int i = 0; i < total_proc; i++) 
    { 
     if(curr_proc == i) 
     { 
      upperlimit = upperlimit + limit; 
      lowerlimit = (upperlimit - limit) + 1; 
      partial_sum = summation(upperlimit, lowerlimit); 
      array[i] = partial_sum; 
     } 
     else 
     { 
      upperlimit = upperlimit + limit; 
      lowerlimit = (upperlimit - limit) + 1; 
     } 
    } 

    cout << "** Partial Sum From Process " << curr_proc << " is " << array[curr_proc] << endl; 

    /* send and receive - non blocking */ 
    for(int i = 1; i < total_proc; i++) 
    { 
     if(curr_proc == i) /* (i = current processor) */ 
     { 
      MPI_Isend(&array[i], 1, MPI_INT, 0, i, MPI_COMM_WORLD, &send_request); 
      cout << "-> Process " << i << " sent " << array[i] << " to Process 0" << endl; 

      MPI_Irecv(&array[i], 1, MPI_INT, i, i, MPI_COMM_WORLD, &recv_request); 
      //cout << "<- Process 0 received " << array[i] << " from Process " << i << endl; 
     } 
    } 

    MPI_Finalize(); 

    if(curr_proc == 0) 
    { 
     for(int i = 1; i < total_proc; i++) 
      array[0] = array[0] + array[i]; 
     cout << "Sum is " << array[0] << endl; 
    } 

    return 0; 
} 

int summation(int u, int l) 
{ 
    int result = 0; 
    for(int i = l; i <= u; i++) 
     result = result + i; 
    return result; 
} 

出力:

** Partial Sum From Process 0 is 5050 
** Partial Sum From Process 3 is 35050 
-> Process 3 sent 35050 to Process 0 
<- Process 0 received 35050 from Process 3 
** Partial Sum From Process 4 is 45050 
-> Process 4 sent 45050 to Process 0 
<- Process 0 received 45050 from Process 4 
** Partial Sum From Process 5 is 55050 
-> Process 5 sent 55050 to Process 0 
<- Process 0 received 55050 from Process 5 
** Partial Sum From Process 6 is 65050 
** Partial Sum From Process 8 is 85050 
-> Process 8 sent 85050 to Process 0 
<- Process 0 received 85050 from Process 8 
-> Process 6 sent 65050 to Process 0 
** Partial Sum From Process 1 is 15050 
** Partial Sum From Process 2 is 25050 
-> Process 2 sent 25050 to Process 0 
<- Process 0 received 25050 from Process 2 
<- Process 0 received 65050 from Process 6 
** Partial Sum From Process 7 is 75050 
-> Process 1 sent 15050 to Process 0 
<- Process 0 received 15050 from Process 1 
-> Process 7 sent 75050 to Process 0 
<- Process 0 received 75050 from Process 7 
** Partial Sum From Process 9 is 95050 
-> Process 9 sent 95050 to Process 0 
<- Process 0 received 95050 from Process 9 
Sum is -1544080023 

配列の内容を出力:私はこれを引き起こしているものを知りたい

5050 
536870912 
-1579286148 
-268433415 
501219332 
32666 
501222192 
32666 
1 
0 

ここ

はコードです。

MPI_Finalizeが呼び出される前に配列を印刷すると問題なく動作します。

答えて

2

あなたのプログラムが持つ最も重要な欠陥は、仕事をどのように分割するかということです。 MPIでは、すべてのプロセスがメイン関数を実行しています。したがって、結果を構築するために共同作業する場合は、すべてのプロセスがsummation関数を実行するようにする必要があります。

forループは必要ありません。すべてのプロセスがメインを別々に実行しています。彼らはただ異なるcurr_proc値を持っている、とあなたは彼らがそれに基づいて実行する必要がどのジョブの部分を計算することができます

/* assigning jobs to processors */ 
int chunk_size = 1000/total_proc; 
lowerlimit = curr_proc * chunk_size; 
upperlimit = (curr_proc+1) * chunk_size; 
partial_sum = summation(upperlimit, lowerlimit); 

次に、マスター・プロセスは、他のすべてのプロセスの部分和をどのように受け取るか正しくありません。

  • MPIランク値(curr_proc)はMPI_Comm_size出力値(total_proc-1)までフォーム0を開始します。
  • プロセス#1だけがデータを送受信しています。
  • 即時の送受信のバージョン:MPI_IsendMPI_recvを使用していますが、それらの要求が完了するまで待っているわけではありません。そのためにはMPI_Waitallを使用してください。

正しいバージョンは、次のようになります。これは、すべての対1の通信パターンがとして知られている

if(curr_proc == 0) { 
    // master process receives all data 
    for(int i = 1; i < total_proc; i++) 
     MPI_Recv(&array[i], MPI_INT, 1, i, 0, MPI_COMM_WORLD); 
} else { 
    // other processes send data to the master 
    MPI_Send(&partial_sum, MPI_INT, 1, 0, 0, MPI_COMM_WORLD); 
} 

を収集します。MPIには既にこの機能を実行する関数があります:MPI_Gather

最後に、実行するものはとなります。:与えられた量の数値を取り、1回の操作(あなたの場合は合計)を連続して実行することによって単一の出力値を生成します。 MPIには、それを行う関数もあります:MPI_Reduce

あなた自身で作成しようとする前に、some basic guided exercisesを行うことを強くお勧めします。 MPIは当初理解するのが難しいです。後で複雑さを加えることができるようにするには、良い基盤を構築することが不可欠です。 A hands on tutorialも、MPIを使い始める良い方法です。

EDIT:あなたは、リソースの数(total_proc)により(この場合は1000)、問題の大きさのさえdivissionを強制する必要はありませんことを言及し忘れました。場合によっては、単一のプロセスに残りを割り当てることができ、次のいずれか

chunk_size = 1000/total_proc; 
if(curr_proc == 0) 
    chunk_size += 1000 % total_proc; 

あるいはそれにできるだけ多くのバランスをとる:

int remainder = curr_proc < (1000 % proc)? 1 : 0; 
lowerlimit = curr_proc * chunk_size /* as usual */ 
      + curr_proc;    /* cumulative remainder */ 
upperlimit = (curr_proc + 1) * chunk_size /* as usual */ 
      + remainder;     /* curr_proc remainder */ 

第二のケースを、負荷のアンバランスは限りとなります1であるが、第1の場合、負荷の不平衡は、最悪の場合、total_proc-1に達する可能性がある。

0

curr_procに対応する要素array[i]のみを初期化しています。その配列内の他の要素は初期化されず、結果としてランダムな値が返されます。送信/受信印刷ループでは、初期化された要素にのみアクセスします。

私は推測しているので、arrayを割り当てることをお勧めします。MPI_Initを呼び出してください。または、個々のプロセスではなく、プロセス0のMPI_Receiveに電話してください。