ComPPare 1.0.0
Loading...
Searching...
No Matches
nvbench.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 | Leong Fan FUNG | funglf | stanleyfunglf@gmail.com
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the “Software”), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE.
22
23*/
24
34#pragma once
35#ifdef HAVE_NV_BENCH
36#include <nvbench/nvbench.cuh>
38
39#include <memory>
40#include <functional>
41#include <sstream>
42#include <string>
43
45{
51 template <typename F>
53 {
54 F f;
55
56 void operator()(nvbench::state &st, nvbench::type_list<>)
57 {
58 f(st);
59 }
60
61 NvbenchCallable(F _f) : f(std::move(_f)) {}
62 ~NvbenchCallable() = default;
63
64 NvbenchCallable(const NvbenchCallable &other) = default;
65 NvbenchCallable &operator=(const NvbenchCallable &other) = default;
66 NvbenchCallable(NvbenchCallable &&other) = default;
68 };
69
80 class state
81 {
82 public:
83 state(const state &) = delete;
84 state &operator=(const state &) = delete;
85 state(state &&) = delete;
86 state &operator=(state &&) = delete;
87
88 static state &instance()
89 {
90 static state inst;
91 return inst;
92 }
93
94 static void set_state(nvbench::state *st)
95 {
96 instance().st_ = st;
97 }
98
99 static nvbench::state *get_state()
100 {
101 if (!instance().st_)
102 throw std::runtime_error("Benchmark state is not set.");
103 return instance().st_;
104 }
105
106 private:
107 state() = default;
108 nvbench::state *st_ = nullptr;
109 };
110
119 {
120 public:
121 nvbench_manager() = default;
122 ~nvbench_manager() = default;
123
134 void initialize(int &argc, char **argv)
135 {
136 auto [tmp_argc, tmp_argv] = nvbench_parser_.parse(argc, argv);
137 nvbench_argc = tmp_argc;
138 nvbench_argv = tmp_argv;
140 }
141
176 template <typename Func, typename... Args>
177 nvbench::benchmark_base &add_nvbench(const char *name, Func f, Args &&...args)
178 {
179 std::tuple<std::decay_t<Args>...> cargs(std::forward<Args>(args)...);
180
181 auto nvbench_wrapper = [f, cargs = std::move(cargs)](nvbench::state &st) mutable
182 {
184 std::apply([&](auto &&...unpacked)
185 { f(std::forward<decltype(unpacked)>(unpacked)...); }, cargs);
186 };
187
188 using Callable = NvbenchCallable<decltype(nvbench_wrapper)>;
189
190 return nvbench::benchmark_manager::get()
191 .add(std::make_unique<nvbench::benchmark<Callable>>(Callable{std::move(nvbench_wrapper)}))
192 .set_name(name);
193 }
194
202 void run()
203 {
204 NVBENCH_MAIN_INITIALIZE(nvbench_argc, nvbench_argv);
205 {
206 NVBENCH_MAIN_PARSE(nvbench_argc, nvbench_argv);
207
208 NVBENCH_MAIN_PRINT_PREAMBLE(parser);
209 NVBENCH_MAIN_RUN_BENCHMARKS(parser);
210 NVBENCH_MAIN_PRINT_EPILOGUE(parser);
211
212 NVBENCH_MAIN_PRINT_RESULTS(parser);
213 } /* Tear down parser before finalization */
214 NVBENCH_MAIN_FINALIZE();
215 }
216
217 private:
222
225 {
226
227 std::cout << "\n"
228 << std::left << comppare::internal::ansi::BOLD
229 << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n============= "
230 << comppare::internal::ansi::ITALIC("nvbench")
231 << " =============\n*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
232 << comppare::internal::ansi::BOLD_OFF << "\n\n";
233
234 std::cout << "nvbench cmdline arguments:\n";
235 for (int i = 0; i < nvbench_argc; ++i)
236 {
237 std::cout << std::setw(2) << std::right << " "
238 << " [" << i << "] " << std::quoted(nvbench_argv[i]) << "\n";
239 }
240
241 std::cout << std::left
242 << comppare::internal::ansi::BOLD("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*")
243 << "\n\n";
244 }
245 };
246
258 template <class InTup, class OutTup>
259 class nvbenchPlugin final : public Plugin<InTup, OutTup>
260 {
262
263 private:
264 nvbenchPlugin() = default;
266
267 public:
268 nvbenchPlugin(const nvbenchPlugin &) = delete;
270
279 static std::shared_ptr<Self> instance()
280 {
281 static std::shared_ptr<Self> inst{new Self};
282 return inst;
283 }
284
300 template <class Func>
301 nvbench::benchmark_base &register_impl(const std::string &name,
302 Func &&user_fn,
303 const InTup &inputs,
304 OutTup &outs)
305 {
306 return std::apply([&](auto &&...in_vals) -> nvbench::benchmark_base &
307 { return std::apply([&](auto &&...out_vals) -> nvbench::benchmark_base &
308 { return nb_.add_nvbench(name.c_str(),
309 std::forward<Func>(user_fn),
310 std::forward<decltype(in_vals)>(in_vals)...,
311 std::forward<decltype(out_vals)>(out_vals)...); }, outs); }, inputs);
312 }
313
322 void initialize(int &argc, char **argv) override
323 {
324 nb_.initialize(argc, argv);
325 }
326
332 void run() override
333 {
334 nb_.run();
335 }
336 };
337}
338
342#define PLUGIN_HOTLOOP_BENCH \
343 auto state_ = comppare::plugin::nvbenchplugin::state::get_state(); \
344 state_->exec([&](nvbench::launch &launch) { hotloop_body(); });
345
349#define GPU_PLUGIN_HOTLOOP_BENCH PLUGIN_HOTLOOP_BENCH
350
351#endif
Argument parser for plugin command-line arguments.
Definition plugin.hpp:107
std::pair< int, char ** > parse(int argc, char **argv)
Definition plugin.hpp:118
Base class for plugins in the ComPPare framework.
Definition plugin.hpp:60
NVBench plugin for the ComPPare framework.
Definition nvbench.hpp:260
comppare::plugin::nvbenchplugin::nvbench_manager nb_
Definition nvbench.hpp:265
nvbench::benchmark_base & register_impl(const std::string &name, Func &&user_fn, const InTup &inputs, OutTup &outs)
Register an implementation with the NVBench plugin.
Definition nvbench.hpp:301
static std::shared_ptr< Self > instance()
Get the singleton instance of the nvbenchPlugin.
Definition nvbench.hpp:279
nvbenchPlugin & operator=(const nvbenchPlugin &)=delete
void initialize(int &argc, char **argv) override
Initialize the NVBench plugin.
Definition nvbench.hpp:322
nvbenchPlugin(const nvbenchPlugin &)=delete
void run() override
Run the registered benchmarks.
Definition nvbench.hpp:332
Manager class for NVBench integration.
Definition nvbench.hpp:119
int nvbench_argc
Definition nvbench.hpp:218
void run()
Run the registered benchmarks.
Definition nvbench.hpp:202
nvbench::benchmark_base & add_nvbench(const char *name, Func f, Args &&...args)
Register a benchmark function with NVBench.
Definition nvbench.hpp:177
char ** nvbench_argv
Definition nvbench.hpp:219
void print_benchmark_header()
Print the benchmark header.
Definition nvbench.hpp:224
void initialize(int &argc, char **argv)
Initialize the NVBench library.
Definition nvbench.hpp:134
comppare::plugin::PluginArgParser nvbench_parser_
Argument parser for NVBench.
Definition nvbench.hpp:221
State class to manage the benchmark state.
Definition nvbench.hpp:81
nvbench::state * st_
Definition nvbench.hpp:108
state & operator=(const state &)=delete
static nvbench::state * get_state()
Definition nvbench.hpp:99
static void set_state(nvbench::state *st)
Definition nvbench.hpp:94
state & operator=(state &&)=delete
static state & instance()
Definition nvbench.hpp:88
Definition nvbench.hpp:45
This file contains the base Plugin class and concept of a valid Plugin class for the ComPPare framewo...
Wrapper for a callable object to be used within NVBench.
Definition nvbench.hpp:53
NvbenchCallable(F _f)
Definition nvbench.hpp:61
NvbenchCallable & operator=(const NvbenchCallable &other)=default
void operator()(nvbench::state &st, nvbench::type_list<>)
Definition nvbench.hpp:56
NvbenchCallable(const NvbenchCallable &other)=default
NvbenchCallable & operator=(NvbenchCallable &&other)=default
NvbenchCallable(NvbenchCallable &&other)=default