2011-03-11 3 views
0

나는 순진한 indexOf 함수를 작성하려고합니다. 현재 작동하고 올바른 위치를 얻습니다. 그러나 비교 횟수를 계산할 때 오버플로됩니다. 나는 긴 long ints로 그것들을 모두 변환하려고 시도했지만 어떤 차이를 만드는 것 같지 않습니다. 이 문제를 해결하려면 어떻게해야합니까?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'입니다; 범람하는 변수가 아니라고 확신합니까? 또한,'totalCom'을 업데이트하기 위해'#pragma omp critical '을 필요로하지 않습니다; 대신'reduce (+ : totalCom)'을'parallel for' 헤더에 추가하십시오. –

+0

고마워요, 그 문제가 해결되었지만 지금은 경감 조건이 잘못되어 있습니다. – Kurru

+0

위치에 대한 경쟁 조건이 있지만 totalCom으로 감소를 사용하면 문제가 없습니다. – ejd

답변

1

totalCom 변수는 int이고; 오버플로를 일으킬 가능성이 더 큽니다. 또한 totalCom을 업데이트하기 위해 #pragma omp critical이 필요하지 않습니다. reduction(+ : totalCom)parallel for 헤더에 대신 추가하십시오.

관련 문제