1!! Copyright (C) 2003-2006 M. Marques, A. 
Castro, A. 
Rubio, G. Bertsch
 
    4!! it under the terms of the GNU General Public License as published by
 
    8!! This 
program is distributed 
in the hope that it will be useful,
 
    9!! but WITHOUT ANY 
WARRANTY; without 
even the implied warranty of
 
   10!! MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   11!! GNU General Public License 
for more details.
 
   13!! You should have received a copy of the GNU General Public License
 
   14!! along with 
this program; 
if not, write to the Free Software
 
   23! If the compiler accepts 
long Fortran 
lines, it is better to use that
 
   24! capacity, and build all the preprocessor definitions 
in one line. In
 
   25! 
this way, the debuggers will provide the right line numbers.
 
   26#
if defined(LONG_LINES)
 
   30#  define _newline_ \newline 
   31#  define _anl_ & \newline 
   35! If the compiler accepts line number 
markers, then 
"CARDINAL" and 
"ACARDINAL" 
   36! will put them.  
Otherwise, just a 
new line 
or a ampersand plus a 
new line.
 
   38! put immedialty before a line where a compilation error might occur and at the
 
   40! Note that the 
"cardinal" and 
"newline" words are substituted by the 
program 
   41! preprocess.pl by the ampersand and by a real 
new line just before compilation.
 
   42#
if defined(LONG_LINES)
 
   46#  if defined(F90_ACCEPTS_LINE_NUMBERS) 
   47#    define CARDINAL _newline_\cardinal __LINE__ __FILE__ _newline_ 
   48#    define ACARDINAL _anl_\cardinal __LINE__ __FILE__ _newline_ 
   50#    define CARDINAL _newline_ 
   51#    define ACARDINAL _anl_ 
   57! is defined). 
Otherwise it is merely a logical assertion that, when 
fails,
 
   58! prints 
out the assertion 
string, the 
file, and the line. The subroutine
 
   61#  define ASSERT(expr)  \ 
   62  if(.not.(expr)) ACARDINAL \ 
   63     call assert_die(TOSTRING(expr), ACARDINAL __FILE__, ACARDINAL  __LINE__) \ 
   71! Some compilers will 
not have the 
sizeof intrinsic.
 
   73#  define FC_SIZEOF(x) sizeof(x) 
   75#  define FC_SIZEOF(x) 1 
   79! a helpful error 
if the allocation 
or deallocation 
fails. They also take care of
 
   80! calling the memory profiler. The 
"MY_DEALLOCATE" macro is only used 
in this file;
 
   81! 
in the 
code, one should use SAFE_DEALLOCATE_P 
for pointers and SAFE_DEALLOCATE_A
 
   83! A special 
version of the SAFE_ALLOCATE macro named SAFE_ALLOCATE_TYPE is also
 
   84! provided to allocate a polymorphic variable. This is necessary because of the
 
   85! special Fortran syntax 
