Using OMP_SCHEDULE with #pragma omp for parallel schedule(runtime)

12,435

There are 4 types of OMP scheduling. They are static, dynamic, runtime and guided. Each scheduling has its advantages. The scheduling is present for better load balancing among the threads.

I will give you some example for static and dynamic scheduling. It is similar for the guided as well.

The schedule(runtime) clause tells it to set the schedule using the environment variable. The environment variable can be set to any other scheduling type. It can be set by

setenv OMP_SCHEDULE “dynamic,5”

Static Scheduling

Static scheduling is used when you know that each thread will more-or-less do same amount of work at the compile time.

For example, the following code can be parallelized using OMP. Let assume that we use only 4 threads.

If we are using the default static scheduling and place pragma on the outer for loop, then each thread will do 25% of the outer loop (i) work and eqaul amount of inner loop (j) work Hence, the total amount of work done by each thread is same. Hence, we could simply stick with the default static scheduling to give optimal load balancing.

float A[100][100];

for(int i = 0; i < 100; i++)
{
   for(int j = 0; j < 100; j++)
   {
      A[i][j] = 1.0f;
   }
}

Dynamic Scheduling

Dynamic scheduling is used when you know that each thread won't do same amount of work by using static scheduling.

Whereas, in this following code,

float A[100][100];

for(int i = 0; i < 100; i++)
{
   for(int j = 0; j < i; j++)
   {
      A[i][j] = 1.0f;
   }
}

The inner loop variable j is dependent on the i. If you use the default static scheduling, the outer loop (i) work might be divided equally between the 4 threads, but the inner loop (j) work will be large for some threads. This means that each thread won't do equal amount of work with static scheduling. Static scheduling won't result in optimal load balancing between threads. Hence we switch to dynamic scheduling (scheduling is done at the run time). This way you can make sure that the code achieves optimal load balance.

Note: you can also specify the chunk_size for scheduling. It depends on the loop size.

Share:
12,435
Flame_Phoenix
Author by

Flame_Phoenix

I have been programming all my life since I was a little boy. It all started with Warcraft 3 and the JASS and vJASS languages that oriented me into the path of the programmer. After having that experience I decided that was what I wanted to do for a living, so I entered University (FCT UNL ftw!) where my skills grew immensely and today, after several years of studying, professional experience and a master's degree, I have meddled with pretty much every language you can think of: from NASM to Java, passing by C# Ruby, Python, and so many others I don't even have enough space to mention them all! But don't let that make you think I am a pro. If there is one thing I learned, is that there is always the new guy can teach me. What will you teach me?

Updated on June 15, 2022

Comments

  • Flame_Phoenix
    Flame_Phoenix about 2 years

    I am trying to understand how to use the schedule(runtime) directive of OpenMP in C++. After some research I found OMP_SCHEDULE(1) and OMP_SCHEDULE(2).

    I concluded that I need to set the varibale OMP_SCHEDULE to a certain value. However, I don't know how to do that and I have not found any working C++ examples that explain me how to correctly do so.

    Can some one explain me how to set variable and provide a working C++ example?