From 6fc0b0b3e3f42c5d68d4d924cfa9079415266b15 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Sat, 17 Oct 2020 10:40:43 +0100 Subject: [PATCH] Added capabilities for multiple queues --- single_include/kompute/Kompute.hpp | 12 +++-- src/Manager.cpp | 79 +++++++++++++++++++----------- src/include/kompute/Manager.hpp | 12 +++-- 3 files changed, 65 insertions(+), 38 deletions(-) mode change 100644 => 100755 src/Manager.cpp diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp index 9b032bb4f..15e03ccc4 100755 --- a/single_include/kompute/Kompute.hpp +++ b/single_include/kompute/Kompute.hpp @@ -1245,7 +1245,7 @@ namespace kp { */ class Manager { - public: +public: /** Base constructor and default used which creates the base resources including choosing the device 0 by default. @@ -1257,9 +1257,10 @@ class Manager * they would like to create the resources on. * * @param physicalDeviceIndex The index of the physical device to use + * @param familyQueueIndeces (Optional) List of queue indeces to add for explicit allocation * @param totalQueues The total number of compute queues to create. */ - Manager(uint32_t physicalDeviceIndex, uint32_t totalComputeQueues = 1); + Manager(uint32_t physicalDeviceIndex, const std::vector & familyQueueIndeces = {}); /** * Manager constructor which allows your own vulkan application to integrate @@ -1457,13 +1458,14 @@ class Manager uint32_t mPhysicalDeviceIndex = -1; std::shared_ptr mDevice = nullptr; bool mFreeDevice = false; - uint32_t mComputeQueueFamilyIndex = -1; - std::vector> mComputeQueues; // -------------- ALWAYS OWNED RESOURCES std::unordered_map> mManagedSequences; + std::vector mComputeQueueFamilyIndeces; + std::vector> mComputeQueues; + #if DEBUG #ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS vk::DebugReportCallbackEXT mDebugReportCallback; @@ -1473,7 +1475,7 @@ class Manager // Create functions void createInstance(); - void createDevice(uint32_t totalComputeQueues); + void createDevice(const std::vector & familyQueueIndeces = {}); }; } // End namespace kp diff --git a/src/Manager.cpp b/src/Manager.cpp old mode 100644 new mode 100755 index 58f067843..7e5cc831a --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -25,15 +25,15 @@ debugMessageCallback(VkDebugReportFlagsEXT flags, #endif Manager::Manager() - : Manager(0, 1) + : Manager(0) {} -Manager::Manager(uint32_t physicalDeviceIndex, uint32_t totalComputeQueues) +Manager::Manager(uint32_t physicalDeviceIndex, const std::vector & familyQueueIndeces) { this->mPhysicalDeviceIndex = physicalDeviceIndex; this->createInstance(); - this->createDevice(totalComputeQueues); + this->createDevice(familyQueueIndeces); } Manager::Manager(std::shared_ptr instance, @@ -113,7 +113,7 @@ Manager::createManagedSequence(std::string sequenceName, uint32_t queueIndex) { std::make_shared(this->mPhysicalDevice, this->mDevice, this->mComputeQueues[queueIndex], - this->mComputeQueueFamilyIndex); + this->mComputeQueueFamilyIndeces[queueIndex]); sq->init(); this->mManagedSequences.insert({ sequenceName, sq }); return sq; @@ -205,7 +205,7 @@ Manager::createInstance() } void -Manager::createDevice(uint32_t totalComputeQueues) +Manager::createDevice(const std::vector & familyQueueIndeces) { SPDLOG_DEBUG("Kompute Manager creating Device"); @@ -236,48 +236,71 @@ Manager::createDevice(uint32_t totalComputeQueues) this->mPhysicalDeviceIndex, physicalDeviceProperties.deviceName); - // Find compute queue - std::vector allQueueFamilyProperties = - physicalDevice.getQueueFamilyProperties(); + if (!familyQueueIndeces.size()) { + // Find compute queue + std::vector allQueueFamilyProperties = + physicalDevice.getQueueFamilyProperties(); - this->mComputeQueueFamilyIndex = -1; - for (uint32_t i = 0; i < allQueueFamilyProperties.size(); i++) { - vk::QueueFamilyProperties queueFamilyProperties = - allQueueFamilyProperties[i]; + uint32_t computeQueueFamilyIndex = -1; + for (uint32_t i = 0; i < allQueueFamilyProperties.size(); i++) { + vk::QueueFamilyProperties queueFamilyProperties = + allQueueFamilyProperties[i]; - if (queueFamilyProperties.queueFlags & vk::QueueFlagBits::eCompute) { - this->mComputeQueueFamilyIndex = i; - break; + if (queueFamilyProperties.queueFlags & vk::QueueFlagBits::eCompute) { + computeQueueFamilyIndex = i; + break; + } } + + if (computeQueueFamilyIndex < 0) { + throw std::runtime_error("Compute queue is not supported"); + } + + this->mComputeQueueFamilyIndeces.push_back(computeQueueFamilyIndex); + } + else { + this->mComputeQueueFamilyIndeces = familyQueueIndeces; } - if (this->mComputeQueueFamilyIndex < 0) { - throw std::runtime_error("Compute queue is not supported"); + std::unordered_map familyQueueCounts; + std::unordered_map> familyQueuePriorities; + for (const auto& value : this->mComputeQueueFamilyIndeces) { + familyQueueCounts[value]++; + familyQueuePriorities[value].push_back(1.0f); } - const float defaultQueuePriority(0.0f); - const uint32_t defaultQueueCount(totalComputeQueues); - vk::DeviceQueueCreateInfo deviceQueueCreateInfo( - vk::DeviceQueueCreateFlags(), - this->mComputeQueueFamilyIndex, - defaultQueueCount, - &defaultQueuePriority); + std::unordered_map familyQueueIndexCount; + std::vector deviceQueueCreateInfos; + for (const auto& familyQueueInfo : familyQueueCounts) { + // Setting the device count to 0 + familyQueueIndexCount[familyQueueInfo.first] = 0; + + // Creating the respective device queue + vk::DeviceQueueCreateInfo deviceQueueCreateInfo( + vk::DeviceQueueCreateFlags(), + familyQueueInfo.first, + familyQueueInfo.second, + familyQueuePriorities[familyQueueInfo.first].data()); + deviceQueueCreateInfos.push_back(deviceQueueCreateInfo); + } vk::DeviceCreateInfo deviceCreateInfo(vk::DeviceCreateFlags(), - 1, // Number of deviceQueueCreateInfo - &deviceQueueCreateInfo); + deviceQueueCreateInfos.size(), + deviceQueueCreateInfos.data()); this->mDevice = std::make_shared(); physicalDevice.createDevice( &deviceCreateInfo, nullptr, this->mDevice.get()); SPDLOG_DEBUG("Kompute Manager device created"); - for (uint32_t i = 0; i < totalComputeQueues; i++) + for (const uint32_t & familyQueueIndex : this->mComputeQueueFamilyIndeces) { std::shared_ptr currQueue = std::make_shared(); this->mDevice->getQueue( - this->mComputeQueueFamilyIndex, i, currQueue.get()); + familyQueueIndex, familyQueueIndexCount[familyQueueIndex], currQueue.get()); + + familyQueueIndexCount[familyQueueIndex]++; this->mComputeQueues.push_back(currQueue); } diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 340fd782d..2581139d9 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -17,7 +17,7 @@ namespace kp { */ class Manager { - public: +public: /** Base constructor and default used which creates the base resources including choosing the device 0 by default. @@ -29,9 +29,10 @@ class Manager * they would like to create the resources on. * * @param physicalDeviceIndex The index of the physical device to use + * @param familyQueueIndeces (Optional) List of queue indeces to add for explicit allocation * @param totalQueues The total number of compute queues to create. */ - Manager(uint32_t physicalDeviceIndex, uint32_t totalComputeQueues = 1); + Manager(uint32_t physicalDeviceIndex, const std::vector & familyQueueIndeces = {}); /** * Manager constructor which allows your own vulkan application to integrate @@ -229,13 +230,14 @@ class Manager uint32_t mPhysicalDeviceIndex = -1; std::shared_ptr mDevice = nullptr; bool mFreeDevice = false; - uint32_t mComputeQueueFamilyIndex = -1; - std::vector> mComputeQueues; // -------------- ALWAYS OWNED RESOURCES std::unordered_map> mManagedSequences; + std::vector mComputeQueueFamilyIndeces; + std::vector> mComputeQueues; + #if DEBUG #ifndef KOMPUTE_DISABLE_VK_DEBUG_LAYERS vk::DebugReportCallbackEXT mDebugReportCallback; @@ -245,7 +247,7 @@ class Manager // Create functions void createInstance(); - void createDevice(uint32_t totalComputeQueues); + void createDevice(const std::vector & familyQueueIndeces = {}); }; } // End namespace kp