"type::var".
 
   87#  define SAFE_ALLOCATE(x) allocate(x) 
   89#  define SAFE_ALLOCATE_TYPE(type, x) allocate(type::x) 
   91#  define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds) allocate(type::x bounds) 
   93#  define SAFE_ALLOCATE_SOURCE(x, y) \ 
   94  allocate(x, source=y); CARDINAL \ 
   97#  define SAFE_ALLOCATE_SOURCE_A(x, y) \ 
   98  if(allocated(y)) then;   CARDINAL \ 
   99    allocate(x, source=y); CARDINAL \ 
  103#  define SAFE_ALLOCATE_SOURCE_P(x, y) \ 
  104  if(associated(y)) then;  CARDINAL \ 
  105    allocate(x, source=y); CARDINAL \ 
  107    nullify(x);            CARDINAL \ 
  111#  define SAFE_DEALLOCATE_P(x) \ 
  112  if(associated(x)) then; CARDINAL \ 
  113    deallocate(x);        CARDINAL \ 
  114    nullify(x);           CARDINAL \ 
  117#  define SAFE_DEALLOCATE_A(x) \ 
  118  if(allocated(x)) then; CARDINAL \ 
  119    deallocate(x);       CARDINAL \ 
  124#  define SAFE_ALLOCATE_PROFILE(x)              \ 
  125  if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0 .or. global_alloc_err.ne.0) ACARDINAL \ 
  126  global_sizeof = FC_SIZEOF( ACARDINAL x ACARDINAL ); CARDINAL \ 
  127  if(iand(prof_vars%mode, PROFILING_MEMORY).ne.0) ACARDINAL \ 
  128    call profiling_memory_allocate(ACARDINAL TOSTRING(x), ACARDINAL __FILE__, ACARDINAL __LINE__, ACARDINAL global_sizeof); CARDINAL \ 
  129  if(global_alloc_err.ne.0) ACARDINAL \ 
  130    call alloc_error(global_sizeof, ACARDINAL __FILE__, ACARDINAL __LINE__); \ 
  133#  define SAFE_ALLOCATE(x)      \ 
  134  allocate( ACARDINAL x, ACARDINAL stat=global_alloc_err); CARDINAL \ 
  135  SAFE_ALLOCATE_PROFILE(x) 
  137#  define SAFE_ALLOCATE_TYPE(type, x)     \ 
  138  allocate( ACARDINAL type::x, ACARDINAL stat=global_alloc_err); CARDINAL \ 
  139  SAFE_ALLOCATE_PROFILE(x) 
  141! Some versions of GCC have a bug 
in the 
sizeof() function such that the compiler crashes with a ICE
 
  142! when passing a polymorphic variable to the function and 
explicit array bounds are given.
 
  143! The workaround is 
not to pass the bounds to 
sizeof. 
Otherwise we could just use SAFE_ALLOCATE_TYPE.
 
  144#  define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds)     \
 
  147  global_sizeof = FC_SIZEOF( ACARDINAL x ACARDINAL ); CARDINAL \
 
  154#  define SAFE_ALLOCATE_SOURCE_P(x, y) \ 
  155  if(associated(y)) then;     CARDINAL \ 
  156    allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \ 
  157    SAFE_ALLOCATE_PROFILE(x); CARDINAL \ 
  159    nullify(x);               CARDINAL \ 
  163#  define SAFE_ALLOCATE_SOURCE_A(x, y) \ 
  164  if(allocated(y)) then; CARDINAL \ 
  165    allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \ 
  166    SAFE_ALLOCATE_PROFILE(x); CARDINAL \ 
  170#  define SAFE_ALLOCATE_SOURCE(x, y) \ 
  171  allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \ 
  172  SAFE_ALLOCATE_PROFILE(x); CARDINAL \ 
  175#  define MY_DEALLOCATE(x) \ 
  176  global_sizeof = FC_SIZEOF(x) ; \ 
  178  deallocate(x, stat=global_alloc_err, errmsg=global_alloc_errmsg); CARDINAL \ 
  179  if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0) ACARDINAL \ 
  180    call profiling_memory_deallocate(TOSTRING(x), ACARDINAL __FILE__, ACARDINAL __LINE__, ACARDINAL global_sizeof); CARDINAL \ 
  181  if(global_alloc_err.ne.0) then; CARDINAL \ 
  182    write(stderr,'(a)') global_alloc_errmsg; CARDINAL \ 
  183    call dealloc_error(global_sizeof, ACARDINAL __FILE__, ACARDINAL __LINE__); CARDINAL \ 
  187#  define SAFE_DEALLOCATE_P(x) \ 
  188  if(associated(x)) then; CARDINAL \ 
  189    MY_DEALLOCATE(x);     CARDINAL \ 
  190    nullify(x);           CARDINAL \ 
  194#  define SAFE_DEALLOCATE_A(x) \ 
  195  if(allocated(x)) then;  CARDINAL \ 
  196    MY_DEALLOCATE(x);     CARDINAL \ 
  204#define SAFE_TOL(x, tol) sign(max(abs(x),tol),x) 
  207! the TOSTRING macro converts a macro into a 
string 
  208! 
