0% found this document useful (0 votes)
20 views7 pages

Assignment 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Assignment 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Walchand College of Engineering, Sangli

Department of Computer Science and Engineering


Class: Final Year (Computer Science and Engineering)
Year: 2024-25 Semester: 1
Course: High Performance Computing Lab

Practical No. 4
Exam Seat No: 21510021
Title of practical:
Study and Implementation of Synchronization

1|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering

Problem Statement 1:

Analyse and implement a Parallel code for below programs using OpenMP
considering synchronization requirements. (Demonstrate the use of different
clauses and constructs wherever applicable)

Fibonacci Computation:

Screenshots:

2|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering

Information:

1. Parallel Region:
The parallel region is created with #pragma omp parallel. This tells OpenMP to spawn
multiple threads (specified by num_threads(4)) to execute the code inside the region in
parallel.
2. Single Directive:
#pragma omp single ensures that only one thread initiates the Fibonacci computation.
Without this, every thread in the parallel region would start computing Fibonacci from
the top, which would result in redundant calculations.
3. Tasks:
OpenMP tasks (#pragma omp task) are used to parallelize the recursive calls to the fib
function. Each recursive call to compute fib(n-1) and fib(n-2) is treated as a separate
task that can be executed by different threads.
The shared(x) and shared(y) clauses ensure that the results of the tasks are shared
across threads, while firstprivate(n) initializes each task with its own copy of the
variable n.
4. Task Synchronization:
#pragma omp taskwait is used to synchronize the tasks, ensuring that the program
waits for both recursive tasks (fib(n-1) and fib(n-2)) to complete before proceeding to
combine the results.
5. Thread Management:

3|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering
The number of threads is explicitly set using num_threads(4). This tells OpenMP to use
4 threads for the parallel region, but you can adjust this based on the number of
available cores or the desired level of parallelism.

4|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering
Problem Statement 2:

Analyse and implement a Parallel code for below programs using OpenMP
considering synchronization requirements. (Demonstrate the use of different
clauses and constructs wherever applicable)

Producer Consumer Problem

Screenshots:

5|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering

Information:
1. Critical Section (#pragma omp critical):
 This directive ensures that only one thread (either producer or consumer) can execute the
enclosed code block at a time.
 Purpose: Prevents race conditions, ensuring that the producer and consumer do not
simultaneously access the buffer, which could lead to data corruption.
2. Producer:
 The producer generates a random item and places it into the buffer, but only if the buffer is
not full.
 The entire check and insertion process occurs inside a critical section to ensure that the
consumer does not access the buffer at the same time.
6|Page
Final Year: High Performance Computing Lab 2024-25 Sem I
Walchand College of Engineering, Sangli
Department of Computer Science and Engineering
3. Consumer:
 The consumer removes an item from the buffer, but only if the buffer is not empty.
 The removal process is also enclosed in a critical section to ensure the producer does not
modify the buffer during this time.

Github Link:

7|Page
Final Year: High Performance Computing Lab 2024-25 Sem I

You might also like