2011-03-11 34 views
0

我正在努力編寫一個簡單的indexOf函數。它目前的工作,並獲得正確的位置。但是,在計算比較次數時會溢出。我試圖將它們全部轉換爲長整型,但它似乎沒有任何區別。我能做些什麼來解決這個問題?在OpenMP中溢出int的

int hostMatch(long long int *comparisons) 
{ 
    long int i,j,k, lastI; 

    i=0; 
    j=0; 
    k=0; 
    lastI = textLength-patternLength; 
    *comparisons=0; 

    int lastIi = lastI+1; 
    int position = -1; 

    int numberThreads = 1; 
    int totalCom = 0; 

    #pragma omp parallel for default(none) num_threads(numberThreads) \ 
     shared(totalCom, position) \ 
     private(i,j,k) \ 
     firstprivate(lastIi,patternLength, textData, patternData) 
    for (i=0;i<lastIi;i++) 
    { 
     if (position != -1) 
     { 
      // found 
     } 
     else 
     { 
      k=i; 
      long long int count = 0L; 
      for (j=0;j<patternLength;j++) 
      { 
       count++; 
       if (textData[k] == patternData[j]) 
       { 
        if (j == patternLength - 1) 
        { 
         // found 
         position = i; 
        } 
       } 
       else 
       { 
        break; 
       } 
       k++; 
      } 
      #pragma omp critical (totalLock) 
      { 
       totalCom += count; 
      } 
     } 
    } 
    /* END OF PARALLEL SECTION*/ 
    printf("Total Comparisons = %i\n", totalCom); 
    (*comparisons) = totalCom; 
    return position; 
} 
+2

'totalCom'是一個'int';你確定它不是溢出的變量嗎?另外,你不需要'#pragma omp critical'來更新'totalCom';相反,將'reduction(+:totalCom)'添加到'parallel for'標題中。 – 2011-03-11 01:08:07

+0

謝謝,那固定了這個問題,但現在我有競爭條件使得還原變量出錯 – Kurru 2011-03-11 01:45:43

+0

你有一個競爭條件的位置,但使用totalCom減少應該工作正常。 – ejd 2011-03-11 04:45:12

回答

1

totalCom變量是一個int;這更可能導致溢出。此外,您不需要#pragma omp critical來更新totalCom;改爲將reduction(+ : totalCom)添加到您的parallel for標題中。