Sei sulla pagina 1di 6

OpenMP Synchronization constructs:

In the following code, what is the value of t printed if


OMP_NUM_THREADS=4

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main (int argc, char *argv[])
{

int t ;

t = omp_get_thread_num() ;

#pragma omp parallel

int x ;

x = omp_get_thread_num() ;

#pragma omp master

t += x ;

printf( "%d\n", t ) ;
return 0;
}

priya@priya-K55N:~$ gcc -fopenmp 1.c


priya@priya-K55N:~$ export OMP_NUM_THREADS=4
priya@priya-K55N:~$ ./a.out
0

In the code below, What is the value of t printed if OMP_NUM_THREADS=2

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main ()
{

int t ;

t = omp_get_num_threads() ;
#pragma omp parallel

int x,y ;

x = omp_get_num_threads() ;

y = omp_get_thread_num() ;

#pragma omp single

t += x ;

printf( "%d\n", t ) ;
return 0;
}

priya@priya-K55N:~$ gcc -fopenmp 1.c


priya@priya-K55N:~$ export OMP_NUM_THREADS=2
priya@priya-K55N:~$ ./a.out
3

OpenMP: Barrier. It is a point in the execution of a program where


threads wait for each other. No thread is allowed to continue until all
threads in a team reach the barrier. Basically, a barrier is a
synchronization point in a program.

In contrast to the single directive, there is no implicit barrier


associated with the master directive.

Which of the following codes will always produce the output s=2 when run
with OMP_NUM_THREADS=2.

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main ()
{
int s = 0 ;
#pragma omp parallel shared( s )

{
int tid=omp_get_thread_num();
#pragma omp single
s++ ;
#pragma omp master
s++ ;
#pragma omp barrier
printf( "s=%d by thread %d\n", s,tid ) ;
}
return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main ()
{

int s = 0 ;
#pragma omp parallel shared( s )

{
int tid=omp_get_thread_num();
#pragma omp master

s++ ;
#pragma omp single
s++ ;
#pragma omp barrier
printf( "s=%d by thread %d\n", s,tid ) ;

}
return 0;
}

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int main ()
{

int t ;

t = omp_get_num_threads() ;

#pragma omp parallel

{
int x,y ;
int tid=omp_get_thread_num();
x = omp_get_num_threads() ;

y = omp_get_thread_num() ;

#pragma omp single


{
t += x ;
printf( "%d by thread %d\n", t ,tid) ;
}
}

return 0;
}
Nowait:

OpenMP provides a clause – nowait, which can be used with a for directive
to indicate that the threads can proceed to the next statement without
waiting for all other threads to complete the for loop execution.

#pragma omp parallel


{
#pragma omp for nowait
for (i=0; i<N; i++)
a[i] = // some expression
#pragma omp for
for (i=0; i<N; i++)
b[i] = ......
}

Critical: It is often used to protect shared data from race conditions.


The typical application of a critical section is to update a variable.

#pragma omp parallel


{
int mytid = omp_get_thread_num();
double tmp = some_function(mytid);
#pragma omp critical
sum += tmp;
}

int a=0, b=0;


#pragma omp parallel num_threads(4)
{
#pragma omp single
a++;
#pragma omp critical
b++;
}
printf("single: %d -- critical: %d\n", a, b);

Sections:

This sections directive assigns the structured block corresponding to


each section to one thread.

The clause list may include the following clauses – private,


firstprivate, lastprivate, reduction, and no wait.

The lastprivate clause, in this case, specifies that the last section
(lexically) of the sections directive updates the value of the variable.

The nowait clause specifies that there is no implicit synchronization


among all threads at the end of the sections directive.
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
{
taskA();
}
#pragma omp section
{
taskB();
}
}
}

The sections construct distributes the blocks/tasks between existing


threads. The requirement is that each block must be independent of the
other blocks. Then each thread executes one block at a time. Each block
is executed only once by one thread.

#pragma omp parallel sections


{
#pragma omp section
{
taskA();
}
#pragma omp section
{
taskB();
}
}

The for Directive:

The clauses that can be used in this context are: private, firstprivate,
lastprivate, reduction, schedule, nowait and ordered.

OpenMP Run time Library Functions:

Controlling Number of Threads and Processors

void omp_set_num_threads (int num_threads);


int omp_get_num_threads ();
int omp_get_max_threads ();
int omp_get_thread_num ();
int omp_get_num_procs ();
int omp_in_parallel();

Controlling and Monitoring Thread Creation:

void omp_set_dynamic (int dynamic_threads);


int omp_get_dynamic ();
void omp_set_nested (int nested);
int omp_get_nested ();
Mutual Exclusion:

void omp_init_lock (omp_lock_t *lock);


void omp_destroy_lock (omp_lock_t *lock);
void omp_set_lock (omp_lock_t *lock);
void omp_unset_lock (omp_lock_t *lock);
int omp_test_lock (omp_lock_t *lock); - omp_test_lock can be used to
attempt to set a lock. If the function returns a non-zero value, the lock
has been successfully set, otherwise the lock is currently owned by
another thread.

Environment Variables in OpenMP:

setenv OMP_NUM_THREADS 8
setenv OMP_SCHEDULE "static,4"
setenv OMP_SCHEDULE "dynamic"
setenv OMP_SCHEDULE "guided"

Potrebbero piacerti anche