diff --git a/README.md b/README.md index 4f146074a..e0ae774f4 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Use default equations ```c++ int main() { - kp::Manager kManager(); // Chooses device 0 + kp::Manager kManager; // Chooses device 0 unless specified kp::Tensor inputOne = kp::Tensor({0, 1, 2, 3}); diff --git a/src/Algorithm.cpp b/src/Algorithm.cpp index c24965e43..eb6b5786d 100644 --- a/src/Algorithm.cpp +++ b/src/Algorithm.cpp @@ -32,8 +32,6 @@ void Algorithm::init(std::string shaderFilePath, std::vector> tensorParams) { SPDLOG_DEBUG("Kompute Algorithm init started"); - spdlog::info("Loading shader with file path {}", shaderFilePath); - // TODO: Move to util function this->createParameters(tensorParams); this->createShaderModule(shaderFilePath); @@ -41,29 +39,29 @@ void Algorithm::init(std::string shaderFilePath, } void Algorithm::createParameters(std::vector>& tensorParams) { - std::vector descriptorPoolSizes; + SPDLOG_DEBUG("Kompute Algorithm createParameters started"); - for (std::shared_ptr tensorParam : tensorParams) { - descriptorPoolSizes.push_back( + // TODO: Explore design for having multiple descriptor pool sizes + std::vector descriptorPoolSizes = { vk::DescriptorPoolSize( vk::DescriptorType::eStorageBuffer, 1 // Descriptor count ) - ); - } + }; // TODO: Explore design for having more than 1 set configurable vk::DescriptorPoolCreateInfo descriptorPoolInfo( vk::DescriptorPoolCreateFlags(), 1, // Max sets - descriptorPoolSizes.size(), + static_cast(descriptorPoolSizes.size()), descriptorPoolSizes.data()); + SPDLOG_DEBUG("Kompute Algorithm creating descriptor pool"); this->mDescriptorPool = std::make_shared(); this->mDevice->createDescriptorPool(&descriptorPoolInfo, nullptr, this->mDescriptorPool.get()); - // TODO: Explore allowing descriptor set bind index std::vector descriptorSetBindings; + // TODO: Explore allowing descriptor set bind index to be configurable by user to specify which tensors woudl go on each binding for (size_t i = 0; i < tensorParams.size(); i++) { descriptorSetBindings.push_back( vk::DescriptorSetLayoutBinding( @@ -77,10 +75,11 @@ void Algorithm::createParameters(std::vector>& tensorPar // This is the component that is fed into the pipeline vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutInfo( vk::DescriptorSetLayoutCreateFlags(), - descriptorSetBindings.size(), + static_cast(descriptorSetBindings.size()), descriptorSetBindings.data() ); + SPDLOG_DEBUG("Kompute Algorithm creating descriptor set layout"); // TODO: We createa signle descriptor set layout which would have to be extended if multiple set layouts to be supported this->mDescriptorSetLayout = std::make_shared(); this->mDevice->createDescriptorSetLayout(&descriptorSetLayoutInfo, nullptr, this->mDescriptorSetLayout.get()); @@ -90,30 +89,40 @@ void Algorithm::createParameters(std::vector>& tensorPar 1, // Descriptor set layout count this->mDescriptorSetLayout.get()); + SPDLOG_DEBUG("Kompute Algorithm allocating descriptor sets"); std::vector descriptorSets = this->mDevice->allocateDescriptorSets(descriptorSetAllocateInfo); - if (descriptorSets.size() != tensorParams.size()) { - throw std::runtime_error("Number of descriptor sets does not match number of paramters"); - } + this->mDescriptorSet = std::make_shared(); + this->mDevice->allocateDescriptorSets(&descriptorSetAllocateInfo, this->mDescriptorSet.get()); std::vector computeWriteDescriptorSets; - for (size_t i = 0; i < descriptorSets.size(); i++) { + for (size_t i = 0; i < tensorParams.size(); i++) { - std::shared_ptr currTensor = tensorParams[i]; - vk::DescriptorSet& currDescriptorSet = descriptorSets[i]; - this->mDescriptorSets.push_back(std::make_shared(currDescriptorSet)); - - vk::DescriptorBufferInfo descriptorBufferInfo = currTensor->constructDescriptorBufferInfo(); + vk::DescriptorBufferInfo descriptorBufferInfo = + tensorParams[i]->constructDescriptorBufferInfo(); + // TODO: Explore design exposing the destination array element computeWriteDescriptorSets.push_back( - vk::WriteDescriptorSet()); + vk::WriteDescriptorSet( + *this->mDescriptorSet, + i, // Destination binding + 0, // Destination array element + 1, // Descriptor count + vk::DescriptorType::eStorageBuffer, + nullptr, // Descriptor image info + &descriptorBufferInfo)); } + SPDLOG_DEBUG("Kompute Algorithm updating descriptor sets"); this->mDevice->updateDescriptorSets(computeWriteDescriptorSets, nullptr); + + SPDLOG_DEBUG("Kompue Algorithm successfully run init"); } void Algorithm::createShaderModule(std::string shaderFilePath) { + SPDLOG_DEBUG("Kompute Algorithm createShaderModule started"); + std::ifstream fileStream( shaderFilePath, std::ios::binary | std::ios::in | std::ios::ate); @@ -166,13 +175,13 @@ void Algorithm::recordDispatch(uint32_t x, uint32_t y, uint32_t z) { this->mCommandBuffer->bindPipeline(vk::PipelineBindPoint::eCompute, *this->mPipeline); - // TODO: Simplify interaction given we store array of pointers - std::vector descriptorSetRefs(this->mDescriptorSets.size()); - for (size_t i = 0; i < this->mDescriptorSets.size(); i++) { - descriptorSetRefs[i] = *this->mDescriptorSets[i]; - } - - this->mCommandBuffer->bindDescriptorSets(vk::PipelineBindPoint::eCompute, *this->mPipelineLayout, 0, descriptorSetRefs, nullptr); + this->mCommandBuffer->bindDescriptorSets( + vk::PipelineBindPoint::eCompute, + *this->mPipelineLayout, + 0, // First set + *this->mDescriptorSet, + nullptr // Dispatcher + ); this->mCommandBuffer->dispatch(x, y, z); } diff --git a/src/Algorithm.hpp b/src/Algorithm.hpp index c8e3f38c2..7888a33b3 100644 --- a/src/Algorithm.hpp +++ b/src/Algorithm.hpp @@ -41,8 +41,8 @@ private: bool mFreeDescriptorSetLayout = false; std::shared_ptr mDescriptorPool; bool mFreeDescriptorPool = false; - // TODO: Potentially change to vector pointer of objects depending on whether it will be expected to pass a pointer (or reference) to the constructor - std::vector> mDescriptorSets; + // TODO: Explore design for multiple descriptor sets + std::shared_ptr mDescriptorSet; bool mFreeDescriptorSet = false; std::shared_ptr mShaderModule; bool mFreeShaderModule = false; diff --git a/src/Manager.hpp b/src/Manager.hpp index 5218cc81f..8b94f3f82 100644 --- a/src/Manager.hpp +++ b/src/Manager.hpp @@ -30,20 +30,20 @@ class Manager template void evalOp(std::vector> tensors) { - SPDLOG_DEBUG("Kompute Manager eval triggered"); + SPDLOG_DEBUG("Kompute Manager evalOp triggered"); Sequence sq(this->mPhysicalDevice, this->mDevice, this->mComputeQueue, this->mComputeQueueFamilyIndex); - SPDLOG_DEBUG("Kompute Manager created sequence"); + SPDLOG_DEBUG("Kompute Manager evalOp running sequence BEGIN"); sq.begin(); - SPDLOG_DEBUG("Kompute Manager sequence begin"); + SPDLOG_DEBUG("Kompute Manager evalOp running sequence RECORD"); sq.record(tensors); - SPDLOG_DEBUG("Kompute Manager sequence end"); + SPDLOG_DEBUG("Kompute Manager evalOp running sequence END"); sq.end(); - SPDLOG_DEBUG("Kompute Manager sequence eval"); + SPDLOG_DEBUG("Kompute Manager evalOp running sequence EVAL"); sq.eval(); - SPDLOG_DEBUG("Kompute Manager sequence done"); + SPDLOG_DEBUG("Kompute Manager evalOp running sequence SUCCESS"); } private: diff --git a/src/OpBase.hpp b/src/OpBase.hpp index a4ce407aa..97c2dabff 100644 --- a/src/OpBase.hpp +++ b/src/OpBase.hpp @@ -20,13 +20,15 @@ class OpBase { private: public: - OpBase() {} + OpBase() { + SPDLOG_DEBUG("Compute OpBase base constructor"); + } OpBase(std::shared_ptr physicalDevice, std::shared_ptr device, std::shared_ptr commandBuffer) { - SPDLOG_DEBUG("Compute OpBase constructor started"); + SPDLOG_DEBUG("Compute OpBase constructor with params"); this->mPhysicalDevice = physicalDevice; this->mDevice = device; diff --git a/src/OpMult.cpp b/src/OpMult.cpp index 89217e8c9..13949374d 100644 --- a/src/OpMult.cpp +++ b/src/OpMult.cpp @@ -19,7 +19,7 @@ OpMult::OpMult(std::shared_ptr physicalDevice, { SPDLOG_DEBUG("Kompute OpMult constructor with params"); - this->mAlgorithm = Algorithm(device, commandBuffer); + this->mAlgorithm = std::make_shared(device, commandBuffer); } OpMult::~OpMult() @@ -46,7 +46,7 @@ OpMult::init(std::vector> tensors) this->mTensorOutputStaging= std::make_shared( this->mTensorOutput->data(), Tensor::TensorTypes::eStaging); - this->mAlgorithm.init( + this->mAlgorithm->init( "shaders/glsl/computeheadless.comp.spv", tensors); } @@ -55,7 +55,7 @@ OpMult::record() { SPDLOG_DEBUG("Kompute OpMult record called"); - this->mAlgorithm.recordDispatch(1, 1, 1); + this->mAlgorithm->recordDispatch(1, 1, 1); this->mTensorOutputStaging->recordCopyFrom(this->mTensorOutput); } diff --git a/src/OpMult.hpp b/src/OpMult.hpp index 364029f29..c3c9274ab 100644 --- a/src/OpMult.hpp +++ b/src/OpMult.hpp @@ -35,7 +35,7 @@ class OpMult : public OpBase void postSubmit() override; private: - Algorithm mAlgorithm; + std::shared_ptr mAlgorithm; std::shared_ptr mTensorLHS; std::shared_ptr mTensorRHS; std::shared_ptr mTensorOutput; diff --git a/src/Tensor.cpp b/src/Tensor.cpp index d98105df9..aa11676c2 100644 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -11,7 +11,7 @@ Tensor::Tensor() Tensor::Tensor(std::vector data, TensorTypes tensorType) { - SPDLOG_DEBUG("Kompute Tensor constructor shape and type"); + SPDLOG_DEBUG("Kompute Tensor constructor data and type"); this->mData = data; this->mShape = { data.size() }; @@ -139,7 +139,7 @@ vk::DescriptorBufferInfo Tensor::constructDescriptorBufferInfo() { return vk::DescriptorBufferInfo( *this->mBuffer, 0, // offset - this->memorySize() + VK_WHOLE_SIZE ); } diff --git a/src/main.cpp b/src/main.cpp index 89616f811..810917314 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -650,7 +650,7 @@ main() return 0; } catch (const std::exception& exc) { - spdlog::error(exc.what()); + spdlog::error("Exception caught: {}", exc.what()); return 1; } catch (...) { spdlog::error("Uncaught exception");