Shared Memory Programming with OpenMP
V.M.Prabhakaran, AP/CSE, KIT- Coimbatore
V.M.Prabhakaran, AP/CSE 1
OpenMP Directives
OpenMp Directives
2V.M.Prabhakaran, AP/CSE
Introduction
• Insist which Instructions or Task to be
executed and distributed among multiple
threads.
• Understood only by OpenMP Compilers.
• In C and C++, the standard preprocessing
directive is starting with #pragma omp
• #pragma omp directive-name [optional_clauses]
3V.M.Prabhakaran, AP/CSE
Commonly Used Directives
• Parallel
• For
• Parallel for
• Sections
• Parallel sections
• Critical
• Single
4V.M.Prabhakaran, AP/CSE
A Sequential program
can be converted
into a parallel program
by including OpenMP directives
1. Parallel Directives
• Executed in parallel using
multiple threads.
Syntax:
#pragma omp parallel[clauses]
{
// Block of code
}
• Clauses specified is optional
• OpenMP Clauses
– copyin
– default(OpenMP)
– firstprivate
– if(OpenMP)
– num_threads,
– private(OpenMP)
– reduction and
shared(OpenMP)
V.M.Prabhakaran, AP/CSE 5
1. Parallel Directives ( Cont)
Example:
#include<stdio.h>
#include<omp.h>
Void main()
{
#pragma omp parallel num_threads(2)
{
int NUM=omp_get_thread_num(); // returns current thread number
printf(“Hello IV CSE A from thread %dn”,NUM);
}
}
V.M.Prabhakaran, AP/CSE 6
Output:
Hello IV CSE A from thread 0
Hello IV CSE A from thread 1
2. For Directives
• For Loop precedes with for directive.
• It split for loop into different portions that each
thread can execute each portion in parallel.
Syntax:
#pragma omp for
… for loop… // Executed by multiple threads
V.M.Prabhakaran, AP/CSE 7
2. For Directives ( cont)
Example:
#pragma omp for
for(int n=0;n<5;n++)
{
Printf(“%dt”,n);
}
Output:
0 1 2 3 4
V.M.Prabhakaran, AP/CSE 8
OpenMP Clauses supported
by for directive:
•firstprivate
•lastprivate
•nowait
•ordered
•private
•reduction
•schedule
3. Parallel for Directive
• Combination of parallel and for directive
• Syntax:
#pragma omp parallel for
• Parallel directive split the current thread into
multiple threads to execute task in parallel.
• for directive divides the execution of for loop
among multiple threads.
• Parallel for is the combination of both directives.
V.M.Prabhakaran, AP/CSE 9
3. Parallel for Directive (cont)
Example:
#pragma omp parallel
{
#pragma omp for
for(int n=0;n<5;n++)
{
print(“%dt”,n);
}
}
Output:
0 1 2 3 4
Rewritten as:
#pragma omp parallel for
for(int n=0;n<5;n++)
{
print(“%dt”,n);
}
V.M.Prabhakaran, AP/CSE 10
4. Section and Sections Directives
• A Set of series blocks that can be executed in
parallel.
• It identifies a block of code to be executed in
parallel.
• It can have zero, one or more section
directives.
V.M.Prabhakaran, AP/CSE 11
4. Section and Sections Directives( Cont)
Syntax:
#pragma omp sections
{
#pragma omp section
{
Code_block
}
}
Clauses supported:
• firstprivate
• lastprivate
• nowait
• private(OpenMP)
• reduction
V.M.Prabhakaran, AP/CSE 12
4. Section and Sections Directives( Cont)
Example:
#pragma omp parallel // Starts a new team
{
#pragma omp sections num_threads(5) // Splits the team into sections
{
printf(“Hello IV CSE A from thread %dn,id1”);
#prama omp section
{
int id1=omp_get_thread_num();
printf(“Hello IV CSE A from thread %dn,id1”);
}
#prama omp section
{
int id2=omp_get_thread_num();
printf(“Hello IV CSE A from thread %dn,id2”);
}
}
} V.M.Prabhakaran, AP/CSE 13
5. Parallel Sections Directive
• Combination of parallel and section directives.
Example:
#pragma omp parallel sections
{
#prama omp section
{
int id1=omp_get_thread_num();
printf(“Hello IV CSE A from thread %dn,id1”);
}
#prama omp section
{
int id2=omp_get_thread_num();
printf(“Hello IV CSE A from thread %dn,id2”);
}
}
V.M.Prabhakaran, AP/CSE 14
6. Critical Directive
• Code can only be
executed by one thread at
a time.
• Does not support any of
the OpenMP Clauses.
• Syntax:
#pragma omp critical
{
…. Block of code…//
Executed by only one
thread
{
Example:
# pragma omp critical
{
value = value + details;
}
V.M.Prabhakaran, AP/CSE 15
7. Single Directive
• Block of code executed by a Single thread.
• Syntax:
#pragma omp single
{
code_block
}
• Clauses supported: copyprivate,
firstprivate, nowait,
private(OpenMP)
V.M.Prabhakaran, AP/CSE 16
7. Single Directive (cont)
Example:
#pragma omp parallel num_threads(2)
{
#pragma omp single // Only one thread can read
print(“read input”);
#pragma omp single // Only one thread can write output
print(“Write output”);
V.M.Prabhakaran, AP/CSE 17
V.M.Prabhakaran, AP/CSE 18

Open mp directives

  • 1.
    Shared Memory Programmingwith OpenMP V.M.Prabhakaran, AP/CSE, KIT- Coimbatore V.M.Prabhakaran, AP/CSE 1 OpenMP Directives
  • 2.
  • 3.
    Introduction • Insist whichInstructions or Task to be executed and distributed among multiple threads. • Understood only by OpenMP Compilers. • In C and C++, the standard preprocessing directive is starting with #pragma omp • #pragma omp directive-name [optional_clauses] 3V.M.Prabhakaran, AP/CSE
  • 4.
    Commonly Used Directives •Parallel • For • Parallel for • Sections • Parallel sections • Critical • Single 4V.M.Prabhakaran, AP/CSE A Sequential program can be converted into a parallel program by including OpenMP directives
  • 5.
    1. Parallel Directives •Executed in parallel using multiple threads. Syntax: #pragma omp parallel[clauses] { // Block of code } • Clauses specified is optional • OpenMP Clauses – copyin – default(OpenMP) – firstprivate – if(OpenMP) – num_threads, – private(OpenMP) – reduction and shared(OpenMP) V.M.Prabhakaran, AP/CSE 5
  • 6.
    1. Parallel Directives( Cont) Example: #include<stdio.h> #include<omp.h> Void main() { #pragma omp parallel num_threads(2) { int NUM=omp_get_thread_num(); // returns current thread number printf(“Hello IV CSE A from thread %dn”,NUM); } } V.M.Prabhakaran, AP/CSE 6 Output: Hello IV CSE A from thread 0 Hello IV CSE A from thread 1
  • 7.
    2. For Directives •For Loop precedes with for directive. • It split for loop into different portions that each thread can execute each portion in parallel. Syntax: #pragma omp for … for loop… // Executed by multiple threads V.M.Prabhakaran, AP/CSE 7
  • 8.
    2. For Directives( cont) Example: #pragma omp for for(int n=0;n<5;n++) { Printf(“%dt”,n); } Output: 0 1 2 3 4 V.M.Prabhakaran, AP/CSE 8 OpenMP Clauses supported by for directive: •firstprivate •lastprivate •nowait •ordered •private •reduction •schedule
  • 9.
    3. Parallel forDirective • Combination of parallel and for directive • Syntax: #pragma omp parallel for • Parallel directive split the current thread into multiple threads to execute task in parallel. • for directive divides the execution of for loop among multiple threads. • Parallel for is the combination of both directives. V.M.Prabhakaran, AP/CSE 9
  • 10.
    3. Parallel forDirective (cont) Example: #pragma omp parallel { #pragma omp for for(int n=0;n<5;n++) { print(“%dt”,n); } } Output: 0 1 2 3 4 Rewritten as: #pragma omp parallel for for(int n=0;n<5;n++) { print(“%dt”,n); } V.M.Prabhakaran, AP/CSE 10
  • 11.
    4. Section andSections Directives • A Set of series blocks that can be executed in parallel. • It identifies a block of code to be executed in parallel. • It can have zero, one or more section directives. V.M.Prabhakaran, AP/CSE 11
  • 12.
    4. Section andSections Directives( Cont) Syntax: #pragma omp sections { #pragma omp section { Code_block } } Clauses supported: • firstprivate • lastprivate • nowait • private(OpenMP) • reduction V.M.Prabhakaran, AP/CSE 12
  • 13.
    4. Section andSections Directives( Cont) Example: #pragma omp parallel // Starts a new team { #pragma omp sections num_threads(5) // Splits the team into sections { printf(“Hello IV CSE A from thread %dn,id1”); #prama omp section { int id1=omp_get_thread_num(); printf(“Hello IV CSE A from thread %dn,id1”); } #prama omp section { int id2=omp_get_thread_num(); printf(“Hello IV CSE A from thread %dn,id2”); } } } V.M.Prabhakaran, AP/CSE 13
  • 14.
    5. Parallel SectionsDirective • Combination of parallel and section directives. Example: #pragma omp parallel sections { #prama omp section { int id1=omp_get_thread_num(); printf(“Hello IV CSE A from thread %dn,id1”); } #prama omp section { int id2=omp_get_thread_num(); printf(“Hello IV CSE A from thread %dn,id2”); } } V.M.Prabhakaran, AP/CSE 14
  • 15.
    6. Critical Directive •Code can only be executed by one thread at a time. • Does not support any of the OpenMP Clauses. • Syntax: #pragma omp critical { …. Block of code…// Executed by only one thread { Example: # pragma omp critical { value = value + details; } V.M.Prabhakaran, AP/CSE 15
  • 16.
    7. Single Directive •Block of code executed by a Single thread. • Syntax: #pragma omp single { code_block } • Clauses supported: copyprivate, firstprivate, nowait, private(OpenMP) V.M.Prabhakaran, AP/CSE 16
  • 17.
    7. Single Directive(cont) Example: #pragma omp parallel num_threads(2) { #pragma omp single // Only one thread can read print(“read input”); #pragma omp single // Only one thread can write output print(“Write output”); V.M.Prabhakaran, AP/CSE 17
  • 18.