2016-09-24 72 views
0

我正在編寫一個程序,計算每個數字總和最多爲1000.例如,1 + 2 + 3 + 4 + 5 .... + 100。首先,我將求和作業分配給10個處理器:處理器0得到1-100,處理器1得到101-200等等。總和存儲在一個數組中。MPI(求和)

經過所有求和並行完成後,處理器將它們的值發送給處理器0(處理器0使用非阻塞發送/接收接收值),處理器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)啓動形式0至MPI_Comm_size輸出值(total_proc-1)。
  • 只有進程#1正在發送/接收數據。
  • 您正在使用即時版本的發送和接收:MPI_IsendMPI_recv但您不等待這些請求完成。您應該爲此使用MPI_Waitall

正確的版本會是像下面這樣:

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

最後,你打算執行什麼被稱爲減少:通過連續執行單個操作(在你的情況下,總和)獲取給定數量的數值並生成單個輸出值。在MPI中也有這樣的功能:MPI_Reduce

我強烈建議您在嘗試製作自己的作品之前先做some basic guided exercises。 MPI在開始時很難理解。建立一個良好的基礎對於您以後能夠增加複雜性至關重要。 A hands on tutorial也是開始使用MPI的好方法。

編輯:忘了提,你不需要通過資源(total_proc)的數量,以執行問題大小的甚至divission(1000在這種情況下)。根據不同的情況下,你可以將剩餘分配到一個單一的過程:

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,而在第一種情況下,在最壞的情況下負載不平衡可能達到total_proc-1

0

您只初始化array[i],對應於curr_proc id的元素。該數組中的其他元素將被初始化,導致隨機值。在您的發送/接收打印循環中,您只能訪問已初始化的元素。

我對MPI並不熟悉,所以我在猜測,但在撥打MPI_Init之前,您可能需要分配array。或者在流程0上致電MPI_Receive,而不是每個人。