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

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Tue May 28 17:42:45 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 benchmarks {
+
+FixedVector<Benchmark *, 64> benchmarks_to_run;
+
+void Benchmark::add_benchmark(Benchmark *benchmark) {
+  benchmarks_to_run.push_back(benchmark);
+}
+
+int Benchmark::run_benchmarks() {
+  for (auto it = benchmarks_to_run.rbegin(), e = benchmarks_to_run.rend();
+       it != e; ++it)
+    (*it)->run();
+  return 0;
+}
+
+BenchmarkResult benchmark(const BenchmarkOptions &options,
+                          cpp::function<uint64_t(void)> wrapper_func) {
+  BenchmarkResult result;
+  RuntimeEstimationProgression rep;
+  uint32_t total_iterations = 0;
+  uint32_t iterations = options.initial_iterations;
+  if (iterations < 1u)
+    iterations = 1;
+
+  uint32_t samples = 0;
+  uint64_t total_time = 0;
+  uint64_t best_guess = 0;
+  uint64_t total_cycles = 0;
+  uint64_t cycles_squared = 0;
+  uint64_t min = UINT64_MAX;
+  uint64_t max = 0;
+
+  uint64_t overhead = UINT64_MAX;
+  int overhead_iterations = 10;
+  for (int i = 0; i < overhead_iterations; i++)
+    overhead = cpp::min(overhead, LIBC_NAMESPACE::overhead());
+
+  for (uint64_t time_budget = options.max_duration; time_budget >= 0;) {
+    uint64_t sample_cycles = 0;
+    const clock_t start = static_cast<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;
+    time_budget -= duration_ns;
+    samples++;
+    total_cycles += sample_cycles;
+    cycles_squared += 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) {
+      break;
+    } else if (total_time >= options.min_duration &&
+               samples >= options.min_samples &&
+               change_ratio < options.epsilon) {
+      break;
+    }
----------------
jhuber6 wrote:

```suggestion
    if (samples >= options.max_samples ||
        iterations >= options.max_iterations)
      break;
    if (total_time >= options.min_duration &&
               samples >= options.min_samples &&
               change_ratio < options.epsilon) {
      break;
```
```suggestion
    if (samples >= options.max_samples ||
        iterations >= options.max_iterations) {
      break;
    } else if (total_time >= options.min_duration &&
               samples >= options.min_samples &&
               change_ratio < options.epsilon) {
      break;
    }
```
LLVM style, no `else` after a control flow escape.

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


More information about the libc-commits mailing list