From 71129392c2ecb401f7c320636c10de2be59e8a5c Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Fri, 5 Mar 2021 08:19:56 +0000 Subject: [PATCH] Added functionality with atomicadd --- src/Manager.cpp | 41 ++++++++++++++++++++++++----- src/include/kompute/Manager.hpp | 6 +++-- test/TestMultipleAlgoExecutions.cpp | 6 +++-- test/TestPushConstant.cpp | 27 ++++++++++--------- 4 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/Manager.cpp b/src/Manager.cpp index 103e799d8..83676f9ec 100644 --- a/src/Manager.cpp +++ b/src/Manager.cpp @@ -6,9 +6,7 @@ #include "kompute/Manager.hpp" -#if DEBUG #include "fmt/ranges.h" -#endif namespace kp { @@ -35,12 +33,13 @@ Manager::Manager() {} Manager::Manager(uint32_t physicalDeviceIndex, - const std::vector& familyQueueIndices) + const std::vector& familyQueueIndices, + const std::vector& desiredExtensions) { this->mManageResources = true; this->createInstance(); - this->createDevice(familyQueueIndices, physicalDeviceIndex); + this->createDevice(familyQueueIndices, physicalDeviceIndex, desiredExtensions); } Manager::Manager(std::shared_ptr instance, @@ -152,7 +151,10 @@ Manager::createInstance() applicationInfo.applicationVersion = KOMPUTE_VK_API_VERSION; std::vector applicationExtensions; + +#if DEBUG applicationExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); +#endif vk::InstanceCreateInfo computeInstanceCreateInfo; computeInstanceCreateInfo.pApplicationInfo = &applicationInfo; @@ -266,7 +268,8 @@ Manager::clear() void Manager::createDevice(const std::vector& familyQueueIndices, - uint32_t physicalDeviceIndex) + uint32_t physicalDeviceIndex, + const std::vector& desiredExtensions) { KP_LOG_DEBUG("Kompute Manager creating Device"); @@ -294,7 +297,7 @@ Manager::createDevice(const std::vector& familyQueueIndices, KP_LOG_INFO("Using physical device index {} found {}", physicalDeviceIndex, - physicalDeviceProperties.deviceName); + physicalDeviceProperties.deviceName.data()); if (!familyQueueIndices.size()) { // Find compute queue @@ -344,9 +347,33 @@ Manager::createDevice(const std::vector& familyQueueIndices, deviceQueueCreateInfos.push_back(deviceQueueCreateInfo); } + KP_LOG_DEBUG("Kompute Manager desired extension layers {}", desiredExtensions); + + std::vector deviceExtensions = this->mPhysicalDevice->enumerateDeviceExtensionProperties(); + + std::set uniqueExtensionNames; + for (const vk::ExtensionProperties& ext : deviceExtensions) { + std::string extName(ext.extensionName.data()); + uniqueExtensionNames.insert(extName); + } + KP_LOG_DEBUG("Kompute Manager available extensions {}", uniqueExtensionNames); + std::vector validExtensions; + for (std::string ext : desiredExtensions) { + if (uniqueExtensionNames.count(ext) != 0) { + validExtensions.push_back(ext.c_str()); + } + } + if (desiredExtensions.size() != validExtensions.size()) { + KP_LOG_ERROR("Kompute Manager not all extensions were added: {}", validExtensions); + } + vk::DeviceCreateInfo deviceCreateInfo(vk::DeviceCreateFlags(), deviceQueueCreateInfos.size(), - deviceQueueCreateInfos.data()); + deviceQueueCreateInfos.data(), + {}, + {}, + validExtensions.size(), + validExtensions.data()); this->mDevice = std::make_shared(); physicalDevice.createDevice( diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 0dbde246d..1e6b0adb2 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -35,7 +35,8 @@ class Manager * @param totalQueues The total number of compute queues to create. */ Manager(uint32_t physicalDeviceIndex, - const std::vector& familyQueueIndices = {}); + const std::vector& familyQueueIndices = {}, + const std::vector& desiredExtensions = {}); /** * Manager constructor which allows your own vulkan application to integrate @@ -121,7 +122,8 @@ class Manager // Create functions void createInstance(); void createDevice(const std::vector& familyQueueIndices = {}, - uint32_t hysicalDeviceIndex = 0); + uint32_t hysicalDeviceIndex = 0, + const std::vector& desiredExtensions = {}); }; } // End namespace kp diff --git a/test/TestMultipleAlgoExecutions.cpp b/test/TestMultipleAlgoExecutions.cpp index 4b8eedb12..7e772fc80 100644 --- a/test/TestMultipleAlgoExecutions.cpp +++ b/test/TestMultipleAlgoExecutions.cpp @@ -16,6 +16,8 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) std::string shader = (R"( #version 450 + #extension GL_EXT_shader_atomic_float: enable + layout (local_size_x = 1) in; // The input tensors bind index is relative to index in parameter passed @@ -34,8 +36,8 @@ TEST(TestMultipleAlgoExecutions, TestEndToEndFunctionality) void main() { uint index = gl_GlobalInvocationID.x; - out_a[index] += in_a[index] * in_b[index]; - out_b[index] += const_one * push_const.val; + atomicAdd(out_a[index], in_a[index] * in_b[index]); + atomicAdd(out_b[index], const_one * push_const.val); } )"); diff --git a/test/TestPushConstant.cpp b/test/TestPushConstant.cpp index 6b37ab015..184a3f66b 100644 --- a/test/TestPushConstant.cpp +++ b/test/TestPushConstant.cpp @@ -9,6 +9,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) { std::string shader(R"( #version 450 + #extension GL_EXT_shader_atomic_float: enable layout(push_constant) uniform PushConstants { float x; float y; @@ -17,9 +18,9 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { - pa[0] += pcs.x; - pa[1] += pcs.y; - pa[2] += pcs.z; + atomicAdd(pa[0], pcs.x); + atomicAdd(pa[1], pcs.y); + atomicAdd(pa[2], pcs.z); })"); std::vector spirv = kp::Shader::compile_source(shader); @@ -27,7 +28,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride) std::shared_ptr sq = nullptr; { - kp::Manager mgr; + kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" }); std::shared_ptr tensor = mgr.tensor({ 0, 0, 0 }); @@ -53,6 +54,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride) { std::string shader(R"( #version 450 + #extension GL_EXT_shader_atomic_float: enable layout(push_constant) uniform PushConstants { float x; float y; @@ -61,9 +63,9 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride) layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { - pa[0] += pcs.x; - pa[1] += pcs.y; - pa[2] += pcs.z; + atomicAdd(pa[0], pcs.x); + atomicAdd(pa[1], pcs.y); + atomicAdd(pa[2], pcs.z); })"); std::vector spirv = kp::Shader::compile_source(shader); @@ -71,7 +73,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride) std::shared_ptr sq = nullptr; { - kp::Manager mgr; + kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" }); std::shared_ptr tensor = mgr.tensor({ 0, 0, 0 }); @@ -96,6 +98,7 @@ TEST(TestPushConstants, TestConstantsWrongSize) { std::string shader(R"( #version 450 + #extension GL_EXT_shader_atomic_float: enable layout(push_constant) uniform PushConstants { float x; float y; @@ -104,9 +107,9 @@ TEST(TestPushConstants, TestConstantsWrongSize) layout (local_size_x = 1) in; layout(set = 0, binding = 0) buffer a { float pa[]; }; void main() { - pa[0] += pcs.x; - pa[1] += pcs.y; - pa[2] += pcs.z; + atomicAdd(pa[0], pcs.x); + atomicAdd(pa[1], pcs.y); + atomicAdd(pa[2], pcs.z); })"); std::vector spirv = kp::Shader::compile_source(shader); @@ -114,7 +117,7 @@ TEST(TestPushConstants, TestConstantsWrongSize) std::shared_ptr sq = nullptr; { - kp::Manager mgr; + kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" }); std::shared_ptr tensor = mgr.tensor({ 0, 0, 0 });