alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
Serial.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
11#include "alpaka/core/Dict.hpp"
13#include "alpaka/onAcc/Acc.hpp"
15#include "alpaka/tag.hpp"
16
17#include <cassert>
18#include <tuple>
19#include <type_traits>
20
21namespace alpaka::onHost
22{
23 namespace cpu
24 {
25 template<onHost::concepts::ThreadSpec T_ThreadSpec>
26 struct Serial
27 {
28 using NumThreadsVecType = typename T_ThreadSpec::NumThreadsVecType;
29
30 constexpr Serial(T_ThreadSpec threadBlocking, uint32_t numaIdx, bool setThreadAffinity)
31 : m_threadBlocking{std::move(threadBlocking)}
32 , m_numaIdx{numaIdx}
33 , m_setThreadAffinity{setThreadAffinity}
34 {
35 if(m_threadBlocking.getNumThreads().product() != 1u)
36 {
37 throw std::runtime_error("Thread block extent must be 1.");
38 }
39 }
40
41 void operator()(auto const& kernelBundle, auto const& dict) const
42 {
44 internal::hwloc::setThreadAffinity(m_numaIdx);
45 // copy from num blocks to derive correct index type
46 auto blockIdx = m_threadBlocking.getNumBlocks();
47 constexpr uint32_t simdWidth
50
51 auto const blockLayerEntry = DictEntry{
53 onAcc::cpu::GenericLayer{std::cref(blockIdx), std::cref(m_threadBlocking.getNumBlocks())}};
54 auto const threadLayerEntry = DictEntry{layer::thread, onAcc::cpu::OneLayer<NumThreadsVecType>{}};
55 auto const blockSharedMemEntry = DictEntry{layer::shared, std::ref(blockSharedMem)};
56 auto const blockSyncEntry = DictEntry{action::threadBlockSync, onAcc::cpu::NoOp{}};
57
58 // dynamic shared mem
59 uint32_t blockDynSharedMemBytes = onHost::getDynSharedMemBytes(m_threadBlocking, kernelBundle);
60 auto const blockDynSharedMemEntry = DictEntry{layer::dynShared, std::ref(blockSharedMem)};
61 auto const blockDynSharedMemBytesEntry
62 = DictEntry{object::dynSharedMemBytes, std::ref(blockDynSharedMemBytes)};
63
64 /* Only add dynamic shared memory objects if defined by the user, if not we will get a clean static
65 * assert if the kernel tries to access dynamic shared memory */
66 auto additionalDict = conditionalAppendDict<
67 trait::HasUserDefinedDynSharedMemBytes<T_ThreadSpec, ALPAKA_TYPEOF(kernelBundle)>::value>(
68 dict,
69 Dict{blockDynSharedMemEntry, blockDynSharedMemBytesEntry});
70
71 auto const warpSizeEntry = DictEntry{object::warpSize, std::integral_constant<uint32_t, 1u>{}};
72
73 auto acc = onAcc::Acc(joinDict(
74 Dict{blockLayerEntry, threadLayerEntry, blockSharedMemEntry, blockSyncEntry, warpSizeEntry},
75 additionalDict));
77 blockIdx,
78 m_threadBlocking.getNumBlocks(),
79 [&](auto const&)
80 {
81 kernelBundle(acc);
82 acc[layer::shared].reset();
83 });
84 }
85
86 T_ThreadSpec m_threadBlocking;
87 uint32_t m_numaIdx;
89 };
90 } // namespace cpu
91
92 inline auto makeAcc(
93 alpaka::onHost::concepts::ThreadSpec auto const& threadSpec,
94 uint32_t numaIdx,
95 bool setThreadAffinity) requires std::same_as<ALPAKA_TYPEOF(threadSpec.getExecutor()), exec::CpuSerial>
96 {
97 return cpu::Serial(threadSpec, numaIdx, setThreadAffinity);
98 }
99} // namespace alpaka::onHost
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
Concept to check if a type is a ThreadSpec.
constexpr auto host
Definition Api.hpp:39
constexpr auto block
Definition tag.hpp:259
constexpr auto thread
Definition tag.hpp:253
auto ndLoopIncIdx(TExtentVec &idx, TExtentVec const &extent, TFnObj const &f) -> void
Loops over an n-dimensional iteration index variable calling f(idx, args...) for each iteration....
Definition NdLoop.hpp:73
constexpr WarpSize warpSize
Definition tag.hpp:42
constexpr DeviceKind deviceKind
Definition tag.hpp:30
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
auto makeAcc(alpaka::onHost::concepts::ThreadSpec auto const &threadSpec, uint32_t numaIdx, bool setThreadAffinity)
Definition Serial.hpp:92
constexpr uint32_t getDynSharedMemBytes(T_ThreadSpec spec, T_KernelBundle const &kernelBundle)
Definition trait.hpp:185
constexpr auto conditionalAppendDict(Dict< T_Entries0... > const &dict0, Dict< T_Entries1... > const &dict1)
Definition Dict.hpp:215
consteval uint32_t getArchSimdWidth(concepts::Api auto const api, alpaka::concepts::DeviceKind auto const deviceType)
Get the SIMD width in bytes for an API and device kind combination.
Definition trait.hpp:152
constexpr auto joinDict(Dict< T_Entries0... > const &dict0, Dict< T_Entries1... > const &dict1)
Definition Dict.hpp:204
STL namespace.
void operator()(auto const &kernelBundle, auto const &dict) const
Definition Serial.hpp:41
T_ThreadSpec m_threadBlocking
Definition Serial.hpp:86
typename T_ThreadSpec::NumThreadsVecType NumThreadsVecType
Definition Serial.hpp:28
constexpr Serial(T_ThreadSpec threadBlocking, uint32_t numaIdx, bool setThreadAffinity)
Definition Serial.hpp:30