Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit stream benchmarking support #76

Merged
merged 18 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/benchmarks.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ void my_benchmark(nvbench::state& state) {
NVBENCH_BENCH(my_benchmark);
```

The following example shows how to benchmark functions that do not expose stream parameters:
PointKernel marked this conversation as resolved.
Show resolved Hide resolved
```cpp
void my_benchmark(nvbench::state& state) {
state.set_cuda_stream(nvbench::cuda_stream{cudaStreamDefault, false});
PointKernel marked this conversation as resolved.
Show resolved Hide resolved
state.exec([](nvbench::launch&) {
my_func(); // a host API invoking GPU kernels without taking an explicit stream
my_kernel<<<num_blocks, 256>>>(); // or a kernel launched with the default stream
});
}
NVBENCH_BENCH(my_benchmark);
```

There are three main components in the definition of a benchmark:

- A `KernelGenerator` callable (`my_benchmark` above)
Expand Down
58 changes: 53 additions & 5 deletions nvbench/cuda_stream.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand Down Expand Up @@ -28,19 +28,67 @@ namespace nvbench
// RAII wrapper for a cudaStream_t.
struct cuda_stream
{
cuda_stream() { NVBENCH_CUDA_CALL(cudaStreamCreate(&m_stream)); }
~cuda_stream() { NVBENCH_CUDA_CALL(cudaStreamDestroy(m_stream)); }
cuda_stream()
: m_owning{true}
{
NVBENCH_CUDA_CALL(cudaStreamCreate(&m_stream));
}
PointKernel marked this conversation as resolved.
Show resolved Hide resolved

cuda_stream(cudaStream_t stream, bool owning)
: m_stream{stream}
, m_owning{owning}
{}
PointKernel marked this conversation as resolved.
Show resolved Hide resolved
alliepiper marked this conversation as resolved.
Show resolved Hide resolved

// destroy the stream if it's owning
void destroy()
{
if (m_owning)
{
NVBENCH_CUDA_CALL_NOEXCEPT(cudaStreamDestroy(m_stream));
}
}

~cuda_stream() { destroy(); }
PointKernel marked this conversation as resolved.
Show resolved Hide resolved

// move-only
cuda_stream(const cuda_stream &) = delete;
cuda_stream(cuda_stream &&) = default;
cuda_stream &operator=(const cuda_stream &) = delete;
cuda_stream &operator=(cuda_stream &&) = default;

cuda_stream(cuda_stream &&other)
: m_stream{other.get_stream()}
, m_owning{other.get_owning()}
{
if (m_owning)
{
other.set_owning(not m_owning);
}
other.destroy();
}

cuda_stream &operator=(cuda_stream &&other)
{
m_stream = other.get_stream();
m_owning = other.get_owning();

if (m_owning)
{
other.set_owning(not m_owning);
}
other.destroy();

return *this;
}
PointKernel marked this conversation as resolved.
Show resolved Hide resolved

operator cudaStream_t() const { return m_stream; }

cudaStream_t get_stream() const { return m_stream; }

[[nodiscard]] bool get_owning() const { return m_owning; }
void set_owning(bool b) { m_owning = b; }

PointKernel marked this conversation as resolved.
Show resolved Hide resolved
private:
cudaStream_t m_stream;
bool m_owning;
PointKernel marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace nvbench
3 changes: 2 additions & 1 deletion nvbench/detail/measure_cold.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand Down Expand Up @@ -39,6 +39,7 @@ namespace nvbench::detail

measure_cold_base::measure_cold_base(state &exec_state)
: m_state{exec_state}
, m_launch{m_state.get_cuda_stream()}
, m_run_once{exec_state.get_run_once()}
, m_min_samples{exec_state.get_min_samples()}
, m_max_noise{exec_state.get_max_noise()}
Expand Down
9 changes: 7 additions & 2 deletions nvbench/detail/measure_cupti.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand Down Expand Up @@ -169,7 +169,12 @@ std::vector<std::string> add_metrics(nvbench::state &state)
} // namespace

measure_cupti_base::measure_cupti_base(state &exec_state)
try : m_state{exec_state}, m_cupti(*m_state.get_device(), add_metrics(m_state))
try : m_state
{
exec_state
}
, m_launch{m_state.get_cuda_stream()},
m_cupti{*m_state.get_device(), add_metrics(m_state)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. Understandably, clang-format is not a fan of initializer-scope try statements. I'll clean this up a bit in my follow up patch.

{}
catch (const std::exception &ex)
{
Expand Down
3 changes: 2 additions & 1 deletion nvbench/detail/measure_hot.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand Down Expand Up @@ -37,6 +37,7 @@ namespace nvbench::detail

measure_hot_base::measure_hot_base(state &exec_state)
: m_state{exec_state}
, m_launch{m_state.get_cuda_stream()}
, m_min_samples{exec_state.get_min_samples()}
, m_min_time{exec_state.get_min_time()}
, m_skip_time{exec_state.get_skip_time()}
Expand Down
9 changes: 6 additions & 3 deletions nvbench/launch.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand All @@ -25,8 +25,11 @@ namespace nvbench

struct launch
{
explicit launch(const nvbench::cuda_stream &stream)
: m_stream{stream}
{}

// move-only
launch() = default;
launch(const launch &) = delete;
launch(launch &&) = default;
launch &operator=(const launch &) = delete;
Expand All @@ -38,7 +41,7 @@ struct launch
};

private:
nvbench::cuda_stream m_stream;
const nvbench::cuda_stream &m_stream;
};

} // namespace nvbench
21 changes: 15 additions & 6 deletions nvbench/state.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand All @@ -18,6 +18,7 @@

#pragma once

#include <nvbench/cuda_stream.cuh>
#include <nvbench/device_info.cuh>
#include <nvbench/exec_tag.cuh>
#include <nvbench/named_values.cuh>
Expand Down Expand Up @@ -62,6 +63,15 @@ struct state
state &operator=(const state &) = delete;
state &operator=(state &&) = default;

[[nodiscard]] const nvbench::cuda_stream &get_cuda_stream() const
{
return m_cuda_stream;
}
void set_cuda_stream(nvbench::cuda_stream &&stream)
{
m_cuda_stream = std::move(stream);
}

/// The CUDA device associated with with this benchmark state. May be
/// nullopt for CPU-only benchmarks.
[[nodiscard]] const std::optional<nvbench::device_info> &get_device() const
Expand Down Expand Up @@ -259,11 +269,9 @@ struct state

[[nodiscard]] bool is_cupti_required() const
{
return is_l2_hit_rate_collected()
|| is_l1_hit_rate_collected()
|| is_stores_efficiency_collected()
|| is_loads_efficiency_collected()
|| is_dram_throughput_collected();
return is_l2_hit_rate_collected() || is_l1_hit_rate_collected() ||
is_stores_efficiency_collected() ||
is_loads_efficiency_collected() || is_dram_throughput_collected();
}

summary &add_summary(std::string summary_tag);
Expand Down Expand Up @@ -303,6 +311,7 @@ private:
std::optional<nvbench::device_info> device,
std::size_t type_config_index);

nvbench::cuda_stream m_cuda_stream;
std::reference_wrapper<const nvbench::benchmark_base> m_benchmark;
nvbench::named_values m_axis_values;
std::optional<nvbench::device_info> m_device;
Expand Down
20 changes: 19 additions & 1 deletion testing/state.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 NVIDIA Corporation
* Copyright 2021-2022 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
Expand Down Expand Up @@ -51,6 +51,23 @@ struct state_tester : public nvbench::state

using nvbench::detail::state_tester;

void test_streams()
{
dummy_bench bench;

state_tester state{bench};

// Test non-owning stream
state.set_cuda_stream(nvbench::cuda_stream{cudaStreamDefault, false});
ASSERT(state.get_cuda_stream() == cudaStreamDefault);

// Test owning stream
auto stream = nvbench::cuda_stream{};
auto gold = stream.get_stream();
state.set_cuda_stream(std::move(stream));
ASSERT(state.get_cuda_stream() == gold);
}

void test_params()
{
dummy_bench bench;
Expand Down Expand Up @@ -110,6 +127,7 @@ void test_defaults()

int main()
{
test_streams();
test_params();
test_summaries();
test_defaults();
Expand Down