Reformatted

This commit is contained in:
Alejandro Saucedo 2020-08-20 05:45:54 +01:00
parent 8aa7843f0e
commit 0d18dc50e6
13 changed files with 166 additions and 95 deletions

View file

@ -1,13 +1,31 @@
#pragma once
#include <vulkan/vulkan.h>
#include <vulkan/vulkan.hpp>
// SPDLOG_ACTIVE_LEVEL must be defined before spdlog.h import
#if DEBUG
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#endif
#include <spdlog/spdlog.h>
#include "Tensor.hpp"
namespace kp {
class Algorithm
{
private:
public:
Algorithm();
virtual ~Algorithm();
Algorithm(std::shared_ptr<vk::Device> device);
// TODO: Add specialisation data
void init(std::string shaderFilePath,
std::vector<std::shared_ptr<Tensor>> tensorParams);
~Algorithm();
};
} // End namespace kp

View file

@ -166,7 +166,7 @@ Manager::createDevice()
vk::PhysicalDevice physicalDevice =
physicalDevices[this->mPhysicalDeviceIndex];
this->mPhysicalDevice =
this->mPhysicalDevice =
std::make_shared<vk::PhysicalDevice>(physicalDevice);
vk::PhysicalDeviceProperties physicalDeviceProperties =

View file

@ -31,8 +31,10 @@ class Manager
void evalOp(std::vector<std::shared_ptr<Tensor>> tensors)
{
SPDLOG_DEBUG("Kompute Manager eval triggered");
Sequence sq(
this->mPhysicalDevice, this->mDevice, this->mComputeQueue, this->mComputeQueueFamilyIndex);
Sequence sq(this->mPhysicalDevice,
this->mDevice,
this->mComputeQueue,
this->mComputeQueueFamilyIndex);
SPDLOG_DEBUG("Kompute Manager created sequence");
sq.begin();
SPDLOG_DEBUG("Kompute Manager sequence begin");

View file

@ -24,7 +24,8 @@ class OpBase
OpBase(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer) {
std::shared_ptr<vk::CommandBuffer> commandBuffer)
{
SPDLOG_DEBUG("Compute OpBase constructor started");
this->mPhysicalDevice = physicalDevice;
@ -32,18 +33,14 @@ class OpBase
this->mCommandBuffer = commandBuffer;
}
~OpBase()
{
SPDLOG_DEBUG("Compute OpBase destructor started");
}
~OpBase() { SPDLOG_DEBUG("Compute OpBase destructor started"); }
virtual void init(std::vector<std::shared_ptr<Tensor>> tensors) {
virtual void init(std::vector<std::shared_ptr<Tensor>> tensors)
{
SPDLOG_DEBUG("Kompute OpBase init called");
}
virtual void record() {
SPDLOG_DEBUG("Kompute OpBase record called");
}
virtual void record() { SPDLOG_DEBUG("Kompute OpBase record called"); }
protected:
std::shared_ptr<vk::PhysicalDevice> mPhysicalDevice;
@ -52,4 +49,3 @@ class OpBase
};
} // End namespace kp

View file

