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

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Tue May 14 08:58:59 PDT 2024


================
@@ -0,0 +1,70 @@
+#include "LibcGpuBenchmark.h"
+
+namespace LIBC_NAMESPACE {
+namespace libc_gpu_benchmarks {
+
+Benchmark *Benchmark::Start = nullptr;
+Benchmark *Benchmark::End = nullptr;
+
+void Benchmark::addBenchmark(Benchmark *B) {
+  if (End == nullptr) {
+    Start = B;
+    End = B;
+    return;
+  }
+
+  End->Next = B;
+  End = B;
+}
+
+int Benchmark::runBenchmarks() {
+  for (Benchmark *B = Start; B != nullptr; B = B->Next) {
+    B->Run();
+  }
+
+  return 0;
+}
+
+BenchmarkResult benchmark(const BenchmarkOptions &Options,
+                          uint64_t (*WrapperFunc)()) {
+  BenchmarkResult Result;
+  RuntimeEstimationProgression REP;
+  size_t TotalIterations = 0;
+  size_t Iterations = Options.InitialIterations;
+  if (Iterations < (uint32_t)1) {
+    Iterations = 1;
+  }
+  size_t Samples = 0;
+  uint64_t BestGuess = 0;
+  uint64_t TotalCycles = 0;
+  for (;;) {
+    uint64_t SampleCycles = 0;
+    for (uint32_t i = 0; i < Iterations; i++) {
+      auto overhead = LIBC_NAMESPACE::overhead();
+      uint64_t result = WrapperFunc() - overhead;
+      SampleCycles += result;
+    }
+
+    Samples++;
+    TotalCycles += SampleCycles;
+    TotalIterations += Iterations;
+    const double ChangeRatio =
+        REP.ComputeImprovement({Iterations, SampleCycles});
+    BestGuess = REP.CurrentEstimation;
+
+    if (Samples >= Options.MaxSamples || Iterations >= Options.MaxIterations) {
+      break;
+    } else if (Samples >= Options.MinSamples && ChangeRatio < Options.Epsilon) {
+      break;
+    }
+
+    Iterations *= Options.ScalingFactor;
+  }
+  Result.Cycles = BestGuess;
+  Result.Samples = Samples;
+  Result.TotalIterations = TotalIterations;
+  return Result;
+};
----------------
jhuber6 wrote:

budget = 100,000; // milliseconds
for (int i = 0; i < num_iterations; ++i) {
  start = gpu::fixed_frequency_timer();
  result = benchmark();
  max = cpp::max(max, result);
  min = cpp::min(min, result);
  total += result;
  stop = ();
  double time = stop - start / delta;
  budget -= time;
  if (budget exceeded)
    stop();
}

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


More information about the libc-commits mailing list