|
| 1 | +/* |
| 2 | + * Copyright 2021 NVIDIA Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 with the LLVM exception |
| 5 | + * (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://llvm.org/foundation/relicensing/LICENSE.txt |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <nvbench/nvbench.cuh> |
| 20 | + |
| 21 | +// Grab some testing kernels from NVBench: |
| 22 | +#include <nvbench/test_kernels.cuh> |
| 23 | + |
| 24 | +// Thrust vectors simplify memory management: |
| 25 | +#include <thrust/device_vector.h> |
| 26 | + |
| 27 | +#include <random> |
| 28 | + |
| 29 | +//============================================================================== |
| 30 | +// Multiple parameters: |
| 31 | +// Varies block_size and num_blocks while invoking a naive copy of 256 MiB worth |
| 32 | +// of int32_t. |
| 33 | +void copy_sweep_grid_shape(nvbench::state &state) |
| 34 | +{ |
| 35 | + // Get current parameters: |
| 36 | + const int block_size = static_cast<int>(state.get_int64("BlockSize")); |
| 37 | + const int num_blocks = static_cast<int>(state.get_int64("NumBlocks")); |
| 38 | + |
| 39 | + // Number of int32s in 256 MiB: |
| 40 | + const std::size_t num_values = 256 * 1024 * 1024 / sizeof(nvbench::int32_t); |
| 41 | + |
| 42 | + // Report throughput stats: |
| 43 | + state.add_element_count(num_values); |
| 44 | + state.add_global_memory_reads<nvbench::int32_t>(num_values); |
| 45 | + state.add_global_memory_writes<nvbench::int32_t>(num_values); |
| 46 | + |
| 47 | + // Allocate device memory: |
| 48 | + thrust::device_vector<nvbench::int32_t> in(num_values, 0); |
| 49 | + thrust::device_vector<nvbench::int32_t> out(num_values, 0); |
| 50 | + |
| 51 | + state.exec( |
| 52 | + [block_size, |
| 53 | + num_blocks, |
| 54 | + num_values, |
| 55 | + in_ptr = thrust::raw_pointer_cast(in.data()), |
| 56 | + out_ptr = thrust::raw_pointer_cast(out.data())](nvbench::launch &launch) { |
| 57 | + nvbench::copy_kernel<<<num_blocks, block_size, 0, launch.get_stream()>>>( |
| 58 | + in_ptr, |
| 59 | + out_ptr, |
| 60 | + num_values); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +//============================================================================== |
| 65 | +// Tied iteration space allows you to iterate two or more axes at the same |
| 66 | +// time allowing for sparse exploration of the search space. Can also be used |
| 67 | +// to test the diagonal of a square matrix |
| 68 | +// |
| 69 | +void tied_copy_sweep_grid_shape(nvbench::state &state) |
| 70 | +{ |
| 71 | + copy_sweep_grid_shape(state); |
| 72 | +} |
| 73 | +NVBENCH_BENCH(tied_copy_sweep_grid_shape) |
| 74 | + // Every power of two from 64->1024: |
| 75 | + .add_int64_axis("BlockSize", {32,64,128,256}) |
| 76 | + .add_int64_axis("NumBlocks", {1024,512,256,128}) |
| 77 | + .tie_axes({"BlockSize", "NumBlocks"}); |
| 78 | + |
| 79 | +//============================================================================== |
| 80 | +// under_diag: |
| 81 | +// Custom iterator that only searches the `X` locations of two axi |
| 82 | +// [- - - - X] |
| 83 | +// [- - - X X] |
| 84 | +// [- - X X X] |
| 85 | +// [- X X X X] |
| 86 | +// [X X X X X] |
| 87 | +// |
| 88 | +struct under_diag final : nvbench::user_axis_space |
| 89 | +{ |
| 90 | + under_diag(std::vector<std::size_t> input_indices, |
| 91 | + std::vector<std::size_t> output_indices) |
| 92 | + : nvbench::user_axis_space(std::move(input_indices), std::move(output_indices)) |
| 93 | + {} |
| 94 | + |
| 95 | + mutable std::size_t x_pos = 0; |
| 96 | + mutable std::size_t y_pos = 0; |
| 97 | + mutable std::size_t x_start = 0; |
| 98 | + |
| 99 | + nvbench::detail::axis_space_iterator do_iter(axes_info info) const |
| 100 | + { |
| 101 | + // generate our increment function |
| 102 | + auto adv_func = [&, info](std::size_t &inc_index, |
| 103 | + std::size_t /*len*/) -> bool { |
| 104 | + inc_index++; |
| 105 | + x_pos++; |
| 106 | + if (x_pos == info[0].size) |
| 107 | + { |
| 108 | + x_pos = ++x_start; |
| 109 | + y_pos = x_start; |
| 110 | + return true; |
| 111 | + } |
| 112 | + return false; |
| 113 | + }; |
| 114 | + |
| 115 | + // our update function |
| 116 | + std::vector<std::size_t> locs = m_output_indices; |
| 117 | + auto diag_under = |
| 118 | + [&, locs, info](std::size_t, |
| 119 | + std::vector<nvbench::detail::axis_index> &indices) { |
| 120 | + nvbench::detail::axis_index temp = info[0]; |
| 121 | + temp.index = x_pos; |
| 122 | + indices[locs[0]] = temp; |
| 123 | + |
| 124 | + temp = info[1]; |
| 125 | + temp.index = y_pos; |
| 126 | + indices[locs[1]] = temp; |
| 127 | + }; |
| 128 | + |
| 129 | + const size_t iteration_length = ((info[0].size * (info[1].size + 1)) / 2); |
| 130 | + return nvbench::detail::make_space_iterator(2, |
| 131 | + iteration_length, |
| 132 | + adv_func, |
| 133 | + diag_under); |
| 134 | + } |
| 135 | + |
| 136 | + std::size_t do_size(const axes_info &info) const |
| 137 | + { |
| 138 | + return ((info[0].size * (info[1].size + 1)) / 2); |
| 139 | + } |
| 140 | + |
| 141 | + std::size_t do_valid_count(const axes_info &info) const |
| 142 | + { |
| 143 | + return ((info[0].size * (info[1].size + 1)) / 2); |
| 144 | + } |
| 145 | + |
| 146 | + std::unique_ptr<nvbench::axis_space_base> do_clone() const |
| 147 | + { |
| 148 | + return std::make_unique<under_diag>(*this); |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +void user_copy_sweep_grid_shape(nvbench::state &state) |
| 153 | +{ |
| 154 | + copy_sweep_grid_shape(state); |
| 155 | +} |
| 156 | +NVBENCH_BENCH(user_copy_sweep_grid_shape) |
| 157 | + // Every power of two from 64->1024: |
| 158 | + .add_int64_power_of_two_axis("BlockSize", nvbench::range(6, 10)) |
| 159 | + .add_int64_power_of_two_axis("NumBlocks", nvbench::range(6, 10)) |
| 160 | + .user_iteration_axes({"NumBlocks", "BlockSize"}, |
| 161 | + [](auto... args) |
| 162 | + -> std::unique_ptr<nvbench::axis_space_base> { |
| 163 | + return std::make_unique<under_diag>(args...); |
| 164 | + }); |
| 165 | + |
| 166 | + |
| 167 | +//============================================================================== |
| 168 | +// gauss: |
| 169 | +// Custom iteration space that uses a gauss distribution to |
| 170 | +// sample the points near the middle of the index space |
| 171 | +// |
| 172 | +struct gauss final : nvbench::user_axis_space |
| 173 | +{ |
| 174 | + |
| 175 | + gauss(std::vector<std::size_t> input_indices, |
| 176 | + std::vector<std::size_t> output_indices) |
| 177 | + : nvbench::user_axis_space(std::move(input_indices), std::move(output_indices)) |
| 178 | + {} |
| 179 | + |
| 180 | + nvbench::detail::axis_space_iterator do_iter(axes_info info) const |
| 181 | + { |
| 182 | + const double mid_point = static_cast<double>((info[0].size / 2)); |
| 183 | + |
| 184 | + std::random_device rd{}; |
| 185 | + std::mt19937 gen{rd()}; |
| 186 | + std::normal_distribution<> d{mid_point, 2}; |
| 187 | + |
| 188 | + const size_t iteration_length = info[0].size; |
| 189 | + std::vector<std::size_t> gauss_indices(iteration_length); |
| 190 | + for (auto &g : gauss_indices) |
| 191 | + { |
| 192 | + auto v = std::min(static_cast<double>(info[0].size), d(gen)); |
| 193 | + v = std::max(0.0, v); |
| 194 | + g = static_cast<std::size_t>(v); |
| 195 | + } |
| 196 | + |
| 197 | + // our update function |
| 198 | + std::vector<std::size_t> locs = m_output_indices; |
| 199 | + auto gauss_func = [=](std::size_t index, |
| 200 | + std::vector<nvbench::detail::axis_index> &indices) { |
| 201 | + nvbench::detail::axis_index temp = info[0]; |
| 202 | + temp.index = gauss_indices[index]; |
| 203 | + indices[locs[0]] = temp; |
| 204 | + }; |
| 205 | + |
| 206 | + return nvbench::detail::make_space_iterator(1, |
| 207 | + iteration_length, |
| 208 | + gauss_func); |
| 209 | + } |
| 210 | + |
| 211 | + std::size_t do_size(const axes_info &info) const { return info[0].size; } |
| 212 | + |
| 213 | + std::size_t do_valid_count(const axes_info &info) const |
| 214 | + { |
| 215 | + return info[0].size; |
| 216 | + } |
| 217 | + |
| 218 | + std::unique_ptr<axis_space_base> do_clone() const |
| 219 | + { |
| 220 | + return std::make_unique<gauss>(*this); |
| 221 | + } |
| 222 | +}; |
| 223 | +//============================================================================== |
| 224 | +// Dual parameter sweep: |
| 225 | +void dual_float64_axis(nvbench::state &state) |
| 226 | +{ |
| 227 | + const auto duration_A = state.get_float64("Duration_A"); |
| 228 | + const auto duration_B = state.get_float64("Duration_B"); |
| 229 | + |
| 230 | + state.exec([duration_A, duration_B](nvbench::launch &launch) { |
| 231 | + nvbench::sleep_kernel<<<1, 1, 0, launch.get_stream()>>>(duration_A + |
| 232 | + duration_B); |
| 233 | + }); |
| 234 | +} |
| 235 | +NVBENCH_BENCH(dual_float64_axis) |
| 236 | + .add_float64_axis("Duration_A", nvbench::range(0., 1e-4, 1e-5)) |
| 237 | + .add_float64_axis("Duration_B", nvbench::range(0., 1e-4, 1e-5)) |
| 238 | + .user_iteration_axes({"Duration_A"}, |
| 239 | + [](auto... args) |
| 240 | + -> std::unique_ptr<nvbench::axis_space_base> { |
| 241 | + return std::make_unique<gauss>(args...); |
| 242 | + }) |
| 243 | + .user_iteration_axes({"Duration_B"}, |
| 244 | + [](auto... args) |
| 245 | + -> std::unique_ptr<nvbench::axis_space_base> { |
| 246 | + return std::make_unique<gauss>(args...); |
| 247 | + }); |
0 commit comments