Site Tools


openmp

**This is an old revision of the document!**

Table of Contents

OpenMP

OpenMP is a standardized parallel computing API mostly used for CPU parallelism. It's created for C, C++ and Fortran programming languages.

It's a bit weird when you first see it, but essentially OpenMP relies on a lesser used compiler directive known as #pragma. This directive tells the compiler to “do something special”. In OpenMP's case, it injects code that creates threads and locks in order to establish a parallel region. In that region there are multiple working threads

// #include <omp.h>
#pragma omp parallel
{
    int tid = omp_get_thread_num();
    printf("Hello, I'm thread %d\n", tid);
}

For example, if your environment variable is set to export OMP_NUM_THREADS=5 you would see the following. Each worker thread

Hello, I'm thread 4
Hello, I'm thread 1
Hello, I'm thread 3
Hello, I'm thread 2
Hello, I'm thread 0
openmp.1781127201.txt.gz · Last modified: by Ivan Janevski