0% found this document useful (0 votes)
4 views

Practice_OpenMP

The document provides exercises for parallel programming using OpenMP, focusing on calculating the value of PI, testing Goldbach's conjecture, and performing histogram equalization on images. It includes instructions for compiling code, implementing functions, and measuring execution time while utilizing OpenMP for parallelization. The exercises aim to enhance understanding of parallel computing concepts and performance optimization techniques.

Uploaded by

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

Practice_OpenMP

The document provides exercises for parallel programming using OpenMP, focusing on calculating the value of PI, testing Goldbach's conjecture, and performing histogram equalization on images. It includes instructions for compiling code, implementing functions, and measuring execution time while utilizing OpenMP for parallelization. The exercises aim to enhance understanding of parallel computing concepts and performance optimization techniques.

Uploaded by

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

Parallel Programming Using OpenMP

Recall: to compile a code program.cpp with OpenMP and generate execution file program, use the
following command:
g++ -fopenmp program.cpp -o program

Exercise 1: Compute the value of PI

The number PI can be defined as the integral from 0 to 1 of the function:

The way to compute the approximate value of this integral is to discretize the study set of the
function using N points. (s = 1/N);

A PI.cpp skeleton code is provided to work on it

1) Complete the function Compute_PI(int N) which returns result of the integral above in type of
floating value. N is the number of samples. Test your function with the different value of N:
1000,100000, 1000000, 1000000000 . Mesuring the computation time in each case.

2) Using OpenMP pragma to parallelize the principal for loop of your code with different number of
threads (4, 8, 16, 32). Mesuring the computation time in each case.

3) The function Compute_PI_ato(int N) will return exactly the same result as function Compute_PI
but we must parallelize for loop “by hand”. It means that we don’t use “#pragma omp parallel for”.
Instead of that, we use “#pragma omp parallel” and inside this region, we will get the id of thread.
The thread with identification id will compute the local sum from iteration (id*N/P) to (id+1)*N/P
with P is the number of threads used. After finishing the computation, the thread will add the local
sum to the global value PI using critical or atomic region. Complete this function and compare the
performance (execution time) with version omp parallel for.

Exercise 2: The guess of Goldbach

The purpose of this exercise is to demonstrate the different of using the schedule (dynamic and
static) clause of OpenMP in order to improve performance of parallel code. A goldbach.cpp
skeleton code is provided to work on it. In the world of number theory, according to Goldbach's
guess: every even number greater than two is the sum of two prime numbers.

In this exercise, you will write code to test this guess. The code finds the number of “Goldbach
pairs” for a given even number i (example: the number of pairs of prime numbers P1, P2 such that
P1 + P2 = i) for i = 4, . . . , 8000. The computational cost is proportional to i^1.5 , so to obtain
optimal performance with multiple threads, the workload should be distributed by appropriately
using the schedule clause of OpenMP.

1) Write a code to main function to add all even number from 4 to 8000 to “vector<int>
even_nums”. Resize the vector goldbach_pairs_nums with the same size of vector even_nums.
2) Complete the function testPrime to test an integer number whether or not a prime number. Return
true if it is a prime number.
3) Complete the function goldbach(int x) which returns the number of “Goldbach pairs” of an
integer x. Using this function, then make a for loop in the main function to compute “Goldbach
pairs” for each element of even_nums and add the result to the same position in
goldbach_pairs_nums. The following table is the result of some even number. Use it to verify your
program.

4) Now, try to parallelize your code in question 3 using OpenMP using two types of scheduling
(static and then dynamic). Note that you must verify the correctness of result while using multi-
thread.
5) We have another type of scheduling (guided). Using it and analyze its performance. Here is the
reference link for 3 types of scheduling:
(https://610yilingliu.github.io/2020/07/15/ScheduleinOpenMP/)

Exercise 3: Histogram Equalization for an image

In the Histogram folder, you have a file histogram.cpp which can be compile using Makefile
presented in the same folder (you can compile directly by typing “make” in the terminal if you use
OpenCV 3.4, or you can modify Makefile or using Cmake to compile the code like previous
practice).

In the code, we have the function Histogram_Equalization which implements a very well-known
algorithm to enhance the contrast of an image.

1) Compile the code and run it (change the name of the image in the main function to test with 3
images provided in the folder.

2) To understand the algorithm, read the examples part of the following link:
https://en.wikipedia.org/wiki/Histogram_equalization

3) Now, observe the execution time of the function on your laptop (printed in terminal). Take it like
a reference. Try to use OpenMP or whatever techniques to accelerate the processing of
Histogram_Equalization function.

You might also like