Refactored descriptor sets

This commit is contained in:
Alejandro Saucedo 2020-08-22 10:15:34 +01:00
parent eecadbe36b
commit 9d97ca07a7
9 changed files with 56 additions and 45 deletions

View file

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

View file

@ -32,8 +32,6 @@ void Algorithm::init(std::string shaderFilePath,
std::vector<std::shared_ptr<Tensor>> 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<std::shared_ptr<Tensor>>& tensorParams) {
std::vector<vk::DescriptorPoolSize> descriptorPoolSizes;
SPDLOG_DEBUG("Kompute Algorithm createParameters started");
for (std::shared_ptr<Tensor> tensorParam : tensorParams) {
descriptorPoolSizes.push_back(
// TODO: Explore design for having multiple descriptor pool sizes
std::vector<vk::DescriptorPoolSize> 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<uint32_t>(descriptorPoolSizes.size()),
descriptorPoolSizes.data());
SPDLOG_DEBUG("Kompute Algorithm creating descriptor pool");
this->mDescriptorPool = std::make_shared<vk::DescriptorPool>();
this->mDevice->createDescriptorPool(&descriptorPoolInfo, nullptr, this->mDescriptorPool.get());
// TODO: Explore allowing descriptor set bind index
std::vector<vk::DescriptorSetLayoutBinding> 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<std::shared_ptr<Tensor>>& tensorPar
// This is the component that is fed into the pipeline
vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutInfo(
vk::DescriptorSetLayoutCreateFlags(),
descriptorSetBindings.size(),
static_cast<uint32_t>(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<vk::DescriptorSetLayout>();
this->mDevice->createDescriptorSetLayout(&descriptorSetLayoutInfo, nullptr, this->mDescriptorSetLayout.get());
@ -90,30 +89,40 @@ void Algorithm::createParameters(std::vector<std::shared_ptr<Tensor>>& tensorPar
1, // Descriptor set layout count
this->mDescriptorSetLayout.get());
SPDLOG_DEBUG("Kompute Algorithm allocating descriptor sets");
std::vector<vk::DescriptorSet> 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<vk::DescriptorSet>();
this->mDevice->allocateDescriptorSets(&descriptorSetAllocateInfo, this->mDescriptorSet.get());
std::vector<vk::WriteDescriptorSet> computeWriteDescriptorSets;
for (size_t i = 0; i < descriptorSets.size(); i++) {
for (size_t i = 0; i < tensorParams.size(); i++) {
std::shared_ptr<Tensor> currTensor = tensorParams[i];
vk::DescriptorSet& currDescriptorSet = descriptorSets[i];
this->mDescriptorSets.push_back(std::make_shared<vk::DescriptorSet>(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<vk::DescriptorSet> 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);
}

View file

@ -41,8 +41,8 @@ private:
bool mFreeDescriptorSetLayout = false;
std::shared_ptr<vk::DescriptorPool> 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<std::shared_ptr<vk::DescriptorSet>> mDescriptorSets;
// TODO: Explore design for multiple descriptor sets
std::shared_ptr<vk::DescriptorSet> mDescriptorSet;
bool mFreeDescriptorSet = false;
std::shared_ptr<vk::ShaderModule> mShaderModule;
bool mFreeShaderModule = false;

View file

@ -30,20 +30,20 @@ class Manager
template<typename T, typename... TArgs>
void evalOp(std::vector<std::shared_ptr<Tensor>> 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<T>(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:

View file

@ -20,13 +20,15 @@ class OpBase
{
private:
public:
OpBase() {}
OpBase() {
SPDLOG_DEBUG("Compute OpBase base constructor");
}
OpBase(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer)
{
SPDLOG_DEBUG("Compute OpBase constructor started");
SPDLOG_DEBUG("Compute OpBase constructor with params");
this->mPhysicalDevice = physicalDevice;
this->mDevice = device;

View file

@ -19,7 +19,7 @@ OpMult::OpMult(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
{
SPDLOG_DEBUG("Kompute OpMult constructor with params");
this->mAlgorithm = Algorithm(device, commandBuffer);
this->mAlgorithm = std::make_shared<Algorithm>(device, commandBuffer);
}
OpMult::~OpMult()
@ -46,7 +46,7 @@ OpMult::init(std::vector<std::shared_ptr<Tensor>> tensors)
this->mTensorOutputStaging= std::make_shared<Tensor>(
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);
}

View file

@ -35,7 +35,7 @@ class OpMult : public OpBase
void postSubmit() override;
private:
Algorithm mAlgorithm;
std::shared_ptr<Algorithm> mAlgorithm;
std::shared_ptr<Tensor> mTensorLHS;
std::shared_ptr<Tensor> mTensorRHS;
std::shared_ptr<Tensor> mTensorOutput;

View file

@ -11,7 +11,7 @@ Tensor::Tensor()
Tensor::Tensor(std::vector<uint32_t> 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
);
}

View file

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