[libc-commits] [libc] [libc] NVPTX Profiling Draft (PR #92009)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed May 22 13:14:30 PDT 2024


================
@@ -0,0 +1,92 @@
+#include "LibcGpuBenchmark.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/time/gpu/time_utils.h"
+
+namespace LIBC_NAMESPACE {
+namespace libc_gpu_benchmarks {
+
+Benchmark *Benchmark::start = nullptr;
+Benchmark *Benchmark::end = nullptr;
+
+void Benchmark::add_benchmark(Benchmark *benchmark) {
+  if (end == nullptr) {
+    start = benchmark;
+    end = benchmark;
+    return;
+  }
+  end->next = benchmark;
+  end = benchmark;
+}
+
+int Benchmark::run_benchmarks() {
+  for (Benchmark *b = start; b != nullptr; b = b->next)
+    b->run();
+  return 0;
+}
+
+BenchmarkResult benchmark(const BenchmarkOptions &options,
+                          cpp::function<uint64_t(void)> wrapper_func) {
+  BenchmarkResult result;
+  RuntimeEstimationProgression rep;
+  size_t total_iterations = 0;
+  size_t iterations = options.initial_iterations;
+  if (iterations < (uint32_t)1)
+    iterations = 1;
+
+  size_t samples = 0;
+  uint64_t total_time = 0;
+  uint64_t best_guess = 0;
+  uint64_t total_cycles = 0;
+  uint64_t cycles_2 = 0;
+  uint64_t min = UINT_MAX;
+  uint64_t max = 0;
+  for (;;) {
+    uint64_t sample_cycles = 0;
+    uint64_t overhead = LIBC_NAMESPACE::overhead();
+    const clock_t start = (double)clock();
+    for (uint32_t i = 0; i < iterations; i++) {
+      auto wrapper_intermediate = wrapper_func();
+      uint64_t result = wrapper_intermediate - overhead;
+      max = cpp::max(max, result);
+      min = cpp::min(min, result);
+      sample_cycles += result;
+    }
+    const clock_t end = clock();
+    const clock_t duration_ns =
+        ((end - start) * 1000 * 1000 * 1000) / CLOCKS_PER_SEC;
+    total_time += duration_ns;
+    samples++;
+    total_cycles += sample_cycles;
+    cycles_2 += sample_cycles * sample_cycles;
+
+    total_iterations += iterations;
+    const double change_ratio =
+        rep.compute_improvement({iterations, sample_cycles});
+    best_guess = rep.current_estimation;
+
+    if (samples >= options.max_samples ||
+        iterations >= options.max_iterations ||
+        total_time >= options.max_duration) {
+      break;
+    } else if (total_time >= options.min_duration &&
+               samples >= options.min_samples &&
+               change_ratio < options.epsilon) {
+      break;
+    }
+
+    iterations *= options.scaling_factor;
+  }
+  result.cycles = best_guess;
+  result.standard_deviation = fputil::sqrt((double)cycles_2 / total_iterations -
----------------
jhuber6 wrote:

```suggestion
  result.standard_deviation = fputil::sqrt((static_cast<double>(cycles_2) / total_iterations -
```

https://github.com/llvm/llvm-project/pull/92009


More information about the libc-commits mailing list