Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compiler flag question

0 views
Skip to first unread message

tomg...@hotmail.com

unread,
Aug 20, 2008, 8:37:54 AM8/20/08
to
Hi

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

Tim Prince

unread,
Aug 20, 2008, 10:32:10 AM8/20/08
to

You can find examples on the use of
#ifdef _OPENMP
by a simple web search.

tomg...@hotmail.com

unread,
Aug 20, 2008, 10:52:44 AM8/20/08
to
cheers for the help.

glen herrmannsfeldt

unread,
Aug 20, 2008, 4:42:03 PM8/20/08
to
tomg...@hotmail.com wrote:

> 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

Ron Ford

unread,
Aug 20, 2008, 9:23:05 PM8/20/08
to
On Wed, 20 Aug 2008 12:42:03 -0800, glen herrmannsfeldt posted:

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

tju329

unread,
Aug 21, 2008, 12:15:23 AM8/21/08
to

If machine dependent, use
#ifdef _OPENMP
call sub_openmp ( parms )
#else
call sub_other (parms )
#endif


Arjen Markus

unread,
Aug 21, 2008, 3:07:20 AM8/21/08
to
> H. L. Mencken- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

You can use COCO if you want a Fortran-only solution

Regards,

Arjen

Arjen Markus

unread,
Aug 21, 2008, 3:08:55 AM8/21/08
to

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

Reinhold Bader

unread,
Aug 21, 2008, 5:03:55 AM8/21/08
to
How about

imax = 5
!$ imax = 10
do i=1, imax
:
end do

Regards

tomg...@hotmail.com schrieb:

tomg...@hotmail.com

unread,
Aug 21, 2008, 5:59:47 AM8/21/08
to
I basically want to have different output to certain files depending
on how the user compiles the code.

I just gave the loop problem as an example. I'm also incorporating
some other features that I want to enable by compiler flags.

Arjen Markus

unread,
Aug 21, 2008, 6:11:09 AM8/21/08
to

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

Tim Prince

unread,
Aug 21, 2008, 8:50:19 AM8/21/08
to
Arjen Markus wrote:

> 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.

Arjen Markus

unread,
Aug 21, 2008, 8:58:33 AM8/21/08
to
On 21 aug, 14:50, Tim Prince <tpri...@nospamcomputer.org> wrote:
> Arjen Markus wrote:
> > 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.
>
>

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

0 new messages