Getting started with OPENMP

In this post, we will see a simple example to create threads, how to create a required number of threads, how to parallelize “for” loop and how to allocate different work to a different thread.

 

1.A simple example of OPENMP:

#pragma omp parallel
{
    printf("Hello World\n");
}

This will print “Hello World” multiple times depending on how many threads have been created. By default, the number of threads created is equal to a number of Processor cores. 

output:

Hello World
Hello World
Hello World
Hello World

(For your convenience, I’ve place the code here.)

2.Create a required number of threads:

#pragma omp parallel num_threads(3)
{
    printf("Hello World\n");
}

If we want to create a specific number of threads, use num_threads() and a number indicating a number of threads to be created. In above example, three threads will be created. Each one will be printing “Hello World”.

output:

Hello World
Hello World
Hello World

(For your convenience, I’ve place the code here.)

3. Parallelize “for” loop using OPENMP:

int a[100000];
#pragma omp parallel for
for (int i = 0; i < 100000; i++) {
    a[i] = 2 * i;
}

#pragma omp parallel for tells the compiler to auto-parallelize the for loop. In above code, since we are not mentioning the number of threads, the number of threads will be equal to the number of cores by default.

(For your convenience, I’ve place the code here.)

4.Allocate different work to a different thread:

  #pragma omp parallel sections
   {
     #pragma omp section
      {
        int tid;
        tid=omp_get_thread_num();
        printf("X printed by thread with id=%d\n",tid);
      }
     #pragma omp section
      {
        int tid;
        tid=omp_get_thread_num();
        printf("Y printed by thread with id=%d\n",tid);
      }
     #pragma omp section
      {
        int tid;
        tid=omp_get_thread_num();
        printf("Z printed by thread with id=%d\n",tid);
      }
   }

I have used section to allocate different work to different threads. We have to use #pragma omp parallel sectionsI have provided a simple example, which creates three threads. Each thread does the distinct job. The first thread is printing ‘X’, second thread ‘Y’ and third thread ‘Z’. I am also printing thread id, so that you can easily understand, that threads are different and doing an independent job. omp_get_thread_num() is used to print the thread id

output:

Y printed by thread with id=0
X printed by thread with id=3
Z printed by thread with id=1

(For your convenience, I’ve place the code here.)

How To Run: 
To Compile:
gcc -fopenmp code.c   
To Run:
./a.out

References:

Wikipedia | Getting started with openmp(intel)

Leave a comment