Rookie HPC

Scheduling

C | FORTRAN

omp_set_schedule

Definition

The function omp_set_schedule is one of the two ways to specify the schedule to apply when a runtime clause is encountered during execution. The other one is the environment variable OMP_SCHEDULE. The expected format for the schedule is: the kind of scheduling (static, dynamic, auto or guided). Optionally, the kind can also be succeeded by a comma and the chunk size. To summarise, it can be represented as follows: “[modifier:]kind[,chunksize]”. Please note that if this format is not respected, the behaviour is implementation defined. It is also worth mentioning that implementation specific schedules can be used in the omp_set_schedule function, but they cannot be used in the environment variable OMP_SCHEDULE. If none of the environment variable OMP_SCHEDULE or the function omp_set_schedule is used, the schedule to apply when a runtime clause is encountered during execution is implementation defined. To see the schedule to apply when the runtime clause is encountered, one can call omp_get_schedule.

Copy

Feedback

void omp_set_schedule(omp_sched_t* policy,
                      int* chunk_size);

Parameters

policy
The OpenMP scheduling policy to use, possible values are omp_sched_static, omp_sched_dynamic, omp_sched_guided and omp_sched_auto.
chunk_size
The number of iterations that make a chunk. If this number is less than 1, the defaulted chunk size is used. For the auto schedule, this argument has no meaning.

Example

Copy

Feedback

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

/**
 * @brief Illustrates the use of the omp_set_schedule function.
 * @details A for loop is parallelised across 4 threads using the runtime
 * schedule policy. The schedule to apply is defined by omp_set_schedule as 
 * being a dynamic scheduling with chunks made of 4 iterations.
 **/
int main(int argc, char* argv[])
{
    // Use 4 threads when creating OpenMP parallel regions
    omp_set_num_threads(4);

    // Tell OpenMP which scheduling policy and chunk size use for runtime schedule clauses
    omp_set_schedule(omp_sched_dynamic, 2);

    // Parallelise the for loop using the runtime schedule
    #pragma omp parallel for schedule(runtime)
    for(int i = 0; i < 10; i++)
    {
        printf("Thread %d processes iteration %d.\n", omp_get_thread_num(), i);
    }

    return EXIT_SUCCESS;
}