B1 - OpenMP
-
it includes a set of compiler directives to describe parallelism in the source code
-
also includes a subroutine library
-
uses a set of environment variables
-
parallelism based on threads
thread
- the smallest sequence of programmed instruction that can be managed independently by a scheduler
-
the program begins as a single process, the master thread
-
it executes sequentially until the first fork, where a team of parallel threads is created
-
upon completion, the threads synchronise and terminate or join, bringing control back to a single master thread
-
in C, directives take the form of pragmas
#pragma omp parallel default(shared) private(x,y)
- for a sequential compilation, it will be interpreted as comments
- each C directive applies only to the next statement or block, hence a structured block
{...}is always needed
#include <omp.h>
#include <stdio.h>
int main()
{
int nthreads, tid;
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
printf("Hello world from thread = %d\n", tid);
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
}
}
- can combine parallel directive with for directive:
#pragma omp parallel for
for (i = 0; i < i_max; i++)
{
z[i] = a * x[i] + y;
}
- there are no data dependencies, and each iteration must be independent of the others
- the loop iterations get split across threads