OpenMP, atomic vs critical?

General OpenMP discussion

OpenMP, atomic vs critical?

Postby johnmarson » Sun Mar 31, 2013 11:50 pm

What is the difference between atomic and critical in openMP? I can do this

Code: Select all
#pragma omp atomic

but isn't this same as

Code: Select all
#pragma omp critical

Thanks in advance
Posts: 1
Joined: Fri Mar 29, 2013 3:22 am

Re: OpenMP, atomic vs critical?

Postby MarkB » Mon Apr 01, 2013 3:35 am

Hi there,

For a simple increment to a shared variable, atomic and critical are semantically equivalent, but atomic allows the compiler more opportunities for optimisation (using hardware instructions, for example).

In other cases, there are differences. If incrementing array elements (e.g. a[i]++ ), atomic allows different threads to update different elements of the array concurrently whereas critical does not. If there is a more complicated expression on the RHS (e.g. a+=foo() ) then the evaluation of foo() is protected from concurrent execution with critical but not with atomic.

Using a critical section is a legitimate way of implementing atomics inside the compiler/runtime, but most current OpenMP compilers do a better job than this.

Hope that helps,
Posts: 673
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 5 guests