| 1 |
#if defined(__linux__) || (__APPLE__ && __aarch64__) |
| 2 |
#define USING_COUNTERS |
| 3 |
#endif |
| 4 |
#include "event_counter.h" |
| 5 |
#include <algorithm> |
| 6 |
#include "fast_float/fast_float.h" |
| 7 |
#include <chrono> |
| 8 |
#include <climits> |
| 9 |
#include <cmath> |
| 10 |
#include <cstdint> |
| 11 |
#include <cstdio> |
| 12 |
#include <cstdlib> |
| 13 |
#include <cstring> |
| 14 |
#include <ctype.h> |
| 15 |
#include <fstream> |
| 16 |
#include <iomanip> |
| 17 |
#include <iostream> |
| 18 |
#include <random> |
| 19 |
#include <sstream> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
#include <locale.h> |
| 24 |
|
| 25 |
template <typename CharT> |
| 26 |
double findmax_fastfloat64(std::vector<std::basic_string<CharT>> &s) { |
| 27 |
double answer = 0; |
| 28 |
double x = 0; |
| 29 |
for (auto &st : s) { |
| 30 |
auto [p, ec] = fast_float::from_chars(st.data(), st.data() + st.size(), x); |
| 31 |
if (p == st.data()) { |
| 32 |
throw std::runtime_error("bug in findmax_fastfloat"); |
| 33 |
} |
| 34 |
answer = answer > x ? answer : x; |
| 35 |
} |
| 36 |
return answer; |
| 37 |
} |
| 38 |
|
| 39 |
template <typename CharT> |
| 40 |
double findmax_fastfloat32(std::vector<std::basic_string<CharT>> &s) { |
| 41 |
float answer = 0; |
| 42 |
float x = 0; |
| 43 |
for (auto &st : s) { |
| 44 |
auto [p, ec] = fast_float::from_chars(st.data(), st.data() + st.size(), x); |
| 45 |
if (p == st.data()) { |
| 46 |
throw std::runtime_error("bug in findmax_fastfloat"); |
| 47 |
} |
| 48 |
answer = answer > x ? answer : x; |
| 49 |
} |
| 50 |
return answer; |
| 51 |
} |
| 52 |
|
| 53 |
event_collector collector{}; |
| 54 |
|
| 55 |
#ifdef USING_COUNTERS |
| 56 |
template <class T, class CharT> |
| 57 |
std::vector<event_count> |
| 58 |
time_it_ns(std::vector<std::basic_string<CharT>> &lines, T const &function, |
| 59 |
size_t repeat) { |
| 60 |
std::vector<event_count> aggregate; |
| 61 |
bool printed_bug = false; |
| 62 |
for (size_t i = 0; i < repeat; i++) { |
| 63 |
collector.start(); |
| 64 |
double ts = function(lines); |
| 65 |
if (ts == 0 && !printed_bug) { |
| 66 |
printf("bug\n"); |
| 67 |
printed_bug = true; |
| 68 |
} |
| 69 |
aggregate.push_back(collector.end()); |
| 70 |
} |
| 71 |
return aggregate; |
| 72 |
} |
| 73 |
|
| 74 |
void pretty_print(double volume, size_t number_of_floats, std::string name, |
| 75 |
std::vector<event_count> events) { |
| 76 |
double volumeMB = volume / (1024. * 1024.); |
| 77 |
double average_ns{0}; |
| 78 |
double min_ns{DBL_MAX}; |
| 79 |
double cycles_min{DBL_MAX}; |
| 80 |
double instructions_min{DBL_MAX}; |
| 81 |
double cycles_avg{0}; |
| 82 |
double instructions_avg{0}; |
| 83 |
double branches_min{0}; |
| 84 |
double branches_avg{0}; |
| 85 |
double branch_misses_min{0}; |
| 86 |
double branch_misses_avg{0}; |
| 87 |
for (event_count e : events) { |
| 88 |
double ns = e.elapsed_ns(); |
| 89 |
average_ns += ns; |
| 90 |
min_ns = min_ns < ns ? min_ns : ns; |
| 91 |
|
| 92 |
double cycles = e.cycles(); |
| 93 |
cycles_avg += cycles; |
| 94 |
cycles_min = cycles_min < cycles ? cycles_min : cycles; |
| 95 |
|
| 96 |
double instructions = e.instructions(); |
| 97 |
instructions_avg += instructions; |
| 98 |
instructions_min = |
| 99 |
instructions_min < instructions ? instructions_min : instructions; |
| 100 |
|
| 101 |
double branches = e.branches(); |
| 102 |
branches_avg += branches; |
| 103 |
branches_min = branches_min < branches ? branches_min : branches; |
| 104 |
|
| 105 |
double branch_misses = e.missed_branches(); |
| 106 |
branch_misses_avg += branch_misses; |
| 107 |
branch_misses_min = |
| 108 |
branch_misses_min < branch_misses ? branch_misses_min : branch_misses; |
| 109 |
} |
| 110 |
cycles_avg /= events.size(); |
| 111 |
instructions_avg /= events.size(); |
| 112 |
average_ns /= events.size(); |
| 113 |
branches_avg /= events.size(); |
| 114 |
printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(), |
| 115 |
volumeMB * 1000000000 / min_ns, |
| 116 |
(average_ns - min_ns) * 100.0 / average_ns); |
| 117 |
printf("%8.2f Mfloat/s ", number_of_floats * 1000 / min_ns); |
| 118 |
if (instructions_min > 0) { |
| 119 |
printf(" %8.2f i/B %8.2f i/f (+/- %.1f %%) ", instructions_min / volume, |
| 120 |
instructions_min / number_of_floats, |
| 121 |
(instructions_avg - instructions_min) * 100.0 / instructions_avg); |
| 122 |
|
| 123 |
printf(" %8.2f c/B %8.2f c/f (+/- %.1f %%) ", cycles_min / volume, |
| 124 |
cycles_min / number_of_floats, |
| 125 |
(cycles_avg - cycles_min) * 100.0 / cycles_avg); |
| 126 |
printf(" %8.2f i/c ", instructions_min / cycles_min); |
| 127 |
printf(" %8.2f b/f ", branches_avg / number_of_floats); |
| 128 |
printf(" %8.2f bm/f ", branch_misses_avg / number_of_floats); |
| 129 |
printf(" %8.2f GHz ", cycles_min / min_ns); |
| 130 |
} |
| 131 |
printf("\n"); |
| 132 |
} |
| 133 |
#else |
| 134 |
template <class T, class CharT> |
| 135 |
std::pair<double, double> |
| 136 |
time_it_ns(std::vector<std::basic_string<CharT>> &lines, T const &function, |
| 137 |
size_t repeat) { |
| 138 |
std::chrono::high_resolution_clock::time_point t1, t2; |
| 139 |
double average = 0; |
| 140 |
double min_value = DBL_MAX; |
| 141 |
bool printed_bug = false; |
| 142 |
for (size_t i = 0; i < repeat; i++) { |
| 143 |
t1 = std::chrono::high_resolution_clock::now(); |
| 144 |
double ts = function(lines); |
| 145 |
if (ts == 0 && !printed_bug) { |
| 146 |
printf("bug\n"); |
| 147 |
printed_bug = true; |
| 148 |
} |
| 149 |
t2 = std::chrono::high_resolution_clock::now(); |
| 150 |
double dif = |
| 151 |
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count(); |
| 152 |
average += dif; |
| 153 |
min_value = min_value < dif ? min_value : dif; |
| 154 |
} |
| 155 |
average /= repeat; |
| 156 |
return std::make_pair(min_value, average); |
| 157 |
} |
| 158 |
|
| 159 |
void pretty_print(double volume, size_t number_of_floats, std::string name, |
| 160 |
std::pair<double, double> result) { |
| 161 |
double volumeMB = volume / (1024. * 1024.); |
| 162 |
printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(), |
| 163 |
volumeMB * 1000000000 / result.first, |
| 164 |
(result.second - result.first) * 100.0 / result.second); |
| 165 |
printf("%8.2f Mfloat/s ", number_of_floats * 1000 / result.first); |
| 166 |
printf(" %8.2f ns/f \n", double(result.first) / number_of_floats); |
| 167 |
} |
| 168 |
#endif |
| 169 |
|
| 170 |
// this is okay, all chars are ASCII |
| 171 |
inline std::u16string widen(std::string line) { |
| 172 |
std::u16string u16line; |
| 173 |
u16line.resize(line.size()); |
| 174 |
for (size_t i = 0; i < line.size(); ++i) { |
| 175 |
u16line[i] = char16_t(line[i]); |
| 176 |
} |
| 177 |
return u16line; |
| 178 |
} |
| 179 |
|
| 180 |
std::vector<std::u16string> widen(const std::vector<std::string> &lines) { |
| 181 |
std::vector<std::u16string> u16lines; |
| 182 |
u16lines.reserve(lines.size()); |
| 183 |
for (auto const &line : lines) { |
| 184 |
u16lines.push_back(widen(line)); |
| 185 |
} |
| 186 |
return u16lines; |
| 187 |
} |
| 188 |
|
| 189 |
void process(std::vector<std::string> &lines, size_t volume) { |
| 190 |
size_t repeat = 1000; |
| 191 |
double volumeMB = volume / (1024. * 1024.); |
| 192 |
std::cout << "ASCII volume = " << volumeMB << " MB " << std::endl; |
| 193 |
pretty_print(volume, lines.size(), "fastfloat (64)", |
| 194 |
time_it_ns(lines, findmax_fastfloat64<char>, repeat)); |
| 195 |
pretty_print(volume, lines.size(), "fastfloat (32)", |
| 196 |
time_it_ns(lines, findmax_fastfloat32<char>, repeat)); |
| 197 |
|
| 198 |
std::vector<std::u16string> lines16 = widen(lines); |
| 199 |
volume = 2 * volume; |
| 200 |
volumeMB = volume / (1024. * 1024.); |
| 201 |
std::cout << "UTF-16 volume = " << volumeMB << " MB " << std::endl; |
| 202 |
pretty_print(volume, lines.size(), "fastfloat (64)", |
| 203 |
time_it_ns(lines16, findmax_fastfloat64<char16_t>, repeat)); |
| 204 |
pretty_print(volume, lines.size(), "fastfloat (32)", |
| 205 |
time_it_ns(lines16, findmax_fastfloat32<char16_t>, repeat)); |
| 206 |
} |
| 207 |
|
| 208 |
void fileload(std::string filename) { |
| 209 |
std::ifstream inputfile(filename); |
| 210 |
if (!inputfile) { |
| 211 |
std::cerr << "can't open " << filename << std::endl; |
| 212 |
return; |
| 213 |
} |
| 214 |
std::cout << "#### " << std::endl; |
| 215 |
std::cout << "# reading " << filename << std::endl; |
| 216 |
std::cout << "#### " << std::endl; |
| 217 |
std::string line; |
| 218 |
std::vector<std::string> lines; |
| 219 |
lines.reserve(10000); // let us reserve plenty of memory. |
| 220 |
size_t volume = 0; |
| 221 |
while (getline(inputfile, line)) { |
| 222 |
volume += line.size(); |
| 223 |
lines.push_back(line); |
| 224 |
} |
| 225 |
std::cout << "# read " << lines.size() << " lines " << std::endl; |
| 226 |
process(lines, volume); |
| 227 |
} |
| 228 |
|
| 229 |
int main(int argc, char **argv) { |
| 230 |
if (collector.has_events()) { |
| 231 |
std::cout << "# Using hardware counters" << std::endl; |
| 232 |
} else { |
| 233 |
#if defined(__linux__) || (__APPLE__ && __aarch64__) |
| 234 |
std::cout << "# Hardware counters not available, try to run in privileged " |
| 235 |
"mode (e.g., sudo)." |
| 236 |
<< std::endl; |
| 237 |
#endif |
| 238 |
} |
| 239 |
if (argc > 1) { |
| 240 |
fileload(argv[1]); |
| 241 |
return EXIT_SUCCESS; |
| 242 |
} |
| 243 |
fileload(std::string(BENCHMARK_DATA_DIR) + "/canada.txt"); |
| 244 |
fileload(std::string(BENCHMARK_DATA_DIR) + "/mesh.txt"); |
| 245 |
return EXIT_SUCCESS; |
| 246 |
} |