@ -5,20 +5,22 @@
namespace kp {
OpCreateTensor::OpCreateTensor() {
OpCreateTensor::OpCreateTensor()
{
SPDLOG_DEBUG("Kompute OpCreateTensor constructor base");
}
OpCreateTensor::OpCreateTensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer)
OpCreateTensor::OpCreateTensor(
std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer)
: OpBase(physicalDevice, device, commandBuffer)
{
SPDLOG_DEBUG("Kompute OpCreateTensor constructor with params");
}
OpCreateTensor::~OpCreateTensor() {
OpCreateTensor::~OpCreateTensor()
{
SPDLOG_DEBUG("Kompute OpCreateTensor destructor started");
}
@ -28,7 +30,8 @@ OpCreateTensor::init(std::vector<std::shared_ptr<Tensor>> tensors)
SPDLOG_DEBUG("Kompute OpCreateTensor init called");
if (tensors.size() < 1) {
throw std::runtime_error("Kompute OpCreateTensor called with less than 1 tensor");
throw std::runtime_error(
"Kompute OpCreateTensor called with less than 1 tensor");
} else if (tensors.size() > 1) {
spdlog::warn("Kompute OpCreateTensor called with more than 1 tensor");
}
@ -37,15 +40,18 @@ OpCreateTensor::init(std::vector<std::shared_ptr<Tensor>> tensors)
std::vector<uint32_t> data = this->mPrimaryTensor->data();
if (this->mPrimaryTensor->tensorType() == Tensor::TensorTypes::eDevice) {
this->mPrimaryTensor->init(this->mPhysicalDevice, this->mDevice, this->mCommandBuffer);
this->mPrimaryTensor->init(
this->mPhysicalDevice, this->mDevice, this->mCommandBuffer);
this->mStagingTensor = std::make_shared<Tensor>(this->mPrimaryTensor->data(), Tensor::TensorTypes::eStaging);
this->mStagingTensor = std::make_shared<Tensor>(
this->mPrimaryTensor->data(), Tensor::TensorTypes::eStaging);
this->mStagingTensor->init(this->mPhysicalDevice, this->mDevice, this->mCommandBuffer, data);
this->mStagingTensor->init(
this->mPhysicalDevice, this->mDevice, this->mCommandBuffer, data);
}
else {
this->mPrimaryTensor->init(this->mPhysicalDevice, this->mDevice, this->mCommandBuffer, data);
} else {
this->mPrimaryTensor->init(
this->mPhysicalDevice, this->mDevice, this->mCommandBuffer, data);
}
}

View file

@ -32,7 +32,6 @@ class OpCreateTensor : public OpBase
void record() override;
private:
std::shared_ptr<Tensor> mPrimaryTensor;
std::shared_ptr<Tensor> mStagingTensor;
};

View file

@ -6,20 +6,21 @@
namespace kp {
OpMult::OpMult() {
OpMult::OpMult()
{
SPDLOG_DEBUG("Kompute OpMult constructor base");
}
OpMult::OpMult(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer)
OpMult::OpMult(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer)
: OpBase(physicalDevice, device, commandBuffer)
{
SPDLOG_DEBUG("Kompute OpMult constructor with params");
}
OpMult::~OpMult() {
OpMult::~OpMult()
{
SPDLOG_DEBUG("Kompute OpMult destructor started");
}
@ -29,18 +30,17 @@ OpMult::init(std::vector<std::shared_ptr<Tensor>> tensors)
SPDLOG_DEBUG("Kompute OpMult init called");
if (tensors.size() < 2) {
throw std::runtime_error("Kompute OpMult called with less than 1 tensor");
throw std::runtime_error(
"Kompute OpMult called with less than 1 tensor");
} else if (tensors.size() > 2) {
spdlog::warn("Kompute OpMult called with more than 2 tensor");
}
}
void
OpMult::record()
{
SPDLOG_DEBUG("Kompute OpMult record called");
}
}

View file

@ -22,8 +22,8 @@ class OpMult : public OpBase
OpMult();
OpMult(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer);
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer);
~OpMult();
@ -32,7 +32,6 @@ class OpMult : public OpBase
void record() override;
private:
std::shared_ptr<Tensor> mPrimaryTensor;
std::shared_ptr<Tensor> mStagingTensor;
};

View file

