-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathbenchmark_common.h
233 lines (188 loc) · 5.75 KB
/
benchmark_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
/*
* SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES.
* All rights reserved. SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
#pragma once
#ifndef VERBOSE
#define VERBOSE 0
#endif
#include "nvcomp.hpp"
#include "nvcomp/cascaded.h"
#if defined(_WIN32)
#undef max
#undef min
#endif
#include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
#define CUDA_CHECK(func) \
do { \
cudaError_t rt = (func); \
if (rt != cudaSuccess) { \
std::cout << "API call failure \"" #func "\" with " << rt << " at " \
<< __FILE__ << ":" << __LINE__ << std::endl; \
throw; \
} \
} while (0);
namespace nvcomp
{
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
// returns nano-seconds
inline uint64_t get_time(timespec start, timespec end)
{
constexpr const uint64_t BILLION = 1000000000ULL;
const uint64_t elapsed_time
= BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
return elapsed_time;
}
// size in bytes, returns GB/s
inline double gibs(struct timespec start, struct timespec end, size_t s)
{
uint64_t t = get_time(start, end);
return (double)s / t * 1e9 / 1024 / 1024 / 1024;
}
// size in bytes, returns GB/s
inline double
gbs(const std::chrono::time_point<std::chrono::steady_clock>& start,
const std::chrono::time_point<std::chrono::steady_clock>& end,
size_t s)
{
return (double)s / std::chrono::nanoseconds(end - start).count();
}
inline double
gbs(const std::chrono::nanoseconds duration,
size_t s)
{
return (double)s / duration.count();
}
inline double
average_gbs(
const std::vector<std::chrono::nanoseconds>& durations,
size_t s)
{
size_t count_sum = 0;
for (auto duration : durations) {
count_sum += duration.count();
}
size_t avg_duration = count_sum / durations.size();
return (double)s / avg_duration;
}
inline double
average_gbs(
const std::vector<float>& durations,
size_t s)
{
double duration_sum = 0;
for (auto duration : durations) {
duration_sum += duration;
}
double avg_duration = (duration_sum / durations.size()) / 1e3;
return (double)s / 1e9 / avg_duration;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
template <>
inline nvcompType_t TypeOf<float>()
{
return NVCOMP_TYPE_INT;
}
inline bool startsWith(const std::string input, const std::string subStr)
{
return input.substr(0, subStr.length()) == subStr;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
void benchmark_assert(const bool pass, const std::string& msg)
{
if (!pass) {
throw std::runtime_error("ERROR: " + msg);
}
}
std::vector<uint8_t>
gen_data(const int max_byte, const size_t size, std::mt19937& rng)
{
if (max_byte < 0
|| max_byte > static_cast<int>(std::numeric_limits<uint8_t>::max())) {
throw std::runtime_error("Invalid byte value: " + std::to_string(max_byte));
}
std::uniform_int_distribution<uint16_t> dist(0, max_byte);
std::vector<uint8_t> data;
for (size_t i = 0; i < size; ++i) {
data.emplace_back(static_cast<uint8_t>(dist(rng) & 0xff));
}
return data;
}
// Load dataset from binary file into an array of type T
template <typename T>
std::vector<T> load_dataset_from_binary(char* fname, size_t* input_element_count)
{
FILE* fileptr = fopen(fname, "rb");
if (fileptr == NULL) {
printf("Binary input file not found.\n");
exit(1);
}
// find length of file
fseek(fileptr, 0, SEEK_END);
size_t filelen = ftell(fileptr);
rewind(fileptr);
// If input_element_count is already set, use it, otherwise load the whole file
if (*input_element_count == 0 || filelen / sizeof(T) < *input_element_count) {
*input_element_count = filelen / sizeof(T);
}
const size_t numElements = *input_element_count;
std::vector<T> buffer(numElements);
// Read binary file in to buffer
const size_t numRead = fread(buffer.data(), sizeof(T), numElements, fileptr);
if (numRead != numElements) {
throw std::runtime_error(
"Failed to read file: " + std::string(fname) + " read "
+ std::to_string(numRead) + "/"
+ std::to_string(*input_element_count * sizeof(T)) + " elements.");
}
fclose(fileptr);
return buffer;
}
// Load dataset from binary file into an array of type T
template <typename T>
std::vector<T> load_dataset_from_txt(char* fname, size_t* input_element_count)
{
std::vector<T> buffer;
FILE* fileptr;
fileptr = fopen(fname, "rb");
if (fileptr == NULL) {
printf("Text input file not found.\n");
exit(1);
}
size_t i = 0;
constexpr size_t MAX_LINE_LEN = 100;
char line[MAX_LINE_LEN];
while (fgets(line, MAX_LINE_LEN, fileptr) && i < *input_element_count) {
// std::stringstream row(line);
buffer.push_back((T)std::stof(line));
i++;
}
fclose(fileptr);
return buffer;
}
} // namespace nvcomp