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.