You are on page 1of 14

OpenMP Constructs

and
Parallel Sum
Private and Shared Variables

 In OpenMP, we use a private (tid) clause to privatize the tid variable as each thread needs to set its own
value.
 You
Shared (list)this text for
can edit all threads see the same copy of variables in the list
 Private
your presentation.
(list) each thread has its own copy of the variables in the list
 Default (shared | none) define the default sharing attribute
 If you set the sharing attribute to “none” then, you need to specify the sharing attribute for all the
variables used in the parallel region
for your presentation.
Title Here
You can edit this text for
your presentation.
Private Variables

 Private variables are specific to each thread within a parallel region.


 Each thread gets its own copy of the private variable, and changes made to it by one thread do not affect
You can edit this text for
the value seen by other threads.
your presentation.
 Private variables are typically used for temporary or loop index variables that should not be shared
among threads to prevent data races.

Title Here Title Here


Private Variables

int main() {
int x = 10;
Output:
#pragma omp parallel private(x)
You
{ can edit this text for
// Each
your thread has its own private copy of x
presentation.

int thread_id = omp_get_thread_num();


x += thread_id;

printf("Thread %d: x = %d\n", thread_id, x);


}
Title Here Title Here
printf("Outside parallel region: x = %d\n", x);
return 0;
}
Public Variables

 Public variables, also known as shared variables, are accessible to all threads within a parallel region.
 Changes made to a public variable by one thread are visible to all other threads.
 Public
You variables can
can edit this textlead
for to data races if not managed properly, so you should use synchronization
your presentation.
mechanisms like critical sections, locks, or atomic operations when necessary to ensure thread safety.

Title Here Title Here


#include <omp.h>
#include <stdio.h> Public Variables
int main() {
int y = 10;
Output:
#pragma omp parallel shared(y)
{ can edit this text for
You
// All threads share the same variable y
your presentation.
int thread_id = omp_get_thread_num();

#pragma omp critical


y += thread_id;

printf("Thread %d: y = %d\n", thread_id, y);


}
Title Here Title Here
// y outside the parallel region reflects
changes made by threads
printf("Outside parallel region: y = %d\n", y);
return 0;
}
Loop Constructs
Collapse (n)

 One of the valid clauses for the loop construct is collapse (n) where n must be a constant positive integer
expression. The clause is used to indicate how many loops are associated with the loop construct.
You can edit this text for
 In the example given, the i and j loops are associated with the loop construct.
your presentation.

Title Here
Loop Constructs

You can edit this text for


your presentation.

Title Here
Section Construct

 Another work sharing construct for distributing computation across the threads is the sections construct
of the syntax:

You can edit this text for


your presentation.

Title Here
Section Construct
int main() {
#pragma omp parallel sections
{
#pragma omp section
{
printf("Section 1 executed by Thread %d\n", omp_get_thread_num()); Output:
You can edit this text for
}
your presentation.
#pragma omp section
{
printf("Section 2 executed by Thread %d\n", omp_get_thread_num());
}

#pragma omp section


{ Title Here
printf("Section 3 executed by Thread %d\n", omp_get_thread_num());
}
}

return 0;
}
Single Construct

 As the name of the construct suggests, the block of code enclosed by the construct is implemented by a
single thread. This thread is a member of the team of threads created to execute the innermost enclosing
parallel construct.
You can edit this text for
your presentation.

Title Here
Task Construct

 The task construct in OpenMP creates and manages parallel tasks in a multithreaded program. It
provides a way to express fine-grained parallelism by defining tasks that can be executed concurrently
by available threads.
You can edit this textThe
for primary purpose of the task construct is to improve the utilization of CPU
yourand
resources presentation.
facilitate the parallelization of algorithms that have task-level parallelism.
Taskyield Construct

 The one directive taskyield construct of the syntax:

 constitutes
You can an
editexplicit
this texttask
for scheduling point. When a thread reaches the taskyield construct, the OpenMP
your
runtime presentation.
system may enforce the thread to make a task switch. This involves suspending the execution of
the current task and beginning or resuming the execution of a different task assigned to the current team
of threads.

Title Here
Graded TASK

YouYou
have tothis
can edit compute
text for the factorial of a given integer using parallelism
with OpenMP. You'll use an array to accumulate partial results from
your presentation.

each thread and then combine these results after the parallel region
using “OpenMP Constructs”.

You might also like