Skip to content

Instantly share code, notes, and snippets.

@jdoerfert
Created February 25, 2020 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdoerfert/d0014c88d8a522a61b40771c5a5003d0 to your computer and use it in GitHub Desktop.
Save jdoerfert/d0014c88d8a522a61b40771c5a5003d0 to your computer and use it in GitHub Desktop.
enum class BindKind {
teams,
parallel,
thread,
};
typedef void (*body_t)(int);
void loop_init_teams(int lb, int ub, body_t body, bool HasInnerLoop) {
if (HasInnerLoop) {
#pragma omp distribute
for (int i = lb; i < ub; ++i)
body(i);
} else {
#pragma omp distribute parallel for simd
for (int i = lb; i < ub; ++i)
body(i);
}
}
void loop_init_parallel(int lb, int ub, body_t body, bool HasInnerLoop) {
#pragma omp for simd
for (int i = lb; i < ub; ++i)
body(i);
}
void loop_init_thread(int lb, int ub, body_t body, bool HasInnerLoop) {
if (HasInnerLoop) {
#pragma omp parallel for simd num_threads(some_heuristic())
for (int i = lb; i < ub; ++i)
body(i);
} else {
#pragma omp parallel for simd
for (int i = lb; i < ub; ++i)
body(i);
}
}
void loop_init(BindKind kind, int lb, int ub, body_t body, bool HasInnerLoop) {
switch (kind) {
case BindKind::teams:
return loop_init_teams(lb, ub, body, HasInnerLoop);
case BindKind::parallel:
return loop_init_parallel(lb, ub, body, HasInnerLoop);
default:
return loop_init_thread(lb, ub, body, HasInnerLoop);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment