Kompute
Manager.hpp
1 // SPDX-License-Identifier: Apache-2.0
2 #pragma once
3 
4 #include <set>
5 #include <unordered_map>
6 
7 #include "kompute/Core.hpp"
8 
9 #include "kompute/Sequence.hpp"
10 #include "logger/Logger.hpp"
11 
12 #define KP_DEFAULT_SESSION "DEFAULT"
13 
14 namespace kp {
15 
19 class Manager
20 {
21  public:
27 
38  Manager(uint32_t physicalDeviceIndex,
39  const std::vector<uint32_t>& familyQueueIndices = {},
40  const std::vector<std::string>& desiredExtensions = {});
41 
51  Manager(std::shared_ptr<vk::Instance> instance,
52  std::shared_ptr<vk::PhysicalDevice> physicalDevice,
53  std::shared_ptr<vk::Device> device);
54 
60 
70  std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0,
71  uint32_t totalTimestamps = 0);
72 
81  template<typename T>
82  std::shared_ptr<TensorT<T>> tensorT(
83  const std::vector<T>& data,
85  {
86  KP_LOG_DEBUG("Kompute Manager tensor creation triggered");
87 
88  std::shared_ptr<TensorT<T>> tensor{ new kp::TensorT<T>(
89  this->mPhysicalDevice, this->mDevice, data, tensorType) };
90 
91  if (this->mManageResources) {
92  this->mManagedTensors.push_back(tensor);
93  }
94 
95  return tensor;
96  }
97 
98  std::shared_ptr<TensorT<float>> tensor(
99  const std::vector<float>& data,
101  {
102  return this->tensorT<float>(data, tensorType);
103  }
104 
105  std::shared_ptr<Tensor> tensor(
106  void* data,
107  uint32_t elementTotalCount,
108  uint32_t elementMemorySize,
109  const Tensor::TensorDataTypes& dataType,
111  {
112  std::shared_ptr<Tensor> tensor{ new kp::Tensor(this->mPhysicalDevice,
113  this->mDevice,
114  data,
115  elementTotalCount,
116  elementMemorySize,
117  dataType,
118  tensorType) };
119 
120  if (this->mManageResources) {
121  this->mManagedTensors.push_back(tensor);
122  }
123 
124  return tensor;
125  }
126 
142  std::shared_ptr<Algorithm> algorithm(
143  const std::vector<std::shared_ptr<Tensor>>& tensors = {},
144  const std::vector<uint32_t>& spirv = {},
145  const Workgroup& workgroup = {},
146  const std::vector<float>& specializationConstants = {},
147  const std::vector<float>& pushConstants = {})
148  {
149  return this->algorithm<>(
150  tensors, spirv, workgroup, specializationConstants, pushConstants);
151  }
152 
167  template<typename S = float, typename P = float>
168  std::shared_ptr<Algorithm> algorithm(
169  const std::vector<std::shared_ptr<Tensor>>& tensors,
170  const std::vector<uint32_t>& spirv,
171  const Workgroup& workgroup,
172  const std::vector<S>& specializationConstants,
173  const std::vector<P>& pushConstants)
174  {
175 
176  KP_LOG_DEBUG("Kompute Manager algorithm creation triggered");
177 
178  std::shared_ptr<Algorithm> algorithm{ new kp::Algorithm(
179  this->mDevice,
180  tensors,
181  spirv,
182  workgroup,
183  specializationConstants,
184  pushConstants) };
185 
186  if (this->mManageResources) {
187  this->mManagedAlgorithms.push_back(algorithm);
188  }
189 
190  return algorithm;
191  }
192 
196  void destroy();
201  void clear();
202 
209  vk::PhysicalDeviceProperties getDeviceProperties() const;
210 
216  std::vector<vk::PhysicalDevice> listDevices() const;
217 
224  std::shared_ptr<vk::Instance> getVkInstance() const;
225 
226  private:
227  // -------------- OPTIONALLY OWNED RESOURCES
228  std::shared_ptr<vk::Instance> mInstance = nullptr;
229  bool mFreeInstance = false;
230  std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice = nullptr;
231  std::shared_ptr<vk::Device> mDevice = nullptr;
232  bool mFreeDevice = false;
233 
234  // -------------- ALWAYS OWNED RESOURCES
235  std::vector<std::weak_ptr<Tensor>> mManagedTensors;
236  std::vector<std::weak_ptr<Sequence>> mManagedSequences;
237  std::vector<std::weak_ptr<Algorithm>> mManagedAlgorithms;
238 
239  std::vector<uint32_t> mComputeQueueFamilyIndices;
240  std::vector<std::shared_ptr<vk::Queue>> mComputeQueues;
241 
242  bool mManageResources = false;
243 
244 #ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS
245  vk::DebugReportCallbackEXT mDebugReportCallback;
246  vk::DispatchLoaderDynamic mDebugDispatcher;
247 #endif
248 
249  // Create functions
250  void createInstance();
251  void createDevice(const std::vector<uint32_t>& familyQueueIndices = {},
252  uint32_t hysicalDeviceIndex = 0,
253  const std::vector<std::string>& desiredExtensions = {});
254 };
255 
256 } // End namespace kp
Definition: Algorithm.hpp:17
Definition: Manager.hpp:20
std::shared_ptr< Algorithm > algorithm(const std::vector< std::shared_ptr< Tensor >> &tensors, const std::vector< uint32_t > &spirv, const Workgroup &workgroup, const std::vector< S > &specializationConstants, const std::vector< P > &pushConstants)
Definition: Manager.hpp:168
Manager(uint32_t physicalDeviceIndex, const std::vector< uint32_t > &familyQueueIndices={}, const std::vector< std::string > &desiredExtensions={})
vk::PhysicalDeviceProperties getDeviceProperties() const
std::vector< vk::PhysicalDevice > listDevices() const
std::shared_ptr< Sequence > sequence(uint32_t queueIndex=0, uint32_t totalTimestamps=0)
void destroy()
std::shared_ptr< vk::Instance > getVkInstance() const
void clear()
std::shared_ptr< Algorithm > algorithm(const std::vector< std::shared_ptr< Tensor >> &tensors={}, const std::vector< uint32_t > &spirv={}, const Workgroup &workgroup={}, const std::vector< float > &specializationConstants={}, const std::vector< float > &pushConstants={})
Definition: Manager.hpp:142
std::shared_ptr< TensorT< T > > tensorT(const std::vector< T > &data, Tensor::TensorTypes tensorType=Tensor::TensorTypes::eDevice)
Definition: Manager.hpp:82
Manager(std::shared_ptr< vk::Instance > instance, std::shared_ptr< vk::PhysicalDevice > physicalDevice, std::shared_ptr< vk::Device > device)
Definition: Tensor.hpp:300
Definition: Tensor.hpp:20
TensorTypes
Definition: Tensor.hpp:29
@ eDevice
Type is device memory, source and destination.