do not use the STRINGIFY macro
 
  209#define STRINGIFY(x) #x 
  210#define TOSTRING(x)  STRINGIFY(x) 
  213! Whenever a procedure is 
not called too many times, one should 
start it
 
  214! and finish it with the PUSH_SUB and POP_SUB 
macros, which are these
 
  215! pieces of 
code that call the push_sub and pop_sub routines defined
 
  216! 
in the messages_m module.
 
  218#define PUSH_SUB(routine) \ 
  219  if(debug%trace) then; if(not_in_openmp()) then; CARDINAL \ 
  220    call debug_push_sub(__FILE__+"." ACARDINAL +TOSTRING(routine)); CARDINAL \
 
  223#define POP_SUB(routine) \ 
  224  if(debug%trace) then; if(not_in_openmp()) then; CARDINAL \ 
  225    call debug_pop_sub(__FILE__+"." ACARDINAL +TOSTRING(routine)); CARDINAL \
 
  229#define PUSH_SUB(routine) 
  230#define POP_SUB(routine) 
  233! push_pop_mismatch: begin ignore
 
  234#define PUSH_SUB_WITH_PROFILE(routine) \ 
  235  PUSH_SUB(routine) CARDINAL \ 
  236  call profiling_in(TOSTRING(routine)); CARDINAL 
  237#define POP_SUB_WITH_PROFILE(routine) \ 
  238  call profiling_out(TOSTRING(routine)); CARDINAL \ 
  239  POP_SUB(routine) CARDINAL 
  240! push_pop_mismatch: end ignore
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the file
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran lines
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug mode(NDEBUG ! is defined). Otherwise it is merely a logical assertion that
 
if write to the Free Software Franklin Street
 
A A G Bertsch !This program is free software
 
!in the one should use SAFE_DEALLOCATE_P for pointers and SAFE_DEALLOCATE_A !for arrays !A special version of the SAFE_ALLOCATE macro named SAFE_ALLOCATE_TYPE is also !provided to allocate a polymorphic variable This is necessary because of the !special Fortran syntax type::var !Some versions of GCC have a bug in the one should start it !and finish it with the PUSH_SUB and POP_SUB macros
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them Otherwise
 
if write to the Free Software Franklin Fifth Boston
 
you can redistribute it and or modify !it under the terms of the GNU General Public License as published by !the Free Software Foundation
 
without even the implied warranty of !MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE See the !GNU General Public License for more details !You should have received a copy of the GNU General Public License !along with this program
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion string
 
if write to the Free Software Inc
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this way
 
if write to the Free Software Franklin Fifth MA
 
either or(at your option) !! any later version. !! !! This program is distributed in the hope that it will be useful
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number markers
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the and the line The subroutine !assert_die is in the global_m module !Some compilers will not have the sizeof intrinsic !In octopus
 
if write to the Free Software Franklin Fifth Floor
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the and the line The subroutine !assert_die is in the global_m module !Some compilers will not have the sizeof intrinsic !In one should normally use the SAFE_(DE) ALLOCATE macros below
 
either !but WITHOUT ANY WARRANTY
 
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran it is better to use that and build all the preprocessor definitions in one line In !this the debuggers will provide the right line numbers !If the compiler accepts line number then CARDINAL and ACARDINAL !will put them just a new line or a ampersand plus a new line !These macros should be used in macros that span several lines They should by !put immedialty before a line where a compilation error might occur and at the !end of the macro !Note that the cardinal and newline words are substituted by the program !preprocess pl by the ampersand and by a real new line just before compilation !The assertions are ignored if the code is compiled in not debug when fails
 
subroutine assert_die(s, f, l)
This subroutine is called by the assert macro, it is not in a module so it can be called from any fil...
 
type(debug_t), save, public debug
 
integer(int64), public global_sizeof
 
logical pure function, public not_in_openmp()
 
integer, public global_alloc_err
 
logical pure function, public even(n)
Returns if n is even.
 
subroutine, public alloc_error(size, file, line)
 
type(profile_vars_t), target, save, public prof_vars
 
subroutine, public profiling_memory_allocate(var, file, line, size_)
 
subroutine start()
start the timer (save starting time)