-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.h
389 lines (352 loc) · 11.8 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (c) 2023, Signaloid.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#ifdef __cplusplus
#define ZERO_STRUCT_INIT
#else
#define ZERO_STRUCT_INIT 0
#endif
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
kCommonConstantMaxCharsPerFilepath = 1024,
kCommonConstantMaxCharsPerLine = 1024 * 1024,
kCommonConstantMaxNumberOfInputSamples = 10000,
kCommonConstantMaxCharsPerJSONVariableSymbol = 256,
kCommonConstantMaxCharsPerJSONVariableDescription = 1024,
} CommonConstant;
typedef enum
{
kCommonConstantReturnTypeError = 1,
kCommonConstantReturnTypeSuccess = 0,
} CommonConstantReturnType;
typedef enum
{
kFloatingPointVariableTypeUnknown,
kFloatingPointVariableTypeFloat,
kFloatingPointVariableTypeDouble,
} FloatingPointVariableType;
typedef enum
{
kJSONVariableTypeUnknown,
kJSONVariableTypeFloat,
kJSONVariableTypeDouble,
kJSONVariableTypeFloatParticle,
kJSONVariableTypeDoubleParticle,
} JSONVariableType;
typedef union
{
const double * asDouble;
const float * asFloat;
} JSONVariablePointer;
typedef struct JSONVariable
{
char variableSymbol[kCommonConstantMaxCharsPerJSONVariableSymbol];
char variableDescription[kCommonConstantMaxCharsPerJSONVariableDescription];
JSONVariablePointer values;
JSONVariableType type;
size_t size;
} JSONVariable;
/**
* @brief Prevent compiler optimising away an otherwise unused value.
* Used for native Monte Carlo benchmarking.
*
* @param ptr Pointer to value to force compiler to keep. Must not be `NULL`.
*/
void
doNotOptimize(void * ptr);
/**
* @brief fatal print and exit.
*
* @param fmt error message to print before exit
* @param format args
*/
__attribute__((noreturn))
void fatal(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2)));
/**
* @brief Read single-precision floating-point data from a CSV file. Data entries are either numbers or Ux-values.
*
* @param inputFilePath path to CSV file to read from
* @param expectedHeaders array of headers that should be in the CSV data
* @param inputDistributions array of input distributions to be obtained from the read CSV data
* @param numberOfDistributions size of `inputDistributions` _and_ `expectedHeaders` arrays
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
readInputFloatDistributionsFromCSV(
const char * inputFilePath,
const char * const * expectedHeaders,
float * inputDistributions,
size_t numberOfDistributions);
/**
* @brief Read double-precision floating-point data from a CSV file. Data entries are either numbers or Ux-values.
*
* @param inputFilePath path to CSV file to read from
* @param expectedHeaders array of headers that should be in the CSV data
* @param inputDistributions array of input distributions to be obtained from the read CSV data
* @param numberOfDistributions size of `inputDistributions` _and_ `expectedHeaders` arrays
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
readInputDoubleDistributionsFromCSV(
const char * inputFilePath,
const char * const * expectedHeaders,
double * inputDistributions,
size_t numberOfDistributions);
/**
* @brief Write Ux-valued data of single-precision floating-point variables to a CSV file.
*
* @param outputFilePath path prefix for the output CSV file to write to
* @param outputVariables array of output distributions whose Ux representations we will write to the output CSV
* @param outputVariableNames names of output distributions whose Ux representations we will write to the output CSV
* @param numberOfOutputDistributions number of output distributions whose Ux representations we will write to the output CSV
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
writeOutputFloatDistributionsToCSV(
const char * outputFilePath,
const float * outputVariables,
const char * const * outputVariableNames,
size_t numberOfOutputDistributions);
/**
* @brief Write Ux-valued data of double-precision floating-point variables to a CSV file.
*
* @param outputFilePath path prefix for the output CSV file to write to
* @param outputVariables array of output distributions whose Ux representations we will write to the output CSV
* @param outputVariableNames names of output distributions whose Ux representations we will write to the output CSV
* @param numberOfOutputDistributions number of output distributions whose Ux representations we will write to the output CSV
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
writeOutputDoubleDistributionsToCSV(
const char * outputFilePath,
const double * outputVariables,
const char * const * outputVariableNames,
size_t numberOfOutputDistributions);
/**
* @brief Print Ux-valued data to `stdout` in JSON format.
*
* @param jsonVariables array of JSONVariable items
* @param count number of JSONVariable items
* @param description JSON description
*/
void
printJSONVariables(
JSONVariable * jsonVariables,
size_t count,
const char * description);
/**
* @brief Parse an integer at the start of `str`, trailing characters are ignored.
*
* @param str String to parse
* @param out Will be set to parsed value
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
parseIntChecked(
const char * str,
int * out);
/**
* @brief Parse a single-precision floating-point value at the start of `str`, trailing characters are ignored.
*
* @param str String to parse
* @param out Will be set to parsed value
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
parseFloatChecked(
const char * str,
float * out);
/**
* @brief Parse a double-precision floating-pointvalue at the start of `str`, trailing characters are ignored.
*
* @param str String to parse
* @param out Will be set to parsed value
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
parseDoubleChecked(
const char * str,
double * out);
typedef struct
{
char outputFilePath[kCommonConstantMaxCharsPerFilepath];
char inputFilePath[kCommonConstantMaxCharsPerFilepath];
bool isWriteToFileEnabled;
bool isTimingEnabled;
size_t numberOfMonteCarloIterations;
size_t outputSelect;
bool isOutputSelected;
bool isVerbose;
bool isInputFromFileEnabled;
bool isOutputJSONMode;
bool isHelpEnabled;
bool isBenchmarkingMode;
bool isMonteCarloMode;
bool isSingleShotExecution;
} CommonCommandLineArguments;
typedef struct
{
/**
* @brief The command-line flag (without leading `-` or `--`) for this option.
*/
const char * opt;
/**
* @brief An alternative command-line flag (without leading `-` or `--`) for this option.
*
* @details Often used for short options.
*/
const char * optAlternative;
/**
* @brief Whether this option takes an argument.
*
* @details If set, then parsing will fail unless the command-line flag has an
* argument.
*/
bool hasArg;
/**
* @brief Pass pointer to variable in which any argument will be stored.
*
* @details May be to `NULL` (e.g. if this option does not have an argument), in which
* case nothing will be written.
*/
const char ** foundArg;
/**
* @brief Pass pointer to variable which will be set to true if option is found.
*
* @details May be to `NULL`, in which case nothing will be written.
*/
bool * foundOpt;
} DemoOption;
/**
* @brief Parse command-line arguments into `args`.
*
* @details Grouped short options are not supported. (Use `-W -j` rather than `-Wj`.)
*
* @param argc As provided to `main()`
* @param argv As provided to `main()`
* @param args Parsed command-line arguments are stored here
* @param demoSpecificOptions Extra command-line arguments to parse
* @return `kCommonConstantReturnTypeError` on error, `kCommonConstantReturnTypeSuccess` on success
*/
CommonConstantReturnType
parseArgs(
int argc,
char * const argv[],
CommonCommandLineArguments * arguments,
DemoOption * demoSpecificOptions);
/**
* @brief Print the common part of the usage to stdout.
*/
void
printCommonUsage(void);
typedef struct
{
double mean;
double variance;
} MeanAndVariance;
/**
* @brief Caluculate mean and variance of float data.
*
* @param dataArray data array for which to compute mean and variance
* @param dataArraySize number of items in `dataArray`
* @return calculated mean and variance
*/
MeanAndVariance
calculateMeanAndVarianceOfFloatSamples(
const float * dataArray,
size_t dataArraySize);
/**
* @brief Caluculate mean and variance of double data.
*
* @param dataArray data array for which to compute mean and variance
* @param dataArraySize number of items in `dataArray`
* @return calculated mean and variance
*/
MeanAndVariance
calculateMeanAndVarianceOfDoubleSamples(
const double * dataArray,
size_t dataArraySize);
/**
* @brief Writes Monte Carlo samples to a file 'data.out'
*
* @param benchmarkingDataSamples Array (length `numberOfMonteCarloIterations`) of float Monte Carlo samples
* @param cpuTimeElapsedMicroSeconds Execution time of kernel
* @param numberOfMonteCarloIterations Number of samples (one per iteration)
* @return void
*/
void
saveMonteCarloFloatDataToDataDotOutFile(
const float * benchmarkingDataSamples,
uint64_t cpuTimeElapsedMicroSeconds,
size_t numberOfMonteCarloIterations);
/**
* @brief Writes Monte Carlo samples to a file 'data.out'
*
* @param benchmarkingDataSamples Array (length `numberOfMonteCarloIterations`) of double Monte Carlo samples
* @param cpuTimeElapsedMicroSeconds Execution time of kernel
* @param numberOfMonteCarloIterations Number of samples (one per iteration)
* @return void
*/
void
saveMonteCarloDoubleDataToDataDotOutFile(
const double * benchmarkingDataSamples,
uint64_t cpuTimeElapsedMicroSeconds,
size_t numberOfMonteCarloIterations);
/**
* @brief Call Malloc and abort on allocation failure.
*
* @param size Passed to malloc
* @param file File name to include in error message
* @param line Line number to include in error message
* @return Pointer as returned by malloc
*/
void *
checkedMalloc(
size_t size,
const char * file,
int line);
/**
* @brief Call Calloc and abort on allocation failure.
*
* @param size Passed to calloc
* @param num Passed to calloc
* @param file File name to include in error message
* @param line Line number to include in error message
* @return Pointer as returned by calloc
*/
void *
checkedCalloc(
size_t num,
size_t size,
const char * file,
int line);
#ifdef __cplusplus
} /* extern "C" */
#endif