How to use OpenMP Lock Routines?

1

I need to create 5 easy-to-understand algorithms for each of the locking functions below in C or C ++ to be able to exemplify the operation of each and present to the staff in my room.

  • omp_init_lock_with_hint
  • omp_init_nest_lock_with_hint

The problem is that I almost did not understand the operation of the lock functions. I am using as reference the most up-to-date OpenMP documentation available at the link: link (pages 270 to 275).

Could you explain how the locking functions work and help you create an example using one of the functions so I understand the operation and can you create other examples to present to my class?

Thank you in advance.

    
asked by anonymous 14.05.2017 / 19:36

1 answer

2

The difference between the two specified functions, omp_init_lock_with_hint() and omp_init_nest_lock_with_hint() is that it initializes a simple lock and this initializes a nested lock.

The difference between the two (apart from the obvious that one receives a omp_lock_t * and the other a omp_nest_lock_t * ) is that a task that obtains a simple lock via omp_set_lock() can not call % with% on same lock without first releasing it using omp_set_lock() , #

Other OpenMP tasks can call omp_unset_lock() on a lock already belonging to a different task; they will lock until the task that is currently the owner of the lock releases it using omp_set_lock() (and the other tasks in front of the queue waiting for the lock also get and release the lock).

Already for a omp_unset_lock() , if the task owner of the lock calls omp_nest_lock_t on it, it will merely increment an internal counter, and when calling omp_set_nest_lock() this counter will be decremented. If the counter value reaches zero, the lock is released. In other words, although the omp_unset_nest_lock() call on a task-bound lock does not cause deadlock , the task has to issue as many omp_set_nest_lock() as it issued omp_unset_nest_lock() before the lock is released. p>

As for the usage examples, I will need some time to restore my knowledge in OpenMP, but the general idea is that, with the simple lock, each task gets the lock with omp_set_nest_lock() , perform the synchronous and infallible operations (do not raise exceptions) in the critical region, and then release the lock with omp_set_lock() . In the case of nested_lock, the task can do more complex things, such as getting the lock with omp_unset_lock() , waiting for an event, and calling omp_set_nest_lock() on a callback .

    
16.05.2017 / 16:18