0

In my experience, when I update a varible in 1 task the variable is not updated in other tasks even if the first task that updated the variable is done executing. For example given the code,

int nThreads = atoi(argv[1]);
omp_set_num_threads(nThreads);
    
int currentInt = 0;
    
int numEdges = 1000000;


#pragma omp parallel shared(currentInt)
    {
        #pragma omp single
        {
            #pragma omp task shared(currentInt)
            {
               
                
                    printf("I am doing kruskals: Thread %d\n", omp_get_thread_num());
                    while(currentInt < numEdges)
                    {
                        currentInt++;
                    }
                    printf("Kruskals Done! %d\n", currentInt);
                

                #pragma omp shared(currentInt)
                {

                    for(int i = 0; i < 10000000; i++){
                        
                    }
                    printf("Helper: Current Int %d Thread %d \n", currentInt, omp_get_thread_num());
                }
                
            }
            
            #pragma omp taskwait
        }
    }

It will always print currentInt 0. Even if the first task finishes before the second. I need this because I am trying to parallize an algorithm where a have a sequential task going through a large array and many parallel tasks excuting simultanously on parts of that array and once the sequential task reaches the portion of the array that a parallel task is working on the parallel task can stop itself because it is no longer needed. The parallel and sequential tasks share no dependancies so that is not a problem.

Any help will be appreciated.

  • 1
    Use sequentally consistent atomic operations to communicate between threads. e.g read [this answer](https://stackoverflow.com/questions/70915412/how-can-i-execute-two-for-loops-in-parallel-in-c/70917161#70917161) to have an idea about it. – Laci Dec 04 '22 at 13:49
  • 1) didn't you forgot the `task` keyword in `#pragma omp shared(currentInt)`? 2) Please copy/paste the output of your program; 3) I have tested this code (just adding the missing `task` keyword) with 4 threads and I get the the expected `Helper: Current Int 1000000 Thread 2` at the end (the thread number can vary between runs) – PierU Dec 05 '22 at 11:31

0 Answers0