OpenMP Flashcards
When was OpenMP first introduced for Fortran and C/C++?
- Fortran: October 1997
- C/C++: October 1998
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 fork-join execution model.
Execution starts with a single master thread, which forks worker threads in parallel regions and joins them back at the end.
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
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 compilation without OpenMP support, enabling serial execution.
What preprocessor directive is used for conditional compilation in OpenMP?
#ifdef _OPENMP
How can you conditionally include OpenMP-specific code?
#ifdef _OPENMP include <omp.h> 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 work to be divided among threads, 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?
- 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 occurs when multiple threads modify a shared variable simultaneously?
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(+ : sum)