如何动态选择调度类型?

How to dynamically choose scheduling type?

我需要比较具有不同调度类型和线程数的循环与 openMP 的执行时间。

我可以通过直接在 pragma 宏中传递一个 int 变量来动态设置线程数:

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

但我正在尝试对 schedule 执行相同的操作,我需要比较 staticdynamicguided 类型。但是看来我不能使用 int 作为枚举,也不能使用 char* 作为这个的名称。

有什么方法可以动态选择它,还是我必须写 3 次循环然后选择用 if 调用哪一个,这看起来有点脏?

当应用的日程为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

这是通过函数void omp_set_schedule(omp_sched_t kind, int chunk_size);

完成的

可用的调度类型在omp.h中定义为下面的枚举

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