add citation information
[LbmBenchmarkKernelsPublic.git] / src / Base.h
CommitLineData
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
0fde6e45
MW
30#include "Config.h"
31
10988083
MW
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include <sys/time.h>
37
38
39static inline double Time()
40{
41 struct timeval time;
42
43 gettimeofday(&time, NULL);
44
45 return (time.tv_sec + 1e-6 * time.tv_usec);
46}
47
48
10988083
MW
49#define STRINGIFYX(x) #x
50#define STRINGIFY(x) STRINGIFYX(x)
51
52// See top of BoostJoin.h for Boost Licence.
53#include "BoostJoin.h"
54#define JOIN(X, Y) BOOST_DO_JOIN(X, Y)
55
56
57// Some macro fu to remove the first comma.
58// "x" is an empty macro agrument in EXPAND2
59// before the first comma which is skipped
60#ifndef EXPAND
61 #define EXPAND2(x, ...) __VA_ARGS__
62 #define EXPAND(x, ...) EXPAND2(x, ## __VA_ARGS__)
63#endif
64
65#ifdef DEBUG
66
67 #define Assert(expression) \
68 do { \
69 if (!(expression)) { \
70 Error("%s:%d assertion \"%s\" failed with code %d\n", \
71 __FILE__, __LINE__, \
72 #expression, expression); \
73 __asm__ ("int $3\n"); \
74 exit(-1); \
75 } \
76 } while (0)
77
78 #define AssertMsg(expression, formatString, ...) \
79 do { \
80 if (!(expression)) { \
81 Error("%s:%d assertion \"%s\" failed with code %d\n", \
82 __FILE__, __LINE__, \
83 #expression, expression); \
84 Error(formatString, ##__VA_ARGS__); \
85 __asm__ ("int $3\n"); \
86 exit(-1); \
87 } \
88 } while (0)
89#else
90
91 #define Assert(expression)
92 #define AssertMsg(expression, formatString, ...)
93
94#endif
95
96 #define Verify(expression) \
97 do { \
98 if (!(expression)) { \
99 Error("%s:%d verify \"%s\" failed with code %d\n", \
100 __FILE__, __LINE__, \
101 #expression, expression); \
102 __asm__ ("int $3\n"); \
103 exit(-1); \
104 } \
105 } while (0)
106
107 #define VerifyMsg(expression, formatString, ...) \
108 do { \
109 if (!(expression)) { \
110 Error("%s:%d verify \"%s\" failed with code %d\n", \
111 __FILE__, __LINE__, \
112 #expression, expression); \
113 Error(formatString, ##__VA_ARGS__); \
114 __asm__ ("int $3\n"); \
115 exit(-1); \
116 } \
117 } while (0)
118
119 #define Print(formatString, ...) \
e3f82424 120 fprintf(stdout, SHC_MAGENTA formatString SHC_NC, ##__VA_ARGS__)
10988083
MW
121
122 #define Warning(formatString, ...) \
e3f82424 123 fprintf(stdout, SHC_BROWN "WARNING: " SHC_NC formatString, ##__VA_ARGS__)
10988083
MW
124
125 #define Error(formatString, ...) \
e3f82424
MW
126 fprintf(stderr, SHC_RED "ERROR: " formatString SHC_NC , ##__VA_ARGS__)
127/*
10988083 128 #define DebugPrint(formatString, ...) \
e3f82424
MW
129 fprintf(stderr, "DEBUG: " formatString, ##__VA_ARGS__)
130*/
10988083
MW
131 #ifndef NO_SHELL_COLORS
132
133 // or "\e"
134 #define ESC "\x1b"
135
136 // No Color
137 #define SHC_NC ESC "[0m"
138
139 #define SHC_BLACK ESC "[0;30m"
140 #define SHC_MAGENTA ESC "[0;35m"
141 #define SHC_RED ESC "[0;31m"
142 #define SHC_DARK_RED ESC "[1;31m"
143 #define SHC_CYAN ESC "[0;36m"
144 #define SHC_BROWN ESC "[0;33m"
145 #define SHC_DARK_GREEN ESC "[1;32m"
146
147 #else // NO_SHELL_COLORS
148
149 // No Color
150 #define SHC_NC ""
151
152 #define SHC_BLACK ""
153 #define SHC_MAGENTA ""
154 #define SHC_RED ""
155 #define SHC_DARK_RED ""
156 #define SHC_CYAN ""
157 #define SHC_BROWN ""
158 #define SHC_DARK_GREEN ""
159
160 #endif // NO_SHELL_COLORS
161
162
163 #define N_ELEMS(x) (sizeof(x) / sizeof((x)[0]))
164
165
166 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
167
168static inline int MinI(int a, int b) { return a <= b ? a : b; }
169
170// Raises a breakpoint if a debugger is attached, else SIG_TRAP is raised.
171#define DEBUG_BREAK_POINT() __asm__ ("int $3\n")
172
173#define UNUSED(variable) (void)(variable)
174
175
176static inline char * ByteToHuman(size_t bytes)
177{
178 static char buffer[256] = { 0 };
179
180 if (bytes < 1024) {
181 snprintf(buffer, sizeof(buffer), "%lu b", bytes);
182 return buffer;
183 }
184
185 double KiB = bytes / 1024.0;
186
187 if (KiB < 1024.0) {
188 snprintf(buffer, sizeof(buffer), "%9.2e KiB", KiB);
189 return buffer;
190 }
191
192 double MiB = KiB / 1024.0;
193 if (MiB < 1024.0) {
194 snprintf(buffer, sizeof(buffer), "%9.2e MiB", MiB);
195 return buffer;
196 }
197
198 double GiB = MiB / 1024.0;
199 snprintf(buffer, sizeof(buffer), "%9.2e GiB", GiB);
200 return buffer;
201}
202
203
204#endif // __BASE_H__
This page took 0.083263 seconds and 5 git commands to generate.