Search code examples
copenmpschedulepragma

How to dynamically choose scheduling type?


I need to compare execution time of a loop with different scheduling type and threads number with openMP.

I can set threads number dynamically by passing an int variable directly in the pragma macro:

int threads_number = 4;
#pragma omp parallel for num_threads(threads_number)

But I'm trying to do the same for schedule, I need to compare static, dynamic and guided types. But it appears I cannot use an int as enum nor a char* as name for this one.

Is there any way to choose it dynamically, or do I have to write the loop 3 times and choose which one to call with an if, which seems kind of dirty ?


Solution

  • You can set the scheduling kind when the applied schedule is runtime.

    When schedule(runtime) is specified, the decision regarding scheduling is deferred until runtime. The schedule kind and size of the chunks can be chosen at run time by setting the environment variable OMP_SCHEDULE. If this environment variable is not set, the resulting schedule is implementation-defined. When schedule(runtime) is specified, chunk_size must not be specified. - OpenMP-4.5 Specification

    This is done with the function void omp_set_schedule(omp_sched_t kind, int chunk_size);

    The scheduling kinds that are available are defined in omp.h as this enum below

    typedef enum omp_sched_t {
        omp_sched_static = 1,
        omp_sched_dynamic = 2,
        omp_sched_guided = 3,
        omp_sched_auto = 4
    } omp_sched_t;