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!!
19#include "config_F90.h"
20#include "options.h"
21#include "defaults.h"
22
23! The assertions are ignored if the code is compiled in not-debug mode (NDEBUG
24! is defined). Otherwise it is merely a logical assertion that, when fails,
25! prints out the assertion string, the file, and the line. The subroutine
26! assert_die is in the global_m module.
27#if !defined(NDEBUG)
28# define ASSERT(expr) \
29 if(.not.(expr)) \
30 call assert_die(TOSTRING(expr), __FILE__, __LINE__)
31#else
32# define ASSERT(expr)
33#endif
34
35! In octopus, one should normally use the SAFE_(DE)ALLOCATE macros below, which emit
36! a helpful error if the allocation or deallocation fails. They also take care of
37! calling the memory profiler. The "MY_DEALLOCATE" macro is only used in this file;
38! in the code, one should use SAFE_DEALLOCATE_P for pointers and SAFE_DEALLOCATE_A
39! for arrays.
40! A special version of the SAFE_ALLOCATE macro named SAFE_ALLOCATE_TYPE is also
41! provided to allocate a polymorphic variable. This is necessary because of the
42! special Fortran syntax "type::var".
43#if defined(NDEBUG)
44# define SAFE_ALLOCATE(x) allocate(x)
45
46# define SAFE_ALLOCATE_TYPE(type, x) allocate(type::x)
47
48# define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds) allocate(type::x bounds)
49
50# define SAFE_ALLOCATE_SOURCE(x, y) \
51 allocate(x, source=y);
52
53# define SAFE_ALLOCATE_SOURCE_A(x, y) \
54 if(allocated(y)) then; \
55 allocate(x, source=y); \
56 end if;
57
58# define SAFE_ALLOCATE_SOURCE_P(x, y) \
59 if(associated(y)) then; \
60 allocate(x, source=y); \
61 else; \
62 nullify(x); \
63 end if;
64
65# define SAFE_DEALLOCATE_P(x) \
66 if(associated(x)) then; \
67 deallocate(x); \
68 nullify(x); \
69 end if;
70# define SAFE_DEALLOCATE_A(x) \
71 if(allocated(x)) then; \
72 deallocate(x); \
73 end if;
74
75#else
76# define SAFE_ALLOCATE_PROFILE(x) \
77 if(global_alloc_err.ne.0) call alloc_error(global_alloc_errmsg, "allocate", __FILE__, __LINE__); \
78 if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0) \
79 call profiling_memory_allocate(TOSTRING(x), __FILE__, __LINE__, int(sizeof(x), int64));
80
81# define SAFE_ALLOCATE(x) \
82 allocate(x, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
83 SAFE_ALLOCATE_PROFILE(x)
84
85# define SAFE_ALLOCATE_TYPE(type, x) \
86 allocate(type::x, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
87 SAFE_ALLOCATE_PROFILE(x)
88
89! Some versions of GCC have a bug in the sizeof() function such that the compiler crashes with a ICE
90! when passing a polymorphic variable to the function and explicit array bounds are given.
91! The workaround is not to pass the bounds to sizeof. Otherwise we could just use SAFE_ALLOCATE_TYPE.
92# define SAFE_ALLOCATE_TYPE_ARRAY(type, x, bounds) \
93 allocate(type::x bounds, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
94 if(global_alloc_err.ne.0) call alloc_error(global_alloc_errmsg, "allocate", __FILE__, __LINE__); \
95 if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0) \
96 call profiling_memory_allocate(TOSTRING(x)+TOSTRING(bounds), __FILE__, __LINE__, int(sizeof(x), int64));
97
98# define SAFE_ALLOCATE_SOURCE_P(x, y) \
99 if(associated(y)) then; \
100 allocate(x, source=y, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
101 SAFE_ALLOCATE_PROFILE(x); \
102 else; \
103 nullify(x); \
104 end if;
105
106# define SAFE_ALLOCATE_SOURCE_A(x, y) \
107 if(allocated(y)) then; \
108 allocate(x, source=y, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
109 SAFE_ALLOCATE_PROFILE(x); \
110 end if;
111
112# define SAFE_ALLOCATE_SOURCE(x, y) \
113 allocate(x, source=y, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
114 SAFE_ALLOCATE_PROFILE(x);
115
116# define MY_DEALLOCATE(x) \
117 global_sizeof = sizeof(x) ; \
118 deallocate(x, stat=global_alloc_err, errmsg=global_alloc_errmsg); \
119 if(global_alloc_err.ne.0) call alloc_error(global_alloc_errmsg, "deallocate", __FILE__, __LINE__); \
120 if(not_in_openmp() .and. iand(prof_vars%mode, PROFILING_MEMORY).ne.0) \
121 call profiling_memory_deallocate(TOSTRING(x), __FILE__, __LINE__, global_sizeof);
122
123# define SAFE_DEALLOCATE_P(x) \
124 if(associated(x)) then; \
125 MY_DEALLOCATE(x); \
126 nullify(x); \
127 end if;
128
129# define SAFE_DEALLOCATE_A(x) \
130 if(allocated(x)) then; \
131 MY_DEALLOCATE(x); \
132 end if;
133
134#endif
135
136
137
138#define SAFE_TOL(x, tol) sign(max(abs(x),tol),x)
139
140
141! the TOSTRING macro converts a macro into a string
142! do not use the STRINGIFY macro
143#define STRINGIFY(x) #x
144#define TOSTRING(x) STRINGIFY(x)
145
146
147! Whenever a procedure is not called too many times, one should start it
148! and finish it with the PUSH_SUB and POP_SUB macros, which are these
149! pieces of code that call the push_sub and pop_sub routines defined
150! in the messages_m module.
151#define PUSH_SUB(routine) \
152 if(debug%trace .or. debug%instrument) then; if(not_in_openmp()) then; \
153 call debug_push_sub(__FILE__+"."+TOSTRING(routine)); \
154 endif; endif;
155#define POP_SUB(routine) \
156 if(debug%trace .or. debug%instrument) then; if(not_in_openmp()) then; \
157 call debug_pop_sub(__FILE__+"."+TOSTRING(routine)); \
158 endif; endif;
159
160! push_pop_mismatch: begin ignore
161#define PUSH_SUB_WITH_PROFILE(routine) \
162 PUSH_SUB(routine) \
163 call profiling_in(TOSTRING(routine));
164#define POP_SUB_WITH_PROFILE(routine) \
165 call profiling_out(TOSTRING(routine)); \
166 POP_SUB(routine)
167! push_pop_mismatch: end ignore
168
169!! Local Variables:
170!! mode: f90
171!! coding: utf-8
172!! End:
ssize_t ssize_t write(int __fd, const void *__buf, size_t __n) __attribute__((__access__(__read_only__
if write to the Free Software Franklin Fifth USA !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 !In octopus
Definition: global.h:35
if write to the Free Software Franklin Fifth USA !The assertions are ignored if the code is compiled in not debug when fails
Definition: global.h:31
if write to the Free Software Franklin Street
Definition: global.h:19
A A G Bertsch !This program is free software
Definition: global.h:7
!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
Definition: global.h:55
A A Rubio
Definition: global.h:5
if write to the Free Software Franklin Fifth Boston
Definition: global.h:19
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:9
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:18
if write to the Free Software Inc
Definition: global.h:19
A Castro
Definition: global.h:5
if write to the Free Software Franklin Fifth USA !The assertions are ignored if the code is compiled in not debug when !prints out the assertion string
Definition: global.h:32
!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 sizeof() function such that the compiler crashes with a ICE ! when passing a polymorphic variable to the function and explicit array bounds are given. ! The workaround is not to pass the bounds to sizeof. Otherwise we could just use SAFE_ALLOCATE_TYPE. ! the TOSTRING macro converts a macro into a string ! do not use the STRINGIFY macro ! Whenever a procedure is not called too many times
!in the code
Definition: global.h:38
if write to the Free Software Franklin Fifth USA !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 !In one should normally use the SAFE_(DE) ALLOCATE macros below
if write to the Free Software Franklin Fifth MA
Definition: global.h:20
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 !The assertions are ignored if the code is compiled in not debug when !prints out the assertion the file
Definition: global.h:32
if write to the Free Software Franklin Fifth USA !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 Fifth Floor
Definition: global.h:19
either !but WITHOUT ANY WARRANTY
Definition: global.h:13
if not
Definition: global.h:18
either version
Definition: global.h:9
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:1283
type(debug_t), save, public debug
Definition: debug.F90:133
subroutine, public debug_push_sub(sub_name)
Push a routine to the debug trace.
Definition: debug.F90:419
logical pure function, public not_in_openmp()
Definition: global.F90:573
character(len=100), public global_alloc_errmsg
Definition: global.F90:247
integer, public global_alloc_err
Definition: global.F90:245
logical pure function, public even(n)
Returns if n is even.
Definition: math.F90:759
subroutine, public alloc_error(msg, stmt, file, line)
Definition: messages.F90:645
integer, parameter in
Definition: pes_mask.F90:237
integer, parameter out
Definition: pes_mask.F90:237
type(profile_vars_t), target, save, public prof_vars
Definition: profiling.F90:255
subroutine, public profiling_memory_allocate(var, file, line, size_)
Definition: profiling.F90:1372
subroutine start()
start the timer (save starting time)
Definition: walltimer.F90:271