Skip to content

Commit

Permalink
Merge pull request #369 from ThePseudo/master
Browse files Browse the repository at this point in the history
Issue #925: Added noncopy constructor for Tensor and TensorT
  • Loading branch information
axsaucedo authored Aug 20, 2024
2 parents 40f9adf + 5f6777b commit 1748b82
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
this->rebuild(data, elementTotalCount, elementMemorySize);
}

Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t elementTotalCount,
uint32_t elementMemorySize,
const TensorDataTypes& dataType,
const TensorTypes& tensorType)
{
KP_LOG_DEBUG("Kompute Tensor constructor data length: {}, and type: {}",
elementTotalCount,
Tensor::toString(tensorType));

this->mPhysicalDevice = physicalDevice;
this->mDevice = device;
this->mDataType = dataType;
this->mTensorType = tensorType;

this->reserve(elementTotalCount, elementMemorySize);
}

Tensor::~Tensor()
{
KP_LOG_DEBUG("Kompute Tensor destructor started. Type: {}",
Expand All @@ -70,6 +89,23 @@ Tensor::~Tensor()
KP_LOG_DEBUG("Kompute Tensor destructor success");
}

void
Tensor::reserve(uint32_t elementTotalCount, uint32_t elementMemorySize)
{
KP_LOG_DEBUG("Reserving {} bytes for memory", elementTotalCount);

this->mSize = elementTotalCount;
this->mDataTypeMemorySize = elementMemorySize;

if (this->mPrimaryBuffer || this->mPrimaryMemory) {
KP_LOG_DEBUG(
"Kompute Tensor destroying existing resources before rebuild");
this->destroy();
}

this->allocateMemoryCreateGPUResources();
}

void
Tensor::rebuild(void* data,
uint32_t elementTotalCount,
Expand Down
20 changes: 20 additions & 0 deletions src/include/kompute/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ class Manager
return tensor;
}

std::shared_ptr<Tensor> tensor(
uint32_t elementTotalCount,
uint32_t elementMemorySize,
const Tensor::TensorDataTypes& dataType,
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice)
{
std::shared_ptr<Tensor> tensor{ new kp::Tensor(this->mPhysicalDevice,
this->mDevice,
elementTotalCount,
elementMemorySize,
dataType,
tensorType) };

if (this->mManageResources) {
this->mManagedTensors.push_back(tensor);
}

return tensor;
}

/**
* Default non-template function that can be used to create algorithm
* objects which provides default types to the push and spec constants as
Expand Down
42 changes: 41 additions & 1 deletion src/include/kompute/Tensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ class Tensor
const TensorDataTypes& dataType,
const TensorTypes& tensorType = TensorTypes::eDevice);

/**
* Constructor with size provided which would be used to create the
* respective vulkan buffer and memory. Data is not copied.
*
* @param physicalDevice The physical device to use to fetch properties
* @param device The device to use to create the buffer and memory from
* @param elmentTotalCount the number of elements of the array
* @param elementMemorySize the size of the element
* @param tensorTypes Type for the tensor which is of type TensorTypes
*/
Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t elementTotalCount,
uint32_t elementMemorySize,
const TensorDataTypes& dataType,
const TensorTypes& tensorType = TensorTypes::eDevice);

/**
* Destructor which is in charge of freeing vulkan resources unless they
* have been provided externally.
Expand All @@ -79,6 +96,15 @@ class Tensor
uint32_t elementTotalCount,
uint32_t elementMemorySize);

/**
* Function to reserve memory on the tensor. This does not copy any data, it
* just reserves memory, similarly to std::vector reserve() method.
*
* @param elementTotalCount Total number of elements for new size
* @param elementMemorySize Memory size of element
*/
void reserve(uint32_t elementTotalCount, uint32_t elementMemorySize);

/**
* Destroys and frees the GPU resources which include the buffer and memory.
*/
Expand Down Expand Up @@ -301,6 +327,20 @@ class TensorT : public Tensor
{

public:
TensorT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
const size_t size,
const TensorTypes& tensorType = TensorTypes::eDevice)
: Tensor(physicalDevice,
device,
size,
sizeof(T),
this->dataType(),
tensorType)
{
KP_LOG_DEBUG("Kompute TensorT constructor with data size {}", size);
}

TensorT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
const std::vector<T>& data,
Expand All @@ -313,7 +353,7 @@ class TensorT : public Tensor
this->dataType(),
tensorType)
{
KP_LOG_DEBUG("Kompute TensorT constructor with data size {}",
KP_LOG_DEBUG("Kompute TensorT filling constructor with data size {}",
data.size());
}

Expand Down
8 changes: 8 additions & 0 deletions test/TestTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ TEST(TestTensor, ConstructorData)
EXPECT_EQ(tensor->vector(), vec);
}

TEST(TestTensor, ReserveData)
{
kp::Manager mgr;
std::shared_ptr<kp::TensorT<float>> tensor = mgr.tensor(3, sizeof(uint32_t), Tensor::TensorDataType::eUnsignedInt);
EXPECT_EQ(tensor->size(), 0);
EXPECT_EQ(tensor->dataTypeMemorySize(), sizeof(uint32_t));
}

TEST(TestTensor, DataTypes)
{
kp::Manager mgr;
Expand Down

0 comments on commit 1748b82

Please sign in to comment.