2015-02-07 53 views
2

我讀了一堆的頁面,但我找不到我的問題的解決方案:我必須從根過程經過3個整數數組工作進程,其中:MPI_SEND和MPI_receive串

  • 第一int是由每個處理
  • 被驗證的頁面的數量的第二INT是id從哪裏開始,以驗證
  • 第三int是要搜索在該頁面的字符串的長度。

發送此數組後,我發送要搜索的字符串。問題是接收:發送的字符串的長度是正確的,但是當我輸出字符串時,我有這個:例如,如果我發送「運動」,輸出上我有「sport222 |||」。以下是代碼:

int main(int argc, char *argv[]){ 
    int rank; 
    int noOfProcs; 
    string stringToBeSearched = ""; 
    int root_process; 
    int ierr; 
    int avgPerProcess; 
    int totalNoOfPages; 
    int noOfPagesForLastProc; 
    int proc; 
    char* receivedString = ""; 
    int receivedSLen; 
    int dataToSend[3]; 
    int receivedData[3]; 
    vector<HtmlPage> pages; 
    char* s = ""; 
    clock_t tStart; 
    MPI_Status status; 

    ierr = MPI_Init(&argc, &argv); 
    root_process = 0; 
    ierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    ierr = MPI_Comm_size(MPI_COMM_WORLD, &noOfProcs); 

    if (rank == root_process) { 
     cout << "Enter text: "; 
     getline(cin, stringToBeSearched); 
     //calculate the number of pages to be verified by each process 
     totalNoOfPages = getTotalNumberOfPages(); 
     avgPerProcess = totalNoOfPages/noOfProcs; 
     //send the array to each process 
     noOfPagesForLastProc = avgPerProcess + (totalNoOfPages % noOfProcs); 
     for (proc = 1; proc < noOfProcs; proc++) { 
      dataToSend[0] = avgPerProcess; 
      dataToSend[1] = proc * avgPerProcess; 
      dataToSend[2] = stringToBeSearched.size(); 
      if (proc == noOfProcs - 1){ 
       dataToSend[0] = noOfPagesForLastProc; 
      } 
      ierr = MPI_Send(&dataToSend[0], 3, MPI_INT, 
       proc, send_data_tag, MPI_COMM_WORLD);    
      //send the string to be searched 
      ierr = MPI_Send(stringToBeSearched.c_str(), stringToBeSearched.size(), MPI_CHAR, proc, send_data_tag, MPI_COMM_WORLD); 
     } 
     //verify pages in the segment assigned 
     //to the root process 
     //get the pages assigned 

     pages = getPagesForProcess(avgPerProcess, 0); 
     for (int i = 0; i < pages.size(); i++) { 
      string searched = pages[i].getContent(); 
      const char* ch = searched.c_str(); 
      if (strstr(ch, s) != NULL){ 
       cout << "Process 0" << " founded matches at: " << endl; 
       cout << pages[i].getUrl() << endl; 
      } 
     } 
    } 
    else { 
     //worker process receive the array segment, 
     //storing it in a "local" array 
     ierr = MPI_Recv(&receivedData[0], 3, MPI_INT, 
     root_process, send_data_tag, MPI_COMM_WORLD, &status); 
     receivedSLen = receivedData[2];  
     receivedString = new char[receivedSLen]; 
     ierr = MPI_Recv(receivedString, receivedSLen, MPI_CHAR, root_process, send_data_tag, MPI_COMM_WORLD, &status); 
     //this line of code outputs the "bad" string 
     cout << "received string = " << receivedString << endl; 

     //get the assigned pages to verify 
     pages = getPagesForProcess(receivedData[0], receivedData[1]); 
     for (int i = 0; i < pages.size(); i++) { 
      string searched = pages[i].getContent(); 
      const char* ch = searched.c_str(); 
      if (strstr(ch, receivedString) != NULL){ 
       cout << "Process " << rank << " founded matches at: " << endl; 
       cout << pages[i].getUrl() << endl; 
      } 
      else { 
       cout << "No matches founded by process " << rank << endl; 
      } 
     }  
    } 
    cout << "Time taken for execution " << (double)(clock() - tStart)/CLOCKS_PER_SEC << " s." << endl; 
    ierr = MPI_Finalize(); 
    _getch(); 
    return 0; 
} 
+0

哪一行代碼輸出是 「壞」 的字符串? – PaulMcKenzie 2015-02-07 08:01:06

+0

@PaulMcKenzie這行代碼輸出「bad」字符串 'cout <<「received string =」<< receivedString << endl;' – laura 2015-02-07 08:04:43

回答

2

問題是您的字符串缺少空終止符。

receivedString = new char[receivedSLen]; 

應該是:

receivedString = new char[receivedSLen + 1](); 

此分配用於終止空1多個字符,並且還的數據初始化爲默認值(即0反正)。

更重要的是,通過使用std::vector

std::vector<char> receivedString(receivedSLen + 1,0); 
cout << "received string = " << &receivedString[0] << endl; 

,你應該考慮的問題是,如果字符串可以包含嵌入的NULL人物擺脫了內存泄漏。如果是這樣,那麼你必須使用不同的方法來處理字符串,因爲你現在使用的函數現在依賴於終止的\0

在這種情況下數據可能已經嵌入NULL S,

cout << "received string = "; 
cout.write(receivedString, receivedSLen); 
cout << endl; 

如果使用std::vector

cout << "received string = "; 
cout.write(&receivedString[0], receivedSLen); 
cout << endl;