Working implementation with tests

This commit is contained in:
Alejandro Saucedo 2021-03-07 08:00:19 +00:00
parent cf7d46cd23
commit f02b9d6915
21 changed files with 297 additions and 216 deletions

View file

@ -75,13 +75,13 @@ class Manager
* @returns Shared pointer with initialised tensor
*/
template <typename T>
std::shared_ptr<TensorView<T>> tensor(
std::shared_ptr<TensorT<T>> tensorT(
const std::vector<T>& data,
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice)
{
KP_LOG_DEBUG("Kompute Manager tensor creation triggered");
std::shared_ptr<TensorView<T>> tensor{ new kp::TensorView<T>(
std::shared_ptr<TensorT<T>> tensor{ new kp::TensorT<T>(
this->mPhysicalDevice, this->mDevice, data, tensorType) };
if (this->mManageResources) {
@ -91,6 +91,13 @@ class Manager
return tensor;
}
std::shared_ptr<TensorT<float>> tensor(
const std::vector<float>& data,
Tensor::TensorTypes tensorType = Tensor::TensorTypes::eDevice)
{
return this->tensorT<float>(data, tensorType);
}
/**
* Create a managed algorithm that will be destroyed by this manager
* if it hasn't been destroyed by its reference count going to zero.

View file

@ -183,11 +183,21 @@ class Tensor
return this->mDataType;
}
// TODO: move to cpp
const void* getRawData() {
void* rawData() {
return this->mRawData;
}
// TODO: move to cpp
template <typename T>
T* data() {
return this->mRawData;
}
template <typename T>
std::vector<T> vector() {
return { (T*)this->mRawData, ((T*)this->mRawData) + this->size() };
}
/**
* Sets / resets the vector data of the tensor. This function does not
* perform any copies into GPU memory and is only performed on the host.
@ -198,6 +208,14 @@ class Tensor
memcpy(this->mRawData, data, this->memorySize());
}
protected:
// -------------- ALWAYS OWNED RESOURCES
TensorTypes mTensorType;
TensorDataTypes mDataType;
uint32_t mSize;
uint32_t mDataTypeMemorySize;
void* mRawData;
private:
void rawMapData() {
@ -236,13 +254,6 @@ class Tensor
std::shared_ptr<vk::DeviceMemory> mStagingMemory;
bool mFreeStagingMemory = false;
// -------------- ALWAYS OWNED RESOURCES
TensorTypes mTensorType;
TensorDataTypes mDataType;
uint32_t mSize;
uint32_t mDataTypeMemorySize;
void* mRawData;
void allocateMemoryCreateGPUResources(); // Creates the vulkan buffer
void createBuffer(std::shared_ptr<vk::Buffer> buffer,
vk::BufferUsageFlags bufferUsageFlags);
@ -266,10 +277,11 @@ class Tensor
// TODO: Limit T to be only float, bool, double, etc
template <typename T>
class TensorView: public Tensor
class TensorT: public Tensor
{
public:
TensorView(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
TensorT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
const std::vector<T>& data,
const TensorTypes& tensorType = TensorTypes::eDevice)
@ -278,35 +290,42 @@ class TensorView: public Tensor
(void*)data.data(),
data.size(),
sizeof(T),
this->dataType())
this->dataType(),
tensorType)
{
KP_LOG_DEBUG("Kompute TensorView constructor with data size {}", data.size());
KP_LOG_DEBUG("Kompute TensorT constructor with data size {}", data.size());
}
~TensorView() {
KP_LOG_DEBUG("Kompute TensorView destructor");
~TensorT() {
KP_LOG_DEBUG("Kompute TensorT destructor");
}
std::vector<T> data() {
return { (T*)this->getRawData(), ((T*)this->getRawData()) + this->size() };
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];
return *(((T*)this->mRawData) + index);
}
void setData(const std::vector<T>& data) {
KP_LOG_DEBUG("Kompute TensorView setting data with data size {}", data.size());
KP_LOG_DEBUG("Kompute TensorT setting data with data size {}", data.size());
if (data.size() != this->mSize) {
throw std::runtime_error(
"Kompute TensorView Cannot set data of different sizes");
"Kompute TensorT Cannot set data of different sizes");
}
Tensor::setRawData(this->mData.data(), this->mData.size(), sizeof(T));
Tensor::setRawData(data.data());
}
TensorDataTypes dataType();
};
} // End namespace kp