|
| 1 | +#include "LibcGpuBenchmark.h" |
| 2 | +#include "src/__support/CPP/algorithm.h" |
| 3 | +#include "src/__support/CPP/array.h" |
| 4 | +#include "src/__support/CPP/string.h" |
| 5 | +#include "src/__support/FPUtil/sqrt.h" |
| 6 | +#include "src/__support/GPU/utils.h" |
| 7 | +#include "src/__support/fixedvector.h" |
| 8 | +#include "src/time/gpu/time_utils.h" |
| 9 | + |
| 10 | +namespace LIBC_NAMESPACE { |
| 11 | +namespace benchmarks { |
| 12 | + |
| 13 | +FixedVector<Benchmark *, 64> benchmarks; |
| 14 | +cpp::array<BenchmarkResult, 1024> results; |
| 15 | + |
| 16 | +void Benchmark::add_benchmark(Benchmark *benchmark) { |
| 17 | + benchmarks.push_back(benchmark); |
| 18 | +} |
| 19 | + |
| 20 | +BenchmarkResult reduce_results(cpp::array<BenchmarkResult, 1024> &results) { |
| 21 | + BenchmarkResult result; |
| 22 | + uint64_t cycles_sum = 0; |
| 23 | + double standard_deviation_sum = 0; |
| 24 | + uint64_t min = UINT64_MAX; |
| 25 | + uint64_t max = 0; |
| 26 | + uint32_t samples_sum = 0; |
| 27 | + uint32_t iterations_sum = 0; |
| 28 | + clock_t time_sum = 0; |
| 29 | + uint64_t num_threads = gpu::get_num_threads(); |
| 30 | + for (uint64_t i = 0; i < num_threads; i++) { |
| 31 | + BenchmarkResult current_result = results[i]; |
| 32 | + cycles_sum += current_result.cycles; |
| 33 | + standard_deviation_sum += current_result.standard_deviation; |
| 34 | + min = cpp::min(min, current_result.min); |
| 35 | + max = cpp::max(max, current_result.max); |
| 36 | + samples_sum += current_result.samples; |
| 37 | + iterations_sum += current_result.total_iterations; |
| 38 | + time_sum += current_result.total_time; |
| 39 | + } |
| 40 | + result.cycles = cycles_sum / num_threads; |
| 41 | + result.standard_deviation = standard_deviation_sum / num_threads; |
| 42 | + result.min = min; |
| 43 | + result.max = max; |
| 44 | + result.samples = samples_sum / num_threads; |
| 45 | + result.total_iterations = iterations_sum / num_threads; |
| 46 | + result.total_time = time_sum / num_threads; |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +void Benchmark::run_benchmarks() { |
| 51 | + uint64_t id = gpu::get_thread_id(); |
| 52 | + gpu::sync_threads(); |
| 53 | + |
| 54 | + for (Benchmark *benchmark : benchmarks) |
| 55 | + results[id] = benchmark->run(); |
| 56 | + gpu::sync_threads(); |
| 57 | + if (id == 0) { |
| 58 | + for (Benchmark *benchmark : benchmarks) { |
| 59 | + BenchmarkResult all_results = reduce_results(results); |
| 60 | + constexpr auto GREEN = "\033[32m"; |
| 61 | + constexpr auto RESET = "\033[0m"; |
| 62 | + log << GREEN << "[ RUN ] " << RESET << benchmark->get_name() << '\n'; |
| 63 | + log << GREEN << "[ OK ] " << RESET << benchmark->get_name() << ": " |
| 64 | + << all_results.cycles << " cycles, " << all_results.min << " min, " |
| 65 | + << all_results.max << " max, " << all_results.total_iterations |
| 66 | + << " iterations, " << all_results.total_time << " ns, " |
| 67 | + << static_cast<long>(all_results.standard_deviation) << " stddev\n"; |
| 68 | + } |
| 69 | + } |
| 70 | + gpu::sync_threads(); |
| 71 | +} |
| 72 | + |
| 73 | +BenchmarkResult benchmark(const BenchmarkOptions &options, |
| 74 | + cpp::function<uint64_t(void)> wrapper_func) { |
| 75 | + BenchmarkResult result; |
| 76 | + RuntimeEstimationProgression rep; |
| 77 | + uint32_t total_iterations = 0; |
| 78 | + uint32_t iterations = options.initial_iterations; |
| 79 | + if (iterations < 1u) |
| 80 | + iterations = 1; |
| 81 | + |
| 82 | + uint32_t samples = 0; |
| 83 | + uint64_t total_time = 0; |
| 84 | + uint64_t best_guess = 0; |
| 85 | + uint64_t total_cycles = 0; |
| 86 | + uint64_t cycles_squared = 0; |
| 87 | + uint64_t min = UINT64_MAX; |
| 88 | + uint64_t max = 0; |
| 89 | + |
| 90 | + uint64_t overhead = UINT64_MAX; |
| 91 | + int overhead_iterations = 10; |
| 92 | + for (int i = 0; i < overhead_iterations; i++) |
| 93 | + overhead = cpp::min(overhead, LIBC_NAMESPACE::overhead()); |
| 94 | + |
| 95 | + for (uint64_t time_budget = options.max_duration; time_budget >= 0;) { |
| 96 | + uint64_t sample_cycles = 0; |
| 97 | + const clock_t start = static_cast<double>(clock()); |
| 98 | + for (uint32_t i = 0; i < iterations; i++) { |
| 99 | + auto wrapper_intermediate = wrapper_func(); |
| 100 | + uint64_t result = wrapper_intermediate - overhead; |
| 101 | + max = cpp::max(max, result); |
| 102 | + min = cpp::min(min, result); |
| 103 | + sample_cycles += result; |
| 104 | + } |
| 105 | + const clock_t end = clock(); |
| 106 | + const clock_t duration_ns = |
| 107 | + ((end - start) * 1000 * 1000 * 1000) / CLOCKS_PER_SEC; |
| 108 | + total_time += duration_ns; |
| 109 | + time_budget -= duration_ns; |
| 110 | + samples++; |
| 111 | + total_cycles += sample_cycles; |
| 112 | + cycles_squared += sample_cycles * sample_cycles; |
| 113 | + |
| 114 | + total_iterations += iterations; |
| 115 | + const double change_ratio = |
| 116 | + rep.compute_improvement({iterations, sample_cycles}); |
| 117 | + best_guess = rep.current_estimation; |
| 118 | + |
| 119 | + if (samples >= options.max_samples || iterations >= options.max_iterations) |
| 120 | + break; |
| 121 | + if (total_time >= options.min_duration && samples >= options.min_samples && |
| 122 | + change_ratio < options.epsilon) |
| 123 | + break; |
| 124 | + |
| 125 | + iterations *= options.scaling_factor; |
| 126 | + } |
| 127 | + result.cycles = best_guess; |
| 128 | + result.standard_deviation = fputil::sqrt<double>( |
| 129 | + static_cast<double>(cycles_squared) / total_iterations - |
| 130 | + static_cast<double>(best_guess * best_guess)); |
| 131 | + result.min = min; |
| 132 | + result.max = max; |
| 133 | + result.samples = samples; |
| 134 | + result.total_iterations = total_iterations; |
| 135 | + result.total_time = total_time; |
| 136 | + return result; |
| 137 | +}; |
| 138 | + |
| 139 | +} // namespace benchmarks |
| 140 | +} // namespace LIBC_NAMESPACE |
0 commit comments