Octopus
global.h
Go to the documentation of this file.
1!! Copyright (C) 2003-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
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.
12!!
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
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "config_F90.h"
20#include "options.h"
21#include "defaults.h"
22
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)
27# define _newline_
28# define _anl_
29#else
30# define _newline_ \newline
31# define _anl_ & \newline
32#endif
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.
37! These macros should be used in macros that span several lines. They should by
38! put immedialty before a line where a compilation error might occur and at the
39! end of the macro.
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)
43# define CARDINAL
44# define ACARDINAL
45#else
46# if defined(F90_ACCEPTS_LINE_NUMBERS)
47# define CARDINAL _newline_\cardinal __LINE__ __FILE__ _newline_
48# define ACARDINAL _anl_\cardinal __LINE__ __FILE__ _newline_
49# else
50# define CARDINAL _newline_
51# define ACARDINAL _anl_
52# endif
53#endif
54
55
56! The assertions are ignored if the code is compiled in not-debug mode (NDEBUG
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
59! assert_die is in the global_m module.
60#if !defined(NDEBUG)
61# define ASSERT(expr) \
62 if(.not.(expr)) ACARDINAL \
63 call assert_die(TOSTRING(expr), ACARDINAL __FILE__, ACARDINAL __LINE__) \
64 CARDINAL
65#else
66# define ASSERT(expr)
67#endif
68
69
70
71! Some compilers will not have the sizeof intrinsic.
72#ifdef HAVE_FC_SIZEOF
73# define FC_SIZEOF(x) sizeof(x)
74#else
75# define FC_SIZEOF(x) 1
76#endif
77
78! In octopus, one should normally use the SAFE_(DE)ALLOCATE macros below, which emit
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
82! for arrays.
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".
86#if defined(NDEBUG)
87# define SAFE_ALLOCATE(x) allocate(x)
88
89# define SAFE_ALLOCATE_TYPE(type, x) allocate(type::x)
90
91# define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds) allocate(type::x bounds)
92
93# define SAFE_ALLOCATE_SOURCE(x, y) \
94 allocate(x, source=y); CARDINAL \
95 CARDINAL
96
97# define SAFE_ALLOCATE_SOURCE_A(x, y) \
98 if(allocated(y)) then; CARDINAL \
99 allocate(x, source=y); CARDINAL \
100 end if; \
101 CARDINAL
102
103# define SAFE_ALLOCATE_SOURCE_P(x, y) \
104 if(associated(y)) then; CARDINAL \
105 allocate(x, source=y); CARDINAL \
106 else; CARDINAL \
107 nullify(x); CARDINAL \
108 end if; \
109 CARDINAL
110
111# define SAFE_DEALLOCATE_P(x) \
112 if(associated(x)) then; CARDINAL \
113 deallocate(x); CARDINAL \
114 nullify(x); CARDINAL \
115 end if; \
116 CARDINAL
117# define SAFE_DEALLOCATE_A(x) \
118 if(allocated(x)) then; CARDINAL \
119 deallocate(x); CARDINAL \
120 end if; \
121 CARDINAL
122
123#else
124# define SAFE_ALLOCATE_PROFILE(x) \
125 if(not_in_openmp()) then; if(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) then; CARDINAL \
130 write(stderr,'(a)') global_alloc_errmsg; CARDINAL \
131 call alloc_error(global_sizeof, ACARDINAL __FILE__, ACARDINAL __LINE__); \
132 end if; \
133 end if;
134 CARDINAL
135
136# define SAFE_ALLOCATE(x) \
137 allocate( ACARDINAL x, ACARDINAL stat=global_alloc_err, ACARDINAL errmsg=global_alloc_errmsg); CARDINAL \
138 SAFE_ALLOCATE_PROFILE(x)
139
140# define SAFE_ALLOCATE_TYPE(type, x) \
141 allocate( ACARDINAL type::x, ACARDINAL stat=global_alloc_err, ACARDINAL errmsg=global_alloc_errmsg); CARDINAL \
142 SAFE_ALLOCATE_PROFILE(x)
143
144! Some versions of GCC have a bug in the sizeof() function such that the compiler crashes with a ICE
145! when passing a polymorphic variable to the function and explicit array bounds are given.
146! The workaround is not to pass the bounds to sizeof. Otherwise we could just use SAFE_ALLOCATE_TYPE.
147# define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds) \
148 allocate( ACARDINAL type::x bounds, ACARDINAL stat=global_alloc_err); CARDINAL \
149 if(not_in_openmp()) then; if(iand(prof_vars%mode, PROFILING_MEMORY).ne.0 .or. global_alloc_err.ne.0) ACARDINAL \
150 global_sizeof = FC_SIZEOF( ACARDINAL x ACARDINAL ); CARDINAL \
151 if(iand(prof_vars%mode, PROFILING_MEMORY).ne.0) ACARDINAL \
152 call profiling_memory_allocate(ACARDINAL TOSTRING(x)+TOSTRING(bounds), ACARDINAL __FILE__, ACARDINAL __LINE__, ACARDINAL global_sizeof); CARDINAL \
153 if(global_alloc_err.ne.0) then; ACARDINAL \
154 write(stderr,'(a)') global_alloc_errmsg; CARDINAL \
155 call alloc_error(global_sizeof, ACARDINAL __FILE__, ACARDINAL __LINE__); \
156 end if; \
157 end if; \
158 CARDINAL
159
160# define SAFE_ALLOCATE_SOURCE_P(x, y) \
161 if(associated(y)) then; CARDINAL \
162 allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \
163 SAFE_ALLOCATE_PROFILE(x); CARDINAL \
164 else; CARDINAL \
165 nullify(x); CARDINAL \
166 end if; \
167 CARDINAL
168
169# define SAFE_ALLOCATE_SOURCE_A(x, y) \
170 if(allocated(y)) then; CARDINAL \
171 allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \
172 SAFE_ALLOCATE_PROFILE(x); CARDINAL \
173 end if; \
174 CARDINAL
175
176# define SAFE_ALLOCATE_SOURCE(x, y) \
177 allocate( ACARDINAL x, ACARDINAL source=y, ACARDINAL stat=global_alloc_err); CARDINAL \
178 SAFE_ALLOCATE_PROFILE(x); CARDINAL \
179 CARDINAL
180
181# define MY_DEALLOCATE(x) \
182 global_sizeof = FC_SIZEOF(x) ; \
183 CARDINAL \
184 deallocate(x, stat=global_alloc_err, errmsg=global_alloc_errmsg); CARDINAL \
185 if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0) ACARDINAL \
186 call profiling_memory_deallocate(TOSTRING(x), ACARDINAL __FILE__, ACARDINAL __LINE__, ACARDINAL global_sizeof); CARDINAL \
187 if(global_alloc_err.ne.0) then; CARDINAL \
188 write(stderr,'(a)') global_alloc_errmsg; CARDINAL \
189 call dealloc_error(global_sizeof, ACARDINAL __FILE__, ACARDINAL __LINE__); CARDINAL \
190 end if; \
191 CARDINAL
192
193# define SAFE_DEALLOCATE_P(x) \
194 if(associated(x)) then; CARDINAL \
195 MY_DEALLOCATE(x); CARDINAL \
196 nullify(x); CARDINAL \
197 end if; \
198 CARDINAL
199
200# define SAFE_DEALLOCATE_A(x) \
201 if(allocated(x)) then; CARDINAL \
202 MY_DEALLOCATE(x); CARDINAL \
203 end if; \
204 CARDINAL
205
206#endif
207
208
209
210#define SAFE_TOL(x, tol) sign(max(abs(x),tol),x)
211
212
213! the TOSTRING macro converts a macro into a string
214! do not use the STRINGIFY macro
215#define STRINGIFY(x) #x
216#define TOSTRING(x) STRINGIFY(x)
217
218
219! Whenever a procedure is not called too many times, one should start it
220! and finish it with the PUSH_SUB and POP_SUB macros, which are these
221! pieces of code that call the push_sub and pop_sub routines defined
222! in the messages_m module.
223#ifndef NDEBUG
224#define PUSH_SUB(routine) \
225 if(debug%trace .or. debug%instrument) then; if(not_in_openmp()) then; CARDINAL \
226 call debug_push_sub(__FILE__+"." ACARDINAL +TOSTRING(routine)); CARDINAL \
227 endif; endif; \
228 CARDINAL
229#define POP_SUB(routine) \
230 if(debug%trace .or. debug%instrument) then; if(not_in_openmp()) then; CARDINAL \
231 call debug_pop_sub(__FILE__+"." ACARDINAL +TOSTRING(routine)); CARDINAL \
232 endif; endif; \
233 CARDINAL
234#else
235#define PUSH_SUB(routine)
236#define POP_SUB(routine)
237#endif
238
239! push_pop_mismatch: begin ignore
240#define PUSH_SUB_WITH_PROFILE(routine) \
241 PUSH_SUB(routine) CARDINAL \
242 call profiling_in(TOSTRING(routine)); CARDINAL
243#define POP_SUB_WITH_PROFILE(routine) \
244 call profiling_out(TOSTRING(routine)); CARDINAL \
245 POP_SUB(routine) CARDINAL
246! push_pop_mismatch: end ignore
247
248!! Local Variables:
249!! mode: f90
250!! coding: utf-8
251!! End:
ssize_t ssize_t write(int __fd, const void *__buf, size_t __n) __attribute__((__access__(__read_only__
FILE * stderr
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
Definition: global.h:46
if write to the Free Software Franklin Fifth USA !If the compiler accepts long Fortran lines
Definition: global.h:32
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
Definition: global.h:21
A A G Bertsch !This program is free software
Definition: global.h:9
A A Rubio
Definition: global.h:7
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
Definition: global.h:37
if write to the Free Software Franklin Fifth Boston
Definition: global.h:21
you can redistribute it and or modify !it under the terms of the GNU General Public License as published by !the Free Software Foundation
Definition: global.h:11
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
Definition: global.h:20
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
Definition: global.h:46
if write to the Free Software Inc
Definition: global.h:21
A Castro
Definition: global.h:7
!in the code
Definition: global.h:59
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
Definition: global.h:34
if write to the Free Software Franklin Fifth MA
Definition: global.h:22
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
Definition: global.h:36
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
Definition: global.h:56
!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 newline !Some versions of GCC have a bug in the one should start it !and finish it with the PUSH_SUB and POP_SUB macros
Definition: global.h:78
if write to the Free Software Franklin Fifth Floor
Definition: global.h:21
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
Definition: global.h:15
if not
Definition: global.h:20
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
Definition: global.h:45
either version
Definition: global.h:11
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...
Definition: messages.F90:1320
type(debug_t), save, public debug
Definition: debug.F90:158
integer(int64), public global_sizeof
Definition: global.F90:250
logical pure function, public not_in_openmp()
Definition: global.F90:459
character(len=100), public global_alloc_errmsg
Definition: global.F90:251
integer, public global_alloc_err
Definition: global.F90:249
logical pure function, public even(n)
Returns if n is even.
Definition: math.F90:695
subroutine, public alloc_error(size, file, line)
Definition: messages.F90:675
integer, parameter in
Definition: pes_mask.F90:260
integer, parameter out
Definition: pes_mask.F90:260
type(profile_vars_t), target, save, public prof_vars
Definition: profiling.F90:248
subroutine, public profiling_memory_allocate(var, file, line, size_)
Definition: profiling.F90:1321
subroutine start()
start the timer (save starting time)
Definition: walltimer.F90:266