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.
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.
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, . . .
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.
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.
You can use a macro and use the -cpp
flag.
#define pure
pure subroutine s
print *,"hello"
end
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With