@ -34,12 +34,11 @@ Sequence::~Sequence()
return;
}
if (this->mFreeCommandBuffer) {
spdlog::info("Freeing CommandBuffer");
if (!this->mCommandBuffer) {
spdlog::error(
"Kompute Sequence destructor reached with null CommandPool pointer");
spdlog::error("Kompute Sequence destructor reached with null "
"CommandPool pointer");
return;
}
this->mDevice->freeCommandBuffers(
@ -50,8 +49,8 @@ Sequence::~Sequence()
if (this->mFreeCommandPool) {
spdlog::info("Destroying CommandPool");
if (this->mCommandPool == nullptr) {
spdlog::error(
"Kompute Sequence destructor reached with null CommandPool pointer");
spdlog::error("Kompute Sequence destructor reached with null "
"CommandPool pointer");
return;
}
this->mDevice->destroy(*this->mCommandPool);
@ -110,7 +109,8 @@ Sequence::eval()
vk::Fence fence = this->mDevice->createFence(vk::FenceCreateInfo());
SPDLOG_DEBUG("Kompute sequence submitting command buffer into compute queue");
SPDLOG_DEBUG(
"Kompute sequence submitting command buffer into compute queue");
this->mComputeQueue->submit(1, &submitInfo, fence);
this->mDevice->waitForFences(1, &fence, VK_TRUE, UINT64_MAX);

View file

@ -16,7 +16,6 @@ namespace kp {
class Sequence
{
private:
public:
Sequence();
Sequence(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
@ -33,14 +32,16 @@ class Sequence
template<typename T, typename... TArgs>
void record(TArgs&&... args)
{
static_assert(std::is_base_of<OpBase, T>::value, "Template only valid with OpBase derived classes");
static_assert(std::is_base_of<OpBase, T>::value,
"Template only valid with OpBase derived classes");
SPDLOG_DEBUG("Kompute Sequence record");
T* op = new T(this->mPhysicalDevice, this->mDevice, this->mCommandBuffer);
T* op =
new T(this->mPhysicalDevice, this->mDevice, this->mCommandBuffer);
OpBase* baseOp = dynamic_cast<OpBase*>(op);
std::unique_ptr<OpBase> baseOpPtr{baseOp};
std::unique_ptr<OpBase> baseOpPtr{ baseOp };
baseOpPtr->init(std::forward<TArgs>(args)...);
baseOpPtr->record();

View file

@ -3,7 +3,8 @@
namespace kp {
Tensor::Tensor() {
Tensor::Tensor()
{
SPDLOG_DEBUG("Kompute Tensor base constructor");
this->mTensorType = TensorTypes::eDevice;
}
@ -13,11 +14,11 @@ Tensor::Tensor(std::vector<uint32_t> data, TensorTypes tensorType)
SPDLOG_DEBUG("Kompute Tensor constructor shape and type");
this->mData = data;
this->mShape = {data.size()};
this->mShape = { data.size() };
this->mTensorType = tensorType;
}
Tensor::~Tensor()
Tensor::~Tensor()
{
SPDLOG_DEBUG("Kompute Tensor destructor started");
@ -29,7 +30,8 @@ Tensor::~Tensor()
if (this->mFreeBuffer) {
if (!this->mBuffer) {
spdlog::error("Kompose Tensor expected to free buffer but got null buffer");
spdlog::error(
"Kompose Tensor expected to free buffer but got null buffer");
} else {
SPDLOG_DEBUG("Kompose Tensor destroying buffer");
this->mDevice->destroy(*this->mBuffer);
@ -38,7 +40,8 @@ Tensor::~Tensor()
if (this->mFreeMemory) {
if (!this->mMemory) {
spdlog::error("Kompose Tensor expected to free buffer but got null memory");
spdlog::error(
"Kompose Tensor expected to free buffer but got null memory");
} else {
SPDLOG_DEBUG("Kompose Tensor freeing memory");
this->mDevice->freeMemory(*this->mMemory);
@ -48,8 +51,14 @@ Tensor::~Tensor()
SPDLOG_DEBUG("Kompute Tensor destructor success");
}
void Tensor::init(std::shared_ptr<vk::PhysicalDevice> physicalDevice, std::shared_ptr<vk::Device> device, std::shared_ptr<vk::CommandBuffer> commandBuffer, std::vector<uint32_t> data) {
SPDLOG_DEBUG("Kompute Tensor running init with physicalDevice and logical device");
void
Tensor::init(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer,
std::vector<uint32_t> data)
{
SPDLOG_DEBUG(
"Kompute Tensor running init with physicalDevice and logical device");
this->mPhysicalDevice = physicalDevice;
this->mDevice = device;
@ -60,36 +69,50 @@ void Tensor::init(std::shared_ptr<vk::PhysicalDevice> physicalDevice, std::share
this->createBuffer(data.data());
}
std::vector<uint32_t> Tensor::data() {
std::vector<uint32_t>
Tensor::data()
{
return this->mData;
}
uint64_t Tensor::memorySize() {
uint64_t
Tensor::memorySize()
{
return this->size() * sizeof(uint32_t);
}
uint32_t Tensor::size() {
uint32_t
Tensor::size()
{
return this->mShape[0];
}
std::array<uint32_t, KP_MAX_DIM_SIZE> Tensor::shape() {
std::array<uint32_t, KP_MAX_DIM_SIZE>
Tensor::shape()
{
return this->mShape;
}
Tensor::TensorTypes Tensor::tensorType() {
Tensor::TensorTypes
Tensor::tensorType()
{
return this->mTensorType;
}
bool Tensor::isInit() {
bool
Tensor::isInit()
{
return this->mIsInit;
}
void Tensor::recordCopyFrom(std::shared_ptr<Tensor> copyFromTensor) {
void
Tensor::recordCopyFrom(std::shared_ptr<Tensor> copyFromTensor)
{
SPDLOG_DEBUG("Kompute Tensor recordCopyFrom called");
if (!this->mIsInit) {
throw std::runtime_error("Kompute Tensor attempted to run createBuffer without init");
throw std::runtime_error(
"Kompute Tensor attempted to run createBuffer without init");
}
// TODO: Allow for dst and src offsets to be configured
@ -100,21 +123,24 @@ void Tensor::recordCopyFrom(std::shared_ptr<Tensor> copyFromTensor) {
SPDLOG_DEBUG("Kompute Tensor copying data size {}.", bufferSize);
// TODO: Ensure command buffer is in same device from buffer
this->mCommandBuffer->copyBuffer(*copyFromTensor->mBuffer, *this->mBuffer, copyRegion);
this->mCommandBuffer->copyBuffer(
*copyFromTensor->mBuffer, *this->mBuffer, copyRegion);
this->mData = copyFromTensor->mData;
}
vk::BufferUsageFlags Tensor::getBufferUsageFlags() {
vk::BufferUsageFlags
Tensor::getBufferUsageFlags()
{
switch (this->mTensorType) {
case TensorTypes::eDevice:
return vk::BufferUsageFlagBits::eStorageBuffer |
vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
break;
case TensorTypes::eStaging:
return vk::BufferUsageFlagBits::eTransferSrc|
vk::BufferUsageFlagBits::eTransferDst;
return vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
break;
case TensorTypes::eStorage:
return vk::BufferUsageFlagBits::eStorageBuffer;
@ -124,7 +150,9 @@ vk::BufferUsageFlags Tensor::getBufferUsageFlags() {
}
}
vk::MemoryPropertyFlags Tensor::getMemoryPropertyFlags() {
vk::MemoryPropertyFlags
Tensor::getMemoryPropertyFlags()
{
switch (this->mTensorType) {
case TensorTypes::eDevice:
return vk::MemoryPropertyFlagBits::eDeviceLocal;
@ -146,7 +174,8 @@ Tensor::createBuffer(void* data)
SPDLOG_DEBUG("Kompute Tensor creating buffer");
if (!this->mIsInit) {
throw std::runtime_error("Kompute Tensor attempted to run createBuffer without init");
throw std::runtime_error(
"Kompute Tensor attempted to run createBuffer without init");
}
if (!this->mPhysicalDevice) {
@ -161,43 +190,59 @@ Tensor::createBuffer(void* data)
vk::BufferUsageFlags usageFlags = this->getBufferUsageFlags();
vk::DeviceSize bufferSize = this->memorySize();
SPDLOG_DEBUG("Kompute Tensor creating buffer with memory size: {}, and usage flags: {}", bufferSize, vk::to_string(usageFlags));
SPDLOG_DEBUG("Kompute Tensor creating buffer with memory size: {}, and "
"usage flags: {}",
bufferSize,
vk::to_string(usageFlags));
vk::BufferCreateInfo bufferInfo(vk::BufferCreateFlags(), bufferSize, usageFlags, vk::SharingMode::eExclusive);
vk::BufferCreateInfo bufferInfo(vk::BufferCreateFlags(),
bufferSize,
usageFlags,
vk::SharingMode::eExclusive);
this->mBuffer = std::make_shared<vk::Buffer>();
this->mDevice->createBuffer(&bufferInfo, nullptr, this->mBuffer.get());
SPDLOG_DEBUG("Kompute Tensor buffer created now creating memory");
vk::PhysicalDeviceMemoryProperties memoryProperties = this->mPhysicalDevice->getMemoryProperties();
vk::PhysicalDeviceMemoryProperties memoryProperties =
this->mPhysicalDevice->getMemoryProperties();
vk::MemoryRequirements memoryRequirements = this->mDevice->getBufferMemoryRequirements(*this->mBuffer);
vk::MemoryRequirements memoryRequirements =
this->mDevice->getBufferMemoryRequirements(*this->mBuffer);
vk::MemoryPropertyFlags memoryPropertyFlags =
this->getMemoryPropertyFlags();
this->getMemoryPropertyFlags();
uint32_t memoryTypeIndex = -1;
for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) {
if (memoryRequirements.memoryTypeBits & (1 << i)) {
if ((memoryProperties.memoryTypes[i].propertyFlags & memoryPropertyFlags) == memoryPropertyFlags) {
if ((memoryProperties.memoryTypes[i].propertyFlags &
memoryPropertyFlags) == memoryPropertyFlags) {
memoryTypeIndex = i;
break;
}
}
}
if (memoryTypeIndex < 0) {
throw std::runtime_error("Memory type index for buffer creation not found");
throw std::runtime_error(
"Memory type index for buffer creation not found");
}
this->mFreeMemory = true;
SPDLOG_DEBUG("Kompute Tensor allocating memory index: {}, size {}, flags: {}", memoryTypeIndex, memoryRequirements.size, vk::to_string(memoryPropertyFlags));
SPDLOG_DEBUG(
"Kompute Tensor allocating memory index: {}, size {}, flags: {}",
memoryTypeIndex,
memoryRequirements.size,
vk::to_string(memoryPropertyFlags));
vk::MemoryAllocateInfo memoryAllocateInfo(memoryRequirements.size, memoryTypeIndex);
vk::MemoryAllocateInfo memoryAllocateInfo(memoryRequirements.size,
memoryTypeIndex);
this->mMemory = std::make_shared<vk::DeviceMemory>();
this->mDevice->allocateMemory(&memoryAllocateInfo, nullptr, this->mMemory.get());
this->mDevice->allocateMemory(
&memoryAllocateInfo, nullptr, this->mMemory.get());
this->mDevice->bindBufferMemory(*this->mBuffer, *this->mMemory, 0);
@ -207,7 +252,8 @@ Tensor::createBuffer(void* data)
SPDLOG_DEBUG("Kompute Tensor mapping data to buffer");
// TODO: Verify if flushed memory ranges should happend in sequence
void* mapped = this->mDevice->mapMemory(*this->mMemory, 0, bufferSize, vk::MemoryMapFlags());
void* mapped = this->mDevice->mapMemory(
*this->mMemory, 0, bufferSize, vk::MemoryMapFlags());
memcpy(mapped, data, bufferSize);
vk::MappedMemoryRange mappedRange(*this->mMemory, 0, bufferSize);
this->mDevice->flushMappedMemoryRanges(1, &mappedRange);

View file

@ -17,7 +17,6 @@ namespace kp {
class Tensor
{
public:
enum class TensorTypes
{
eDevice = 0,
@ -27,11 +26,15 @@ class Tensor
Tensor();
Tensor(std::vector<uint32_t> data, TensorTypes tensorType = TensorTypes::eDevice);
Tensor(std::vector<uint32_t> data,
TensorTypes tensorType = TensorTypes::eDevice);
~Tensor();
void init(std::shared_ptr<vk::PhysicalDevice> physicalDevice, std::shared_ptr<vk::Device> device, std::shared_ptr<vk::CommandBuffer> commandBuffer, std::vector<uint32_t> data = std::vector<uint32_t>());
void init(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
std::shared_ptr<vk::CommandBuffer> commandBuffer,
std::vector<uint32_t> data = std::vector<uint32_t>());
// Create functions
void createBuffer(void* data = nullptr);
@ -64,7 +67,6 @@ class Tensor
bool mIsInit = false;
// uint32_t mDataType;
// Private util functions
vk::BufferUsageFlags getBufferUsageFlags();
vk::MemoryPropertyFlags getMemoryPropertyFlags();

View file

@ -20,9 +20,9 @@
#include <vulkan/vulkan.h>
#include <vulkan/vulkan.hpp>
#include "Manager.hpp"
#include "OpCreateTensor.hpp"
#include "Tensor.hpp"
#include "Manager.hpp"
#define BUFFER_ELEMENTS 32
@ -623,12 +623,14 @@ main()
kp::Manager mgr;
spdlog::info("Creating first tensor");
std::shared_ptr<kp::Tensor> tensorOne{new kp::Tensor({0.0, 1.0, 2.0})};
mgr.evalOp<kp::OpCreateTensor>({tensorOne});
std::shared_ptr<kp::Tensor> tensorOne{ new kp::Tensor(
{ 0.0, 1.0, 2.0 }) };
mgr.evalOp<kp::OpCreateTensor>({ tensorOne });
spdlog::info("Creating second tensor");
std::shared_ptr<kp::Tensor> tensorTwo{new kp::Tensor({0.0, 1.0, 2.0})};
mgr.evalOp<kp::OpCreateTensor>({tensorTwo});
std::shared_ptr<kp::Tensor> tensorTwo{ new kp::Tensor(
{ 0.0, 1.0, 2.0 }) };
mgr.evalOp<kp::OpCreateTensor>({ tensorTwo });
spdlog::info("Called manager eval success");
spdlog::info("Tensor one: {}", tensorOne->data());