Added functionality with atomicadd

This commit is contained in:
Alejandro Saucedo 2021-03-05 08:19:56 +00:00
parent 9a40465d69
commit 71129392c2
4 changed files with 57 additions and 23 deletions

View file

@ -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<uint32_t>& familyQueueIndices)
const std::vector<uint32_t>& familyQueueIndices,
const std::vector<std::string>& desiredExtensions)
{
this->mManageResources = true;
this->createInstance();
this->createDevice(familyQueueIndices, physicalDeviceIndex);
this->createDevice(familyQueueIndices, physicalDeviceIndex, desiredExtensions);
}
Manager::Manager(std::shared_ptr<vk::Instance> instance,
@ -152,7 +151,10 @@ Manager::createInstance()
applicationInfo.applicationVersion = KOMPUTE_VK_API_VERSION;
std::vector<const char*> 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<uint32_t>& familyQueueIndices,
uint32_t physicalDeviceIndex)
uint32_t physicalDeviceIndex,
const std::vector<std::string>& desiredExtensions)
{
KP_LOG_DEBUG("Kompute Manager creating Device");
@ -294,7 +297,7 @@ Manager::createDevice(const std::vector<uint32_t>& 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<uint32_t>& familyQueueIndices,
deviceQueueCreateInfos.push_back(deviceQueueCreateInfo);
}
KP_LOG_DEBUG("Kompute Manager desired extension layers {}", desiredExtensions);
std::vector<vk::ExtensionProperties> deviceExtensions = this->mPhysicalDevice->enumerateDeviceExtensionProperties();
std::set<std::string> 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<const char*> 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<vk::Device>();
physicalDevice.createDevice(

View file

@ -35,7 +35,8 @@ class Manager
* @param totalQueues The total number of compute queues to create.
*/
Manager(uint32_t physicalDeviceIndex,
const std::vector<uint32_t>& familyQueueIndices = {});
const std::vector<uint32_t>& familyQueueIndices = {},
const std::vector<std::string>& 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<uint32_t>& familyQueueIndices = {},
uint32_t hysicalDeviceIndex = 0);
uint32_t hysicalDeviceIndex = 0,
const std::vector<std::string>& desiredExtensions = {});
};
} // End namespace kp

View file

@ -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);
}
)");

View file

@ -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<uint32_t> spirv = kp::Shader::compile_source(shader);
@ -27,7 +28,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchOverride)
std::shared_ptr<kp::Sequence> sq = nullptr;
{
kp::Manager mgr;
kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" });
std::shared_ptr<kp::Tensor> 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<uint32_t> spirv = kp::Shader::compile_source(shader);
@ -71,7 +73,7 @@ TEST(TestPushConstants, TestConstantsAlgoDispatchNoOverride)
std::shared_ptr<kp::Sequence> sq = nullptr;
{
kp::Manager mgr;
kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" });
std::shared_ptr<kp::Tensor> 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<uint32_t> spirv = kp::Shader::compile_source(shader);
@ -114,7 +117,7 @@ TEST(TestPushConstants, TestConstantsWrongSize)
std::shared_ptr<kp::Sequence> sq = nullptr;
{
kp::Manager mgr;
kp::Manager mgr(0, {}, { "VK_EXT_shader_atomic_float", "SPV_EXT_shader_atomic_float_add" });
std::shared_ptr<kp::Tensor> tensor = mgr.tensor({ 0, 0, 0 });