0

I am getting following results roughly 20% of the execution with the given code and lastprivate clause is not working as expected. Pls note that end value of b should be 50 however at times it is not updated by last thread. Can anyone guide me what am I doing wrong? thanks.

#include <stdio.h> 
#include <omp.h> 
#include <unistd.h>
int main(int argc, char** argv){
   const int N=10;
   int a = 50;
   int b = 10;
   const int k=0;
   omp_set_num_threads(4);
   #pragma omp parallel for firstprivate(a) schedule(dynamic,2) lastprivate(b)
   for(int i=0;i<N;i++){
      b = a+i;
      printf("%d, %d, a=%d, b=%d\n",omp_get_thread_num(),i, a, b);
   }
   printf("a=%d b=%d (expected a = 50 b=59)\n",a,b);
   return 0;
}

occasional output

3, 8, a=50, b=58
3, 9, a=50, b=59
3, 2, a=50, b=52
3, 3, a=50, b=53
1, 4, a=50, b=54
0, 0, a=50, b=50
2, 6, a=50, b=56
2, 7, a=50, b=57
0, 1, a=50, b=51
1, 5, a=50, b=55
a=50 b=10 (expected a = 50 b=59) 
8
  • Which compiler and which version do you use? Your expectation seems valid, so this looks like a compiler bug. I could not reproduce your result with different compilers I tested on godbolt. Commented Mar 22 at 13:22
  • Your output does not correspond to your code: the word "expected" is not there. Commented Mar 22 at 13:48
  • To be nitpicking: the code also does not compile, because of missing includes for printf and openmp functions. Commented Mar 22 at 14:05
  • 3
    Please update (edit) the question with such information, in order to have the right information ready when reading the question. Commented Mar 24 at 6:40
  • 3
    Code in comments is unreadable. Please edit the original qustion. Commented Mar 24 at 11:59

1 Answer 1

3

The behavior you observe is a result of a compiler bug, which is fixed in newer versions of Clang. I can reproduce the behavior with clang 16 on Linux/x86. Clang 17 produces the correct and expected results. Your best option is to switch to an up-to-date compiler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.