Search code examples
cfor-loopparallel-processingopenmp

How to use atomic directive into sections in openmp


i am parallelizing two loops using section directive of openMP.

#pragma omp parallel sections
{
    #pragma omp section    //section 1
    {
       
        for (conditions){
            statement
    } }
    #pragma omp section    //section 2
    {
     #pragma omp for
     for (conditions){
            statement
      } }
}

The reason is both two loops are independent of each other. The first loop has dependent statements so it can not be parallelized and its execution time is large whereas the second loop can be parallelized. But if I apply parallel directives only on the second loop then it has to wait for its execution until the first one is completed.

So I am trying to execute both loops in parallel using a section or task where in the first section first loop can be executed sequentially and in the second section the second one can be parallelized.

I don't know whether it will speed up the program or not. But before getting any results, I was stuck on how to run the first loop in the section sequentially.


Solution

  • You should use tasks to achieve your goal (to run 2 loops simultaneously: one of them sequentially and the other in parallel):

    #pragma omp parallel
    #pragma omp single
    {
        #pragma omp task    
        for (conditions){
            //serial loop
        }
        
        #pragma omp taskloop
        for (conditions){
            //parallel loop
        }        
    }
    

    Note that your question has nothing to do with atomic operations.