alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
KernelExecutionFixture.hpp
Go to the documentation of this file.
1/* Copyright 2024 Benjamin Worpitz, Andrea Bocci, Bernhard Manfred Gruber, Jan Stephan, Aurora Perego
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/alpaka.hpp"
8
9#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) && !ALPAKA_LANG_CUDA
10# error If ALPAKA_ACC_GPU_CUDA_ENABLED is set, the compiler has to support CUDA!
11#endif
12
13#if defined(ALPAKA_ACC_GPU_HIP_ENABLED) && !ALPAKA_LANG_HIP
14# error If ALPAKA_ACC_GPU_HIP_ENABLED is set, the compiler has to support HIP!
15#endif
16
17#include "alpaka/test/Check.hpp"
19
20#include <utility>
21
22namespace alpaka::test
23{
24 //! The fixture for executing a kernel on a given accelerator.
25 template<typename TAcc>
27 {
28 public:
29 using Acc = TAcc;
37
38 KernelExecutionFixture(WorkDiv workDiv) : m_queue{m_device}, m_workDiv{std::move(workDiv)}
39 {
40 }
41
42 template<typename TExtent>
43 KernelExecutionFixture(TExtent const& extent) : m_queue{m_device}
44 , m_extent{extent}
45 {
46 }
47
49 : m_platform{} // if the platform is not stateless, this is wrong; we ignore it because it is not be used
50 , m_device{alpaka::getDev(queue)}
51 , m_queue{std::move(queue)}
52 , m_workDiv{std::move(workDiv)}
53 {
54 }
55
56 template<typename TExtent>
57 KernelExecutionFixture(Queue queue, TExtent const& extent)
58 : m_platform{} // if the platform is not stateless, this is wrong; we ignore it because it is not be used
59 , m_device{alpaka::getDev(queue)}
60 , m_queue{std::move(queue)}
61 , m_extent{extent}
62 {
63 }
64
65 template<typename TKernelFnObj, typename... TArgs>
66 auto operator()(TKernelFnObj kernelFnObj, TArgs&&... args) -> bool
67 {
68 // Allocate the result value
69 auto bufAccResult = allocBuf<bool, Idx>(m_device, static_cast<Idx>(1u));
70 memset(m_queue, bufAccResult, static_cast<std::uint8_t>(true));
71
72
73 alpaka::KernelCfg<Acc> const kernelCfg = {m_extent, Vec<Dim, Idx>::ones()};
74
75 // set workdiv if it is not before
77 m_workDiv = alpaka::getValidWorkDiv(
78 kernelCfg,
79 m_device,
80 kernelFnObj,
81 getPtrNative(bufAccResult),
82 std::forward<TArgs>(args)...);
83
84 exec<Tag>(m_queue, m_workDiv, kernelFnObj, getPtrNative(bufAccResult), std::forward<TArgs>(args)...);
85
86 // Copy the result value to the host
87 auto bufHostResult = allocBuf<bool, Idx>(m_devHost, static_cast<Idx>(1u));
88 memcpy(m_queue, bufHostResult, bufAccResult);
89 wait(m_queue);
90
91 auto const result = *getPtrNative(bufHostResult);
92
93 return result;
94 }
95
96 private:
97 PlatformCpu m_platformHost{};
98 DevCpu m_devHost{getDevByIdx(m_platformHost, 0)};
99 Platform m_platform{};
100 Device m_device{getDevByIdx(m_platform, 0)};
101 Queue m_queue;
103 Vec<Dim, Idx> m_extent;
104 };
105
106} // namespace alpaka::test
The CPU device handle.
Definition DevCpu.hpp:56
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto ones() -> Vec< TDim, TVal >
One value constructor.
Definition Vec.hpp:106
ALPAKA_NO_HOST_ACC_WARNING static ALPAKA_FN_HOST_ACC constexpr auto all(TVal const &val) -> Vec< TDim, TVal >
Single value constructor.
Definition Vec.hpp:89
The fixture for executing a kernel on a given accelerator.
KernelExecutionFixture(Queue queue, WorkDiv workDiv)
KernelExecutionFixture(Queue queue, TExtent const &extent)
auto operator()(TKernelFnObj kernelFnObj, TArgs &&... args) -> bool
The test specifics.
Definition TestAccs.hpp:27
typename trait::DefaultQueueType< TDev >::type DefaultQueue
The queue type that should be used for the given device.
Definition Queue.hpp:108
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition Traits.hpp:29
ALPAKA_FN_HOST auto memcpy(TQueue &queue, alpaka::detail::DevGlobalImplGeneric< TTag, TTypeDst > &viewDst, TViewSrc const &viewSrc) -> void
ALPAKA_FN_HOST auto getValidWorkDiv(KernelCfg< TAcc, TGridElemExtent, TThreadElemExtent > const &kernelCfg, TDev const &dev, TKernelFnObj const &kernelFnObj, TArgs &&... args) -> WorkDivMembers< Dim< TAcc >, Idx< TAcc > >
typename trait::DevType< T >::type Dev
The device type trait alias template to remove the ::type.
Definition Traits.hpp:56
ALPAKA_FN_HOST auto getPtrNative(TView const &view) -> Elem< TView > const *
Gets the native pointer of the memory view.
Definition Traits.hpp:139
ALPAKA_FN_HOST auto memset(TQueue &queue, TViewFwd &&view, std::uint8_t const &byte, TExtent const &extent) -> void
Sets the bytes of the memory of view, described by extent, to the given value.
Definition Traits.hpp:252
ALPAKA_FN_HOST auto getDevByIdx(TPlatform const &platform, std::size_t const &devIdx) -> Dev< TPlatform >
Definition Traits.hpp:62
ALPAKA_FN_HOST auto getDev(T const &t)
Definition Traits.hpp:68
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
typename trait::PlatformType< T >::type Platform
The platform type trait alias template to remove the ::type.
Definition Traits.hpp:51
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition Traits.hpp:19
typename trait::AccToTag< TAcc >::type AccToTag
maps an acc type to a tag type
Definition Tag.hpp:67
STL namespace.
Kernel start configuration to determine a valid work division.
The CPU device platform.