openMP conditional pragma “if else”

故事扮演 提交于 2019-12-05 01:21:54

This is an interesting question. Basically, you want to change schedule policy at runtime. As far as I know, there is no such directive for the current OpenMP.

I had the exactly same problem you did. My solution ended up making the loop body as a function, as you mentioned. Otherwise, you need to use an ugly macro.

However, I also tried to use schedule(runtime), which reads the environment variable OMP_SCHEDULE. So, I changed this environment variable at runtime, but didn't work. It's because OpenMP runtime read this environment only once at the beginning. It may be an implementation-specific issue. So, other implementation may read this environment variable on the fly. You may try this approach.

This is years late, but for this particular case you can use the runtime library to set the schedule at runtime. It is defined in §3.2.12 in OpenMP 4.5:

void omp_set_schedule(omp_sched_t kind, int chunk_size);
typedef enum omp_sched_t {
    omp_sched_static = 1,
    omp_sched_dynamic = 2,
    omp_sched_guided = 3,
    omp_sched_auto = 4
} omp_sched_t;

For your case, what you do is:

/* wherever currently isDynamic is set */
if (isDynamic) {
    omp_set_schedule(omp_sched_dynamic, 10);
} else {
     /* chunk_size < 1 uses default */
    omp_set_schedule(static, 0);
}

/* later */
#pragma omp parallel for num_threads(thread_count) default(shared) private(...) schedule(runtime)
for (...) {
   /* do a thing */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!