alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
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 <chrono>
11#include <type_traits>
12#include <utility>
13
15{
16 //! Measures and returns the runtime in ms of the passed callable.
17 //! \param callable An object with operator().
18 template<typename TCallable>
19 auto measureRunTimeMs(TCallable&& callable) -> std::chrono::milliseconds::rep
20 {
21 auto const start = std::chrono::high_resolution_clock::now();
22 std::forward<TCallable>(callable)();
23 auto const end = std::chrono::high_resolution_clock::now();
24 return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
25 }
26
27 //! \return The run time of the given kernel.
28 template<typename TQueue, typename TTask>
29 auto measureTaskRunTimeMs(TQueue& queue, TTask&& task) -> std::chrono::milliseconds::rep
30 {
31#if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
32 std::cout << "measureKernelRunTime("
33 << " queue: " << core::demangled<TQueue> << " task: " << core::demangled<std::decay_t<TTask>> << ")"
34 << std::endl;
35#endif
36 // Wait for the queue to finish all tasks enqueued prior to the given task.
37 alpaka::wait(queue);
38
39 return measureRunTimeMs(
40 [&]
41 {
42 alpaka::enqueue(queue, std::forward<TTask>(task));
43
44 // Wait for the queue to finish the task execution to measure its run time.
45 alpaka::wait(queue);
46 });
47 }
48} // namespace alpaka::test::integ
auto clipCast(V const &val) -> T
Definition ClipCast.hpp:16
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