0

I have a code snippet that looks like this. When i try to execute this,i was able to see multiple threads on this particular are of code, even if i am using '#pragma omp single ' directive above that.How did this happen?

        int local_sum=0;
        int lo=(max/tasks)*(t+0)+1;
        int hi=(max/tasks)*(t+1)+0;
        printf("%d : %d...%d--->%d\n",omp_get_thread_num(),lo,hi,t);
        for (int i=lo;i<=hi;i++)
            local_sum=local_sum+i;
        #pragma omp atomic
            sum=sum+local_sum;

The full code is given below;

#include <stdio.h>
#include<omp.h>
int main(int argc, char *argv[]) {
   int max;sscanf(argv[1],"%d",&max);
   int tasks;sscanf(argv[2],"%d",&tasks);
   if(max % tasks !=0) return 1;
   int sum=0;
   #pragma omp parallel num_threads(3)
   {
        #pragma omp single
        for (int t=0;t<tasks;t++){
        printf("%d--->%d\n",omp_get_thread_num(),t);
        #pragma omp task 
        {
            int local_sum=0;
            int lo=(max/tasks)*(t+0)+1;
            int hi=(max/tasks)*(t+1)+0;
            printf("%d : %d...%d--->%d\n",omp_get_thread_num(),lo,hi,t);
            for (int i=lo;i<=hi;i++)
                local_sum=local_sum+i;
            #pragma omp atomic
                sum=sum+local_sum;
        }
    }
   }
   printf("%d\n",sum);
   
   return 0;
}
VC_Vish
  • 3
  • 2
  • 2
    It is because the code starts tasks. It seems that you are not familiar with how tasking works, so my suggestion is to read a chapter about tasks in your favorite OpenMP book or at least read this: https://stackoverflow.com/questions/13788638/difference-between-section-and-task-openmp – Laci Jun 28 '22 at 11:00
  • The use of `atomic` further suggests an unfamiliarity with OpenMP `reduction`, another topic well worth reading about. – High Performance Mark Jun 28 '22 at 11:06

0 Answers0