Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Nov 25, 2024
1 parent 3f2cd66 commit 8832c53
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 127 deletions.
28 changes: 11 additions & 17 deletions benchmarks/apple_arm_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ inline performance_counters operator-(const performance_counters &a,
a.instructions - b.instructions);
}



typedef float f32;
typedef double f64;
typedef int8_t i8;
Expand Down Expand Up @@ -616,9 +614,7 @@ typedef struct {

#define lib_nelems(x) (sizeof(x) / sizeof((x)[0]))
#define lib_symbol_def(name) \
{ \
#name, (void **)&name \
}
{ #name, (void **)&name }

static const lib_symbol lib_symbols_kperf[] = {
lib_symbol_def(kpc_pmu_version),
Expand Down Expand Up @@ -933,7 +929,7 @@ typedef struct {
static const event_alias profile_events[] = {
{"cycles",
{
"FIXED_CYCLES", // Apple A7-A15//CORE_ACTIVE_CYCLE
"FIXED_CYCLES", // Apple A7-A15//CORE_ACTIVE_CYCLE
"CPU_CLK_UNHALTED.THREAD", // Intel Core 1th-10th
"CPU_CLK_UNHALTED.CORE", // Intel Yonah, Merom
}},
Expand Down Expand Up @@ -976,7 +972,6 @@ u64 counters_0[KPC_MAX_COUNTERS] = {0};
u64 counters_1[KPC_MAX_COUNTERS] = {0};
const usize ev_count = sizeof(profile_events) / sizeof(profile_events[0]);


bool setup_performance_counters() {
static bool init = false;
static bool worked = false;
Expand All @@ -995,7 +990,7 @@ bool setup_performance_counters() {
// check permission
int force_ctrs = 0;
if (kpc_force_all_ctrs_get(&force_ctrs)) {
//printf("Permission denied, xnu/kpc requires root privileges.\n");
// printf("Permission denied, xnu/kpc requires root privileges.\n");
return (worked = false);
}
int ret;
Expand Down Expand Up @@ -1101,17 +1096,16 @@ inline performance_counters get_counters() {
}
return 1;
}
/*printf("counters value:\n");
for (usize i = 0; i < ev_count; i++) {
const event_alias *alias = profile_events + i;
usize idx = counter_map[i];
u64 val = counters_1[idx] - counters_0[idx];
printf("%14s: %llu\n", alias->alias, val);
}*/
/*printf("counters value:\n");
for (usize i = 0; i < ev_count; i++) {
const event_alias *alias = profile_events + i;
usize idx = counter_map[i];
u64 val = counters_1[idx] - counters_0[idx];
printf("%14s: %llu\n", alias->alias, val);
}*/
return performance_counters{
counters_0[counter_map[0]], counters_0[counter_map[2]],
counters_0[counter_map[3]],
counters_0[counter_map[1]]};
counters_0[counter_map[3]], counters_0[counter_map[1]]};
}

#endif
98 changes: 46 additions & 52 deletions benchmarks/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if defined(__linux__) || (__APPLE__ && __aarch64__)
#if defined(__linux__) || (__APPLE__ && __aarch64__)
#define USING_COUNTERS
#include "event_counter.h"
#endif
Expand All @@ -22,7 +22,6 @@
#include <vector>
#include <locale.h>


template <typename CharT>
double findmax_fastfloat64(std::vector<std::basic_string<CharT>> &s) {
double answer = 0;
Expand Down Expand Up @@ -55,8 +54,9 @@ event_collector collector{};

#ifdef USING_COUNTERS
template <class T, class CharT>
std::vector<event_count> time_it_ns(std::vector<std::basic_string<CharT>> &lines,
T const &function, size_t repeat) {
std::vector<event_count>
time_it_ns(std::vector<std::basic_string<CharT>> &lines, T const &function,
size_t repeat) {
std::vector<event_count> aggregate;
bool printed_bug = false;
for (size_t i = 0; i < repeat; i++) {
Expand All @@ -71,7 +71,8 @@ std::vector<event_count> time_it_ns(std::vector<std::basic_string<CharT>> &lines
return aggregate;
}

void pretty_print(double volume, size_t number_of_floats, std::string name, std::vector<event_count> events) {
void pretty_print(double volume, size_t number_of_floats, std::string name,
std::vector<event_count> events) {
double volumeMB = volume / (1024. * 1024.);
double average_ns{0};
double min_ns{DBL_MAX};
Expand All @@ -83,7 +84,7 @@ void pretty_print(double volume, size_t number_of_floats, std::string name, std:
double branches_avg{0};
double branch_misses_min{0};
double branch_misses_avg{0};
for(event_count e : events) {
for (event_count e : events) {
double ns = e.elapsed_ns();
average_ns += ns;
min_ns = min_ns < ns ? min_ns : ns;
Expand All @@ -94,51 +95,46 @@ void pretty_print(double volume, size_t number_of_floats, std::string name, std:

double instructions = e.instructions();
instructions_avg += instructions;
instructions_min = instructions_min < instructions ? instructions_min : instructions;
instructions_min =
instructions_min < instructions ? instructions_min : instructions;

double branches = e.branches();
branches_avg += branches;
branches_min = branches_min < branches ? branches_min : branches;

double branch_misses = e.missed_branches();
branch_misses_avg += branch_misses;
branch_misses_min = branch_misses_min < branch_misses ? branch_misses_min : branch_misses;
branch_misses_min =
branch_misses_min < branch_misses ? branch_misses_min : branch_misses;
}
cycles_avg /= events.size();
instructions_avg /= events.size();
average_ns /= events.size();
branches_avg /= events.size();
printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(),
volumeMB * 1000000000 / min_ns,
(average_ns - min_ns) * 100.0 / average_ns);
printf("%8.2f Mfloat/s ",
number_of_floats * 1000 / min_ns);
if(instructions_min > 0) {
printf(" %8.2f i/B %8.2f i/f (+/- %.1f %%) ",
instructions_min / volume,
instructions_min / number_of_floats,
volumeMB * 1000000000 / min_ns,
(average_ns - min_ns) * 100.0 / average_ns);
printf("%8.2f Mfloat/s ", number_of_floats * 1000 / min_ns);
if (instructions_min > 0) {
printf(" %8.2f i/B %8.2f i/f (+/- %.1f %%) ", instructions_min / volume,
instructions_min / number_of_floats,
(instructions_avg - instructions_min) * 100.0 / instructions_avg);

printf(" %8.2f c/B %8.2f c/f (+/- %.1f %%) ",
cycles_min / volume,
cycles_min / number_of_floats,
printf(" %8.2f c/B %8.2f c/f (+/- %.1f %%) ", cycles_min / volume,
cycles_min / number_of_floats,
(cycles_avg - cycles_min) * 100.0 / cycles_avg);
printf(" %8.2f i/c ",
instructions_min /cycles_min);
printf(" %8.2f b/f ",
branches_avg /number_of_floats);
printf(" %8.2f bm/f ",
branch_misses_avg /number_of_floats);
printf(" %8.2f GHz ",
cycles_min / min_ns);
printf(" %8.2f i/c ", instructions_min / cycles_min);
printf(" %8.2f b/f ", branches_avg / number_of_floats);
printf(" %8.2f bm/f ", branch_misses_avg / number_of_floats);
printf(" %8.2f GHz ", cycles_min / min_ns);
}
printf("\n");

}
#else
template <class T, class CharT>
std::pair<double, double> time_it_ns(std::vector<std::basic_string<CharT>> &lines,
T const &function, size_t repeat) {
std::pair<double, double>
time_it_ns(std::vector<std::basic_string<CharT>> &lines, T const &function,
size_t repeat) {
std::chrono::high_resolution_clock::time_point t1, t2;
double average = 0;
double min_value = DBL_MAX;
Expand All @@ -160,21 +156,16 @@ std::pair<double, double> time_it_ns(std::vector<std::basic_string<CharT>> &line
return std::make_pair(min_value, average);
}




void pretty_print(double volume, size_t number_of_floats, std::string name, std::pair<double,double> result) {
void pretty_print(double volume, size_t number_of_floats, std::string name,
std::pair<double, double> result) {
double volumeMB = volume / (1024. * 1024.);
printf("%-40s: %8.2f MB/s (+/- %.1f %%) ", name.data(),
volumeMB * 1000000000 / result.first,
(result.second - result.first) * 100.0 / result.second);
printf("%8.2f Mfloat/s ",
number_of_floats * 1000 / result.first);
printf(" %8.2f ns/f \n",
double(result.first) /number_of_floats );
volumeMB * 1000000000 / result.first,
(result.second - result.first) * 100.0 / result.second);
printf("%8.2f Mfloat/s ", number_of_floats * 1000 / result.first);
printf(" %8.2f ns/f \n", double(result.first) / number_of_floats);
}
#endif

#endif

// this is okay, all chars are ASCII
inline std::u16string widen(std::string line) {
Expand All @@ -195,21 +186,23 @@ std::vector<std::u16string> widen(const std::vector<std::string> &lines) {
return u16lines;
}


void process(std::vector<std::string> &lines, size_t volume) {
size_t repeat = 100;
double volumeMB = volume / (1024. * 1024.);
std::cout << "ASCII volume = " << volumeMB << " MB " << std::endl;
pretty_print(volume, lines.size(), "fastfloat (64)", time_it_ns(lines, findmax_fastfloat64<char>, repeat));
pretty_print(volume, lines.size(), "fastfloat (32)", time_it_ns(lines, findmax_fastfloat32<char>, repeat));
pretty_print(volume, lines.size(), "fastfloat (64)",
time_it_ns(lines, findmax_fastfloat64<char>, repeat));
pretty_print(volume, lines.size(), "fastfloat (32)",
time_it_ns(lines, findmax_fastfloat32<char>, repeat));

std::vector<std::u16string> lines16 = widen(lines);
volume = 2 * volume;
volumeMB = volume / (1024. * 1024.);
std::cout << "UTF-16 volume = " << volumeMB << " MB " << std::endl;
pretty_print(volume, lines.size(), "fastfloat (64)", time_it_ns(lines16, findmax_fastfloat64<char16_t>, repeat));
pretty_print(volume, lines.size(), "fastfloat (32)", time_it_ns(lines16, findmax_fastfloat32<char16_t>, repeat));

pretty_print(volume, lines.size(), "fastfloat (64)",
time_it_ns(lines16, findmax_fastfloat64<char16_t>, repeat));
pretty_print(volume, lines.size(), "fastfloat (32)",
time_it_ns(lines16, findmax_fastfloat32<char16_t>, repeat));
}

void fileload(std::string filename) {
Expand All @@ -233,13 +226,14 @@ void fileload(std::string filename) {
process(lines, volume);
}


int main(int argc, char **argv) {
if(collector.has_events()) {
if (collector.has_events()) {
std::cout << "# Using hardware counters" << std::endl;
} else {
#if defined(__linux__) || (__APPLE__ && __aarch64__)
std::cout << "# Hardware counters not available, try to run in privileged mode (e.g., sudo)." << std::endl;
#if defined(__linux__) || (__APPLE__ && __aarch64__)
std::cout << "# Hardware counters not available, try to run in privileged "
"mode (e.g., sudo)."
<< std::endl;
#endif
}
fileload(std::string(BENCHMARK_DATA_DIR) + "/canada.txt");
Expand Down
Loading

0 comments on commit 8832c53

Please sign in to comment.