Astaroth  2.2
errchk.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2020, Johannes Pekkila, Miikka Vaisala.
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 <stdio.h>
30 #include <stdlib.h>
31 #include <time.h>
32 
33 /*
34  * =============================================================================
35  * General error checking
36  * =============================================================================
37  */
38 #define ERROR(str) \
39  { \
40  time_t t; \
41  time(&t); \
42  fprintf(stderr, "%s", ctime(&t)); \
43  fprintf(stderr, "\tError in file %s line %d: %s\n", __FILE__, __LINE__, str); \
44  fflush(stderr); \
45  exit(EXIT_FAILURE); \
46  abort(); \
47  }
48 
49 #define WARNING(str) \
50  { \
51  time_t t; \
52  time(&t); \
53  fprintf(stderr, "%s", ctime(&t)); \
54  fprintf(stderr, "\tWarning in file %s line %d: %s\n", __FILE__, __LINE__, str); \
55  fflush(stderr); \
56  }
57 
58 // DO NOT REMOVE BRACKETS AROUND RETVAL. F.ex. if (!a < b) vs if (!(a < b)).
59 #define ERRCHK(retval) \
60  { \
61  if (!(retval)) \
62  ERROR(#retval " was false"); \
63  }
64 #define WARNCHK(retval) \
65  { \
66  if (!(retval)) \
67  WARNING(#retval " was false"); \
68  }
69 #define WARNCHK_ALWAYS(retval) \
70  { \
71  if (!(retval)) \
72  WARNING(#retval " was false"); \
73  }
74 #define ERRCHK_ALWAYS(retval) \
75  { \
76  if (!(retval)) \
77  ERROR(#retval " was false"); \
78  }
79 
80 /*
81  * =============================================================================
82  * CUDA-specific error checking
83  * =============================================================================
84  */
85 #if defined(__CUDA_RUNTIME_API_H__)
86 static inline void
87 cuda_assert(cudaError_t code, const char* file, int line, bool abort)
88 {
89  if (code != cudaSuccess) {
90  time_t t;
91  time(&t);
92  fprintf(stderr, "%s", ctime(&t));
93  fprintf(stderr, "\tCUDA error in file %s line %d: %s\n", file, line,
94  cudaGetErrorString(code));
95  fflush(stderr);
96 
97  if (abort)
98  exit(code);
99  }
100 }
101 
102 #ifdef NDEBUG
103 #undef ERRCHK
104 #undef WARNCHK
105 #define ERRCHK(params)
106 #define WARNCHK(params)
107 #define ERRCHK_CUDA(params) params
108 #define WARNCHK_CUDA(params) params
109 #define ERRCHK_CUDA_KERNEL() \
110  { \
111  }
112 #else
113 #define ERRCHK_CUDA(params) \
114  { \
115  cuda_assert((params), __FILE__, __LINE__, true); \
116  }
117 #define WARNCHK_CUDA(params) \
118  { \
119  cuda_assert((params), __FILE__, __LINE__, false); \
120  }
121 
122 #define ERRCHK_CUDA_KERNEL() \
123  { \
124  ERRCHK_CUDA(cudaPeekAtLastError()); \
125  ERRCHK_CUDA(cudaDeviceSynchronize()); \
126  }
127 #endif
128 
129 #define ERRCHK_CUDA_ALWAYS(params) \
130  { \
131  cuda_assert((params), __FILE__, __LINE__, true); \
132  }
133 
134 #define ERRCHK_CUDA_KERNEL_ALWAYS() \
135  { \
136  ERRCHK_CUDA_ALWAYS(cudaPeekAtLastError()); \
137  ERRCHK_CUDA_ALWAYS(cudaDeviceSynchronize()); \
138  }
139 
140 #define WARNCHK_CUDA_ALWAYS(params) \
141  { \
142  cuda_assert((params), __FILE__, __LINE__, false); \
143  }
144 #endif // __CUDA_RUNTIME_API_H__