13 #include <type_traits>
15 #include <xpti/xpti.hpp>
55 struct [[gnu::abi_tag("xpti_" XPTI_ABI_TAG)]] KnobConfig
57 std::optional<uint32_t> batchSize;
58 std::optional<uint32_t> clusterBitmap;
59 std::optional<pxl::LocalityMode> locality;
61 std::optional<pxl::Map::CompletionCallback> onComplete;
62 void* onCompleteArg =
nullptr;
63 std::optional<pxl::Map::MessageCallback> onMessage;
64 void* onMessageArg =
nullptr;
65 std::optional<pxl::Map::ErrorCallback> onError;
66 void* onErrorArg =
nullptr;
74 std::optional<pxl::Stream*> stream;
75 std::optional<xpti::ProfileConfig> profileConfig;
78 struct [[gnu::abi_tag("xpti_" XPTI_ABI_TAG)]] StageEntry
80 std::string kernelName;
81 uint32_t taskCount = 0;
82 std::vector<ArgInfo_t> argList;
83 std::vector<std::shared_ptr<void>> constStorage;
84 std::vector<std::pair<void*, size_t>> syncTargets;
88 struct StageDirectionMetadata
90 ArgDirections directions;
93 void operator()(
void*)
const noexcept
98 inline void attachStageDirections(StageEntry& entry, ArgDirections directions)
100 if (directions.args.empty())
104 entry.constStorage.emplace_back(
nullptr, StageDirectionMetadata{std::move(directions)});
107 inline const ArgDirections& stageDirections(
const StageEntry& entry)
109 static const ArgDirections LegacyDirections;
110 for (
auto it = entry.constStorage.rbegin(); it != entry.constStorage.rend(); ++it)
112 if (
const auto* metadata = std::get_deleter<StageDirectionMetadata>(*it))
114 return metadata->directions;
117 return LegacyDirections;
120 struct [[gnu::abi_tag("xpti_" XPTI_ABI_TAG)]] StageGroup
122 std::vector<StageEntry> entries;
127 class [[gnu::abi_tag("xpti_" XPTI_ABI_TAG)]] StageGroupBuilder;
165 template <auto Func, typename... Args,
169 const char* kernelName =
nullptr;
170 #if defined(__PXCC_ANALYSIS__)
171 kernelName =
"__pxcc_placeholder__";
174 "Kernel function not found in KernelTraits — was it compiled with pxcc?");
178 impl::StageEntry entry;
179 entry.kernelName = kernelName;
180 entry.taskCount = taskCount;
181 entry.argList.reserve(
sizeof...(Args));
182 if constexpr (
sizeof...(Args) > 0)
184 buildArgList(entry.argList, entry.constStorage, entry.syncTargets, std::forward<Args>(args)...);
186 impl::attachStageDirections(entry, detail::MakeKernelDirections<Func>());
188 impl::StageGroup group;
189 group.entries.push_back(std::move(entry));
190 stages_.push_back(std::move(group));
200 template <
auto Func,
typename... Args,
204 template <
auto Func,
typename... Args,
252 template <
typename T,
typename... Rest>
253 void buildArgList(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
254 std::vector<std::pair<void*, size_t>>& syncTargets, T&& arg, Rest&&... rest)
256 addArg(argList, storage, syncTargets, std::forward<T>(arg));
257 if constexpr (
sizeof...(rest) > 0)
259 buildArgList(argList, storage, syncTargets, std::forward<Rest>(rest)...);
263 template <
typename T>
264 std::enable_if_t<std::is_pointer_v<std::decay_t<T>>>
265 addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>&,
266 std::vector<std::pair<void*, size_t>>&, T&& ptr)
271 template <
typename T>
272 std::enable_if_t<std::is_arithmetic_v<std::decay_t<T>> && !std::is_pointer_v<std::decay_t<T>> &&
273 !std::is_same_v<std::decay_t<T>,
bool>>
274 addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
275 std::vector<std::pair<void*, size_t>>&, T&& value)
277 auto stored = std::make_shared<std::decay_t<T>>(std::forward<T>(value));
278 argList.emplace_back(ArgInfo_t{
ArgType::Constant, stored.get(),
sizeof(std::decay_t<T>)});
279 storage.push_back(stored);
282 template <
typename T>
283 void addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
284 std::vector<std::pair<void*, size_t>>& syncTargets,
293 auto storedNdarray = std::make_shared<pxl::NDArray<T>>(ndarrayArg);
295 storage.push_back(storedNdarray);
296 const auto byteSize = ndarrayArg.
size();
297 syncTargets.emplace_back(
const_cast<void*
>(
static_cast<const void*
>(ndarrayArg.
data())),
298 byteSize > 0 ?
static_cast<size_t>(byteSize) : 0);
301 std::shared_ptr<impl::DeviceHandle> device_;
302 std::vector<impl::StageGroup> stages_;
303 uint32_t reserveCount_ = 0;
307 std::atomic<bool> started_{
false};
309 StageGroupBuilder takeStageGroup() &&;
335 template <
auto Func,
typename... Args,
339 const char* kernelName =
nullptr;
340 #if defined(__PXCC_ANALYSIS__)
341 kernelName =
"__pxcc_placeholder__";
344 "Kernel function not found in KernelTraits — was it compiled with pxcc?");
348 impl::StageEntry entry;
349 entry.kernelName = kernelName;
350 entry.taskCount = taskCount;
351 entry.argList.reserve(
sizeof...(Args));
352 if constexpr (
sizeof...(Args) > 0)
354 parent_.buildArgList(entry.argList, entry.constStorage, entry.syncTargets,
355 std::forward<Args>(args)...);
357 impl::attachStageDirections(entry, detail::MakeKernelDirections<Func>());
358 currentGroup_.entries.push_back(std::move(entry));
367 template <
auto Func,
typename... Args,
371 template <
auto Func,
typename... Args,
378 template <
auto Func,
typename... Args,
383 return parent_.stage<Func>(taskCount, std::forward<Args>(args)...);
388 template <
auto Func,
typename... Args,
392 template <
auto Func,
typename... Args,
399 return parent_.stage();
405 return parent_.run();
411 return parent_.runAsync();
468 impl::StageGroup currentGroup_;
469 bool flushed_ = false;
474 friend class detail::TaskCountConfigured;
489 return StageGroupBuilder(std::make_unique<StageBuilder>(std::move(parent_)));
492 template <
auto Func,
typename... Args,
496 stage<Func>(uint32_t{0}, std::forward<Args>(args)...);
500 template <
auto Func,
typename... Args,
504 add<Func>(uint32_t{0}, std::forward<Args>(args)...);
508 template <
auto Func,
typename... Args,
512 auto& parent = stage<Func>(uint32_t{0}, std::forward<Args>(args)...);
Execution context for kernel launches on XCENA devices.
std::function< void(void *message, void *arg)> MessageCallback
Callback function type for task message.
std::function< void(void *arg)> ErrorCallback
Callback function type for task error.
std::function< void(void *arg)> CompletionCallback
Callback function type for task completion.
NDArray is a class that can be used in both host and device code. NDArray is automatically divided in...
std::int64_t size() const
Builds a chain of kernel stages for sequential/parallel execution.
StageBuilder(StageBuilder &&) noexcept
StageBuilder & onMessage(Map::MessageCallback cb, void *arg=nullptr)
Registers a message callback on the most-recently-added stage.
StageBuilder & onError(Map::ErrorCallback cb, void *arg=nullptr)
Registers an error callback on the most-recently-added stage.
StageBuilder & batchSize(uint32_t n)
Sets per-task batch size for the most-recently-added stage. Affects only the last entry added via sta...
detail::TaskCountRequired< StageBuilder > stage(Args &&... args) &=delete
StageBuilder & reserve(uint32_t n)
StageBuilder(std::shared_ptr< impl::DeviceHandle > device)
StageBuilder & locality(LocalityMode mode)
Sets locality mode for the most-recently-added stage.
detail::TaskCountRequired< StageBuilder > stage(Args &&... args) &&
StageBuilder & stream(Stream *s)
Routes the most-recently-added stage through a user-managed stream.
StageBuilder & clusterBitmap(uint32_t bitmap)
Sets cluster bitmap for the most-recently-added stage. A bit beyond the device's clusters per Sub fai...
std::future< LaunchResult > runAsync()
StageBuilder & onComplete(Map::CompletionCallback cb, void *arg=nullptr)
Registers a completion callback on the most-recently-added stage.
StageBuilder & enableProfile(const xpti::ProfileConfig &config)
Enables profiling for the most-recently-added stage. enableProfiling failures are logged but not prop...
StageBuilder & tasks(uint32_t taskCount)
StageGroupBuilder stage()
Collects parallel kernels within a single stage group.
StageGroupBuilder & add(uint32_t taskCount, Args &&... args)
StageGroupBuilder & onComplete(Map::CompletionCallback cb, void *arg=nullptr)
Registers a completion callback on the most-recently-added kernel.
std::future< LaunchResult > runAsync()
StageGroupBuilder & reserve(uint32_t n)
StageGroupBuilder(StageGroupBuilder &&) noexcept
StageGroupBuilder & clusterBitmap(uint32_t bitmap)
Sets cluster bitmap for the most-recently-added kernel in the active group. A bit beyond the device's...
StageGroupBuilder & onError(Map::ErrorCallback cb, void *arg=nullptr)
Registers an error callback on the most-recently-added kernel.
StageGroupBuilder stage()
detail::TaskCountRequired< StageBuilder > stage(Args &&... args) &=delete
detail::TaskCountRequired< StageGroupBuilder > add(Args &&... args) &=delete
StageGroupBuilder & tasks(uint32_t taskCount)
Sets the task count of the most recently added kernel in this group.
StageGroupBuilder & stream(Stream *s)
Routes the most-recently-added kernel through a user-managed stream.
StageBuilder & stage(uint32_t taskCount, Args &&... args)
StageGroupBuilder & locality(LocalityMode mode)
Sets locality mode for the most-recently-added kernel in the active group.
StageGroupBuilder & onMessage(Map::MessageCallback cb, void *arg=nullptr)
Registers a message callback on the most-recently-added kernel.
detail::TaskCountRequired< StageGroupBuilder > add(Args &&... args) &&
StageGroupBuilder & batchSize(uint32_t n)
Sets per-task batch size for the most-recently-added kernel within the active parallel group....
detail::TaskCountRequired< StageBuilder > stage(Args &&... args) &&
StageGroupBuilder & enableProfile(const xpti::ProfileConfig &config)
Enables profiling for the most-recently-added kernel. enableProfiling failures are logged but not pro...
An asynchronous work queue for device operations.
Holds a launch request that still needs its task count.
std::enable_if_t< KernelArgumentsCompatibleV< Func, Actual... >, int > EnableCompatibleKernelArguments
Traits class that maps a kernel function pointer to its name string.