Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
errchk.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2014-2026, Johannes Pekkila, Miikka Vaisala, Touko Puro.
3
4 This file is part of Astaroth.
5
6 Astaroth 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 3 of the License, or
9 (at your option) any later version.
10
11 Astaroth 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 Astaroth. If not, see <http://www.gnu.org/licenses/>.
18*/
19
27#pragma once
28#include <stdbool.h>
29#include <stddef.h>
30#include <stdint.h> // SIZE_MAX
31#include <stdio.h>
32#include <stdlib.h>
33#include <time.h>
34
36#include "host_datatypes.h"
37#ifdef __cplusplus
39#endif
40
41/*
42 * =============================================================================
43 * General error checking
44 * =============================================================================
45 */
46#define _ERRCHK_REPORT_TIME() \
47 time_t terr = 0; \
48 time(&terr); \
49 fprintf(stderr, "%s", ctime(&terr));
50
51#define FATAL() \
52 { \
53 _ERRCHK_REPORT_TIME(); \
54 fprintf(stderr, "Fatal error in file %s line %d\n", __FILE__, __LINE__); \
55 fprintf(stderr, "This error is likely due to a serious programming " \
56 "error. Inspect any prior logs.\n"); \
57 fflush(stderr); \
58 fflush(stdout); \
59 abort(); \
60 }
61
62#define ERROR(str) \
63 { \
64 _ERRCHK_REPORT_TIME(); \
65 fprintf(stderr, "Error in file %s line %d: %s\n", __FILE__, __LINE__, \
66 str); \
67 fflush(stderr); \
68 abort(); \
69 }
70
71#define WARNING(str) \
72 { \
73 _ERRCHK_REPORT_TIME(); \
74 fprintf(stderr, "\tWarning in file %s line %d: %s\n", __FILE__, __LINE__, \
75 str); \
76 fflush(stderr); \
77 }
78
79// DO NOT REMOVE BRACKETS AROUND RETVAL. F.ex. if (!a < b) vs if (!(a < b)).
80#define ERRCHK(retval) \
81 { \
82 if (!(retval)) \
83 ERROR(#retval " was false"); \
84 }
85#define WARNCHK(retval) \
86 { \
87 if (!(retval)) \
88 WARNING(#retval " was false"); \
89 }
90#define WARNCHK_ALWAYS(retval) \
91 { \
92 if (!(retval)) \
93 WARNING(#retval " was false"); \
94 }
95#define ERRCHK_ALWAYS(retval) \
96 { \
97 if (!(retval)) \
98 ERROR(#retval " was false"); \
99 }
100
101#define as_size_t(i) AS_SIZE_T(i, __FILE__, __LINE__)
102
103/*
104 * =============================================================================
105 * CUDA-specific error checking
106 * =============================================================================
107 */
108// #if defined(__CUDA_RUNTIME_API_H__)
109#if AC_CPU_BUILD
110static inline void
111cuda_assert(cudaError_t code, const char* file, int line, bool should_abort)
112{
113 if (code != cudaSuccess) {
114 time_t terr;
115 time(&terr);
116 fprintf(stderr, "%s", ctime(&terr));
117 fprintf(stderr, "\tCUDA error in file %s line %d\n", file, line);
118 fflush(stderr);
119 if (should_abort)
120 abort();
121 }
122}
123#else
124static inline void
125cuda_assert(cudaError_t code, const char* file, int line, bool should_abort)
126{
127 if (code != cudaSuccess) {
128 time_t terr;
129 time(&terr);
130 fprintf(stderr, "%s", ctime(&terr));
131 fprintf(stderr, "\tCUDA error in file %s line %d: %s\n", file, line,
132 cudaGetErrorString(code));
133 fflush(stderr);
134
135 if (should_abort)
136 abort();
137 }
138}
139#endif
140
141#ifdef NDEBUG
142#undef ERRCHK
143#undef WARNCHK
144#define ERRCHK(params) (void)(params)
145#define WARNCHK(params) (void)(params)
146#define ERRCHK_CUDA(params) (void)(params)
147#define WARNCHK_CUDA(params) (void)(params)
148#define ERRCHK_CUDA_KERNEL() \
149 { \
150 }
151#else
152#define ERRCHK_CUDA(params) \
153 { \
154 cuda_assert((params), __FILE__, __LINE__, true); \
155 }
156#define WARNCHK_CUDA(params) \
157 { \
158 cuda_assert((params), __FILE__, __LINE__, false); \
159 }
160
161#define ERRCHK_CUDA_KERNEL() \
162 { \
163 ERRCHK_CUDA(acPeekAtLastError()); \
164 ERRCHK_CUDA(acDeviceSynchronize()); \
165 ERRCHK_CUDA(acPeekAtLastError()); \
166 }
167#endif
168
169#define ERRCHK_CUDA_ALWAYS(params) \
170 { \
171 cuda_assert((params), __FILE__, __LINE__, true); \
172 }
173
174#define ERRCHK_CUDA_KERNEL_ALWAYS() \
175 { \
176 ERRCHK_CUDA_ALWAYS(acPeekAtLastError()); \
177 ERRCHK_CUDA_ALWAYS(acDeviceSynchronize()); \
178 }
179
180#define WARNCHK_CUDA_ALWAYS(params) \
181 { \
182 cuda_assert((params), __FILE__, __LINE__, false); \
183 }
184// #endif // __CUDA_RUNTIME_API_H__
185
186#ifdef __cplusplus
187
188template <typename T>
189static inline int64_t
190as_int64_t(const T i)
191{
192 ERRCHK_ALWAYS(static_cast<long double>(i) >
193 static_cast<long double>(INT64_MIN));
194 ERRCHK_ALWAYS(static_cast<long double>(i) <
195 static_cast<long double>(INT64_MAX));
196 return static_cast<int64_t>(i);
197}
198
199template <typename T>
200static inline int
201as_int(const T i)
202{
203 ERRCHK_ALWAYS(static_cast<long double>(i) >
204 static_cast<long double>(INT_MIN));
205 ERRCHK_ALWAYS(static_cast<long double>(i) <
206 static_cast<long double>(INT_MAX));
207 return static_cast<int>(i);
208}
209#endif
210// TODO: cleanup and integrate with the errors above someday
211#define INDIRECT_ERROR(str, file, line) \
212 { \
213 time_t terr; \
214 time(&terr); \
215 fprintf(stderr, "\n\n\n\n┌──────────────────────── ERROR " \
216 "───────────────────────────┐\n\n"); \
217 fprintf(stderr, "%s", ctime(&terr)); \
218 fprintf(stderr, "Error in file %s line %d: %s\n", file, line, str); \
219 fprintf(stderr, "\n└──────────────────────── ERROR " \
220 "───────────────────────────┘\n\n\n\n"); \
221 fflush(stderr); \
222 abort(); \
223 }
224#define INDIRECT_ERRCHK_ALWAYS(retval, file, line) \
225 { \
226 if (!(retval)) \
227 INDIRECT_ERROR(#retval " was false", file, line); \
228 }
229
230
231static inline size_t
232AS_SIZE_T(const int i, const char* file, const int line)
233{
235 INDIRECT_ERRCHK_ALWAYS((long double)(i) < (long double)(SIZE_MAX), file,
236 line);
237
238 return (size_t)(i);
239}
240
241#ifdef __cplusplus
242template <typename T>
243static inline size_t
244AS_SIZE_T(const T i, const char* file, const int line)
245{
247 INDIRECT_ERRCHK_ALWAYS(static_cast<long double>(i) <
248 static_cast<long double>(SIZE_MAX),file,line);
249 return static_cast<size_t>(i);
250}
251//overloaded with unsigned int to remove compiler warnings about unneeded checks
252static inline size_t
253AS_SIZE_T(unsigned int i, const char* file, const int line)
254{
255 INDIRECT_ERRCHK_ALWAYS(static_cast<long double>(i) <
256 static_cast<long double>(SIZE_MAX),file,line);
257 return static_cast<size_t>(i);
258}
259
260//overloaded with size_t to remove compiler warnings about unneeded checks
261static inline size_t
262AS_SIZE_T(size_t i, const char* file, const int line)
263{
264 INDIRECT_ERRCHK_ALWAYS(static_cast<long double>(i) <
265 static_cast<long double>(SIZE_MAX),file,line);
266 return static_cast<size_t>(i);
267}
268
269static inline size3_t
270AS_SIZE_T(const int3 i, const char* file, const int line)
271{
272 INDIRECT_ERRCHK_ALWAYS(i.x >= 0, file, line);
273 INDIRECT_ERRCHK_ALWAYS(i.y >= 0, file, line);
274 INDIRECT_ERRCHK_ALWAYS(i.z >= 0, file, line);
275
276 return (size3_t){(size_t)i.x,(size_t)i.y,(size_t)i.z};
277}
278#endif
#define ERRCHK_ALWAYS(retval)
Definition errchk.h:95
#define INDIRECT_ERRCHK_ALWAYS(retval, file, line)
Definition errchk.h:224
#define cudaSuccess
Definition hip.h:24
#define cudaError_t
Definition hip.h:23
#define cudaGetErrorString
Definition hip.h:26
Volume size3_t
Definition host_datatypes.h:76
static int FILE * file
Definition string_vec.h:120
char line[10000]
Definition string_vec.h:122