|
| 1 | +/* Copyright 2022 Benjamin Worpitz, Andrea Bocci, Bernhard Manfred Gruber |
| 2 | + * |
| 3 | + * This file is part of alpaka. |
| 4 | + * |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + */ |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <alpaka/alpaka.hpp> |
| 13 | + |
| 14 | +#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) && !BOOST_LANG_CUDA |
| 15 | +# error If ALPAKA_ACC_GPU_CUDA_ENABLED is set, the compiler has to support CUDA! |
| 16 | +#endif |
| 17 | + |
| 18 | +#if defined(ALPAKA_ACC_GPU_HIP_ENABLED) && !BOOST_LANG_HIP |
| 19 | +# error If ALPAKA_ACC_GPU_HIP_ENABLED is set, the compiler has to support HIP! |
| 20 | +#endif |
| 21 | + |
| 22 | +#include <alpaka/test/Check.hpp> |
| 23 | +#include <alpaka/test/queue/Queue.hpp> |
| 24 | + |
| 25 | +#include <catch2/catch.hpp> |
| 26 | + |
| 27 | +#include <string> |
| 28 | +#include <utility> |
| 29 | + |
| 30 | +namespace alpaka::test |
| 31 | +{ |
| 32 | + //! The fixture for executing a kernel on a given accelerator. |
| 33 | + template<typename TAcc> |
| 34 | + class KernelExecutionBenchmarkFixture |
| 35 | + { |
| 36 | + public: |
| 37 | + using Acc = TAcc; |
| 38 | + using Dim = alpaka::Dim<Acc>; |
| 39 | + using Idx = alpaka::Idx<Acc>; |
| 40 | + using DevAcc = Dev<Acc>; |
| 41 | + using PltfAcc = Pltf<DevAcc>; |
| 42 | + using QueueAcc = test::DefaultQueue<DevAcc>; |
| 43 | + using WorkDiv = WorkDivMembers<Dim, Idx>; |
| 44 | + |
| 45 | + KernelExecutionBenchmarkFixture(WorkDiv workDiv) |
| 46 | + : m_devHost(getDevByIdx<PltfCpu>(0u)) |
| 47 | + , m_devAcc(getDevByIdx<PltfAcc>(0u)) |
| 48 | + , m_queue(m_devAcc) |
| 49 | + , m_workDiv(std::move(workDiv)) |
| 50 | + { |
| 51 | + } |
| 52 | + |
| 53 | + template<typename TExtent> |
| 54 | + KernelExecutionBenchmarkFixture(TExtent const& extent) |
| 55 | + : KernelExecutionBenchmarkFixture(getValidWorkDiv<Acc>( |
| 56 | + getDevByIdx<PltfAcc>(0u), |
| 57 | + extent, |
| 58 | + Vec<Dim, Idx>::ones(), |
| 59 | + false, |
| 60 | + GridBlockExtentSubDivRestrictions::Unrestricted)) |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | + template<typename TKernelFnObj, typename... TArgs> |
| 65 | + auto operator()( |
| 66 | + TKernelFnObj const& kernelFnObj, |
| 67 | + std::string const& benchmarkName, |
| 68 | + float& result, |
| 69 | + TArgs&&... args) -> bool |
| 70 | + { |
| 71 | + // Allocate result buffers |
| 72 | + auto bufAccResult = allocBuf<float, Idx>(m_devAcc, static_cast<Idx>(1u)); |
| 73 | + auto bufHostResult = allocBuf<float, Idx>(m_devHost, static_cast<Idx>(1u)); |
| 74 | + |
| 75 | + int numRuns = 0; |
| 76 | + result = 0.0f; |
| 77 | + |
| 78 | + // The following block is executed unknown times during estimation phase, then once per benchmark sample |
| 79 | + BENCHMARK_ADVANCED(std::string(benchmarkName))(Catch::Benchmark::Chronometer meter) |
| 80 | + { |
| 81 | + numRuns++; |
| 82 | + memset(m_queue, bufAccResult, 0.0f); |
| 83 | + wait(m_queue); |
| 84 | + |
| 85 | + // Only the following part is measured as the benchmark part |
| 86 | + meter.measure( |
| 87 | + [&] |
| 88 | + { |
| 89 | + exec<Acc>( |
| 90 | + m_queue, |
| 91 | + m_workDiv, |
| 92 | + kernelFnObj, |
| 93 | + getPtrNative(bufAccResult), |
| 94 | + std::forward<TArgs>(args)...); // run the measured kernel |
| 95 | + wait(m_queue); // wait for the kernel to actually run |
| 96 | + }); |
| 97 | + |
| 98 | + // Copy the result value to the host |
| 99 | + memcpy(m_queue, bufHostResult, bufAccResult); |
| 100 | + wait(m_queue); |
| 101 | + |
| 102 | + auto const resultLocal = *getPtrNative(bufHostResult); |
| 103 | + result += resultLocal; |
| 104 | + return resultLocal; // make sure the benchmark call is not optimized away |
| 105 | + }; |
| 106 | + result /= static_cast<float>(numRuns); |
| 107 | + |
| 108 | + return true; |
| 109 | + // TODO: Can we return the result here and read it from Catch2's REQUIRE or something similar? Or are the |
| 110 | + // returns limited to bools? |
| 111 | + // return result; |
| 112 | + } |
| 113 | + |
| 114 | + protected: |
| 115 | + DevCpu m_devHost; |
| 116 | + DevAcc m_devAcc; |
| 117 | + QueueAcc m_queue; |
| 118 | + WorkDiv m_workDiv; |
| 119 | + }; |
| 120 | +} // namespace alpaka::test |
0 commit comments