OpenMP

cosmos 4th April 2017 at 11:30am

Open Multi-Processing is a standard found in most modern C compilers..

Parallel programming with several processors with shared memory.

One process with multiple threads of execution.

OpenMP API components:

  • C ompiler directives
  • R untime library routines
  • E nvironment variable

To create several threads for a block of code with openMP use: # pragma omp parallel default( none ) private( my_thread ) { /* my parallel code*/

The default(none) clause forces a programmer to explicitly specify the data-sharing attributes of all variables in a parallel region. Using this clause then forces the programmer to think about data-sharing attributes.

omp_get_thread_num() gives a unique thread ID

gcc -fopenmp (flag to link OpenMP runtime library)

Environment variable OMP_NUM_THREADS determines number of threads created by OMP

shared( a, b, c ) private(i): means that the variables a,b,c are on shared memory. i are on each thread's local memory

#pragma omp for: separate for loop into threads

Synchronising Threads

Barrier : a given thread can only proceed once all threads have reached the barrier

Critical region : Only 1 thread can be executing codes within a critical region.

Reduction : Combining multiple private values into a single shared value