OpenMP Flashcards
(29 cards)
When was OpenMP first introduced?
1997
What is the latest version of OpenMP? When was it released?
Version 6.0, released in November 2024.
What are the three main ways OpenMP extends C, C++, and Fortran?
- Compiler Directives
- Environment Variables
- Runtime Library Routines
Describe the OpenMP execution model.
Uses a fork-join model:
1. Execution starts with a single thread (master thread)
2. Worker threads start (fork) on entry to a parallel region
3. Worker threads exit (join) at end of parallel region
How do you include an OpenMP directive in C/C++?
#pragma omp directive [clause1, ...]
How do you create a parallel region in OpenMP?
#pragma omp parallel
Good practice to enclose parallel region in {} braces
What compiler flag is required for OpenMP in GCC?
-fopenmp
How can you control the number of threads used in OpenMP?
Set the OMP_NUM_THREADS
environment variable (e.g., export OMP_NUM_THREADS=4
).
What happens if the number of threads is not specified?
The implementation chooses a default value.
What does the OpenMP runtime library provide?
Functions for:
- Execution Environment Control
- Timing
- Device Memory Management
- Locking
Name three OpenMP functions that query execution environment details.
-
omp_get_thread_num()
: Returns the thread number -
omp_get_num_threads()
: Returns the total number of threads -
omp_get_num_procs()
: Returns the number of available processors
What header file must be included to use OpenMP functions in C?
#include <omp.h>
Why would conditional compilation be needed for OpenMP programs?
To allow us to build the serial version of the program.
What preprocessor directive is used for conditional compilation in OpenMP?
#ifdef _OPENMP
How can you conditionally include OpenMP-specific code?
#ifdef _OPENMP
code_here…endif
What does the _OPENMP
macro indicate?
It is defined only when OpenMP is enabled during compilation.
Why is loop parallelisation important in OpenMP?
It allows threads to carry out different iterations of a loop simultaneously, improving performance.
What directive is used to parallelise a loop inside a parallel region?
#pragma omp for
How do you combine parallel region creation and loop parallelisation?
#pragma omp parallel for
What are the two types of variable scoping in OpenMP?
Explain each
- Shared: A variable is accessible by all threads
- Private: Each thread has its own copy of the variable
How can you explicitly define variable scope in OpenMP?
Use the default
, private
, and shared
clauses in OpenMP directives.
What does default(none)
do in OpenMP?
It forces all variables to be explicitly declared as private
or shared
.
What issue can occur when multiple threads modify a shared variable simultaneously? Why is it bad?
A data race, which can lead to incorrect results.
How can you safely sum values in parallel?
Use a reduction variable:#pragma omp for reduction(+ : variable_name)