Reputation: 371
I am writing a library that will use openmp
however I want to give the user the ability to disable/enable my libraries openmp
with a preprocessor definition (in case they want to use openmp
but avoid not have my lib use it)
The most obvious solution would be something like
void foo() {
#ifndef disable
#pragma omp parallel for
#endif
for (int i = 0; i < 100; ++i) {
std::cout << i << " " << std::endl;
}
#ifndef disable
#pragma omp barrier
#endif
However I have a multitude of methods that utilize openmp. Is there any way I can create an alias for openmp?
IE:
#if OpenMpSupported
#define AliasParallel = #pragma omp parallel for
#define AliasBarrier = #pramgma omp barrier
#else
#define AliasParallel = //some thing that doesn't matter
#define AliasBarrier = //some thing that that doesn't matter
#endif
void foo() {
#AliasParallel
for (int i = 0; i < 100; ++i) {
std::cout << i << " " << std::endl;
}
#AliasBarrier
Simply put is there anyway I could create a shorthand for this.
Upvotes: 0
Views: 2074
Reputation: 85887
In general, there is no way to do this. The result of macro expansion is not rescanned for #
directives.
However, in the particular case of #pragma
there is an alternative (since C++11):
_Pragma("foo bar")
is equivalent to
#pragma foo bar
and the former can be produced by macros.
In your case that would be:
#if OpenMpSupported
#define AliasParallel _Pragma("omp parallel for")
#define AliasBarrier _Pragma("omp barrier")
#else
#define AliasParallel
#define AliasBarrier
#endif
void foo() {
AliasParallel
for (int i = 0; i < 100; ++i) {
std::cout << i << " " << std::endl;
}
AliasBarrier
(I don't know if this works with openmp, however.)
Upvotes: 3