Looking for examples of OpenMP in C++

Dear All,

I would like to use OpenMP in C++. Could someone please direct me to some working examples?

Thanks in advance,

Paul

Assuming your compiler has openMP installed and enabled, something like this should work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <omp.h>
#include <iostream> // I think this is what you need for cout, can't remember

int main () {
    int thread_number;
    #pragma omp parallel private(thread_number)
    {
        #pragma omp for schedule(static) nowait
        for (int i = 0; i < 100; i++) {
            thread_number = omp_get_thread_num();
            cout << "Thread " << thread_number << " says " << i << endl;
        }
    }
    return 69;
}


This will run on every thread, and may or may not output the numbers in consecutive order (each thread will just output the value as it reads it). You should read up on openMP before you try to write a real program with it though, otherwise you'll just frustrate yourself: http://openmp.org/mp-documents/omp-hands-on-SC08.pdf
Last edited on
Thanks, Ausairman, for your useful reply -- I will try to follow your advice.

Paul
Topic archived. No new replies allowed.