In this post, we will see OpenMP Lock with Example | How To Use Lock in OpenMP | Thread Synchronization in OpenMP.
In last post, we have seen how to use "#pragma omp critical ". In this post, we will see how to use OpenMP Lock.
Critical section is the region in program where various threads want to access global variables. In OpenMP, we can avoid such race condition among different threads by using Lock.
Critical section is the region in program where various threads want to access global variables. In OpenMP, we can avoid such race condition among different threads by using Lock.
Lets see a simple C language program (program.c) which uses OpenMP API. Here we will create 300 threads and each thread will try to increment the value of x.
Program: (program.c)
How To Run:
To Compile:
gcc -fopenmp program.c
To Run:
./a.out
Output:
Check above output. We were expecting output as "x=300". But we got various values for x. It is because, in this case, some threads were simultaneously incrementing the value of x.
We can avoid such race condition in OpenMP by using OpenMP Lock.
Check following program:
Program: (openmplocks.c)
How To Run:
To Compile:
gcc -fopenmp openmplocks.c
To Run:
./a.out
Output:
Here, for each execution, we got "x=300" because at one instance only one thread was incrementing the value of x.
Note:
1. omp_lock_t is data type used to declare lock variable.
2. Function omp_init_lock() is used to initialize lock. By default, lock is unset.
3. Function omp_set_lock() is used to set lock. Once any thread calls this function, lock is set and no other thread can access the critical section until calling thread calls omp_unset_lock().
4. Function omp_unset_lock() is used to unset lock. So that other threads can access critical section.
5. omp_destroy_lock() destroys the lock i.e. terminate the binding with lock variable.
Program: (program.c)
#include<stdio.h>
#include<omp.h>
void main()
{
int x=0;
#pragma omp parallel num_threads(300)
{
x=x+1;
}
printf("x=%d\n",x);
}
#include<omp.h>
void main()
{
int x=0;
#pragma omp parallel num_threads(300)
{
x=x+1;
}
printf("x=%d\n",x);
}
How To Run:
To Compile:
gcc -fopenmp program.c
To Run:
./a.out
Output:
Check above output. We were expecting output as "x=300". But we got various values for x. It is because, in this case, some threads were simultaneously incrementing the value of x.
We can avoid such race condition in OpenMP by using OpenMP Lock.
Check following program:
Program: (openmplocks.c)
#include<stdio.h>
#include<omp.h>
void main()
{
int x=0;
omp_lock_t writelock;
omp_init_lock(&writelock);
#pragma omp parallel num_threads(300)
{
omp_set_lock(&writelock);
x=x+1;
omp_unset_lock(&writelock);
}
printf("x=%d\n",x);
omp_destroy_lock(&writelock);
}
#include<omp.h>
void main()
{
int x=0;
omp_lock_t writelock;
omp_init_lock(&writelock);
#pragma omp parallel num_threads(300)
{
omp_set_lock(&writelock);
x=x+1;
omp_unset_lock(&writelock);
}
printf("x=%d\n",x);
omp_destroy_lock(&writelock);
}
How To Run:
To Compile:
gcc -fopenmp openmplocks.c
To Run:
./a.out
Output:
Here, for each execution, we got "x=300" because at one instance only one thread was incrementing the value of x.
Note:
1. omp_lock_t is data type used to declare lock variable.
2. Function omp_init_lock() is used to initialize lock. By default, lock is unset.
3. Function omp_set_lock() is used to set lock. Once any thread calls this function, lock is set and no other thread can access the critical section until calling thread calls omp_unset_lock().
4. Function omp_unset_lock() is used to unset lock. So that other threads can access critical section.
5. omp_destroy_lock() destroys the lock i.e. terminate the binding with lock variable.
No comments:
Post a Comment