12 #include <type_traits>
14 #include <xpti/xpti.hpp>
31 class LaunchBuilderImpl;
32 class LaunchBuilderTestAccess;
246 LaunchBuilder(std::unique_ptr<impl::LaunchBuilderImpl> impl);
248 std::unique_ptr<impl::LaunchBuilderImpl> impl_;
253 std::atomic<bool> started_{
false};
256 friend class impl::LaunchBuilderTestAccess;
353 template <auto Func, typename... Args,
357 const char* kernelName =
nullptr;
359 #if defined(__PXCC_ANALYSIS__)
360 kernelName =
"__pxcc_placeholder__";
363 "Kernel function not found in KernelTraits — was it compiled with pxcc?");
367 std::vector<ArgInfo_t> argList;
368 std::vector<std::shared_ptr<void>> storage;
369 std::vector<std::pair<void*, size_t>> syncTargets;
370 auto directions = detail::MakeKernelDirections<Func>();
371 argList.reserve(
sizeof...(Args));
372 if constexpr (
sizeof...(Args) > 0)
374 buildArgList(argList, storage, syncTargets, std::forward<Args>(args)...);
377 if (impl_ ==
nullptr)
379 initFromArgs(argList, syncTargets);
383 return makeEmptyBuilder();
386 return execute(kernelName, taskCount,
387 std::move(argList), std::move(storage), std::move(syncTargets),
388 std::move(directions));
401 template <
auto Func,
typename... Args,
406 execute<Func>(uint32_t{0}, std::forward<Args>(args)...));
425 template <
auto Func,
typename... Args,
429 if (impl_ ==
nullptr)
431 std::vector<ArgInfo_t> tempArgList;
432 std::vector<std::shared_ptr<void>> tempStorage;
433 std::vector<std::pair<void*, size_t>> tempSyncTargets;
434 tempArgList.
reserve(
sizeof...(Args));
435 if constexpr (
sizeof...(Args) > 0)
437 buildArgList(tempArgList, tempStorage, tempSyncTargets, std::forward<Args>(args)...);
439 initFromArgs(tempArgList, tempSyncTargets);
444 builder.
stage<Func>(taskCount, std::forward<Args>(args)...);
455 template <
auto Func,
typename... Args,
460 stage<Func>(uint32_t{0}, std::forward<Args>(args)...));
499 std::vector<ArgInfo_t> argList,
500 std::vector<std::shared_ptr<void>> storage,
501 std::vector<std::pair<void*, size_t>> syncTargets);
504 std::vector<ArgInfo_t> argList,
505 std::vector<std::shared_ptr<void>> storage,
506 std::vector<std::pair<void*, size_t>> syncTargets,
509 void initFromArgs(
const std::vector<ArgInfo_t>& argList,
510 const std::vector<std::pair<void*, size_t>>& syncTargets);
513 template <
typename T,
typename... Rest>
514 void buildArgList(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
515 std::vector<std::pair<void*, size_t>>& syncTargets, T&& arg, Rest&&... rest)
517 addArg(argList, storage, syncTargets, std::forward<T>(arg));
518 if constexpr (
sizeof...(rest) > 0)
520 buildArgList(argList, storage, syncTargets, std::forward<Rest>(rest)...);
524 template <
typename T>
525 std::enable_if_t<std::is_pointer_v<std::decay_t<T>>>
526 addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>&,
527 std::vector<std::pair<void*, size_t>>&, T&& ptr)
532 template <
typename T>
533 std::enable_if_t<std::is_arithmetic_v<std::decay_t<T>> && !std::is_pointer_v<std::decay_t<T>> &&
534 !std::is_same_v<std::decay_t<T>,
bool>>
535 addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
536 std::vector<std::pair<void*, size_t>>&, T&& value)
538 auto stored = std::make_shared<std::decay_t<T>>(std::forward<T>(value));
539 argList.emplace_back(ArgInfo_t{
ArgType::Constant, stored.get(),
sizeof(std::decay_t<T>)});
540 storage.push_back(stored);
543 template <
typename T>
544 void addArg(std::vector<ArgInfo_t>& argList, std::vector<std::shared_ptr<void>>& storage,
545 std::vector<std::pair<void*, size_t>>& syncTargets,
560 auto storedNdarray = std::make_shared<pxl::NDArray<T>>(ndarrayArg);
562 storage.push_back(storedNdarray);
564 const auto byteSize = ndarrayArg.
size();
565 syncTargets.emplace_back(
const_cast<void*
>(
static_cast<const void*
>(ndarrayArg.
data())),
566 byteSize > 0 ?
static_cast<size_t>(byteSize) : 0);
569 std::shared_ptr<impl::DeviceHandle> impl_;
Configurable kernel execution request.
LaunchBuilder & enableProfile(const xpti::ProfileConfig &config)
Enables profiling for this kernel.
LaunchBuilder(LaunchBuilder &&other) noexcept
LaunchBuilder & clusterBitmap(uint32_t bitmap)
Sets the cluster bitmap (0 = use all clusters).
LaunchBuilder & tasks(uint32_t taskCount)
Sets how many tasks invoke the kernel.
LaunchBuilder & operator=(const LaunchBuilder &)=delete
LaunchBuilder & batchSize(uint32_t n)
Sets per-task batch size for kernel execution. If not called, the runtime uses an implementation-defi...
LaunchBuilder & onError(Map::ErrorCallback cb, void *arg=nullptr)
Registers an error callback for execution failure.
LaunchBuilder & operator=(LaunchBuilder &&other) noexcept
LaunchResult run()
Executes the kernel and waits for completion.
LaunchBuilder & stream(Stream *s)
Routes the kernel through a user-managed stream.
LaunchBuilder & onComplete(Map::CompletionCallback cb, void *arg=nullptr)
Registers a completion callback for successful execution.
LaunchBuilder(const LaunchBuilder &)=delete
LaunchBuilder & reserve(uint32_t n)
Sets the number of Sub resources to reserve (Mid-Level).
LaunchBuilder & locality(LocalityMode mode)
Sets the locality mode for this kernel. If not called, the runtime uses an implementation-defined def...
LaunchBuilder & onMessage(Map::MessageCallback cb, void *arg=nullptr)
Registers a message callback for device-originated messages.
std::future< LaunchResult > runAsync()
Executes the kernel asynchronously.
Execution context for kernel launches on XCENA devices.
uint32_t deviceId() const
Returns the device ID.
StageGroupBuilder stage()
Begins a parallel stage group.
Launcher(uint32_t deviceId)
Constructs a Launcher for an explicit device ID.
StageBuilder stage(uint32_t taskCount, Args &&... args)
Begins a stage chain with a sequential kernel stage.
Launcher(Launcher &&) noexcept
LaunchBuilder execute(uint32_t taskCount, Args &&... args)
Launches a kernel for deferred execution.
Launcher & operator=(const Launcher &)=delete
Launcher(const Launcher &)=delete
detail::TaskCountRequired< LaunchBuilder > execute(Args &&... args)
Launches a kernel with only the kernel's own arguments.
bool isValid() const
Returns whether the launcher was initialized successfully.
detail::TaskCountRequired< StageBuilder > stage(Args &&... args)
Begins a stage chain with only the kernel's own arguments.
Launcher()
Constructs a Launcher with deferred device detection.
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 & stage(uint32_t taskCount, Args &&... args)
StageBuilder & reserve(uint32_t n)
Collects parallel kernels within a single stage group.
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.