How to use lock in OpenMP?
Asked Answered
T

3

33

I have two pieces of C++ code running on 2 different cores. Both of them write to the same file.

How to use OpenMP and make sure there is no crash?

Taeniacide answered 7/3, 2010 at 13:42 Comment(0)
Y
67

You want the OMP_SET_LOCK/OMP_UNSET_LOCK functions: https://hpc.llnl.gov/tuts/openMP/#OMP_SET_LOCK

Basically:

omp_lock_t writelock;

omp_init_lock(&writelock);

#pragma omp parallel for
for ( i = 0; i < x; i++ )
{
    // some stuff
   omp_set_lock(&writelock);
    // one thread at a time stuff
    omp_unset_lock(&writelock);
    // some stuff
}

omp_destroy_lock(&writelock);

Most locking routines such as pthreads semaphores and sysv semaphores work on that sort of logic, although the specific API calls are different.

Yamashita answered 7/3, 2010 at 19:27 Comment(0)
F
41

For the benefit of those coming after, using critical is another option. You can even make named critical sections.

For example:

#include <omp.h>

void myParallelFunction()
{
    #pragma omp parallel for
    for(int i=0;i<1000;++i)
    {

        // some expensive work 

        #pragma omp critical LogUpdate
        {
            // critical section where you update file        
        }

        // other work

        #pragma omp critical LogUpdate
        {
            // critical section where you update file      
        }
    }
} 

Edit: There's a great thread in the comments initiated by Victor Eijkhout. Summarizing and paraphrasing: In short critical locks a code segment. That can be overkill in more complex examples where all you want to do is lock a specific data item. It's important to understand this before you make a choice between the two methods.

Fitz answered 5/11, 2012 at 17:30 Comment(7)
Critical locks a code segment. That's not a good idea if you have thousands of iterations and you simply want to make sure that no two threads execute the same iteration simultaneously. Using critical means that only one thread does any iteration. Named critical sections alleviates this problem a little, but locks are more flexible since they lock a data element, not a code fragment.Brassbound
@VictorEijkhout Sorry, I do not understand how different this solution is from @user257111. To me, replacing #pragma omp critical with lock and unlock would lead to exactly same code in this example, wouldn't it?Slip
@rkioji The original question never specified what the code was that needed exclusive execution, so it's hard to decide what is appropriate here. Take a database as an example. With a lock you can lock a single item in the database, to make sure no two processes update it simultaneously: lock protects a specific data item. On the other hand, if you put a critical section around the update, you make sure that no two processes can do the update statement simultaneously, regardless of what they update. So in the case of a database, a critical section is overkill. (stackoverflow limit reached.)Brassbound
@VictorEijkhout I agree with you. Locking the data sounds more efficient in some situations. What I tried to say is that using omp critical or lock/unlock leads to same performance in the example given in this answer.Slip
These are great points that I wasn't aware of back when I wrote this. I've edited the answer to summarize.Fitz
Regarding the difference between a critical section and a lock: https://mcmap.net/q/452262/-openmp-critical-section-vs-locks/7328782 (the answer says they're the same).Julissajulita
In VisualC++ the optional name of the critical section must be enclosed in parentheses: learn.microsoft.com/en-us/cpp/parallel/openmp/reference/…Satire
S
19
#pragma omp critical
{
    // write to file here
}
Schrick answered 6/5, 2011 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.