Reputation: 14331
I'm trying my hand at writing a naive indexOf function. It currently works and gets the right position. However it overflows when counting the number of comparisons. I've tried converting them all to long long ints but it doesnt seem to be making any difference. What can I do to fix this?
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;
}
Upvotes: 0
Views: 122
Reputation: 30989
The totalCom
variable is an int
; that is more likely to be causing the overflow. Also, you don't need #pragma omp critical
to update totalCom
; add reduction(+ : totalCom)
into your parallel for
header instead.
Upvotes: 1