Major refactor to kompute allowing controlling buffers outside of the tensor object.
This commit is contained in:
parent
5db4a58550
commit
7ac0862445
13 changed files with 234 additions and 284 deletions
|
|
@ -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<uint32_t>(descriptorPoolSizes.size()),
|
||||
descriptorPoolSizes.data());
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ add_library(kompute Algorithm.cpp
|
|||
OpTensorCopy.cpp
|
||||
OpTensorSyncDevice.cpp
|
||||
OpTensorSyncLocal.cpp
|
||||
OpBufferSyncDevice.cpp
|
||||
Sequence.cpp
|
||||
Tensor.cpp
|
||||
Core.cpp)
|
||||
|
|
|
|||
43
src/OpBufferSyncDevice.cpp
Normal file
43
src/OpBufferSyncDevice.cpp
Normal file
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -6,6 +6,8 @@ namespace kp {
|
|||
|
||||
OpTensorSyncDevice::OpTensorSyncDevice(
|
||||
const std::vector<std::shared_ptr<Tensor>>& tensors)
|
||||
: mPrimaryBuffer(nullptr)
|
||||
, mStagingBuffer(nullptr)
|
||||
{
|
||||
KP_LOG_DEBUG("Kompute OpTensorSyncDevice constructor with params");
|
||||
|
||||
|
|
|
|||
200
src/Tensor.cpp
200
src/Tensor.cpp
|
|
@ -44,8 +44,11 @@ Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> 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<vk::PhysicalDevice> 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<vk::DeviceMemory> 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<vk::DeviceMemory> 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<Tensor> 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<vk::Buffer> bufferFrom,
|
||||
std::shared_ptr<vk::Buffer> 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<vk::Buffer>();
|
||||
this->createBuffer(this->mPrimaryBuffer,
|
||||
this->getPrimaryBufferUsageFlags());
|
||||
this->mFreePrimaryBuffer = true;
|
||||
this->mPrimaryMemory = std::make_shared<vk::DeviceMemory>();
|
||||
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<vk::Buffer>(stagingBuffer);
|
||||
this->mFreeStagingBuffer = true;
|
||||
this->mStagingMemory = std::shared_ptr<vk::DeviceMemory>(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<vk::Buffer> 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<vk::Buffer> buffer,
|
||||
std::shared_ptr<vk::DeviceMemory> 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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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<vk::WriteDescriptorSet> 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();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -81,14 +81,16 @@ class Manager
|
|||
template<typename T>
|
||||
std::shared_ptr<TensorT<T>> tensorT(
|
||||
const std::vector<T>& 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<TensorT<T>> tensor{ new kp::TensorT<T>(
|
||||
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<TensorT<float>> tensor(
|
||||
const std::vector<float>& data,
|
||||
vk::DeviceMemory *deviceMemory,
|
||||
vk::Buffer *buffer,
|
||||
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice)
|
||||
{
|
||||
return this->tensorT<float>(data, deviceMemory, buffer, tensorType);
|
||||
}
|
||||
|
||||
std::shared_ptr<Tensor> 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> tensor{ new kp::Tensor(this->mPhysicalDevice,
|
||||
this->mDevice,
|
||||
data,
|
||||
elementTotalCount,
|
||||
elementMemorySize,
|
||||
memorySize,
|
||||
dataType,
|
||||
deviceMemory,
|
||||
buffer,
|
||||
primaryMemory,
|
||||
primaryBuffer,
|
||||
stagingMemory,
|
||||
stagingBuffer,
|
||||
offset,
|
||||
tensorType) };
|
||||
|
||||
if (this->mManageResources) {
|
||||
|
|
|
|||
|
|
@ -120,6 +120,17 @@ class Sequence : public std::enable_shared_from_this<Sequence>
|
|||
std::shared_ptr<T> op{ new T(tensors, std::forward<TArgs>(params)...) };
|
||||
return this->eval(op);
|
||||
}
|
||||
|
||||
template<typename T, typename... TArgs>
|
||||
std::shared_ptr<Sequence> eval(vk::Buffer *primaryBuffer,
|
||||
vk::Buffer *stagingBuffer,
|
||||
vk::DeviceSize size,
|
||||
TArgs&&... params)
|
||||
{
|
||||
std::shared_ptr<T> op{ new T(primaryBuffer, stagingBuffer, size, std::forward<TArgs>(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.
|
||||
|
|
|
|||
|
|
@ -57,10 +57,13 @@ class Tensor
|
|||
std::shared_ptr<vk::Device> 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<vk::PhysicalDevice> mPhysicalDevice;
|
||||
std::shared_ptr<vk::Device> mDevice;
|
||||
vk::Buffer *mPrimaryBuffer = nullptr;
|
||||
vk::Buffer *mStagingBuffer = nullptr;
|
||||
vk::DeviceMemory *mPrimaryMemory = nullptr;
|
||||
vk::DeviceMemory *mStagingMemory = nullptr;
|
||||
|
||||
// -------------- OPTIONALLY OWNED RESOURCES
|
||||
std::shared_ptr<vk::Buffer> mPrimaryBuffer;
|
||||
bool mFreePrimaryBuffer = false;
|
||||
std::shared_ptr<vk::Buffer> mStagingBuffer;
|
||||
bool mFreeStagingBuffer = false;
|
||||
std::shared_ptr<vk::DeviceMemory> mPrimaryMemory;
|
||||
bool mFreePrimaryMemory = false;
|
||||
std::shared_ptr<vk::DeviceMemory> mStagingMemory;
|
||||
bool mFreeStagingMemory = false;
|
||||
|
||||
void allocateMemoryCreateGPUResources(vk::DeviceMemory *stagingMemory, vk::Buffer *stagingBuffer); // Creates the vulkan buffer
|
||||
void createBuffer(std::shared_ptr<vk::Buffer> buffer,
|
||||
vk::BufferUsageFlags bufferUsageFlags);
|
||||
void allocateBindMemory(std::shared_ptr<vk::Buffer> buffer,
|
||||
std::shared_ptr<vk::DeviceMemory> 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<vk::Buffer> bufferFrom,
|
||||
std::shared_ptr<vk::Buffer> 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<typename T>
|
||||
|
|
@ -304,51 +290,8 @@ class TensorT : public Tensor
|
|||
{
|
||||
|
||||
public:
|
||||
TensorT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
|
||||
std::shared_ptr<vk::Device> device,
|
||||
const std::vector<T>& 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<T> vector()
|
||||
{
|
||||
return { (T*)this->mRawData, ((T*)this->mRawData) + this->size() };
|
||||
}
|
||||
|
||||
T& operator[](int index) { return *(((T*)this->mRawData) + index); }
|
||||
|
||||
void setData(const std::vector<T>& 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();
|
||||
};
|
||||
|
||||
|
|
|
|||
52
src/include/kompute/operations/OpBufferSyncDevice.hpp
Normal file
52
src/include/kompute/operations/OpBufferSyncDevice.hpp
Normal file
|
|
@ -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
|
||||
|
|
@ -58,6 +58,9 @@ class OpTensorSyncDevice : public OpBase
|
|||
private:
|
||||
// -------------- ALWAYS OWNED RESOURCES
|
||||
std::vector<std::shared_ptr<Tensor>> mTensors;
|
||||
vk::Buffer *mPrimaryBuffer;
|
||||
vk::Buffer *mStagingBuffer;
|
||||
vk::DeviceSize mSize;
|
||||
};
|
||||
|
||||
} // End namespace kp
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue