alpaka
Abstraction Library for Parallel Kernel Acceleration
MeasureKernelRunTime.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Benjamin Worpitz, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/alpaka.hpp"
9 
10 #include <type_traits>
11 #include <utility>
12 
14 {
15  //! Measures and returns the runtime in ms of the passed callable.
16  //! \param callable An object with operator().
17  template<typename TCallable>
18  auto measureRunTimeMs(TCallable&& callable) -> std::chrono::milliseconds::rep
19  {
20  auto const start = std::chrono::high_resolution_clock::now();
21  std::forward<TCallable>(callable)();
22  auto const end = std::chrono::high_resolution_clock::now();
23  return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
24  }
25 
26  //! \return The run time of the given kernel.
27  template<typename TQueue, typename TTask>
28  auto measureTaskRunTimeMs(TQueue& queue, TTask&& task) -> std::chrono::milliseconds::rep
29  {
30 #if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
31  std::cout << "measureKernelRunTime("
32  << " queue: " << core::demangled<TQueue> << " task: " << core::demangled<std::decay_t<TTask>> << ")"
33  << std::endl;
34 #endif
35  // Wait for the queue to finish all tasks enqueued prior to the given task.
36  alpaka::wait(queue);
37 
38  return measureRunTimeMs(
39  [&]
40  {
41  alpaka::enqueue(queue, std::forward<TTask>(task));
42 
43  // Wait for the queue to finish the task execution to measure its run time.
44  alpaka::wait(queue);
45  });
46  }
47 } // namespace alpaka::test::integ
auto measureTaskRunTimeMs(TQueue &queue, TTask &&task) -> std::chrono::milliseconds::rep
auto measureRunTimeMs(TCallable &&callable) -> std::chrono::milliseconds::rep
Measures and returns the runtime in ms of the passed callable.
ALPAKA_FN_HOST auto end(TView &view) -> Iterator< TView >
Definition: Iterator.hpp:139
ALPAKA_FN_HOST auto enqueue(TQueue &queue, TTask &&task) -> void
Queues the given task in the given queue.
Definition: Traits.hpp:47
ALPAKA_FN_HOST auto wait(TAwaited const &awaited) -> void
Waits the thread for the completion of the given awaited action to complete.
Definition: Traits.hpp:34