How to set up openMP

14,728

Solution 1

In Eclipse you need to set up the following parameters in your project configuration:

C/C++ Build -> Settings -> Includes: add gomp 

C/C++ Build -> Settings -> Miscellaneous -> Other Flags:  add -fopenmp

You need also to install openmp binaries for MinGW:

TDM-GCC

Solution 2

I haven't done much with OpenMP but here is what I found from trying it myself.

OpenMP allows you to tell the compiler that things should run in parallel. Here is an example that counts to 10000000000:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    long result = 0;
    long i;

#pragma omp parallel for reduction(+: result)
    for (i = 0; i < 10000000000; i++) {
        result++;
    }
    printf("%li\n", result);
    return EXIT_SUCCESS;
}

This will compile like a normal C program:

$ gcc -o OpenMPTest main.c

It runs in 30 seconds on my machine:

$ time ./OpenMPTest
10000000000

real 0m30.674s
user 0m30.628s
sys 0m0.014s

Adding the -fopenmp option to gcc enables OpenMP support. That is all you need to have the loop run in multiple threads.

$ gcc -o OpenMPTest main.c -fopenmp

This halves the running time to 15 seconds on a dual core system: 

$ time ./OpenMPTest
10000000000

real 0m15.840s
user 0m30.123s
sys 0m0.037s

Here is a brief description of what that #pragma line does:

#pragma omp

This is a prefix for openmp commands.

parallel

Tells the compiler that the next statement should run in parallel.

for reduction(+: result)

Combines all values of result variable from the parallel loops into one value by addition. Removing this results in the final number printed out being incorrect.

I know you are using eclipse but the MSDN docs are quite good for describing OpenMP: OpenMP in Visual C++

Share:
14,728
TGM
Author by

TGM

Updated on June 04, 2022

Comments

  • TGM
    TGM almost 2 years

    I would like to test openMP with C on Windows 7 with Eclipse IDE. I can't find anywhere some specific steps to follow to set up openMP. Can you help me?