-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathcpu_only.cu
More file actions
83 lines (72 loc) · 2.8 KB
/
cpu_only.cu
File metadata and controls
83 lines (72 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright 2025 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 with the LLVM exception
* (the "License"); you may not use this file except in compliance with
* the License.
*
* You may obtain a copy of the License at
*
* http://llvm.org/foundation/relicensing/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <nvbench/nvbench.cuh>
#include <chrono>
#include <thread>
// Block execution of the current CPU thread for `seconds` seconds.
void sleep_host(double seconds)
{
std::this_thread::sleep_for(
std::chrono::milliseconds(static_cast<nvbench::int64_t>(seconds * 1000)));
}
//=============================================================================
// Simple CPU-only benchmark that sleeps on host for a specified duration.
void simple(nvbench::state &state)
{
const auto duration = state.get_float64("Duration");
state.exec([duration](nvbench::launch &) { sleep_host(duration); });
}
NVBENCH_BENCH(simple)
// 100 -> 500 ms in 100 ms increments.
.add_float64_axis("Duration", nvbench::range(.1, .5, .1))
// Mark as CPU-only.
.set_is_cpu_only(true);
//=============================================================================
// Simple CPU-only benchmark that sleeps on host for a specified duration and
// uses a custom timed region.
void simple_timer(nvbench::state &state)
{
const auto duration = state.get_float64("Duration");
state.exec(nvbench::exec_tag::timer, [duration](nvbench::launch &, auto &timer) {
// Do any setup work before starting the timer here...
timer.start();
// The region of code to be timed:
sleep_host(duration);
timer.stop();
// Any per-run cleanup here...
});
}
NVBENCH_BENCH(simple_timer)
// 100 -> 500 ms in 100 ms increments.
.add_float64_axis("Duration", nvbench::range(.1, .5, .1))
// Mark as CPU-only.
.set_is_cpu_only(true);
//=============================================================================
// Simple CPU-only benchmark that uses the optional `nvbench::exec_tag::no_gpu`
// hint to prevent GPU measurement code from being instantiated. Note that
// `set_is_cpu_only(true)` is still required when using this hint.
void simple_no_gpu(nvbench::state &state)
{
const auto duration = state.get_float64("Duration");
state.exec(nvbench::exec_tag::no_gpu, [duration](nvbench::launch &) { sleep_host(duration); });
}
NVBENCH_BENCH(simple_no_gpu)
// 100 -> 500 ms in 100 ms increments.
.add_float64_axis("Duration", nvbench::range(.1, .5, .1))
// Mark as CPU-only.
.set_is_cpu_only(true);