diff --git a/src/Algorithm.cpp b/src/Algorithm.cpp index 83df93b91..96a303b9f 100644 --- a/src/Algorithm.cpp +++ b/src/Algorithm.cpp @@ -85,19 +85,22 @@ Algorithm::destroy() this->mShaderModule = nullptr; } - // We don't call freeDescriptorSet as the descriptor pool is not created - // with VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT more at - // (https://www.khronos.org/registry/vulkan/specs/1.0/html/vkspec.html#VUID-vkFreeDescriptorSets-descriptorPool-00312)) - // if (this->mFreeDescriptorSet && this->mDescriptorSet) { - // KP_LOG_DEBUG("Kompute Algorithm Freeing Descriptor Set"); - // if (!this->mDescriptorSet) { - // KP_LOG_WARN( - // "Kompute Algorithm Error requested to free descriptor set"); - // } - // this->mDevice->freeDescriptorSets( - // *this->mDescriptorPool, 1, this->mDescriptorSet.get()); - // this->mDescriptorSet = nullptr; - //} + freeParameters(); +} + +void +Algorithm::freeParameters() +{ + if (this->mFreeDescriptorSet && this->mDescriptorSet) { + KP_LOG_DEBUG("Kompute Algorithm Freeing Descriptor Set"); + if (!this->mDescriptorSet) { + KP_LOG_WARN( + "Kompute Algorithm Error requested to free descriptor set"); + } + this->mDevice->freeDescriptorSets( + *this->mDescriptorPool, 1, this->mDescriptorSet.get()); + this->mDescriptorSet = nullptr; + } if (this->mFreeDescriptorSetLayout && this->mDescriptorSetLayout) { KP_LOG_DEBUG("Kompute Algorithm Destroying Descriptor Set Layout"); @@ -137,7 +140,7 @@ Algorithm::createParameters() }; vk::DescriptorPoolCreateInfo descriptorPoolInfo( - vk::DescriptorPoolCreateFlags(), + vk::DescriptorPoolCreateFlags(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT), 1, // Max sets static_cast(descriptorPoolSizes.size()), descriptorPoolSizes.data()); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbb47dbe8..914a9a30a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ add_library(kompute Algorithm.cpp OpTensorCopy.cpp OpTensorSyncDevice.cpp OpTensorSyncLocal.cpp + OpBufferSyncDevice.cpp Sequence.cpp Tensor.cpp Core.cpp) diff --git a/src/OpBufferSyncDevice.cpp b/src/OpBufferSyncDevice.cpp new file mode 100644 index 000000000..1812d04b2 --- /dev/null +++ b/src/OpBufferSyncDevice.cpp @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: Apache-2.0 + +#include "kompute/operations/OpBufferSyncDevice.hpp" + +namespace kp { + +OpBufferSyncDevice::OpBufferSyncDevice( + vk::Buffer *primaryBuffer, + vk::Buffer *stagingBuffer, + vk::DeviceSize size) + : mPrimaryBuffer(primaryBuffer) + , mStagingBuffer(stagingBuffer) + , mSize(size) +{ + KP_LOG_DEBUG("Kompute OpBufferSyncDevice constructor with params"); +} + +OpBufferSyncDevice::~OpBufferSyncDevice() +{ + KP_LOG_DEBUG("Kompute OpBufferSyncDevice destructor started"); +} + +void +OpBufferSyncDevice::record(const vk::CommandBuffer& commandBuffer) +{ + KP_LOG_DEBUG("Kompute OpBufferSyncDevice record called"); + vk::BufferCopy copyRegion(0, 0, mSize); + commandBuffer.copyBuffer(*mStagingBuffer, *mPrimaryBuffer, copyRegion); +} + +void +OpBufferSyncDevice::preEval(const vk::CommandBuffer& /*commandBuffer*/) +{ + KP_LOG_DEBUG("Kompute OpBufferSyncDevice preEval called"); +} + +void +OpBufferSyncDevice::postEval(const vk::CommandBuffer& /*commandBuffer*/) +{ + KP_LOG_DEBUG("Kompute OpBufferSyncDevice postEval called"); +} + +} diff --git a/src/OpTensorSyncDevice.cpp b/src/OpTensorSyncDevice.cpp index a2542357f..b563529ea 100644 --- a/src/OpTensorSyncDevice.cpp +++ b/src/OpTensorSyncDevice.cpp @@ -6,6 +6,8 @@ namespace kp { OpTensorSyncDevice::OpTensorSyncDevice( const std::vector>& tensors) + : mPrimaryBuffer(nullptr) + , mStagingBuffer(nullptr) { KP_LOG_DEBUG("Kompute OpTensorSyncDevice constructor with params"); diff --git a/src/Tensor.cpp b/src/Tensor.cpp index 7dcadf363..d068af2ec 100644 --- a/src/Tensor.cpp +++ b/src/Tensor.cpp @@ -44,8 +44,11 @@ Tensor::Tensor(std::shared_ptr physicalDevice, uint32_t elementTotalCount, uint32_t elementMemorySize, const TensorDataTypes& dataType, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset, const TensorTypes& tensorType) { KP_LOG_DEBUG("Kompute Tensor constructor data length: {}, and type: {}", @@ -57,7 +60,7 @@ Tensor::Tensor(std::shared_ptr physicalDevice, this->mDataType = dataType; this->mTensorType = tensorType; - this->rebuild(data, elementTotalCount, elementMemorySize, deviceMemory, buffer); + this->rebuild(data, elementTotalCount, elementMemorySize, primaryMemory, primaryBuffer, stagingMemory, stagingBuffer, offset); } Tensor::~Tensor() @@ -73,16 +76,20 @@ Tensor::~Tensor() } void -Tensor::rebuild(void* data, +Tensor::rebuild(void* /*data*/, uint32_t elementTotalCount, - uint32_t elementMemorySize, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer) + uint64_t memorySize, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset) { KP_LOG_DEBUG("Kompute Tensor rebuilding with size {}", elementTotalCount); this->mSize = elementTotalCount; - this->mDataTypeMemorySize = elementMemorySize; + this->mMemorySize = memorySize; + this->mOffset = offset; if (this->mPrimaryBuffer || this->mPrimaryMemory) { KP_LOG_DEBUG( @@ -90,11 +97,7 @@ Tensor::rebuild(void* data, this->destroy(); } - this->allocateMemoryCreateGPUResources(deviceMemory, buffer); - - if (this->tensorType() != Tensor::TensorTypes::eStorage) { - this->mRawData = data; - } + this->setGPUResources(primaryMemory, primaryBuffer, stagingMemory, stagingBuffer, offset); } Tensor::TensorTypes @@ -116,16 +119,10 @@ Tensor::size() return this->mSize; } -uint32_t -Tensor::dataTypeMemorySize() -{ - return this->mDataTypeMemorySize; -} - -uint32_t +uint64_t Tensor::memorySize() { - return this->mSize * this->mDataTypeMemorySize; + return this->mMemorySize; } kp::Tensor::TensorDataTypes @@ -146,64 +143,13 @@ Tensor::setRawData(const void* data) memcpy(this->mRawData, data, this->memorySize()); } -void -Tensor::mapRawData() -{ - - KP_LOG_DEBUG("Kompute Tensor mapping data from host buffer"); - - std::shared_ptr hostVisibleMemory = nullptr; - - if (this->mTensorType == TensorTypes::eHost) { - hostVisibleMemory = this->mPrimaryMemory; - } else if (this->mTensorType == TensorTypes::eDevice) { - hostVisibleMemory = this->mStagingMemory; - } else { - KP_LOG_WARN( - "Kompute Tensor mapping data not supported on {} tensor", toString(this->tensorType())); - return; - } - - vk::DeviceSize bufferSize = this->memorySize(); - - // Given we request coherent host memory we don't need to invalidate / - // flush - this->mRawData = this->mDevice->mapMemory( - *hostVisibleMemory, 0, bufferSize, vk::MemoryMapFlags()); - -} - -void -Tensor::unmapRawData() -{ - - KP_LOG_DEBUG("Kompute Tensor mapping data from host buffer"); - - std::shared_ptr hostVisibleMemory = nullptr; - - if (this->mTensorType == TensorTypes::eHost) { - hostVisibleMemory = this->mPrimaryMemory; - } else if (this->mTensorType == TensorTypes::eDevice) { - hostVisibleMemory = this->mStagingMemory; - } else { - KP_LOG_WARN( - "Kompute Tensor mapping data not supported on {} tensor", toString(this->tensorType())); - return; - } - - vk::DeviceSize bufferSize = this->memorySize(); - vk::MappedMemoryRange mappedRange(*hostVisibleMemory, 0, bufferSize); - this->mDevice->flushMappedMemoryRanges(1, &mappedRange); - this->mDevice->unmapMemory(*hostVisibleMemory); -} - void Tensor::recordCopyFrom(const vk::CommandBuffer& commandBuffer, std::shared_ptr copyFromTensor) { vk::DeviceSize bufferSize(this->memorySize()); - vk::BufferCopy copyRegion(0, 0, bufferSize); + vk::BufferCopy copyRegion(mOffset, mOffset, bufferSize); KP_LOG_DEBUG("Kompute Tensor recordCopyFrom data size {}.", bufferSize); @@ -218,7 +164,7 @@ void Tensor::recordCopyFromStagingToDevice(const vk::CommandBuffer& commandBuffer) { vk::DeviceSize bufferSize(this->memorySize()); - vk::BufferCopy copyRegion(0, 0, bufferSize); + vk::BufferCopy copyRegion(mOffset, mOffset, bufferSize); KP_LOG_DEBUG("Kompute Tensor copying data size {}.", bufferSize); @@ -233,7 +179,7 @@ void Tensor::recordCopyFromDeviceToStaging(const vk::CommandBuffer& commandBuffer) { vk::DeviceSize bufferSize(this->memorySize()); - vk::BufferCopy copyRegion(0, 0, bufferSize); + vk::BufferCopy copyRegion(mOffset, mOffset, bufferSize); KP_LOG_DEBUG("Kompute Tensor copying data size {}.", bufferSize); @@ -246,8 +192,8 @@ Tensor::recordCopyFromDeviceToStaging(const vk::CommandBuffer& commandBuffer) void Tensor::recordCopyBuffer(const vk::CommandBuffer& commandBuffer, - std::shared_ptr bufferFrom, - std::shared_ptr bufferTo, + vk::Buffer *bufferFrom, + vk::Buffer *bufferTo, vk::DeviceSize /*bufferSize*/, vk::BufferCopy copyRegion) { @@ -324,7 +270,7 @@ Tensor::constructDescriptorBufferInfo() this->memorySize()); vk::DeviceSize bufferSize = this->memorySize(); return vk::DescriptorBufferInfo(*this->mPrimaryBuffer, - 0, // offset + mOffset, // offset bufferSize); } @@ -396,7 +342,11 @@ Tensor::getStagingMemoryPropertyFlags() } void -Tensor::allocateMemoryCreateGPUResources(vk::DeviceMemory *stagingMemory, vk::Buffer *stagingBuffer) +Tensor::setGPUResources(vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize /*offset*/) { KP_LOG_DEBUG("Kompute Tensor creating buffer"); @@ -409,99 +359,19 @@ Tensor::allocateMemoryCreateGPUResources(vk::DeviceMemory *stagingMemory, vk::Bu KP_LOG_DEBUG("Kompute Tensor creating primary buffer and memory"); - this->mPrimaryBuffer = std::make_shared(); - this->createBuffer(this->mPrimaryBuffer, - this->getPrimaryBufferUsageFlags()); - this->mFreePrimaryBuffer = true; - this->mPrimaryMemory = std::make_shared(); - this->allocateBindMemory(this->mPrimaryBuffer, - this->mPrimaryMemory, - this->getPrimaryMemoryPropertyFlags()); - this->mFreePrimaryMemory = true; + this->mPrimaryBuffer = primaryBuffer; + this->mPrimaryMemory = primaryMemory; if (this->mTensorType == TensorTypes::eDevice) { KP_LOG_DEBUG("Kompute Tensor creating staging buffer and memory"); - this->mStagingBuffer = std::shared_ptr(stagingBuffer); - this->mFreeStagingBuffer = true; - this->mStagingMemory = std::shared_ptr(stagingMemory); - this->mFreeStagingMemory = true; + this->mStagingBuffer = stagingBuffer; + this->mStagingMemory = stagingMemory; } KP_LOG_DEBUG("Kompute Tensor buffer & memory creation successful"); } -void -Tensor::createBuffer(std::shared_ptr buffer, - vk::BufferUsageFlags bufferUsageFlags) -{ - - vk::DeviceSize bufferSize = this->memorySize(); - - if (bufferSize < 1) { - throw std::runtime_error( - "Kompute Tensor attempted to create a zero-sized buffer"); - } - - KP_LOG_DEBUG("Kompute Tensor creating buffer with memory size: {}, and " - "usage flags: {}", - bufferSize, - vk::to_string(bufferUsageFlags)); - - // TODO: Explore having concurrent sharing mode (with option) - vk::BufferCreateInfo bufferInfo(vk::BufferCreateFlags(), - bufferSize, - bufferUsageFlags, - vk::SharingMode::eExclusive); - - this->mDevice->createBuffer(&bufferInfo, nullptr, buffer.get()); -} - -void -Tensor::allocateBindMemory(std::shared_ptr buffer, - std::shared_ptr memory, - vk::MemoryPropertyFlags memoryPropertyFlags) -{ - - KP_LOG_DEBUG("Kompute Tensor allocating and binding memory"); - - vk::PhysicalDeviceMemoryProperties memoryProperties = - this->mPhysicalDevice->getMemoryProperties(); - - vk::MemoryRequirements memoryRequirements = - this->mDevice->getBufferMemoryRequirements(*buffer); - - uint32_t memoryTypeIndex = -1; - bool memoryTypeIndexFound = false; - for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) { - if (memoryRequirements.memoryTypeBits & (1 << i)) { - if (((memoryProperties.memoryTypes[i]).propertyFlags & - memoryPropertyFlags) == memoryPropertyFlags) { - memoryTypeIndex = i; - memoryTypeIndexFound = true; - break; - } - } - } - if (!memoryTypeIndexFound) { - throw std::runtime_error( - "Memory type index for buffer creation not found"); - } - - KP_LOG_DEBUG( - "Kompute Tensor allocating memory index: {}, size {}, flags: {}", - memoryTypeIndex, - memoryRequirements.size, - vk::to_string(memoryPropertyFlags)); - - vk::MemoryAllocateInfo memoryAllocateInfo(memoryRequirements.size, - memoryTypeIndex); - - this->mDevice->allocateMemory(&memoryAllocateInfo, nullptr, memory.get()); - - this->mDevice->bindBufferMemory(*buffer, *memory, 0); -} - void Tensor::destroy() { @@ -511,7 +381,7 @@ Tensor::destroy() // invalidate Tensor this->mRawData = nullptr; this->mSize = 0; - this->mDataTypeMemorySize = 0; + this->mMemorySize = 0; if (!this->mDevice) { KP_LOG_WARN( @@ -519,6 +389,7 @@ Tensor::destroy() return; } +#if 0 // FIXME: This all moves outside of Kompute // Unmap the current memory data if (this->tensorType() != Tensor::TensorTypes::eStorage) { this->unmapRawData(); @@ -579,6 +450,7 @@ Tensor::destroy() this->mFreeStagingMemory = false; } } +#endif if (this->mDevice) { this->mDevice = nullptr; diff --git a/src/include/CMakeLists.txt b/src/include/CMakeLists.txt index e1652fdda..60a9d6d7a 100644 --- a/src/include/CMakeLists.txt +++ b/src/include/CMakeLists.txt @@ -23,6 +23,7 @@ target_sources(kompute PRIVATE kompute/operations/OpTensorCopy.hpp kompute/operations/OpTensorSyncDevice.hpp kompute/operations/OpTensorSyncLocal.hpp + kompute/operations/OpBufferSyncDevice.hpp kompute/logger/Logger.hpp ) diff --git a/src/include/kompute/Algorithm.hpp b/src/include/kompute/Algorithm.hpp index 45ebfe00c..9d9cac320 100644 --- a/src/include/kompute/Algorithm.hpp +++ b/src/include/kompute/Algorithm.hpp @@ -202,7 +202,25 @@ class Algorithm this->setWorkgroup( this->mWorkgroup, this->mTensors.size() ? this->mTensors[0]->size() : 1); - this->createParameters(); + this->createParameters(); // TODO: See if we can reduce this +// for (size_t i = 0; i < this->mTensors.size(); i++) { +// std::vector computeWriteDescriptorSets; + +// vk::DescriptorBufferInfo descriptorBufferInfo = +// this->mTensors[i]->constructDescriptorBufferInfo(); + +// computeWriteDescriptorSets.push_back( +// vk::WriteDescriptorSet(*this->mDescriptorSet, +// i, // Destination binding +// 0, // Destination array element +// 1, // Descriptor count +// vk::DescriptorType::eStorageBuffer, +// nullptr, // Descriptor image info +// &descriptorBufferInfo)); + +// this->mDevice->updateDescriptorSets(computeWriteDescriptorSets, +// nullptr); +// } } /** @@ -316,6 +334,7 @@ class Algorithm void createPipeline(); // Parameters + void freeParameters(); void createParameters(); }; diff --git a/src/include/kompute/Kompute.hpp b/src/include/kompute/Kompute.hpp index e54adc1b7..5646211b5 100644 --- a/src/include/kompute/Kompute.hpp +++ b/src/include/kompute/Kompute.hpp @@ -13,6 +13,7 @@ #include "operations/OpTensorCopy.hpp" #include "operations/OpTensorSyncDevice.hpp" #include "operations/OpTensorSyncLocal.hpp" +#include "operations/OpBufferSyncDevice.hpp" // Will be build by CMake and placed inside the build directory #include "ShaderLogisticRegression.hpp" diff --git a/src/include/kompute/Manager.hpp b/src/include/kompute/Manager.hpp index 8ae36cfad..46737e471 100644 --- a/src/include/kompute/Manager.hpp +++ b/src/include/kompute/Manager.hpp @@ -81,14 +81,16 @@ class Manager template std::shared_ptr> tensorT( const std::vector& data, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) { KP_LOG_DEBUG("Kompute Manager tensor creation triggered"); std::shared_ptr> tensor{ new kp::TensorT( - this->mPhysicalDevice, this->mDevice, data, deviceMemory, buffer, tensorType) }; + this->mPhysicalDevice, this->mDevice, data, primaryMemory, primaryBuffer, stagingMemory, stagingBuffer, tensorType) }; if (this->mManageResources) { this->mManagedTensors.push_back(tensor); @@ -97,32 +99,29 @@ class Manager return tensor; } - std::shared_ptr> tensor( - const std::vector& data, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, - Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) - { - return this->tensorT(data, deviceMemory, buffer, tensorType); - } - std::shared_ptr tensor( void* data, - uint64_t elementTotalCount, - uint64_t elementMemorySize, + uint32_t elementTotalCount, + uint64_t memorySize, const Tensor::TensorDataTypes& dataType, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset, Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice) { std::shared_ptr tensor{ new kp::Tensor(this->mPhysicalDevice, this->mDevice, data, elementTotalCount, - elementMemorySize, + memorySize, dataType, - deviceMemory, - buffer, + primaryMemory, + primaryBuffer, + stagingMemory, + stagingBuffer, + offset, tensorType) }; if (this->mManageResources) { diff --git a/src/include/kompute/Sequence.hpp b/src/include/kompute/Sequence.hpp index de9b9f69c..3b29a6e2e 100644 --- a/src/include/kompute/Sequence.hpp +++ b/src/include/kompute/Sequence.hpp @@ -120,6 +120,17 @@ class Sequence : public std::enable_shared_from_this std::shared_ptr op{ new T(tensors, std::forward(params)...) }; return this->eval(op); } + + template + std::shared_ptr eval(vk::Buffer *primaryBuffer, + vk::Buffer *stagingBuffer, + vk::DeviceSize size, + TArgs&&... params) + { + std::shared_ptr op{ new T(primaryBuffer, stagingBuffer, size, std::forward(params)...) }; + return this->eval(op); + } + /** * Eval sends all the recorded and stored operations in the vector of * operations into the gpu as a submit job with a barrier. diff --git a/src/include/kompute/Tensor.hpp b/src/include/kompute/Tensor.hpp index 541d8d205..316206e5b 100644 --- a/src/include/kompute/Tensor.hpp +++ b/src/include/kompute/Tensor.hpp @@ -57,10 +57,13 @@ class Tensor std::shared_ptr device, void* data, uint32_t elementTotalCount, - uint32_t elementMemorySize, + uint32_t memorySize, const TensorDataTypes& dataType, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset, const TensorTypes& tensorType = TensorTypes::eDevice); /** @@ -78,9 +81,12 @@ class Tensor */ void rebuild(void* data, uint32_t elementTotalCount, - uint32_t elementMemorySize, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer); + uint64_t memorySize, + vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset); /** * Destroys and frees the GPU resources which include the buffer and memory. @@ -182,23 +188,12 @@ class Tensor */ uint32_t size(); - /** - * Returns the total size of a single element of the respective data type - * that this tensor holds. - * - * @return Unsigned integer representing the memory of a single element of - * the respective data type. - */ - uint32_t dataTypeMemorySize(); - /** * Returns the total memory size of the data contained by the Tensor object - * which would equate to (this->size() * this->dataTypeMemorySize()) * - * @return Unsigned integer representing the memory of a single element of - * the respective data type. + * @return Unsigned integer representing the memory of the tensor in bytes. */ - uint32_t memorySize(); + uint64_t memorySize(); /** * Retrieve the data type of the tensor (host, device, storage) @@ -252,34 +247,28 @@ class Tensor // -------------- ALWAYS OWNED RESOURCES TensorTypes mTensorType; TensorDataTypes mDataType; - uint32_t mSize; - uint32_t mDataTypeMemorySize; - void* mRawData; + uint32_t mSize = 0; + uint64_t mMemorySize = 0; + vk::DeviceSize mOffset = 0; + void* mRawData = nullptr; private: // -------------- NEVER OWNED RESOURCES std::shared_ptr mPhysicalDevice; std::shared_ptr mDevice; + vk::Buffer *mPrimaryBuffer = nullptr; + vk::Buffer *mStagingBuffer = nullptr; + vk::DeviceMemory *mPrimaryMemory = nullptr; + vk::DeviceMemory *mStagingMemory = nullptr; - // -------------- OPTIONALLY OWNED RESOURCES - std::shared_ptr mPrimaryBuffer; - bool mFreePrimaryBuffer = false; - std::shared_ptr mStagingBuffer; - bool mFreeStagingBuffer = false; - std::shared_ptr mPrimaryMemory; - bool mFreePrimaryMemory = false; - std::shared_ptr mStagingMemory; - bool mFreeStagingMemory = false; - - void allocateMemoryCreateGPUResources(vk::DeviceMemory *stagingMemory, vk::Buffer *stagingBuffer); // Creates the vulkan buffer - void createBuffer(std::shared_ptr buffer, - vk::BufferUsageFlags bufferUsageFlags); - void allocateBindMemory(std::shared_ptr buffer, - std::shared_ptr memory, - vk::MemoryPropertyFlags memoryPropertyFlags); + void setGPUResources(vk::DeviceMemory *primaryMemory, + vk::Buffer *primaryBuffer, + vk::DeviceMemory *stagingMemory, + vk::Buffer *stagingBuffer, + vk::DeviceSize offset); void recordCopyBuffer(const vk::CommandBuffer& commandBuffer, - std::shared_ptr bufferFrom, - std::shared_ptr bufferTo, + vk::Buffer *bufferFrom, + vk::Buffer *bufferTo, vk::DeviceSize bufferSize, vk::BufferCopy copyRegion); void recordBufferMemoryBarrier(const vk::CommandBuffer& commandBuffer, @@ -294,9 +283,6 @@ class Tensor vk::MemoryPropertyFlags getPrimaryMemoryPropertyFlags(); vk::BufferUsageFlags getStagingBufferUsageFlags(); vk::MemoryPropertyFlags getStagingMemoryPropertyFlags(); - - void mapRawData(); - void unmapRawData(); }; template @@ -304,51 +290,8 @@ class TensorT : public Tensor { public: - TensorT(std::shared_ptr physicalDevice, - std::shared_ptr device, - const std::vector& data, - vk::DeviceMemory *deviceMemory, - vk::Buffer *buffer, - const TensorTypes& tensorType = TensorTypes::eDevice) - : Tensor(physicalDevice, - device, - (void*)data.data(), - data.size(), - sizeof(T), - this->dataType(), - deviceMemory, - buffer, - tensorType) - { - KP_LOG_DEBUG("Kompute TensorT constructor with data size {}", - data.size()); - } - ~TensorT() { KP_LOG_DEBUG("Kompute TensorT destructor"); } - T* data() { return (T*)this->mRawData; } - - std::vector vector() - { - return { (T*)this->mRawData, ((T*)this->mRawData) + this->size() }; - } - - T& operator[](int index) { return *(((T*)this->mRawData) + index); } - - void setData(const std::vector& data) - { - - KP_LOG_DEBUG("Kompute TensorT setting data with data size {}", - data.size()); - - if (data.size() != this->mSize) { - throw std::runtime_error( - "Kompute TensorT Cannot set data of different sizes"); - } - - Tensor::setRawData(data.data()); - } - TensorDataTypes dataType(); }; diff --git a/src/include/kompute/operations/OpBufferSyncDevice.hpp b/src/include/kompute/operations/OpBufferSyncDevice.hpp new file mode 100644 index 000000000..d4bd74ea5 --- /dev/null +++ b/src/include/kompute/operations/OpBufferSyncDevice.hpp @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include "kompute/Core.hpp" +#include "kompute/Tensor.hpp" +#include "kompute/operations/OpBase.hpp" + +namespace kp { + +class OpBufferSyncDevice : public OpBase +{ + public: + OpBufferSyncDevice( + vk::Buffer *primaryBuffer, + vk::Buffer *stagingBuffer, + vk::DeviceSize size); + + /** + * Default destructor. This class does not manage memory so it won't be + * expecting the parent to perform a release. + */ + ~OpBufferSyncDevice() override; + + /** + * For device buffers, it records the copy command for the buffer to copy + * the data from its staging to device memory. + * + * @param commandBuffer The command buffer to record the command into. + */ + void record(const vk::CommandBuffer& commandBuffer) override; + + /** + * Does not perform any preEval commands. + * + * @param commandBuffer The command buffer to record the command into. + */ + virtual void preEval(const vk::CommandBuffer& commandBuffer) override; + + /** + * Does not perform any postEval commands. + * + * @param commandBuffer The command buffer to record the command into. + */ + virtual void postEval(const vk::CommandBuffer& commandBuffer) override; + + private: + vk::Buffer *mPrimaryBuffer; + vk::Buffer *mStagingBuffer; + vk::DeviceSize mSize; +}; + +} // End namespace kp diff --git a/src/include/kompute/operations/OpTensorSyncDevice.hpp b/src/include/kompute/operations/OpTensorSyncDevice.hpp index 3a1792ac6..9b39e490f 100644 --- a/src/include/kompute/operations/OpTensorSyncDevice.hpp +++ b/src/include/kompute/operations/OpTensorSyncDevice.hpp @@ -58,6 +58,9 @@ class OpTensorSyncDevice : public OpBase private: // -------------- ALWAYS OWNED RESOURCES std::vector> mTensors; + vk::Buffer *mPrimaryBuffer; + vk::Buffer *mStagingBuffer; + vk::DeviceSize mSize; }; } // End namespace kp