How can I make my code do something depending on my compiler flag?
For example, if I use the -openmp flag I want a loop to have a
different range.
i.e.
if (flag -openmp)
do i=1,10
else
do i=1,5
endif
do loop
enddo
Thanks
You can find examples on the use of
#ifdef _OPENMP
by a simple web search.
> How can I make my code do something depending on my compiler flag?
> For example, if I use the -openmp flag I want a loop to have a
> different range.
> if (flag -openmp)
> do i=1,10
> else
> do i=1,5
> endif
You can't do that in Fortran, or pretty much any
compiled language. (I have seen it done in BASIC).
But you say compiler flag. If you use the C preprocessor,
you can do
#ifdef OPENMP
do i=1,10
#else
do i=1,5
#endif
where OPENMP is a preprocessor symbol, defined either
with #define, or a compiler option such is -DOPENMP
At compile time, one or the other will be compiled.
-- glen
Yikes. You can do that with C, but would you?
--
Wealth - any income that is at least one hundred dollars more a year than
the income of one's wife's sister's husband. 6
H. L. Mencken
If machine dependent, use
#ifdef _OPENMP
call sub_openmp ( parms )
#else
call sub_other (parms )
#endif
You can use COCO if you want a Fortran-only solution
Regards,
Arjen
Out of curiosity: why is the loop different if you use OpenMP
than if you don't - especially as this is a compile-time
decision, not part of the input for the problem.
Regards,
Arjen
I just gave the loop problem as an example. I'm also incorporating
some other features that I want to enable by compiler flags.
Ah, well preprocessor-defined macros ought to do what you want,
but you have to define them together with the OpenMP compiler option,
as there is no way to get that automatically. At least not standard.
(I got the impression that you would be solving different things
or using different algorithms.)
Though, perhaps the OpenMP routine omp_get_max_threads() could
give the information at run-time (it may be undefined of course
if the program was not compiled with OpenMP, but then you
can supply a dummy routine that will give you just that information!).
Regards,
Arjen
> Ah, well preprocessor-defined macros ought to do what you want,
> but you have to define them together with the OpenMP compiler option,
> as there is no way to get that automatically. At least not standard.
OpenMP standard requires the _OPENMP macro, so it seems cpp style
conditional compilation is at least a de facto requirement for OpenMP.
> Though, perhaps the OpenMP routine omp_get_max_threads() could
> give the information at run-time (it may be undefined of course
> if the program was not compiled with OpenMP, but then you
> can supply a dummy routine that will give you just that information!).
Conditional inclusion of omp functions is one of the most common uses of
_OPENMP. For example, I want to see in my report whether OpenMP is
active, with how many threads.
I was thinking of having access via whatever preprocessor macro to
the active compiler options. As OpenMP does require a specific
macro to be present, so much the better :).
(If OP's compiler can't handle preprocessor macros, a solution
would be to include a library with dummy routines)
Regards,
Arjen