Atomic and Critical Atoms Openmp

I am new to openmp and I play with some things for a school project. I tried to make my program a little faster using atomic rather than critical. I have this piece of code at the end of one of my loops.

  if(prod > final_prod)
  {
    #pragma omp atomic
    final_prod = prod;
  }

Although, when I do this, I get the error below (if I use critical program compilation)

error: invalid form of β€˜#pragma omp atomic’ before β€˜;’ token
     final_prod = prod;
                      ^

From what I have learned so far, you can use atoms instead of critical ones for usually something that can be done in a few machine instructions. Should this work? And what is the main difference between using atomic and critical?

+3
source share
2 answers

:

enter image description here

, , critsec! , , ,

if(prod > final_prod) // unsynchronized read
{
  #pragma omp critical
  final_prod = prod;
}

+4

, #pragma omp atomic:

  • x ++, x-- ..
  • x + = a;, x * = a ..

, .

+1

All Articles