Commit | Line | Data |
---|---|---|
10988083 MW |
1 | // -------------------------------------------------------------------------- |
2 | // | |
3 | // Copyright | |
4 | // Markus Wittmann, 2016-2017 | |
5 | // RRZE, University of Erlangen-Nuremberg, Germany | |
6 | // markus.wittmann -at- fau.de or hpc -at- rrze.fau.de | |
7 | // | |
8 | // Viktor Haag, 2016 | |
9 | // LSS, University of Erlangen-Nuremberg, Germany | |
10 | // | |
11 | // This file is part of the Lattice Boltzmann Benchmark Kernels (LbmBenchKernels). | |
12 | // | |
13 | // LbmBenchKernels is free software: you can redistribute it and/or modify | |
14 | // it under the terms of the GNU General Public License as published by | |
15 | // the Free Software Foundation, either version 3 of the License, or | |
16 | // (at your option) any later version. | |
17 | // | |
18 | // LbmBenchKernels is distributed in the hope that it will be useful, | |
19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | // GNU General Public License for more details. | |
22 | // | |
23 | // You should have received a copy of the GNU General Public License | |
24 | // along with LbmBenchKernels. If not, see <http://www.gnu.org/licenses/>. | |
25 | // | |
26 | // -------------------------------------------------------------------------- | |
27 | #ifndef __BASE_H__ | |
28 | #define __BASE_H__ | |
29 | ||
30 | #include <stdio.h> | |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | ||
34 | #include <sys/time.h> | |
35 | ||
36 | ||
37 | static inline double Time() | |
38 | { | |
39 | struct timeval time; | |
40 | ||
41 | gettimeofday(&time, NULL); | |
42 | ||
43 | return (time.tv_sec + 1e-6 * time.tv_usec); | |
44 | } | |
45 | ||
46 | ||
10988083 MW |
47 | #define STRINGIFYX(x) #x |
48 | #define STRINGIFY(x) STRINGIFYX(x) | |
49 | ||
50 | // See top of BoostJoin.h for Boost Licence. | |
51 | #include "BoostJoin.h" | |
52 | #define JOIN(X, Y) BOOST_DO_JOIN(X, Y) | |
53 | ||
54 | ||
55 | // Some macro fu to remove the first comma. | |
56 | // "x" is an empty macro agrument in EXPAND2 | |
57 | // before the first comma which is skipped | |
58 | #ifndef EXPAND | |
59 | #define EXPAND2(x, ...) __VA_ARGS__ | |
60 | #define EXPAND(x, ...) EXPAND2(x, ## __VA_ARGS__) | |
61 | #endif | |
62 | ||
63 | #ifdef DEBUG | |
64 | ||
65 | #define Assert(expression) \ | |
66 | do { \ | |
67 | if (!(expression)) { \ | |
68 | Error("%s:%d assertion \"%s\" failed with code %d\n", \ | |
69 | __FILE__, __LINE__, \ | |
70 | #expression, expression); \ | |
71 | __asm__ ("int $3\n"); \ | |
72 | exit(-1); \ | |
73 | } \ | |
74 | } while (0) | |
75 | ||
76 | #define AssertMsg(expression, formatString, ...) \ | |
77 | do { \ | |
78 | if (!(expression)) { \ | |
79 | Error("%s:%d assertion \"%s\" failed with code %d\n", \ | |
80 | __FILE__, __LINE__, \ | |
81 | #expression, expression); \ | |
82 | Error(formatString, ##__VA_ARGS__); \ | |
83 | __asm__ ("int $3\n"); \ | |
84 | exit(-1); \ | |
85 | } \ | |
86 | } while (0) | |
87 | #else | |
88 | ||
89 | #define Assert(expression) | |
90 | #define AssertMsg(expression, formatString, ...) | |
91 | ||
92 | #endif | |
93 | ||
94 | #define Verify(expression) \ | |
95 | do { \ | |
96 | if (!(expression)) { \ | |
97 | Error("%s:%d verify \"%s\" failed with code %d\n", \ | |
98 | __FILE__, __LINE__, \ | |
99 | #expression, expression); \ | |
100 | __asm__ ("int $3\n"); \ | |
101 | exit(-1); \ | |
102 | } \ | |
103 | } while (0) | |
104 | ||
105 | #define VerifyMsg(expression, formatString, ...) \ | |
106 | do { \ | |
107 | if (!(expression)) { \ | |
108 | Error("%s:%d verify \"%s\" failed with code %d\n", \ | |
109 | __FILE__, __LINE__, \ | |
110 | #expression, expression); \ | |
111 | Error(formatString, ##__VA_ARGS__); \ | |
112 | __asm__ ("int $3\n"); \ | |
113 | exit(-1); \ | |
114 | } \ | |
115 | } while (0) | |
116 | ||
117 | #define Print(formatString, ...) \ | |
e3f82424 | 118 | fprintf(stdout, SHC_MAGENTA formatString SHC_NC, ##__VA_ARGS__) |
10988083 MW |
119 | |
120 | #define Warning(formatString, ...) \ | |
e3f82424 | 121 | fprintf(stdout, SHC_BROWN "WARNING: " SHC_NC formatString, ##__VA_ARGS__) |
10988083 MW |
122 | |
123 | #define Error(formatString, ...) \ | |
e3f82424 MW |
124 | fprintf(stderr, SHC_RED "ERROR: " formatString SHC_NC , ##__VA_ARGS__) |
125 | /* | |
10988083 | 126 | #define DebugPrint(formatString, ...) \ |
e3f82424 MW |
127 | fprintf(stderr, "DEBUG: " formatString, ##__VA_ARGS__) |
128 | */ | |
10988083 MW |
129 | #ifndef NO_SHELL_COLORS |
130 | ||
131 | // or "\e" | |
132 | #define ESC "\x1b" | |
133 | ||
134 | // No Color | |
135 | #define SHC_NC ESC "[0m" | |
136 | ||
137 | #define SHC_BLACK ESC "[0;30m" | |
138 | #define SHC_MAGENTA ESC "[0;35m" | |
139 | #define SHC_RED ESC "[0;31m" | |
140 | #define SHC_DARK_RED ESC "[1;31m" | |
141 | #define SHC_CYAN ESC "[0;36m" | |
142 | #define SHC_BROWN ESC "[0;33m" | |
143 | #define SHC_DARK_GREEN ESC "[1;32m" | |
144 | ||
145 | #else // NO_SHELL_COLORS | |
146 | ||
147 | // No Color | |
148 | #define SHC_NC "" | |
149 | ||
150 | #define SHC_BLACK "" | |
151 | #define SHC_MAGENTA "" | |
152 | #define SHC_RED "" | |
153 | #define SHC_DARK_RED "" | |
154 | #define SHC_CYAN "" | |
155 | #define SHC_BROWN "" | |
156 | #define SHC_DARK_GREEN "" | |
157 | ||
158 | #endif // NO_SHELL_COLORS | |
159 | ||
160 | ||
161 | #define N_ELEMS(x) (sizeof(x) / sizeof((x)[0])) | |
162 | ||
163 | ||
164 | #define MIN(a, b) ((a) <= (b) ? (a) : (b)) | |
165 | ||
166 | static inline int MinI(int a, int b) { return a <= b ? a : b; } | |
167 | ||
168 | // Raises a breakpoint if a debugger is attached, else SIG_TRAP is raised. | |
169 | #define DEBUG_BREAK_POINT() __asm__ ("int $3\n") | |
170 | ||
171 | #define UNUSED(variable) (void)(variable) | |
172 | ||
173 | ||
174 | static inline char * ByteToHuman(size_t bytes) | |
175 | { | |
176 | static char buffer[256] = { 0 }; | |
177 | ||
178 | if (bytes < 1024) { | |
179 | snprintf(buffer, sizeof(buffer), "%lu b", bytes); | |
180 | return buffer; | |
181 | } | |
182 | ||
183 | double KiB = bytes / 1024.0; | |
184 | ||
185 | if (KiB < 1024.0) { | |
186 | snprintf(buffer, sizeof(buffer), "%9.2e KiB", KiB); | |
187 | return buffer; | |
188 | } | |
189 | ||
190 | double MiB = KiB / 1024.0; | |
191 | if (MiB < 1024.0) { | |
192 | snprintf(buffer, sizeof(buffer), "%9.2e MiB", MiB); | |
193 | return buffer; | |
194 | } | |
195 | ||
196 | double GiB = MiB / 1024.0; | |
197 | snprintf(buffer, sizeof(buffer), "%9.2e GiB", GiB); | |
198 | return buffer; | |
199 | } | |
200 | ||
201 | ||
202 | #endif // __BASE_H__ |