I have the following problem: My C program must count the number of occurrences of a list of words in a text file.
I use OpenMP for this, and the program, in theory, has the correct logic. When I put some printfs inside a For Loop the result of the program is correct and always the same.
When I remove printfs the result is incorrect, and with each execution its value changes. Given this scenario I think the reason is related to the execution time. With printfs the execution time is increased, so there is time to finish counting all threads and the program to work correctly. Without prinfts, the execution time decreases exponentially (0.000893 ms), leaving no time to finish all threads / calculations and for this reason the program prints a different result for each execution.
The parallelized code is as follows:
#pragma omp parallel for schedule(dynamic) num_threads(threadNumber) private(word, wordExists) shared(keyWordsOcurrences)
for (line = 0; line < NUM_LINES; line++)
{
// divides the line into words separated by space
word = strtok(lines[line], " ");
while (word != NULL)
{
// checks if the word being read is one of the monitored words
wordExists = checkWordOcurrences(word);
if (wordExists)
{
#pragma omp critical
keyWordsOcurrences[wordExists - 1] += 1;
}
word = strtok(NULL, " ");
}
}
The checkWordOcurrences function called is where I put the printf responsible to make my code work properly in every execution (increasing execution time).
int checkWordOcurrences(char *word)
{
int res = 0;
int i;
for (i = 0; i < QTD_WORDS; i++)
{
// **this is the almighty Printf that makes everything work properly, and without it things stop working :(**
printf("palavra %d %s - palavra 2 %s \n", i, keyWords[i], word);
// compares current word with monitored words
if (!strcmp(keyWords[i], word))
{
// if it's monitored word, returns its index (+1 because the first word has index 0 and the return type is checked as true or false)
res = i + 1;
}
}
// returns word index or 0, if current word is not monitored
return res;
}
Can someone explain to me what may be happening and / or how to solve it?