Search code examples
copenmp

What is #pragma used for in OpenMP?


I am trying to understand the meaning and usage of #pragma in OpenMP, I have this small sample code:

int main()
{
omp_set_num_threads(16);
#pragma omp parallel
{
printf( "Hello, World!\n" );
}
return 0;
}

I can't understand what is pragma for, that is the first time I see it being used in C. I know that the entire line is to indicate the start of the parallel part. Any help would be appreciated.


Solution

  • The pragma directive is used to access compiler-specific preprocessor extensions. Using the following format:

    #pragma compiler specific extension

    Which means that you're telling your compiler to expand the directive and turn it into a runnable code that uses the desired extension functionality.

    It depends entirely on the compiler being used, so if you use a really old GCC/Clang you might not have all the functionalities of OpenMP or perhaps none at all.

    If you want to go deeper into the #pragma subject you can follow these links:

    1. GNU GCC C++ Docs
    2. CProgramming
    3. Microsoft Docs
    4. Wikipedia