From f29e56fcb8dd26b5c67f7669badc28c49d3569a0 Mon Sep 17 00:00:00 2001 From: Alejandro Saucedo Date: Mon, 31 Aug 2020 09:35:40 +0100 Subject: [PATCH] Introduced simplified interface for creation of algobase --- single_include/kompute/Kompute.hpp | 94 +++++++++++++++++-- src/include/kompute/Manager.hpp | 17 ++++ src/include/kompute/operations/OpAlgoBase.hpp | 77 +++++++++++++-- test/TestCustomOpRawShaders.cpp | 8 +- 4 files changed, 173 insertions(+), 23 deletions(-) diff --git a/single_include/kompute/Kompute.hpp b/single_include/kompute/Kompute.hpp index a7a7bb989..4d7bd0c6b 100755 --- a/single_include/kompute/Kompute.hpp +++ b/single_include/kompute/Kompute.hpp @@ -685,6 +685,23 @@ class Manager SPDLOG_DEBUG("Kompute Manager evalOp running sequence SUCCESS"); } + /** + * Operation that adds extra operations to existing or new created + * sequences. + * + * @param tensors The tensors to be used in the operation recorded + * @param sequenceName The name of the sequence to be retrieved or created + * @param TArgs Template parameters that will be used to initialise + * Operation to allow for extensible configurations on initialisation + */ + template + void evalOpDefault(std::vector> tensors, + TArgs&&... params) + { + SPDLOG_DEBUG("Kompute Manager evalOp Default triggered"); + this->evalOp(tensors, KP_DEFAULT_SESSION, std::forward(params)...); + } + private: // -------------- OPTIONALLY OWNED RESOURCES std::shared_ptr mInstance = nullptr; @@ -845,9 +862,44 @@ class OpAlgoBase : public OpBase std::shared_ptr device, std::shared_ptr commandBuffer, std::vector>& tensors, - bool copyOutputData = false, - std::string shaderFilePath = "", - const std::vector& shaderDataRaw = {}); + bool copyOutputData); + + /** + * Constructor that enables a file to be passed to the operation with + * the contents of the shader. This can be either in raw format or in + * compiled SPIR-V binary format. + * + * @param physicalDevice Vulkan physical device used to find device queues + * @param device Vulkan logical device for passing to Algorithm + * @param commandBuffer Vulkan Command Buffer to record commands into + * @param tensors Tensors that are to be used in this operation + * @param copyOutputData Whether to map device data for all output tensors back to their host data vectors + * @param shaderFilePath Optional parameter to specify the shader to load (either in spirv or raw format) + */ + OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + std::string shaderFilePath); + + /** + * Constructor that enables raw shader data to be passed to the main operation + * which can be either in raw shader glsl code or in compiled SPIR-V binary. + * + * @param physicalDevice Vulkan physical device used to find device queues + * @param device Vulkan logical device for passing to Algorithm + * @param commandBuffer Vulkan Command Buffer to record commands into + * @param tensors Tensors that are to be used in this operation + * @param copyOutputData Whether to map device data for all output tensors back to their host data vectors + * @param shaderDataRaw Optional parameter to specify the shader data either in binary or raw form + */ + OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + const std::vector& shaderDataRaw); /** * Default destructor, which is in charge of destroying the algorithm @@ -920,12 +972,10 @@ OpAlgoBase::OpAlgoBase(std::shared_ptr physicalD std::shared_ptr device, std::shared_ptr commandBuffer, std::vector>& tensors, - bool copyOutputData, - std::string shaderFilePath, - const std::vector& shaderDataRaw) + bool copyOutputData) : OpBase(physicalDevice, device, commandBuffer, tensors, false) { - SPDLOG_DEBUG("Kompute OpAlgoBase constructor with params numTensors: {} copyOutputData: {}, shaderFilePath: {}", tensors.size(), copyOutputData, shaderFilePath); + SPDLOG_DEBUG("Kompute OpAlgoBase constructor with params numTensors: {} copyOutputData: {}, shaderFilePath: {}", tensors.size(), copyOutputData); // The dispatch size is set up based on either explicitly provided template // parameters or by default it would take the shape and size of the tensors @@ -947,13 +997,39 @@ OpAlgoBase::OpAlgoBase(std::shared_ptr physicalD this->mY, this->mZ); - this->mShaderFilePath = shaderFilePath; this->mCopyOutputData = copyOutputData; - this->mShaderDataRaw = shaderDataRaw; this->mAlgorithm = std::make_shared(device, commandBuffer); } +template +OpAlgoBase::OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + std::string shaderFilePath) + : OpAlgoBase(physicalDevice, device, commandBuffer, tensors, copyOutputData) +{ + SPDLOG_DEBUG("Kompute OpAlgoBase shaderFilePath constructo with shaderfile path: {}", shaderFilePath); + + this->mShaderFilePath = shaderFilePath; +} + +template +OpAlgoBase::OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + const std::vector& shaderDataRaw) + : OpAlgoBase(physicalDevice, device, commandBuffer, tensors, copyOutputData) +{ + SPDLOG_DEBUG("Kompute OpAlgoBase shaderFilePath constructo with shader raw data length: {}", shaderDataRaw.size()); + + this->mShaderDataRaw = shaderDataRaw; +} + template OpAlgoBase::~OpAlgoBase() { diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 0d10515ec..71f7ef168 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -95,6 +95,23 @@ class Manager SPDLOG_DEBUG("Kompute Manager evalOp running sequence SUCCESS"); } + /** + * Operation that adds extra operations to existing or new created + * sequences. + * + * @param tensors The tensors to be used in the operation recorded + * @param sequenceName The name of the sequence to be retrieved or created + * @param TArgs Template parameters that will be used to initialise + * Operation to allow for extensible configurations on initialisation + */ + template + void evalOpDefault(std::vector> tensors, + TArgs&&... params) + { + SPDLOG_DEBUG("Kompute Manager evalOp Default triggered"); + this->evalOp(tensors, KP_DEFAULT_SESSION, std::forward(params)...); + } + private: // -------------- OPTIONALLY OWNED RESOURCES std::shared_ptr mInstance = nullptr; diff --git a/src/include/kompute/operations/OpAlgoBase.hpp b/src/include/kompute/operations/OpAlgoBase.hpp index fa9eac913..da845391b 100644 --- a/src/include/kompute/operations/OpAlgoBase.hpp +++ b/src/include/kompute/operations/OpAlgoBase.hpp @@ -57,9 +57,44 @@ class OpAlgoBase : public OpBase std::shared_ptr device, std::shared_ptr commandBuffer, std::vector>& tensors, - bool copyOutputData = false, - std::string shaderFilePath = "", - const std::vector& shaderDataRaw = {}); + bool copyOutputData); + + /** + * Constructor that enables a file to be passed to the operation with + * the contents of the shader. This can be either in raw format or in + * compiled SPIR-V binary format. + * + * @param physicalDevice Vulkan physical device used to find device queues + * @param device Vulkan logical device for passing to Algorithm + * @param commandBuffer Vulkan Command Buffer to record commands into + * @param tensors Tensors that are to be used in this operation + * @param copyOutputData Whether to map device data for all output tensors back to their host data vectors + * @param shaderFilePath Optional parameter to specify the shader to load (either in spirv or raw format) + */ + OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + std::string shaderFilePath); + + /** + * Constructor that enables raw shader data to be passed to the main operation + * which can be either in raw shader glsl code or in compiled SPIR-V binary. + * + * @param physicalDevice Vulkan physical device used to find device queues + * @param device Vulkan logical device for passing to Algorithm + * @param commandBuffer Vulkan Command Buffer to record commands into + * @param tensors Tensors that are to be used in this operation + * @param copyOutputData Whether to map device data for all output tensors back to their host data vectors + * @param shaderDataRaw Optional parameter to specify the shader data either in binary or raw form + */ + OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + const std::vector& shaderDataRaw); /** * Default destructor, which is in charge of destroying the algorithm @@ -132,12 +167,10 @@ OpAlgoBase::OpAlgoBase(std::shared_ptr physicalD std::shared_ptr device, std::shared_ptr commandBuffer, std::vector>& tensors, - bool copyOutputData, - std::string shaderFilePath, - const std::vector& shaderDataRaw) + bool copyOutputData) : OpBase(physicalDevice, device, commandBuffer, tensors, false) { - SPDLOG_DEBUG("Kompute OpAlgoBase constructor with params numTensors: {} copyOutputData: {}, shaderFilePath: {}", tensors.size(), copyOutputData, shaderFilePath); + SPDLOG_DEBUG("Kompute OpAlgoBase constructor with params numTensors: {} copyOutputData: {}, shaderFilePath: {}", tensors.size(), copyOutputData); // The dispatch size is set up based on either explicitly provided template // parameters or by default it would take the shape and size of the tensors @@ -159,13 +192,39 @@ OpAlgoBase::OpAlgoBase(std::shared_ptr physicalD this->mY, this->mZ); - this->mShaderFilePath = shaderFilePath; this->mCopyOutputData = copyOutputData; - this->mShaderDataRaw = shaderDataRaw; this->mAlgorithm = std::make_shared(device, commandBuffer); } +template +OpAlgoBase::OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + std::string shaderFilePath) + : OpAlgoBase(physicalDevice, device, commandBuffer, tensors, copyOutputData) +{ + SPDLOG_DEBUG("Kompute OpAlgoBase shaderFilePath constructo with shaderfile path: {}", shaderFilePath); + + this->mShaderFilePath = shaderFilePath; +} + +template +OpAlgoBase::OpAlgoBase(std::shared_ptr physicalDevice, + std::shared_ptr device, + std::shared_ptr commandBuffer, + std::vector>& tensors, + bool copyOutputData, + const std::vector& shaderDataRaw) + : OpAlgoBase(physicalDevice, device, commandBuffer, tensors, copyOutputData) +{ + SPDLOG_DEBUG("Kompute OpAlgoBase shaderFilePath constructo with shader raw data length: {}", shaderDataRaw.size()); + + this->mShaderDataRaw = shaderDataRaw; +} + template OpAlgoBase::~OpAlgoBase() { diff --git a/test/TestCustomOpRawShaders.cpp b/test/TestCustomOpRawShaders.cpp index b4760ff6c..9407c571b 100644 --- a/test/TestCustomOpRawShaders.cpp +++ b/test/TestCustomOpRawShaders.cpp @@ -10,7 +10,7 @@ TEST_CASE("op_custom_simple_raw_shader") { std::shared_ptr tensorA{ new kp::Tensor({ 3, 4, 5 })}; std::shared_ptr tensorB{ new kp::Tensor({ 0, 0, 0 })}; - mgr.evalOp({ tensorA, tensorB }); + mgr.evalOpDefault({ tensorA, tensorB }); std::string shader( "#version 450\n" @@ -24,11 +24,9 @@ TEST_CASE("op_custom_simple_raw_shader") { "}\n" ); - mgr.evalOp>( + mgr.evalOpDefault>( { tensorA, tensorB }, - "DEFAULT", - true, - "", + true, // Whether to copy output from device std::vector(shader.begin(), shader.end())); REQUIRE(tensorA->data() == std::vector{0, 1, 2});