|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * @license |
| 4 | + * Copyright (C) Codeplay Software Limited |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * For your convenience, a copy of the License has been included in this |
| 12 | + * repository. |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + * |
| 20 | + * portBLAS: BLAS implementation using SYCL |
| 21 | + * |
| 22 | + * @filename axpy_batch.cpp |
| 23 | + * |
| 24 | + **************************************************************************/ |
| 25 | + |
| 26 | +#include "../utils.hpp" |
| 27 | + |
| 28 | +constexpr blas_benchmark::utils::ExtensionOp benchmark_op = |
| 29 | + blas_benchmark::utils::ExtensionOp::axpy_batch; |
| 30 | + |
| 31 | +template <typename scalar_t, blas::helper::AllocType mem_alloc> |
| 32 | +void run(benchmark::State& state, blas::SB_Handle* sb_handle_ptr, index_t size, |
| 33 | + scalar_t alpha, index_t inc_x, index_t inc_y, index_t stride_x_mul, |
| 34 | + index_t stride_y_mul, index_t batch_size, bool* success) { |
| 35 | + // initialize the state label |
| 36 | + blas_benchmark::utils::set_benchmark_label<scalar_t>( |
| 37 | + state, sb_handle_ptr->get_queue()); |
| 38 | + |
| 39 | + // Google-benchmark counters are double. |
| 40 | + blas_benchmark::utils::init_extension_counters<benchmark_op, scalar_t>( |
| 41 | + state, size, batch_size); |
| 42 | + |
| 43 | + blas::SB_Handle& sb_handle = *sb_handle_ptr; |
| 44 | + auto q = sb_handle.get_queue(); |
| 45 | + |
| 46 | + const auto stride_x{size * std::abs(inc_x) * stride_x_mul}; |
| 47 | + const auto stride_y{size * std::abs(inc_y) * stride_y_mul}; |
| 48 | + |
| 49 | + const index_t size_x{stride_x * batch_size}; |
| 50 | + const index_t size_y{stride_y * batch_size}; |
| 51 | + // Create data |
| 52 | + std::vector<scalar_t> vx = |
| 53 | + blas_benchmark::utils::random_data<scalar_t>(size_x); |
| 54 | + std::vector<scalar_t> vy = |
| 55 | + blas_benchmark::utils::random_data<scalar_t>(size_y); |
| 56 | + |
| 57 | + auto inx = blas::helper::allocate<mem_alloc, scalar_t>(size_x, q); |
| 58 | + auto iny = blas::helper::allocate<mem_alloc, scalar_t>(size_y, q); |
| 59 | + |
| 60 | + auto copy_x = |
| 61 | + blas::helper::copy_to_device<scalar_t>(q, vx.data(), inx, size_x); |
| 62 | + auto copy_y = |
| 63 | + blas::helper::copy_to_device<scalar_t>(q, vy.data(), iny, size_y); |
| 64 | + |
| 65 | + sb_handle.wait({copy_x, copy_y}); |
| 66 | + |
| 67 | +#ifdef BLAS_VERIFY_BENCHMARK |
| 68 | + // Run a first time with a verification of the results |
| 69 | + std::vector<scalar_t> y_ref = vy; |
| 70 | + for (auto i = 0; i < batch_size; ++i) { |
| 71 | + reference_blas::axpy(size, static_cast<scalar_t>(alpha), |
| 72 | + vx.data() + i * stride_x, inc_x, |
| 73 | + y_ref.data() + i * stride_y, inc_y); |
| 74 | + } |
| 75 | + std::vector<scalar_t> y_temp = vy; |
| 76 | + { |
| 77 | + auto y_temp_gpu = blas::helper::allocate<mem_alloc, scalar_t>(size_y, q); |
| 78 | + auto copy_temp = blas::helper::copy_to_device<scalar_t>(q, y_temp.data(), |
| 79 | + y_temp_gpu, size_y); |
| 80 | + sb_handle.wait(copy_temp); |
| 81 | + auto axpy_batch_event = |
| 82 | + _axpy_batch(sb_handle, size, alpha, inx, inc_x, stride_x, y_temp_gpu, |
| 83 | + inc_y, stride_y, batch_size); |
| 84 | + sb_handle.wait(axpy_batch_event); |
| 85 | + auto copy_output = |
| 86 | + blas::helper::copy_to_host(q, y_temp_gpu, y_temp.data(), size_y); |
| 87 | + sb_handle.wait(copy_output); |
| 88 | + |
| 89 | + blas::helper::deallocate<mem_alloc>(y_temp_gpu, q); |
| 90 | + } |
| 91 | + |
| 92 | + std::ostringstream err_stream; |
| 93 | + if (!utils::compare_vectors(y_temp, y_ref, err_stream, "")) { |
| 94 | + const std::string& err_str = err_stream.str(); |
| 95 | + state.SkipWithError(err_str.c_str()); |
| 96 | + *success = false; |
| 97 | + }; |
| 98 | +#endif |
| 99 | + |
| 100 | + auto blas_method_def = [&]() -> std::vector<cl::sycl::event> { |
| 101 | + auto event = _axpy_batch(sb_handle, size, alpha, inx, inc_x, stride_x, iny, |
| 102 | + inc_y, stride_y, batch_size); |
| 103 | + sb_handle.wait(event); |
| 104 | + return event; |
| 105 | + }; |
| 106 | + |
| 107 | + // Warmup |
| 108 | + blas_benchmark::utils::warmup(blas_method_def); |
| 109 | + sb_handle.wait(); |
| 110 | + |
| 111 | + blas_benchmark::utils::init_counters(state); |
| 112 | + |
| 113 | + // Measure |
| 114 | + for (auto _ : state) { |
| 115 | + // Run |
| 116 | + std::tuple<double, double> times = |
| 117 | + blas_benchmark::utils::timef(blas_method_def); |
| 118 | + |
| 119 | + // Report |
| 120 | + blas_benchmark::utils::update_counters(state, times); |
| 121 | + } |
| 122 | + |
| 123 | + state.SetItemsProcessed(state.iterations() * state.counters["n_fl_ops"]); |
| 124 | + state.SetBytesProcessed(state.iterations() * |
| 125 | + state.counters["bytes_processed"]); |
| 126 | + |
| 127 | + blas_benchmark::utils::calc_avg_counters(state); |
| 128 | + |
| 129 | + blas::helper::deallocate<mem_alloc>(inx, q); |
| 130 | + blas::helper::deallocate<mem_alloc>(iny, q); |
| 131 | +} |
| 132 | + |
| 133 | +template <typename scalar_t, blas::helper::AllocType mem_alloc> |
| 134 | +void register_benchmark(blas::SB_Handle* sb_handle_ptr, bool* success, |
| 135 | + std::string mem_type, |
| 136 | + std::vector<axpy_batch_param_t<scalar_t>> params) { |
| 137 | + for (auto p : params) { |
| 138 | + index_t n, inc_x, inc_y, stride_x_mul, stride_y_mul, batch_size; |
| 139 | + scalar_t alpha; |
| 140 | + std::tie(n, alpha, inc_x, inc_y, stride_x_mul, stride_y_mul, batch_size) = |
| 141 | + p; |
| 142 | + auto BM_lambda = [&](benchmark::State& st, blas::SB_Handle* sb_handle_ptr, |
| 143 | + index_t size, scalar_t alpha, index_t inc_x, |
| 144 | + index_t inc_y, index_t stride_x_mul, |
| 145 | + index_t stride_y_mul, index_t batch_size, |
| 146 | + bool* success) { |
| 147 | + run<scalar_t, mem_alloc>(st, sb_handle_ptr, size, alpha, inc_x, inc_y, |
| 148 | + stride_x_mul, stride_y_mul, batch_size, success); |
| 149 | + }; |
| 150 | + benchmark::RegisterBenchmark( |
| 151 | + blas_benchmark::utils::get_name<benchmark_op, scalar_t, index_t>( |
| 152 | + n, alpha, inc_x, inc_y, stride_x_mul, stride_y_mul, batch_size, |
| 153 | + mem_type) |
| 154 | + .c_str(), |
| 155 | + BM_lambda, sb_handle_ptr, n, alpha, inc_x, inc_y, stride_x_mul, |
| 156 | + stride_y_mul, batch_size, success) |
| 157 | + ->UseRealTime(); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +template <typename scalar_t> |
| 162 | +void register_benchmark(blas_benchmark::Args& args, |
| 163 | + blas::SB_Handle* sb_handle_ptr, bool* success) { |
| 164 | + auto axpy_batch_params = |
| 165 | + blas_benchmark::utils::get_axpy_batch_params<scalar_t>(args); |
| 166 | + |
| 167 | + register_benchmark<scalar_t, blas::helper::AllocType::buffer>( |
| 168 | + sb_handle_ptr, success, blas_benchmark::utils::MEM_TYPE_BUFFER, |
| 169 | + axpy_batch_params); |
| 170 | +#ifdef SB_ENABLE_USM |
| 171 | + register_benchmark<scalar_t, blas::helper::AllocType::usm>( |
| 172 | + sb_handle_ptr, success, blas_benchmark::utils::MEM_TYPE_USM, |
| 173 | + axpy_batch_params); |
| 174 | +#endif |
| 175 | +} |
| 176 | + |
| 177 | +namespace blas_benchmark { |
| 178 | +void create_benchmark(blas_benchmark::Args& args, |
| 179 | + blas::SB_Handle* sb_handle_ptr, bool* success) { |
| 180 | + BLAS_REGISTER_BENCHMARK(args, sb_handle_ptr, success); |
| 181 | +} |
| 182 | +} // namespace blas_benchmark |
0 commit comments