2
votes

It seems to me that having a critical section withic a parallel block in OpenMP makes no sense! I might just well write a simple serial do loop right?           

In the following (trivial) example instead of            

1.

  !$omp critical
            !$ thread_num = omp_get_thread_num()
            print *, "Hello world from thread number ", thread_num
        !$omp end critical
  !$omp end parallel

2.

    do i=1,num_threads
    print *, "Hello world from thread number ", thread_num
    end do

That being said, I understand the difference: 1. uses different threads while 2. doesn't.

Is there a non-trivial context where the former might actually provide a speed advantage over the latter?

1

1 Answers

2
votes

The $omp critical specifies that code is executed by one thread at a time. So your both examples not run parallel but in serial way. The sense of using critical section in clearly described on wiki so look there to find details (the typical situation is when all thread needs wait for some common value (calculated earlier in parallel way e.g. some sort of elements sum) to continue calculations)