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)