opm-common
Valgrind.hpp
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef OPM_VALGRIND_HPP
28 #define OPM_VALGRIND_HPP
29 
30 #if HAVE_VALGRIND
31 #include <valgrind/memcheck.h>
32 #endif
33 
34 #include <opm/common/utility/gpuDecorators.hpp>
35 
36 namespace Opm {
37 namespace Valgrind {
42 OPM_HOST_DEVICE inline bool IsRunning()
43 {
44 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
45  return RUNNING_ON_VALGRIND;
46 #else
47  return false;
48 #endif
49 }
50 
75 template <class T>
76 OPM_HOST_DEVICE inline bool CheckDefined([[maybe_unused]] const T& value)
77 {
78 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
79  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(&value, sizeof(T));
80  return tmp == 0;
81 #else
82  return true;
83 #endif
84 }
85 
86 
87 
107 template <class T>
108 OPM_HOST_DEVICE inline bool CheckAddressable([[maybe_unused]] const T& value)
109 {
110 // if we run in debug mode AND we have valgrind AND we are NOT in a gpu function
111 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
112  auto tmp = VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&value, sizeof(T));
113  return tmp == 0;
114 #else
115  return true;
116 #endif
117 }
118 
144 template <class T>
145 OPM_HOST_DEVICE inline bool CheckDefined([[maybe_unused]] const T* value,
146  [[maybe_unused]] int size)
147 {
148 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
149  auto tmp = VALGRIND_CHECK_MEM_IS_DEFINED(value, size*sizeof(T));
150  return tmp == 0;
151 #else
152  return true;
153 #endif
154 }
155 
173 template <class T>
174 OPM_HOST_DEVICE inline void SetUndefined([[maybe_unused]] const T& value)
175 {
176 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
177  VALGRIND_MAKE_MEM_UNDEFINED(&value, sizeof(T));
178 #endif
179 }
180 
199 template <class T>
200 OPM_HOST_DEVICE inline void SetUndefined([[maybe_unused]] const T* value,
201  [[maybe_unused]] int size)
202 {
203 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
204  VALGRIND_MAKE_MEM_UNDEFINED(value, size*sizeof(T));
205 #endif
206 }
207 
224 template <class T>
225 OPM_HOST_DEVICE inline void SetDefined([[maybe_unused]] const T& value)
226 {
227 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
228  VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(T));
229 #endif
230 }
231 
250 template <class T>
251 OPM_HOST_DEVICE inline void SetDefined([[maybe_unused]] const T* value,
252  [[maybe_unused]] int n)
253 {
254 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
255  VALGRIND_MAKE_MEM_DEFINED(value, n*sizeof(T));
256 #endif
257 }
258 
275 template <class T>
276 OPM_HOST_DEVICE inline void SetNoAccess([[maybe_unused]] const T& value)
277 {
278 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
279  VALGRIND_MAKE_MEM_NOACCESS(&value, sizeof(T));
280 #endif
281 }
282 
299 template <class T>
300 OPM_HOST_DEVICE inline void SetNoAccess([[maybe_unused]] const T* value,
301  [[maybe_unused]] int size)
302 {
303 #if !defined NDEBUG && HAVE_VALGRIND && !OPM_IS_INSIDE_DEVICE_FUNCTION
304  VALGRIND_MAKE_MEM_NOACCESS(value, size*sizeof(T));
305 #endif
306 }
307 
308 }} // namespace Valgrind, Opm
309 
310 #endif
OPM_HOST_DEVICE void SetNoAccess([[maybe_unused]] const T &value)
Make valgrind complain if an object&#39;s memory is accessed.
Definition: Valgrind.hpp:276
OPM_HOST_DEVICE void SetUndefined([[maybe_unused]] const T &value)
Make the memory on which an object resides undefined in valgrind runs.
Definition: Valgrind.hpp:174
OPM_HOST_DEVICE bool CheckDefined([[maybe_unused]] const T &value)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition: Valgrind.hpp:76
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
OPM_HOST_DEVICE bool IsRunning()
Returns whether the program is running under Valgrind or not.
Definition: Valgrind.hpp:42
OPM_HOST_DEVICE void SetDefined([[maybe_unused]] const T &value)
Make the memory on which an object resides defined.
Definition: Valgrind.hpp:225
OPM_HOST_DEVICE bool CheckAddressable([[maybe_unused]] const T &value)
Make valgrind complain if any of the memory occupied by an object is not addressable.
Definition: Valgrind.hpp:108