Kompute
Loading...
Searching...
No Matches
Manager.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "kompute/Core.hpp"
5
6#include "kompute/Image.hpp"
7#include "kompute/Sequence.hpp"
8#include "logger/Logger.hpp"
9
10#define KP_DEFAULT_SESSION "DEFAULT"
11
12namespace kp {
13
18{
19 public:
25
36 Manager(uint32_t physicalDeviceIndex,
37 const std::vector<uint32_t>& familyQueueIndices = {},
38 const std::vector<std::string>& desiredExtensions = {});
39
49 Manager(std::shared_ptr<vk::Instance> instance,
50 std::shared_ptr<vk::PhysicalDevice> physicalDevice,
51 std::shared_ptr<vk::Device> device);
52
53
58 Manager(const Manager&) = delete;
59 Manager(const Manager&&) = delete;
60 Manager& operator=(const Manager&) = delete;
61 Manager& operator=(const Manager&&) = delete;
62
68
78 std::shared_ptr<Sequence> sequence(uint32_t queueIndex = 0,
79 uint32_t totalTimestamps = 0);
80
89 template<typename T>
90 std::shared_ptr<TensorT<T>> tensorT(
91 const std::vector<T>& data,
93 {
94 KP_LOG_DEBUG("Kompute Manager tensor creation triggered");
95
96 std::shared_ptr<TensorT<T>> tensor{ new kp::TensorT<T>(
97 this->mPhysicalDevice, this->mDevice, data, tensorType) };
98
99 if (this->mManageResources) {
100 this->mManagedMemObjects.push_back(tensor);
101 }
102
103 return tensor;
104 }
105
114 template<typename T>
115 std::shared_ptr<TensorT<T>> tensorT(
116 size_t size,
118 {
119 KP_LOG_DEBUG("Kompute Manager tensor creation triggered");
120
121 std::shared_ptr<TensorT<T>> tensor{ new kp::TensorT<T>(
122 this->mPhysicalDevice, this->mDevice, size, tensorType) };
123
124 if (this->mManageResources) {
125 this->mManagedMemObjects.push_back(tensor);
126 }
127
128 return tensor;
129 }
130 std::shared_ptr<TensorT<float>> tensor(
131 const std::vector<float>& data,
133 {
134 return this->tensorT<float>(data, tensorType);
135 }
136
137 std::shared_ptr<Tensor> tensor(
138 void* data,
139 uint32_t elementTotalCount,
140 uint32_t elementMemorySize,
141 const Memory::DataTypes& dataType,
143 {
144 std::shared_ptr<Tensor> tensor{ new kp::Tensor(this->mPhysicalDevice,
145 this->mDevice,
146 data,
147 elementTotalCount,
148 elementMemorySize,
149 dataType,
150 tensorType) };
151
152 if (this->mManageResources) {
153 this->mManagedMemObjects.push_back(tensor);
154 }
155
156 return tensor;
157 }
158
159 std::shared_ptr<Tensor> tensor(
160 uint32_t elementTotalCount,
161 uint32_t elementMemorySize,
162 const Memory::DataTypes& dataType,
164 {
165 std::shared_ptr<Tensor> tensor{ new kp::Tensor(this->mPhysicalDevice,
166 this->mDevice,
167 elementTotalCount,
168 elementMemorySize,
169 dataType,
170 tensorType) };
171
172 if (this->mManageResources) {
173 this->mManagedMemObjects.push_back(tensor);
174 }
175
176 return tensor;
177 }
178
187 template<typename T>
188 std::shared_ptr<ImageT<T>> imageT(
189 const std::vector<T>& data,
190 uint32_t width,
191 uint32_t height,
192 uint32_t numChannels,
193 vk::ImageTiling tiling,
195 {
196 KP_LOG_DEBUG("Kompute Manager image creation triggered");
197
198 std::shared_ptr<ImageT<T>> image{ new kp::ImageT<T>(
199 this->mPhysicalDevice,
200 this->mDevice,
201 data,
202 width,
203 height,
204 numChannels,
205 tiling,
206 imageType) };
207
208 if (this->mManageResources) {
209 this->mManagedMemObjects.push_back(image);
210 }
211
212 return image;
213 }
214
215 template<typename T>
216 std::shared_ptr<ImageT<T>> imageT(
217 const std::vector<T>& data,
218 uint32_t width,
219 uint32_t height,
220 uint32_t numChannels,
222 {
223 KP_LOG_DEBUG("Kompute Manager image creation triggered");
224
225 std::shared_ptr<ImageT<T>> image{ new kp::ImageT<T>(
226 this->mPhysicalDevice,
227 this->mDevice,
228 data,
229 width,
230 height,
231 numChannels,
232 imageType) };
233
234 if (this->mManageResources) {
235 this->mManagedMemObjects.push_back(image);
236 }
237
238 return image;
239 }
240
241 template<typename T>
242 std::shared_ptr<ImageT<T>> imageT(
243 uint32_t width,
244 uint32_t height,
245 uint32_t numChannels,
246 vk::ImageTiling tiling,
248 {
249 KP_LOG_DEBUG("Kompute Manager image creation triggered");
250
251 std::shared_ptr<ImageT<T>> image{ new kp::ImageT<T>(
252 this->mPhysicalDevice,
253 this->mDevice,
254 width,
255 height,
256 numChannels,
257 tiling,
258 imageType) };
259
260 if (this->mManageResources) {
261 this->mManagedMemObjects.push_back(image);
262 }
263
264 return image;
265 }
266
267 template<typename T>
268 std::shared_ptr<ImageT<T>> imageT(
269 uint32_t width,
270 uint32_t height,
271 uint32_t numChannels,
273 {
274 KP_LOG_DEBUG("Kompute Manager image creation triggered");
275
276 std::shared_ptr<ImageT<T>> image{ new kp::ImageT<T>(
277 this->mPhysicalDevice,
278 this->mDevice,
279 width,
280 height,
281 numChannels,
282 imageType) };
283
284 if (this->mManageResources) {
285 this->mManagedMemObjects.push_back(image);
286 }
287
288 return image;
289 }
290 std::shared_ptr<ImageT<float>> image(
291 const std::vector<float>& data,
292 uint32_t width,
293 uint32_t height,
294 uint32_t numChannels,
295 vk::ImageTiling tiling,
297 {
298 return this->imageT<float>(
299 data, width, height, numChannels, tiling, imageType);
300 }
301
302 std::shared_ptr<ImageT<float>> image(
303 const std::vector<float>& data,
304 uint32_t width,
305 uint32_t height,
306 uint32_t numChannels,
308 {
309 return this->imageT<float>(data, width, height, numChannels, imageType);
310 }
311
312 std::shared_ptr<ImageT<float>> image(
313 uint32_t width,
314 uint32_t height,
315 uint32_t numChannels,
316 vk::ImageTiling tiling,
318 {
319 return this->imageT<float>(
320 width, height, numChannels, tiling, imageType);
321 }
322
323 std::shared_ptr<ImageT<float>> image(
324 uint32_t width,
325 uint32_t height,
326 uint32_t numChannels,
328 {
329 return this->imageT<float>(width, height, numChannels, imageType);
330 }
331
332 std::shared_ptr<Image> image(
333 void* data,
334 size_t dataSize,
335 uint32_t width,
336 uint32_t height,
337 uint32_t numChannels,
338 const Image::DataTypes& dataType,
339 vk::ImageTiling tiling,
341 {
342 std::shared_ptr<Image> image{ new kp::Image(this->mPhysicalDevice,
343 this->mDevice,
344 data,
345 dataSize,
346 width,
347 height,
348 numChannels,
349 dataType,
350 tiling,
351 imageType) };
352
353 if (this->mManageResources) {
354 this->mManagedMemObjects.push_back(image);
355 }
356
357 return image;
358 }
359
360 std::shared_ptr<Image> image(
361 void* data,
362 size_t dataSize,
363 uint32_t width,
364 uint32_t height,
365 uint32_t numChannels,
366 const Memory::DataTypes& dataType,
368 {
369 std::shared_ptr<Image> image{ new kp::Image(this->mPhysicalDevice,
370 this->mDevice,
371 data,
372 dataSize,
373 width,
374 height,
375 numChannels,
376 dataType,
377 imageType) };
378
379 if (this->mManageResources) {
380 this->mManagedMemObjects.push_back(image);
381 }
382
383 return image;
384 }
385
386 std::shared_ptr<Image> image(
387 uint32_t width,
388 uint32_t height,
389 uint32_t numChannels,
390 const Memory::DataTypes& dataType,
391 vk::ImageTiling tiling,
393 {
394 std::shared_ptr<Image> image{ new kp::Image(this->mPhysicalDevice,
395 this->mDevice,
396 width,
397 height,
398 numChannels,
399 dataType,
400 tiling,
401 imageType) };
402
403 if (this->mManageResources) {
404 this->mManagedMemObjects.push_back(image);
405 }
406
407 return image;
408 }
409
410 std::shared_ptr<Image> image(
411 uint32_t width,
412 uint32_t height,
413 uint32_t numChannels,
414 const Memory::DataTypes& dataType,
416 {
417 std::shared_ptr<Image> image{ new kp::Image(this->mPhysicalDevice,
418 this->mDevice,
419 width,
420 height,
421 numChannels,
422 dataType,
423 imageType) };
424
425 if (this->mManageResources) {
426 this->mManagedMemObjects.push_back(image);
427 }
428
429 return image;
430 }
431
448 std::shared_ptr<Algorithm> algorithm(
449 const std::vector<std::shared_ptr<Memory>>& memObjects = {},
450 const std::vector<uint32_t>& spirv = {},
451 const Workgroup& workgroup = {},
452 const std::vector<float>& specializationConstants = {},
453 const std::vector<float>& pushConstants = {})
454 {
455 return this->algorithm<>(
456 memObjects, spirv, workgroup, specializationConstants, pushConstants);
457 }
458
474 template<typename S = float, typename P = float>
475 std::shared_ptr<Algorithm> algorithm(
476 const std::vector<std::shared_ptr<Memory>>& memObjects,
477 const std::vector<uint32_t>& spirv,
478 const Workgroup& workgroup,
479 const std::vector<S>& specializationConstants,
480 const std::vector<P>& pushConstants)
481 {
482
483 KP_LOG_DEBUG("Kompute Manager algorithm creation triggered");
484
485 std::shared_ptr<Algorithm> algorithm{ new kp::Algorithm(
486 this->mDevice,
487 memObjects,
488 spirv,
489 workgroup,
490 specializationConstants,
491 pushConstants) };
492
493 if (this->mManageResources) {
494 this->mManagedAlgorithms.push_back(algorithm);
495 }
496
497 return algorithm;
498 }
499
503 void destroy();
508 void clear();
509
516 vk::PhysicalDeviceProperties getDeviceProperties() const;
517
523 std::vector<vk::PhysicalDevice> listDevices() const;
524
531 std::shared_ptr<vk::Instance> getVkInstance() const;
532
533 private:
534 // -------------- OPTIONALLY OWNED RESOURCES
535 std::shared_ptr<vk::Instance> mInstance = nullptr;
536 bool mFreeInstance = false;
537 std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice = nullptr;
538 std::shared_ptr<vk::Device> mDevice = nullptr;
539 bool mFreeDevice = false;
540
541 // -------------- ALWAYS OWNED RESOURCES
542 std::vector<std::weak_ptr<Memory>> mManagedMemObjects;
543 std::vector<std::weak_ptr<Sequence>> mManagedSequences;
544 std::vector<std::weak_ptr<Algorithm>> mManagedAlgorithms;
545
546 std::vector<uint32_t> mComputeQueueFamilyIndices;
547 std::vector<std::shared_ptr<vk::Queue>> mComputeQueues;
548
549 bool mManageResources = false;
550
551#ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS
552 vk::DebugReportCallbackEXT mDebugReportCallback;
553#ifdef VK_VERSION_1_4
554 vk::detail::DispatchLoaderDynamic mDebugDispatcher;
555#else
556 vk::DispatchLoaderDynamic mDebugDispatcher;
557#endif // VK_VERSION_1_4
558#endif // KOMPUTE_DISABLE_VK_DEBUG_LAYERS
559
560 // Create functions
561 void createInstance();
562 void createDevice(const std::vector<uint32_t>& familyQueueIndices = {},
563 uint32_t hysicalDeviceIndex = 0,
564 const std::vector<std::string>& desiredExtensions = {});
565};
566
567} // End namespace kp
Definition Algorithm.hpp:23
Definition Image.hpp:398
Definition Image.hpp:21
Definition Manager.hpp:18
Manager(uint32_t physicalDeviceIndex, const std::vector< uint32_t > &familyQueueIndices={}, const std::vector< std::string > &desiredExtensions={})
std::shared_ptr< Algorithm > algorithm(const std::vector< std::shared_ptr< Memory > > &memObjects={}, const std::vector< uint32_t > &spirv={}, const Workgroup &workgroup={}, const std::vector< float > &specializationConstants={}, const std::vector< float > &pushConstants={})
Definition Manager.hpp:448
std::vector< vk::PhysicalDevice > listDevices() const
Manager(const Manager &)=delete
Make Manager uncopyable.
vk::PhysicalDeviceProperties getDeviceProperties() const
std::shared_ptr< ImageT< T > > imageT(const std::vector< T > &data, uint32_t width, uint32_t height, uint32_t numChannels, vk::ImageTiling tiling, Memory::MemoryTypes imageType=Memory::MemoryTypes::eDevice)
Definition Manager.hpp:188
std::shared_ptr< Algorithm > algorithm(const std::vector< std::shared_ptr< Memory > > &memObjects, const std::vector< uint32_t > &spirv, const Workgroup &workgroup, const std::vector< S > &specializationConstants, const std::vector< P > &pushConstants)
Definition Manager.hpp:475
void destroy()
std::shared_ptr< TensorT< T > > tensorT(size_t size, Memory::MemoryTypes tensorType=Memory::MemoryTypes::eDevice)
Definition Manager.hpp:115
void clear()
std::shared_ptr< vk::Instance > getVkInstance() const
std::shared_ptr< TensorT< T > > tensorT(const std::vector< T > &data, Memory::MemoryTypes tensorType=Memory::MemoryTypes::eDevice)
Definition Manager.hpp:90
std::shared_ptr< Sequence > sequence(uint32_t queueIndex=0, uint32_t totalTimestamps=0)
Manager(std::shared_ptr< vk::Instance > instance, std::shared_ptr< vk::PhysicalDevice > physicalDevice, std::shared_ptr< vk::Device > device)
MemoryTypes
Definition Memory.hpp:28
@ eDevice
Type is device memory, source and destination.
Definition Tensor.hpp:229
Definition Tensor.hpp:23