OpenMP 2
OpenMP 2
1 OpenMP
OpenMP (Open Multi-Processing) is an Application Programming Interface (API) that supports multi-platform shared-
memory multiprocessing programming. It allows developers to write parallel programs that can leverage the power
of multiple cores or processors within a single computer system.
Key Concepts
• Shared Memory: Processes (threads) in an OpenMP program access the same memory space, enabling them
to share data efficiently.
• Threads: OpenMP uses threads of execution to perform tasks concurrently. These threads share the program's
data and instructions.
• Directives: OpenMP directives are compiler instructions embedded within the source code. These directives
specify how to parallelize specific sections of the code.
Benefits of OpenMP
• Improved Performance: By utilizing multiple cores/processors, OpenMP programs can execute faster
compared to their sequential counterparts.
• Portability: OpenMP is a widely supported standard, allowing code to be portable across different platforms
with minimal modifications.
• Ease of Use: Compared to lower-level parallel programming models, OpenMP offers a relatively simpler
approach for parallelization.
2 Examples
• Code 1 demonstrates a simple use of OpenMp parallel construct. Compile and run the program and study the
output of several runs.
• Code 2 shows a simple OpenMP "Hello, world!" program in which every thread identifies its id. Compile and
run the program and study the output of several runs.
• Code 3, Code 4, and Code 5 show different use cases of for-loop. Compile and run the program and study the
output of several runs.
3 Practice
1. In Code 3, what is the impact of not using the private clause. Explain.
2. What is the difference between Code 3 and Code 4 in terms of the number of thread teams?
3. How many iterations are executed if six threads execute Code 3? What about Code 4?
4. Explain the output of Code 5.
5. Write a parallel version of a program that computes and displays the dot product of 2 vectors, a and b. Make
sure that race conditions are not going to happen.
a) What is the output of the following block b) Now consider the following code:
of code? int i;
int i; float sum = 0.0;
float sum = 0.0; # pragma omp parallel for num threads(2) \
for (i = 0; i < 4; i++) reduction(+:sum)
sum += a[i]; for (i = 0; i < 4; i++)
printf("sum = %4.1f\n", sum); sum += a[i];
printf("sum = %4.1f\n", sum);
Suppose that the run-time system assigns iterations i = 0, 1 to
thread 0 and i = 2, 3 to thread 1. What is the output of this code?
2
Code 1 Code 2
Code 3
Code 4
Code 5