Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gfortran: Treat pure functions as normal functions for debugging purposes?

I need to debug some pure functions in a fortran Program compiled with gfortran. Is there any way to ignore the pure statements so I can use write, print, etc. in these pure functions without great effort? Unfortunately it is not easly possible to just remove the pure statement.

like image 671
Wauzl Avatar asked Sep 26 '13 20:09

Wauzl


People also ask

What is a Fortran pure function?

Fortran procedures can be specified as PURE, meaning that there is no chance that the procedure would have any side effect on data outside the procedure. Only pure procedures can be used in specification expressions. The PURE keyword must be used in the procedure declaration.

How do you call a function in Fortran?

The general way to activate a function is to use the function name in an expression. The function name is followed by a list of inputs, also called arguments, enclosed in parenthesis: answer = functionname (argumentl, argument2, . . .

What are procedures in Fortran?

A procedure is a group of statements that perform a well-defined task and can be invoked from your program. Information (or data) is passed to the calling program, to the procedure as arguments.

Why it is necessary to declare the return type of a user defined function in Fortran?

This is because the function itself returns the value, and Fortran needs to know what sort of value it is. Secondly, within the functions, it is the name of the function which is used to return the final value to the main program.


2 Answers

You can use a macro and use the -cpp flag.

#define pure 

pure subroutine s
 print *,"hello"
end
like image 187
Vladimir F Героям слава Avatar answered Sep 19 '22 01:09

Vladimir F Героям слава


I usually use the pre-processor for this task:

#ifdef DEBUG
subroutine test(...)
#else
pure subroutine(...)
#endif
  ! ...
#ifdef DEBUG
  write(*,*) 'Debug output'
#endif
  ! ...
end subroutine

Then you can compile your code with gfortran -DDEBUG for verbose output. (In fact I personally do not set this flag globally, but via #define DEBUG at the beginning of the file I want debug).

I also have a MACRO defined to ease the use of debugging write statements:

#ifdef DEBUG
#define dwrite write
#else
#define dwrite ! write
#endif

With this, the code above reduces to:

#ifdef DEBUG
subroutine test(...)
#else
pure subroutine(...)
#endif
  ! ...
  dwrite (*,*) 'Debug output'
  ! ...
end subroutine

You can enable the pre-processor with -cpp for gfortran, and -fpp for ifort. Of course, when using .F90 or .F, the pre-processor is enabled by default.

like image 21
Alexander Vogt Avatar answered Sep 22 '22 01:09

Alexander Vogt