PXL
map.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright 2024 XCENA Inc.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <cstdint>
8 #include <functional>
9 #include <memory>
10 #include <pxl/v3_api_guard.hpp>
11 #include <type_traits>
12 #include <vector>
13 #include <xpti/xpti.hpp>
14 
15 #include "pxl/ndarray.hpp"
16 #include "pxl/type.hpp"
17 
18 namespace xpti
19 {
20 class Profile;
21 }
22 
23 namespace pxl
24 {
25 class Function;
26 class Job;
27 class Stream;
28 
29 namespace impl
30 {
31 class MapFactory;
32 class MapImpl;
33 } // namespace impl
34 
49 class Map
50 {
51 public:
52  friend class impl::MapFactory;
53 
61  using CompletionCallback = std::function<void(void* arg)>;
62 
71  using MessageCallback = std::function<void(void* message, void* arg)>;
72 
80  using ErrorCallback = std::function<void(void* arg)>;
81 
99  void setCompletionCallback(const CompletionCallback& callback, void* arg);
100 
116  void setMessageCallback(const MessageCallback& callback, void* arg);
117 
135  void setErrorCallback(const ErrorCallback& callback, void* arg);
136 
142  void setBatchSize(const uint32_t& batchSize);
143 
176  uint32_t getBatchSize() const;
177 
190  uint32_t getTaskCount() const;
191 
201  void setClusterBitmap(const uint32_t& clusterBitmap);
202 
208  void setLocalityMode(const LocalityMode& mode);
209 
225  void setStream();
226 
242  void setStream(Stream* stream);
243 
249  uint32_t streamId() const;
250 
252  {
254  }
255 
296  template <typename... ARGS>
297  Result execute(ARGS&&... args)
298  {
299  std::vector<ArgInfo_t> argInfos;
300  if (buildArgInfo(argInfos, std::forward<ARGS>(args)...) == false)
301  {
303  }
304 
305  return execute(argInfos);
306  }
307 
345  Result execute(std::vector<ArgInfo_t>& args);
346 
360  template <typename... ARG>
361  Result setInput(ARG&&... args)
362  {
363  std::vector<ArgInfo_t> argInfos;
364  if (buildArgInfo(argInfos, std::forward<ARG>(args)...) == false)
365  {
367  }
368  return setInput(argInfos);
369  }
370 
384  template <typename... ARG>
385  Result setOutput(ARG&&... args)
386  {
387  std::vector<ArgInfo_t> argInfos;
388  if (buildArgInfo(argInfos, std::forward<ARG>(args)...) == false)
389  {
391  }
392  return setOutput(argInfos);
393  }
394 
410  Result setInput(std::vector<ArgInfo_t>& args);
411 
427  Result setOutput(std::vector<ArgInfo_t>& args);
428 
491 
535 
565 
588 
612  std::vector<KernelError_t> getKernelError();
613 
630  std::string getStats() const;
631 
641  uint64_t id() const;
642 
646  uint32_t deviceId() const;
647 
674  Result enableProfiling(const xpti::ProfileConfig& config);
675 
699  template <xpti::QueryKey Key>
700  xpti::SessionData<Key> getProfilingData(const xpti::QueryFilter& filter = {}) const
701  {
702  // Use this map's id() as sessionId
703  return xpti::getSessionData<Key>(id(), filter);
704  }
705 
727  Result exportProfiling(const std::string& path, xpti::ExportFormat format) const;
728 
736  Result exportProfilingCsv(const std::string& path = "") const;
737 
738 protected:
742  virtual ~Map() = default;
743 
744 private:
745  Map(std::unique_ptr<impl::MapImpl> impl);
746 
747  template <typename ARG, typename... ARGS>
748  typename std::enable_if<std::is_trivial<typename std::remove_reference<ARG>::type>::value, bool>::type
749  buildArgInfo(std::vector<ArgInfo_t>& argInfos, ARG&& arg, ARGS&&... args)
750  {
751  argInfos.emplace_back(ArgInfo_t{ArgType::Constant, &arg, sizeof(ARG)});
752  if constexpr (sizeof...(args) > 0)
753  {
754  return buildArgInfo(argInfos, std::forward<ARGS>(args)...);
755  }
756  return true;
757  }
758 
759  template <typename ARG, typename... ARGS>
760  typename std::enable_if<!std::is_trivial<typename std::remove_reference<ARG>::type>::value, bool>::type
761  buildArgInfo([[maybe_unused]] std::vector<ArgInfo_t>& argInfos, [[maybe_unused]] ARG&& arg, [[maybe_unused]] ARGS&&... args)
762  {
763  return false;
764  }
765 
766  template <typename ARG, typename... ARGS>
767  bool buildArgInfo(std::vector<ArgInfo_t>& argInfos, NDArray<ARG>& ndarrayArg, ARGS&&... args)
768  {
769  argInfos.emplace_back(ArgInfo_t{ArgType::NDArray, &ndarrayArg, sizeof(NDArray<ARG>)});
770  if constexpr (sizeof...(args) > 0)
771  {
772  return buildArgInfo(argInfos, std::forward<ARGS>(args)...);
773  }
774  return true;
775  }
776 
777  template <typename ARG, typename... ARGS>
778  bool buildArgInfo(std::vector<ArgInfo_t>& argInfos, NDArray<ARG>&& ndarrayArg, ARGS&&... args) = delete;
779 
780  template <typename ARG, typename... ARGS>
781  bool buildArgInfo(std::vector<ArgInfo_t>& argInfos, ARG* arg, ARGS&&... args)
782  {
783  argInfos.emplace_back(ArgInfo_t{ArgType::DeviceMemory, arg, sizeof(void*)});
784  if constexpr (sizeof...(args) > 0)
785  {
786  return buildArgInfo(argInfos, std::forward<ARGS>(args)...);
787  }
788  return true;
789  }
790 
791  std::unique_ptr<impl::MapImpl> impl_;
792 };
793 
794 } // namespace pxl
The Map class represents a map operation in the XCENA execution framework.
Definition: map.hpp:50
void setStream()
Set the default stream for the map operation.
void setErrorCallback(const ErrorCallback &callback, void *arg)
Set the error callback function and user data.
Result execute(ARGS &&... args)
Execute the map operation. This method executes the map operation with the provided input arguments.
Definition: map.hpp:297
std::function< void(void *message, void *arg)> MessageCallback
Callback function type for task message.
Definition: map.hpp:71
void setBatchSize(const uint32_t &batchSize)
Sets batchSize for the task.
friend class impl::MapFactory
Definition: map.hpp:52
uint32_t deviceId() const
Returns the device id this Map runs on.
Result execute()
Definition: map.hpp:251
void setMessageCallback(const MessageCallback &callback, void *arg)
Set the message callback function and user data.
Result setOutput(ARG &&... args)
Set the output for the map operation. This method sets the output for the map operation.
Definition: map.hpp:385
Result setOutput(std::vector< ArgInfo_t > &args)
Set the output for the map operation. This method sets the output for the map operation.
uint64_t id() const
Gets ID of map object.
Result exportProfilingCsv(const std::string &path="") const
Export profiling data to CSV files (convenience wrapper).
Result cancel()
Request cancellation of the map operation and wait until it stops.
std::function< void(void *arg)> ErrorCallback
Callback function type for task error.
Definition: map.hpp:80
std::function< void(void *arg)> CompletionCallback
Callback function type for task completion.
Definition: map.hpp:61
ExecuteStatus getExecuteStatus() const
Gets the execution status of the map operation.
Result enableProfiling(const xpti::ProfileConfig &config)
Enable profiling for this map with the given configuration.
Progress_t getProgress() const
Get dispatch/completion progress for the current or last run.
virtual ~Map()=default
Default virtual destructor.
Result exportProfiling(const std::string &path, xpti::ExportFormat format) const
Export profiling data in the specified format.
uint32_t getTaskCount() const
Returns the task count this map was built with.
Result setInput(std::vector< ArgInfo_t > &args)
Set the input for the map operation. This method sets the input for the map operation.
void setClusterBitmap(const uint32_t &clusterBitmap)
Set the cluster bitmap for the map operation.
void setCompletionCallback(const CompletionCallback &callback, void *arg)
Set the success callback function and user data.
Result execute(std::vector< ArgInfo_t > &args)
Execute the map operation with a vector of argument information. This method executes the map operati...
std::string getStats() const
Gets the execution statistics of the map operation.
xpti::SessionData< Key > getProfilingData(const xpti::QueryFilter &filter={}) const
Get profiling data with type-safe automatic type deduction.
Definition: map.hpp:700
uint32_t getBatchSize() const
Returns the batch size the next execution will use.
void setStream(Stream *stream)
Set the stream for the map operation.
std::vector< KernelError_t > getKernelError()
Gets the failed task ranges of the last execute.
void setLocalityMode(const LocalityMode &mode)
Set the locality mode for the map operation.
Result synchronize()
Synchronize the map operation. This method synchronizes the map operation, ensuring that all tasks ar...
uint32_t streamId() const
Returns the stream ID that the map operation is associated with.
Result setInput(ARG &&... args)
Set the input for the map operation. This method sets the input for the map operation.
Definition: map.hpp:361
An asynchronous work queue for device operations.
Definition: stream.hpp:26
Definition: config.hpp:13
Result
Definition: type.hpp:70
xpti::Profile Profile
Definition: profile.hpp:32
LocalityMode
Definition: type.hpp:107
ExecuteStatus
Definition: type.hpp:14
Definition: map.hpp:19