Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP critical section vs locks

Tags:

openmp

What is the difference between OpenMP locks and critical section? Are they just alternatives for each other? For example if I am writing to a same file using multiple files, should I use the locking or just the critical section before writing into the file?

like image 594
Omkar Avatar asked Apr 12 '15 07:04

Omkar


People also ask

What is critical section OpenMP?

Use OpenMP critical sections to prevent multiple threads from accessing the critical section's code at the same time, thus only one active thread can update the data referenced by the code. Critical sections are useful for a non-nested mutex.

What is OpenMP lock?

The OpenMP lock routines access a lock variable such that they always read and update the most current value of the lock variable. It is not necessary for an OpenMP program to include explicit flush directives to ensure that the lock variable's value is consistent among different tasks.


1 Answers

Critical sections will most commonly use a lock internally, e.g.:

  • libgomp: source
  • libiomp:

    If the optional (name) is omitted, it locks an unnamed global mutex.

The OpenMP specification guarantees the following behaviour:

>

The critical construct restricts execution of the associated structured block to a single thread at a time

A critical section therefore serves the same purpose as acquiring a lock. The difference is that the low-level details are handled for you.

I would advise you to use critical whenever possible due to the simplicity. If you have separate blocks that need to be critical but don't interfere with each other give them names, and only if you need some behaviour that cannot be accommodated by the annotations, use explicit locking.

like image 50
jepio Avatar answered Jan 03 '23 17:01

jepio