From ca64ba37f68c210cf12f6dce158d2d4cfa781fdd Mon Sep 17 00:00:00 2001 From: Robert Quill Date: Tue, 27 Aug 2024 21:08:38 +0100 Subject: [PATCH] Refactor OpTensor/ImageCopy* into a single OpCopy operation Keep seperate files for each type of test. Signed-off-by: Robert Quill --- src/CMakeLists.txt | 7 +- src/Memory.cpp | 47 +++++++-- src/OpCopy.cpp | 67 +++++++++++++ src/OpImageCopy.cpp | 88 ----------------- src/OpImageCopyToTensor.cpp | 98 ------------------- src/OpTensorCopy.cpp | 89 ----------------- src/OpTensorCopyToImage.cpp | 98 ------------------- src/include/CMakeLists.txt | 2 +- src/include/kompute/Image.hpp | 4 +- src/include/kompute/Kompute.hpp | 5 +- src/include/kompute/Memory.hpp | 21 ++++ .../{OpImageCopy.hpp => OpCopy.hpp} | 27 +++-- .../operations/OpImageCopyToTensor.hpp | 65 ------------ .../kompute/operations/OpImageSyncLocal.hpp | 66 ------------- .../kompute/operations/OpTensorCopy.hpp | 63 ------------ .../operations/OpTensorCopyToImage.hpp | 64 ------------ test/CMakeLists.txt | 8 +- ...estOpImageCopy.cpp => TestOpCopyImage.cpp} | 58 ++++------- ...Tensor.cpp => TestOpCopyImageToTensor.cpp} | 68 ++++--------- ...tOpTensorCopy.cpp => TestOpCopyTensor.cpp} | 54 ++++------ ...oImage.cpp => TestOpCopyTensorToImage.cpp} | 68 ++++--------- test/TestOpSync.cpp | 4 +- 22 files changed, 228 insertions(+), 843 deletions(-) create mode 100644 src/OpCopy.cpp delete mode 100644 src/OpImageCopy.cpp delete mode 100644 src/OpImageCopyToTensor.cpp delete mode 100644 src/OpTensorCopy.cpp delete mode 100644 src/OpTensorCopyToImage.cpp rename src/include/kompute/operations/{OpImageCopy.hpp => OpCopy.hpp} (55%) delete mode 100644 src/include/kompute/operations/OpImageCopyToTensor.hpp delete mode 100644 src/include/kompute/operations/OpImageSyncLocal.hpp delete mode 100644 src/include/kompute/operations/OpTensorCopy.hpp delete mode 100644 src/include/kompute/operations/OpTensorCopyToImage.hpp rename test/{TestOpImageCopy.cpp => TestOpCopyImage.cpp} (86%) rename test/{TestOpImageCopyToTensor.cpp => TestOpCopyImageToTensor.cpp} (73%) rename test/{TestOpTensorCopy.cpp => TestOpCopyTensor.cpp} (85%) rename test/{TestOpTensorCopyToImage.cpp => TestOpCopyTensorToImage.cpp} (73%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b58d32e6..36d01505 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,17 +12,14 @@ add_library(kompute Algorithm.cpp Manager.cpp OpAlgoDispatch.cpp OpMemoryBarrier.cpp - OpTensorCopy.cpp - OpTensorCopyToImage.cpp + OpCopy.cpp OpSyncDevice.cpp OpSyncLocal.cpp Sequence.cpp Tensor.cpp Core.cpp Image.cpp - Memory.cpp - OpImageCopy.cpp - OpImageCopyToTensor.cpp) + Memory.cpp) add_library(kompute::kompute ALIAS kompute) diff --git a/src/Memory.cpp b/src/Memory.cpp index 40afbcf6..c086d820 100644 --- a/src/Memory.cpp +++ b/src/Memory.cpp @@ -1,6 +1,8 @@ // SPDX-License-Identifier: Apache-2.0 #include "kompute/Memory.hpp" +#include "kompute/Tensor.hpp" +#include "kompute/Image.hpp" namespace kp { @@ -184,7 +186,7 @@ Memory::unmapRawData() } else if (this->mMemoryType == MemoryTypes::eDevice) { hostVisibleMemory = this->mStagingMemory; } else { - KP_LOG_WARN("Kompute Tensor mapping data not supported on {} memory", + KP_LOG_WARN("Kompute Memory mapping data not supported on {} memory", Memory::toString(this->memoryType())); return; } @@ -239,11 +241,44 @@ Memory::getStagingMemoryPropertyFlags() } } +void +Memory::recordCopyFrom(const vk::CommandBuffer& commandBuffer, + std::shared_ptr copyFromMemory) +{ + std::shared_ptr tensor = std::dynamic_pointer_cast(copyFromMemory); + std::shared_ptr image = std::dynamic_pointer_cast(copyFromMemory); + + if (copyFromMemory->dataType() != this->dataType()) { + throw std::runtime_error(fmt::format( + "Attempting to copy memory of different types from {} to {}", + Memory::toString(copyFromMemory->dataType()), + Memory::toString(this->dataType()))); + } + + if (copyFromMemory->size() != this->size()) { + throw std::runtime_error(fmt::format( + "Attempting to copy tensors of different sizes from {} to {}", + copyFromMemory->size(), + this->size())); + } + + if (tensor) { + this->recordCopyFrom(commandBuffer, tensor); + } + else if (image) { + this->recordCopyFrom(commandBuffer, image); + } + else + { + throw std::runtime_error("Kompute Memory unsupported memory type"); + } +} + void Memory::destroy(void) { // Setting raw data to null regardless whether device is available to - // invalidate Image + // invalidate Memory this->mRawData = nullptr; this->mSize = 0; this->mDataTypeMemorySize = 0; @@ -255,10 +290,10 @@ Memory::destroy(void) if (this->mFreePrimaryMemory) { if (!this->mPrimaryMemory) { - KP_LOG_WARN("Kompose Image expected to free primary memory but " + KP_LOG_WARN("Kompose Memory expected to free primary memory but " "got null memory"); } else { - KP_LOG_DEBUG("Kompose Image freeing primary memory"); + KP_LOG_DEBUG("Kompose Memory freeing primary memory"); this->mDevice->freeMemory( *this->mPrimaryMemory, (vk::Optional)nullptr); @@ -269,10 +304,10 @@ Memory::destroy(void) if (this->mFreeStagingMemory) { if (!this->mStagingMemory) { - KP_LOG_WARN("Kompose Image expected to free staging memory but " + KP_LOG_WARN("Kompose Memory expected to free staging memory but " "got null memory"); } else { - KP_LOG_DEBUG("Kompose Image freeing staging memory"); + KP_LOG_DEBUG("Kompose Memory freeing staging memory"); this->mDevice->freeMemory( *this->mStagingMemory, (vk::Optional)nullptr); diff --git a/src/OpCopy.cpp b/src/OpCopy.cpp new file mode 100644 index 00000000..87df17e5 --- /dev/null +++ b/src/OpCopy.cpp @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: Apache-2.0 + +#include "kompute/operations/OpCopy.hpp" +#include "kompute/Tensor.hpp" +#include "kompute/Image.hpp" + +namespace kp { + +OpCopy::OpCopy(const std::vector>& memObjects) +{ + KP_LOG_DEBUG("Kompute OpCopy constructor with params"); + + if (memObjects.size() < 2) { + throw std::runtime_error( + "Kompute OpCopy called with less than 2 memory objects"); + } + + this->mMemObjects = memObjects; +} + +OpCopy::~OpCopy() +{ + KP_LOG_DEBUG("Kompute OpCopy destructor started"); +} + +void +OpCopy::record(const vk::CommandBuffer& commandBuffer) +{ + KP_LOG_DEBUG("Kompute OpCopy record called"); + + // We iterate from the second memory object onwards and record a copy to all + for (size_t i = 1; i < this->mMemObjects.size(); i++) { + this->mMemObjects[i]->recordCopyFrom(commandBuffer, this->mMemObjects[0]); + } +} + +void +OpCopy::preEval(const vk::CommandBuffer& /*commandBuffer*/) +{ + KP_LOG_DEBUG("Kompute OpCopy preEval called"); +} + +void +OpCopy::postEval(const vk::CommandBuffer& /*commandBuffer*/) +{ + KP_LOG_DEBUG("Kompute OpCopy postEval called"); + + // Do not copy on CPU side if source is storage memory + if (this->mMemObjects[0]->memoryType() == kp::Memory::MemoryTypes::eStorage) { + KP_LOG_DEBUG("Kompute OpCopy not copying tensor source given " + "it's of eStorage type"); + return; + } + + // Copy the data from the first memory object into all the memory objects + for (size_t i = 1; i < this->mMemObjects.size(); i++) { + if (this->mMemObjects[i]->memoryType() == + kp::Memory::MemoryTypes::eStorage) { + KP_LOG_DEBUG("Kompute OpCopy not copying to tensor dest " + "given it's of eStorage type"); + continue; + } + this->mMemObjects[i]->setData(this->mMemObjects[0]->rawData(),this->mMemObjects[0]->memorySize()); + } +} + +} diff --git a/src/OpImageCopy.cpp b/src/OpImageCopy.cpp deleted file mode 100644 index b6c8327d..00000000 --- a/src/OpImageCopy.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#include "kompute/operations/OpImageCopy.hpp" -#include "kompute/Image.hpp" - -namespace kp { - -OpImageCopy::OpImageCopy(const std::vector>& images) -{ - KP_LOG_DEBUG("Kompute OpImageCopy constructor with params"); - - if (images.size() < 2) { - throw std::runtime_error( - "Kompute OpImageCopy called with less than 2 images"); - } - - for (std::shared_ptr image : images) { - if (std::dynamic_pointer_cast(image) == nullptr) { - throw std::runtime_error( - "Kompute OpImageCopy: Memory object is not an Image"); - } - this->mImages.push_back(std::dynamic_pointer_cast(image)); - } - - kp::Image::DataTypes dataType = this->mImages[0]->dataType(); - uint32_t size = this->mImages[0]->size(); - for (const std::shared_ptr& image : this->mImages) { - if (image->dataType() != dataType) { - throw std::runtime_error(fmt::format( - "Attempting to copy images of different types from {} to {}", - Image::toString(dataType), - Image::toString(image->dataType()))); - } - if (image->size() != size) { - throw std::runtime_error(fmt::format( - "Attempting to copy images of different sizes from {} to {}", - size, - image->size())); - } - } -} - -OpImageCopy::~OpImageCopy() -{ - KP_LOG_DEBUG("Kompute OpImageCopy destructor started"); -} - -void -OpImageCopy::record(const vk::CommandBuffer& commandBuffer) -{ - KP_LOG_DEBUG("Kompute OpImageCopy record called"); - - // We iterate from the second image onwards and record a copy to all - for (size_t i = 1; i < this->mImages.size(); i++) { - this->mImages[i]->recordCopyFrom(commandBuffer, this->mImages[0]); - } -} - -void -OpImageCopy::preEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpImageCopy preEval called"); -} - -void -OpImageCopy::postEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpImageCopy postEval called"); - - // Do not copy on CPU side if source is storage image - if (this->mImages[0]->memoryType() == Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG("Kompute OpImageCopy not copying image source given " - "it's of eStorage type"); - return; - } - - // Copy the data from the first image into all the images - for (size_t i = 1; i < this->mImages.size(); i++) { - if (this->mImages[i]->memoryType() == Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG("Kompute OpImageCopy not copying to image dest " - "given it's of eStorage type"); - continue; - } - this->mImages[i]->setData(this->mImages[0]->rawData(),this->mImages[0]->memorySize()); - } -} - -} diff --git a/src/OpImageCopyToTensor.cpp b/src/OpImageCopyToTensor.cpp deleted file mode 100644 index a3c793b3..00000000 --- a/src/OpImageCopyToTensor.cpp +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#include "kompute/operations/OpImageCopyToTensor.hpp" -#include "kompute/Tensor.hpp" - -namespace kp { - -OpImageCopyToTensor::OpImageCopyToTensor( - const std::vector>& mem_objects) -{ - KP_LOG_DEBUG("Kompute OpImageCopyToTensor constructor with params"); - - if (mem_objects.size() < 2) { - throw std::runtime_error( - "Kompute OpImageCopyToTensor called with less than 2 mem objects"); - } - - if (std::dynamic_pointer_cast(mem_objects.at(0)) == nullptr) { - throw std::runtime_error( - "Kompute OpImageCopyToTensor: Memory object is not an Image"); - } - this->mImage = std::dynamic_pointer_cast(mem_objects.at(0)); - - for (size_t i = 1; i < mem_objects.size(); i++) { - if (std::dynamic_pointer_cast(mem_objects.at(i)) == nullptr) { - throw std::runtime_error( - "Kompute OpImageCopyToTensor: Memory object is not a Tensor"); - } - this->mTensors.push_back( - std::dynamic_pointer_cast(mem_objects.at(i))); - } - - kp::Image::DataTypes dataType = this->mImage->dataType(); - uint32_t size = this->mImage->size(); - for (const std::shared_ptr& tensor : this->mTensors) { - if (dataType != tensor->dataType()) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different types from {} to {}", - Image::toString(dataType), - Tensor::toString(tensor->dataType()))); - } - if (tensor->size() != size) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different sizes from {} to {}", - size, - tensor->size())); - } - } -} - -OpImageCopyToTensor::~OpImageCopyToTensor() -{ - KP_LOG_DEBUG("Kompute OpImageCopyToTensor destructor started"); -} - -void -OpImageCopyToTensor::record(const vk::CommandBuffer& commandBuffer) -{ - KP_LOG_DEBUG("Kompute OpImageCopyToTensor record called"); - - for (size_t i = 0; i < this->mTensors.size(); i++) { - this->mTensors[i]->recordCopyFrom(commandBuffer, this->mImage); - } -} - -void -OpImageCopyToTensor::preEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpImageCopyToTensor preEval called"); -} - -void -OpImageCopyToTensor::postEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpImageCopyToTensor postEval called"); - - // Do not copy on CPU side if source is storage tensor - if (this->mImage->memoryType() == kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG( - "Kompute OpImageCopyToTensor not copying tensor source given " - "it's of eStorage type"); - return; - } - - // Copy the data from the tensor into all the images - for (size_t i = 0; i < this->mTensors.size(); i++) { - if (this->mTensors[i]->memoryType() == - kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG( - "Kompute OpImageCopyToTensor not copying to tensor dest " - "given it's of eStorage type"); - continue; - } - this->mTensors[i]->setData(this->mImage->rawData(),this->mImage->memorySize()); - } -} - -} diff --git a/src/OpTensorCopy.cpp b/src/OpTensorCopy.cpp deleted file mode 100644 index 868f9ebb..00000000 --- a/src/OpTensorCopy.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#include "kompute/operations/OpTensorCopy.hpp" -#include "kompute/Tensor.hpp" - -namespace kp { - -OpTensorCopy::OpTensorCopy(const std::vector>& tensors) -{ - KP_LOG_DEBUG("Kompute OpTensorCopy constructor with params"); - - if (tensors.size() < 2) { - throw std::runtime_error( - "Kompute OpTensorCopy called with less than 2 tensors"); - } - - for (std::shared_ptr tensor : tensors) { - if (std::dynamic_pointer_cast(tensor) == nullptr) { - throw std::runtime_error( - "Kompute OpTensorCopy: Memory object is not a Tensor"); - } - this->mTensors.push_back(std::dynamic_pointer_cast(tensor)); - } - - kp::Memory::DataTypes dataType = this->mTensors[0]->dataType(); - uint32_t size = this->mTensors[0]->size(); - for (const std::shared_ptr& tensor : this->mTensors) { - if (tensor->dataType() != dataType) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different types from {} to {}", - Tensor::toString(dataType), - Tensor::toString(tensor->dataType()))); - } - if (tensor->size() != size) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different sizes from {} to {}", - size, - tensor->size())); - } - } -} - -OpTensorCopy::~OpTensorCopy() -{ - KP_LOG_DEBUG("Kompute OpTensorCopy destructor started"); -} - -void -OpTensorCopy::record(const vk::CommandBuffer& commandBuffer) -{ - KP_LOG_DEBUG("Kompute OpTensorCopy record called"); - - // We iterate from the second tensor onwards and record a copy to all - for (size_t i = 1; i < this->mTensors.size(); i++) { - this->mTensors[i]->recordCopyFrom(commandBuffer, this->mTensors[0]); - } -} - -void -OpTensorCopy::preEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpTensorCopy preEval called"); -} - -void -OpTensorCopy::postEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpTensorCopy postEval called"); - - // Do not copy on CPU side if source is storage tensor - if (this->mTensors[0]->memoryType() == kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG("Kompute OpTensorCopy not copying tensor source given " - "it's of eStorage type"); - return; - } - - // Copy the data from the first tensor into all the tensors - for (size_t i = 1; i < this->mTensors.size(); i++) { - if (this->mTensors[i]->memoryType() == - kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG("Kompute OpTensorCopy not copying to tensor dest " - "given it's of eStorage type"); - continue; - } - this->mTensors[i]->setData(this->mTensors[0]->rawData(),this->mTensors[0]->memorySize()); - } -} - -} diff --git a/src/OpTensorCopyToImage.cpp b/src/OpTensorCopyToImage.cpp deleted file mode 100644 index d746130b..00000000 --- a/src/OpTensorCopyToImage.cpp +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -#include "kompute/operations/OpTensorCopyToImage.hpp" -#include "kompute/Tensor.hpp" - -namespace kp { - -OpTensorCopyToImage::OpTensorCopyToImage( - const std::vector>& mem_objects) -{ - KP_LOG_DEBUG("Kompute OpTensorCopyToImage constructor with params"); - - if (mem_objects.size() < 2) { - throw std::runtime_error( - "Kompute OpTensorCopyToImage called with less than 2 mem objects"); - } - - if (std::dynamic_pointer_cast(mem_objects.at(0)) == nullptr) { - throw std::runtime_error( - "Kompute OpTensorCopyToImage: Memory object is not a Tensor"); - } - this->mTensor = std::dynamic_pointer_cast(mem_objects.at(0)); - - for (size_t i = 1; i < mem_objects.size(); i++) { - if (std::dynamic_pointer_cast(mem_objects.at(i)) == nullptr) { - throw std::runtime_error( - "Kompute OpTensorCopyToImage: Memory object is not an Image"); - } - this->mImages.push_back( - std::dynamic_pointer_cast(mem_objects.at(i))); - } - - kp::Memory::DataTypes dataType = this->mTensor->dataType(); - uint32_t size = this->mTensor->size(); - for (const std::shared_ptr& image : this->mImages) { - if (image->dataType() != dataType) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different types from {} to {}", - Tensor::toString(dataType), - Image::toString(image->dataType()))); - } - if (image->size() != size) { - throw std::runtime_error(fmt::format( - "Attempting to copy tensors of different sizes from {} to {}", - size, - image->size())); - } - } -} - -OpTensorCopyToImage::~OpTensorCopyToImage() -{ - KP_LOG_DEBUG("Kompute OpTensorCopyToImage destructor started"); -} - -void -OpTensorCopyToImage::record(const vk::CommandBuffer& commandBuffer) -{ - KP_LOG_DEBUG("Kompute OpTensorCopyToImage record called"); - - for (size_t i = 0; i < this->mImages.size(); i++) { - this->mImages[i]->recordCopyFrom(commandBuffer, this->mTensor); - } -} - -void -OpTensorCopyToImage::preEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpTensorCopyToImage preEval called"); -} - -void -OpTensorCopyToImage::postEval(const vk::CommandBuffer& /*commandBuffer*/) -{ - KP_LOG_DEBUG("Kompute OpTensorCopyToImage postEval called"); - - // Do not copy on CPU side if source is storage tensor - if (this->mTensor->memoryType() == kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG( - "Kompute OpTensorCopyToImage not copying tensor source given " - "it's of eStorage type"); - return; - } - - // Copy the data from the tensor into all the images - for (size_t i = 0; i < this->mImages.size(); i++) { - if (this->mImages[i]->memoryType() == - kp::Memory::MemoryTypes::eStorage) { - KP_LOG_DEBUG( - "Kompute OpTensorCopyToImage not copying to tensor dest " - "given it's of eStorage type"); - continue; - } - this->mImages[i]->setData(this->mTensor->rawData(),this->mTensor->memorySize()); - } -} - -} diff --git a/src/include/CMakeLists.txt b/src/include/CMakeLists.txt index b55f9f15..72f82d67 100644 --- a/src/include/CMakeLists.txt +++ b/src/include/CMakeLists.txt @@ -20,7 +20,7 @@ target_sources(kompute PRIVATE kompute/operations/OpBase.hpp kompute/operations/OpMemoryBarrier.hpp kompute/operations/OpMult.hpp - kompute/operations/OpTensorCopy.hpp + kompute/operations/OpCopy.hpp kompute/operations/OpSyncDevice.hpp kompute/operations/OpSyncLocal.hpp diff --git a/src/include/kompute/Image.hpp b/src/include/kompute/Image.hpp index 03c6e3eb..39c8af3f 100644 --- a/src/include/kompute/Image.hpp +++ b/src/include/kompute/Image.hpp @@ -180,7 +180,7 @@ class Image : public Memory * @param copyFromImage Image to copy the data from */ void recordCopyFrom(const vk::CommandBuffer& commandBuffer, - std::shared_ptr copyFromImage); + std::shared_ptr copyFromImage) override; /** * Records a copy from the memory of the tensor provided to the current @@ -191,7 +191,7 @@ class Image : public Memory * @param copyFromTensor Tensor to copy the data from */ void recordCopyFrom(const vk::CommandBuffer& commandBuffer, - std::shared_ptr copyFromTensor); + std::shared_ptr copyFromTensor) override; /** * Records a copy from the internal staging memory to the device memory diff --git a/src/include/kompute/Kompute.hpp b/src/include/kompute/Kompute.hpp index 9eb7341e..6253d0ca 100644 --- a/src/include/kompute/Kompute.hpp +++ b/src/include/kompute/Kompute.hpp @@ -9,12 +9,9 @@ #include "operations/OpAlgoDispatch.hpp" #include "operations/OpBase.hpp" -#include "operations/OpImageCopy.hpp" -#include "operations/OpImageCopyToTensor.hpp" +#include "operations/OpCopy.hpp" #include "operations/OpMemoryBarrier.hpp" #include "operations/OpMult.hpp" -#include "operations/OpTensorCopy.hpp" -#include "operations/OpTensorCopyToImage.hpp" #include "operations/OpSyncDevice.hpp" #include "operations/OpSyncLocal.hpp" diff --git a/src/include/kompute/Memory.hpp b/src/include/kompute/Memory.hpp index 4811e41c..162b343b 100644 --- a/src/include/kompute/Memory.hpp +++ b/src/include/kompute/Memory.hpp @@ -8,6 +8,10 @@ namespace kp { +// Forward declare the Tensor and Image classes +class Tensor; +class Image; + class Memory { // This is the base class for Tensors and Images. @@ -145,6 +149,18 @@ class Memory vk::PipelineStageFlagBits srcStageMask, vk::PipelineStageFlagBits dstStageMask) = 0; + /** + * Records a copy from the memory provided to the current + * memory. This is intended to pass memory into a processing, to perform + * a staging image transfer, or to gather output (between others). + * + * @param commandBuffer Vulkan Command Buffer to record the commands into + * @param copyFromMemory Memory to copy the data from + */ +void +recordCopyFrom(const vk::CommandBuffer& commandBuffer, + std::shared_ptr copyFromMemory); + /** * Adds this object to a Vulkan descriptor set at \p binding. * @@ -280,6 +296,11 @@ class Memory void updateRawData(void* data); vk::MemoryPropertyFlags getPrimaryMemoryPropertyFlags(); vk::MemoryPropertyFlags getStagingMemoryPropertyFlags(); + + virtual void recordCopyFrom(const vk::CommandBuffer& commandBuffer, + std::shared_ptr copyFromMemory) = 0; + virtual void recordCopyFrom(const vk::CommandBuffer& commandBuffer, + std::shared_ptr copyFromMemory) = 0; }; } // End namespace kp diff --git a/src/include/kompute/operations/OpImageCopy.hpp b/src/include/kompute/operations/OpCopy.hpp similarity index 55% rename from src/include/kompute/operations/OpImageCopy.hpp rename to src/include/kompute/operations/OpCopy.hpp index 424480a8..5e92c3f7 100644 --- a/src/include/kompute/operations/OpImageCopy.hpp +++ b/src/include/kompute/operations/OpCopy.hpp @@ -3,38 +3,37 @@ #include "kompute/Core.hpp" -#include "kompute/Image.hpp" +#include "kompute/Memory.hpp" #include "kompute/operations/OpBase.hpp" namespace kp { /** - * Operation that copies the data from the first image to the rest of the - * images provided, using a record command for all the vectors. This operation - * does not own/manage the memory of the images passed to it. The operation - * must only receive images of type + * Operation that copies the data from the first memory object to the rest of the + * memory objects provided, using a record command for all the vectors. This operation + * does not own/manage the memory of the memory objects passed to it. */ -class OpImageCopy : public OpBase +class OpCopy : public OpBase { public: /** * Default constructor with parameters that provides the core vulkan - * resources and the images that will be used in the operation. + * resources and the memory objects that will be used in the operation. * - * @param images Images that will be used to create in operation. + * @param memObjects memory objects that will be used to create in operation. */ - OpImageCopy(const std::vector>& images); + OpCopy(const std::vector>& memObjects); /** * Default destructor. This class does not manage memory so it won't be * expecting the parent to perform a release. */ - ~OpImageCopy() override; + ~OpCopy() override; /** - * Records the copy commands from the first image into all the other - * images provided. Also optionally records a barrier. + * Records the copy commands from the first memory object into all the other + * memory objects provided. Also optionally records a barrier. * * @param commandBuffer The command buffer to record the command into. */ @@ -48,7 +47,7 @@ class OpImageCopy : public OpBase virtual void preEval(const vk::CommandBuffer& commandBuffer) override; /** - * Copies the local vectors for all the images to sync the data with the + * Copies the local vectors for all the memory objects to sync the data with the * gpu. * * @param commandBuffer The command buffer to record the command into. @@ -57,7 +56,7 @@ class OpImageCopy : public OpBase private: // -------------- ALWAYS OWNED RESOURCES - std::vector> mImages; + std::vector> mMemObjects; }; } // End namespace kp diff --git a/src/include/kompute/operations/OpImageCopyToTensor.hpp b/src/include/kompute/operations/OpImageCopyToTensor.hpp deleted file mode 100644 index a503dbbb..00000000 --- a/src/include/kompute/operations/OpImageCopyToTensor.hpp +++ /dev/null @@ -1,65 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include "kompute/Core.hpp" - -#include "kompute/Image.hpp" - -#include "kompute/operations/OpBase.hpp" - -namespace kp { - -/** - * Operation that copies the data from the first image to the rest of the - * images provided, using a record command for all the vectors. This operation - * does not own/manage the memory of the images passed to it. The operation - * must only receive images of type - */ -class OpImageCopyToTensor : public OpBase -{ - public: - /** - * Default constructor with parameters that provides the core vulkan - * resources and the images that will be used in the operation. - * - * @param images Images that will be used to create in operation. - */ - OpImageCopyToTensor( - const std::vector>& mem_objects); - - /** - * Default destructor. This class does not manage memory so it won't be - * expecting the parent to perform a release. - */ - ~OpImageCopyToTensor() override; - - /** - * Records the copy commands from the first image into all the other - * images provided. Also optionally records a barrier. - * - * @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; - - /** - * Copies the local vectors for all the images to sync the data with the - * gpu. - * - * @param commandBuffer The command buffer to record the command into. - */ - virtual void postEval(const vk::CommandBuffer& commandBuffer) override; - - private: - // -------------- ALWAYS OWNED RESOURCES - std::shared_ptr mImage; - std::vector> mTensors; -}; - -} // End namespace kp diff --git a/src/include/kompute/operations/OpImageSyncLocal.hpp b/src/include/kompute/operations/OpImageSyncLocal.hpp deleted file mode 100644 index e041af0f..00000000 --- a/src/include/kompute/operations/OpImageSyncLocal.hpp +++ /dev/null @@ -1,66 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include "kompute/Core.hpp" - -#include "kompute/Image.hpp" - -#include "kompute/operations/OpBase.hpp" - -namespace kp { - -/** - * Operation that syncs image's local memory by mapping device data into the - * local CPU memory. For ImageTypes::eDevice it will use a record operation - * for the memory to be syncd into GPU memory which means that the operation - * will be done in sync with GPU commands. For ImageTypes::eHost it will - * only map the data into host memory which will happen during preEval before - * the recorded commands are dispatched. - */ -class OpImageSyncLocal : public OpBase -{ - public: - /** - * Default constructor with parameters that provides the core vulkan - * resources and the images that will be used in the operation. The images - * provided cannot be of type ImageTypes::eStorage. - * - * @param images Images that will be used to create in operation. - */ - OpImageSyncLocal(const std::vector>& images); - - /** - * Default destructor. This class does not manage memory so it won't be - * expecting the parent to perform a release. - */ - ~OpImageSyncLocal() override; - - /** - * For device images, it records the copy command for the image to copy - * the data from its device to staging 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; - - /** - * For host images it performs the map command from the host memory into - * local memory. - * - * @param commandBuffer The command buffer to record the command into. - */ - virtual void postEval(const vk::CommandBuffer& commandBuffer) override; - - private: - // -------------- ALWAYS OWNED RESOURCES - std::vector> mImages; -}; - -} // End namespace kp diff --git a/src/include/kompute/operations/OpTensorCopy.hpp b/src/include/kompute/operations/OpTensorCopy.hpp deleted file mode 100644 index 78bac369..00000000 --- a/src/include/kompute/operations/OpTensorCopy.hpp +++ /dev/null @@ -1,63 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include "kompute/Core.hpp" - -#include "kompute/Tensor.hpp" - -#include "kompute/operations/OpBase.hpp" - -namespace kp { - -/** - * Operation that copies the data from the first tensor to the rest of the - * tensors provided, using a record command for all the vectors. This operation - * does not own/manage the memory of the tensors passed to it. The operation - * must only receive tensors of type - */ -class OpTensorCopy : public OpBase -{ - public: - /** - * Default constructor with parameters that provides the core vulkan - * resources and the tensors that will be used in the operation. - * - * @param tensors Tensors that will be used to create in operation. - */ - OpTensorCopy(const std::vector>& tensors); - - /** - * Default destructor. This class does not manage memory so it won't be - * expecting the parent to perform a release. - */ - ~OpTensorCopy() override; - - /** - * Records the copy commands from the first tensor into all the other - * tensors provided. Also optionally records a barrier. - * - * @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; - - /** - * Copies the local vectors for all the tensors to sync the data with the - * gpu. - * - * @param commandBuffer The command buffer to record the command into. - */ - virtual void postEval(const vk::CommandBuffer& commandBuffer) override; - - private: - // -------------- ALWAYS OWNED RESOURCES - std::vector> mTensors; -}; - -} // End namespace kp diff --git a/src/include/kompute/operations/OpTensorCopyToImage.hpp b/src/include/kompute/operations/OpTensorCopyToImage.hpp deleted file mode 100644 index 8c009c4c..00000000 --- a/src/include/kompute/operations/OpTensorCopyToImage.hpp +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -#pragma once - -#include "kompute/Core.hpp" - -#include "kompute/Tensor.hpp" - -#include "kompute/operations/OpBase.hpp" - -namespace kp { - -/** - * Operation that copies the data from the first tensor to the vector of images - * provided, using a record command for all the vectors. This operation - * does not own/manage the memory of the tensor/images passed to it. - */ -class OpTensorCopyToImage : public OpBase -{ - public: - /** - * Default constructor with parameters that provides the core vulkan - * resources and the tensors/images that will be used in the operation. - * - * @param tensors Tensors that will be used to create in operation. - */ - OpTensorCopyToImage( - const std::vector>& mem_objects); - - /** - * Default destructor. This class does not manage memory so it won't be - * expecting the parent to perform a release. - */ - ~OpTensorCopyToImage() override; - - /** - * Records the copy commands from the first tensor into all the other - * tensors provided. Also optionally records a barrier. - * - * @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; - - /** - * Copies the local vectors for all the tensors to sync the data with the - * gpu. - * - * @param commandBuffer The command buffer to record the command into. - */ - virtual void postEval(const vk::CommandBuffer& commandBuffer) override; - - private: - // -------------- ALWAYS OWNED RESOURCES - std::shared_ptr mTensor; - std::vector> mImages; -}; - -} // End namespace kp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a012edc1..2974ee2b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -16,8 +16,6 @@ add_executable(kompute_tests TestAsyncOperations.cpp TestManager.cpp TestMultipleAlgoExecutions.cpp TestOpShadersFromStringAndFile.cpp - TestOpTensorCopy.cpp - TestOpTensorCopyToImage.cpp TestOpTensorCreate.cpp TestOpSync.cpp TestPushConstant.cpp @@ -27,8 +25,10 @@ add_executable(kompute_tests TestAsyncOperations.cpp TestTensor.cpp TestImage.cpp TestOpImageCreate.cpp - TestOpImageCopy.cpp - TestOpImageCopyToTensor.cpp) + TestOpCopyTensor.cpp + TestOpCopyTensorToImage.cpp + TestOpCopyImage.cpp + TestOpCopyImageToTensor.cpp) target_link_libraries(kompute_tests PRIVATE GTest::gtest_main kompute::kompute diff --git a/test/TestOpImageCopy.cpp b/test/TestOpCopyImage.cpp similarity index 86% rename from test/TestOpImageCopy.cpp rename to test/TestOpCopyImage.cpp index 2fd7766b..045e41d8 100644 --- a/test/TestOpImageCopy.cpp +++ b/test/TestOpCopyImage.cpp @@ -7,7 +7,7 @@ #include "shaders/Utils.hpp" -TEST(TestOpImageCopy, CopyDeviceToDeviceImage) +TEST(TestOpCopyImage, CopyDeviceToDeviceImage) { kp::Manager mgr; @@ -22,14 +22,14 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImage) mgr.sequence() ->eval({ imageA, imageB }) - ->eval({ imageA, imageB }) + ->eval({ imageA, imageB }) ->eval({ imageA, imageB }); // Making sure the GPU holds the same vector EXPECT_EQ(imageA->vector(), imageB->vector()); } -TEST(TestOpImageCopy, CopyDeviceToDeviceImage2D) +TEST(TestOpCopyImage, CopyDeviceToDeviceImage2D) { kp::Manager mgr; @@ -49,7 +49,7 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImage2D) mgr.sequence() ->eval({ imageA, imageB }) - ->eval({ imageA, imageB }) + ->eval({ imageA, imageB }) ->eval({ imageA, imageB }); // Making sure the GPU holds the same vector @@ -62,7 +62,7 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImage2D) } } -TEST(TestOpImageCopy, CopyDeviceToDeviceImageMulti) +TEST(TestOpCopyImage, CopyDeviceToDeviceImageMulti) { kp::Manager mgr; @@ -80,7 +80,7 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImageMulti) mgr.sequence() ->eval({ imageA }) - ->eval({ imageA, imageB, tensorC }) + ->eval({ imageA, imageB, tensorC }) ->eval({ imageB, tensorC }); EXPECT_EQ(imageA->vector(), testVecA); @@ -88,7 +88,7 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImageMulti) EXPECT_EQ(tensorC->vector(), testVecA); } -TEST(TestOpImageCopy, CopyDeviceToHostImage) +TEST(TestOpCopyImage, CopyDeviceToHostImage) { kp::Manager mgr; @@ -105,7 +105,7 @@ TEST(TestOpImageCopy, CopyDeviceToHostImage) EXPECT_TRUE(imageA->isInit()); EXPECT_TRUE(imageB->isInit()); - mgr.sequence()->eval({ imageA, imageB }); + mgr.sequence()->eval({ imageA, imageB }); EXPECT_EQ(imageA->vector(), imageB->vector()); @@ -114,7 +114,7 @@ TEST(TestOpImageCopy, CopyDeviceToHostImage) EXPECT_EQ(imageA->vector(), imageB->vector()); } -TEST(TestOpImageCopy, CopyHostToDeviceImage) +TEST(TestOpCopyImage, CopyHostToDeviceImage) { kp::Manager mgr; @@ -131,7 +131,7 @@ TEST(TestOpImageCopy, CopyHostToDeviceImage) EXPECT_TRUE(imageA->isInit()); EXPECT_TRUE(imageB->isInit()); - mgr.sequence()->eval({ imageA, imageB }); + mgr.sequence()->eval({ imageA, imageB }); EXPECT_EQ(imageA->vector(), imageB->vector()); @@ -140,7 +140,7 @@ TEST(TestOpImageCopy, CopyHostToDeviceImage) EXPECT_EQ(imageA->vector(), imageB->vector()); } -TEST(TestOpImageCopy, CopyHostToHostImage) +TEST(TestOpCopyImage, CopyHostToHostImage) { kp::Manager mgr; @@ -157,7 +157,7 @@ TEST(TestOpImageCopy, CopyHostToHostImage) mgr.sequence() ->eval({ imageA }) - ->eval({ imageA, imageB }); + ->eval({ imageA, imageB }); EXPECT_EQ(imageA->vector(), imageB->vector()); @@ -166,7 +166,7 @@ TEST(TestOpImageCopy, CopyHostToHostImage) EXPECT_EQ(imageA->vector(), imageB->vector()); } -TEST(TestOpImageCopy, SingleImageShouldFail) +TEST(TestOpCopyImage, SingleImageShouldFail) { kp::Manager mgr; @@ -177,27 +177,11 @@ TEST(TestOpImageCopy, SingleImageShouldFail) EXPECT_TRUE(imageA->isInit()); - EXPECT_THROW(mgr.sequence()->eval({ imageA }), + EXPECT_THROW(mgr.sequence()->eval({ imageA }), std::runtime_error); } -TEST(TestOpImageCopy, TensorShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr> image = - mgr.image(testVecA, 3, 1, 1, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr tensor = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW(mgr.sequence()->eval({ image, tensor }), - std::runtime_error); -} - -TEST(TestOpImageCopy, CopyThroughStorageImage) +TEST(TestOpCopyImage, CopyThroughStorageImage) { kp::Manager mgr; @@ -212,15 +196,15 @@ TEST(TestOpImageCopy, CopyThroughStorageImage) mgr.sequence() ->eval({ ImageIn, ImageOut }) - ->eval({ ImageIn, tensorStorage }) - ->eval({ tensorStorage, ImageOut }) + ->eval({ ImageIn, tensorStorage }) + ->eval({ tensorStorage, ImageOut }) ->eval({ ImageIn, ImageOut }); // Making sure the GPU holds the same vector EXPECT_EQ(ImageIn->vector(), ImageOut->vector()); } -TEST(TestOpImageCopy, CopyImageThroughStorageViaAlgorithms) +TEST(TestOpCopyImage, CopyImageThroughStorageViaAlgorithms) { kp::Manager mgr; @@ -283,7 +267,7 @@ TEST(TestOpImageCopy, CopyImageThroughStorageViaAlgorithms) EXPECT_EQ(ImageIn->vector(), ImageOut->vector()); } -TEST(TestOpImageCopy, CopyDeviceToDeviceImageUninitialised) +TEST(TestOpCopyImage, CopyDeviceToDeviceImageUninitialised) { kp::Manager mgr; @@ -297,14 +281,14 @@ TEST(TestOpImageCopy, CopyDeviceToDeviceImageUninitialised) mgr.sequence() ->eval({ imageA, imageB }) - ->eval({ imageA, imageB }) + ->eval({ imageA, imageB }) ->eval({ imageA, imageB }); // Making sure the GPU holds the same vector EXPECT_EQ(imageA->vector(), imageB->vector()); } -TEST(TestOpImageCopy, CopyImageThroughStorageViaAlgorithmsUninitialisedOutput) +TEST(TestOpCopyImage, CopyImageThroughStorageViaAlgorithmsUninitialisedOutput) { kp::Manager mgr; diff --git a/test/TestOpImageCopyToTensor.cpp b/test/TestOpCopyImageToTensor.cpp similarity index 73% rename from test/TestOpImageCopyToTensor.cpp rename to test/TestOpCopyImageToTensor.cpp index f7612ec7..ec6a19d1 100644 --- a/test/TestOpImageCopyToTensor.cpp +++ b/test/TestOpCopyImageToTensor.cpp @@ -7,7 +7,7 @@ #include "shaders/Utils.hpp" -TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensor) +TEST(TestOpCopyImageToTensor, CopyDeviceToDeviceTensor) { kp::Manager mgr; @@ -25,7 +25,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensor) mgr.sequence() ->eval({ tensor }) ->eval({ image }) - ->eval({ image, tensor }) + ->eval({ image, tensor }) ->eval({ tensor }) ->eval({ image }); @@ -33,7 +33,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensor) EXPECT_EQ(tensor->vector(), image->vector()); } -TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensorMulti) +TEST(TestOpCopyImageToTensor, CopyDeviceToDeviceTensorMulti) { kp::Manager mgr; @@ -53,7 +53,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensorMulti) mgr.sequence() ->eval({ imageA }) - ->eval({ imageA, tensorB, tensorC }) + ->eval({ imageA, tensorB, tensorC }) ->eval({ tensorB, tensorC }); EXPECT_EQ(testVecA, imageA->vector()); @@ -61,7 +61,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceTensorMulti) EXPECT_EQ(testVecA, tensorC->vector()); } -TEST(TestOpImageCopyToTensor, CopyDeviceToHostTensor) +TEST(TestOpCopyImageToTensor, CopyDeviceToHostTensor) { kp::Manager mgr; @@ -80,7 +80,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToHostTensor) EXPECT_TRUE(imageA->isInit()); EXPECT_TRUE(tensorB->isInit()); - mgr.sequence()->eval({ imageA, tensorB }); + mgr.sequence()->eval({ imageA, tensorB }); EXPECT_EQ(imageA->vector(), tensorB->vector()); @@ -89,7 +89,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToHostTensor) EXPECT_EQ(imageA->vector(), tensorB->vector()); } -TEST(TestOpImageCopyToTensor, CopyHostToDeviceTensor) +TEST(TestOpCopyImageToTensor, CopyHostToDeviceTensor) { kp::Manager mgr; @@ -108,7 +108,7 @@ TEST(TestOpImageCopyToTensor, CopyHostToDeviceTensor) EXPECT_TRUE(imageA->isInit()); EXPECT_TRUE(tensorB->isInit()); - mgr.sequence()->eval({ imageA, tensorB }); + mgr.sequence()->eval({ imageA, tensorB }); EXPECT_EQ(imageA->vector(), tensorB->vector()); @@ -117,7 +117,7 @@ TEST(TestOpImageCopyToTensor, CopyHostToDeviceTensor) EXPECT_EQ(imageA->vector(), tensorB->vector()); } -TEST(TestOpImageCopyToTensor, CopyHostToHostTensor) +TEST(TestOpCopyImageToTensor, CopyHostToHostTensor) { kp::Manager mgr; @@ -135,7 +135,7 @@ TEST(TestOpImageCopyToTensor, CopyHostToHostTensor) mgr.sequence() ->eval({ imageA }) - ->eval({ imageA, tensorB }); + ->eval({ imageA, tensorB }); EXPECT_EQ(imageA->vector(), tensorB->vector()); @@ -144,7 +144,7 @@ TEST(TestOpImageCopyToTensor, CopyHostToHostTensor) EXPECT_EQ(imageA->vector(), tensorB->vector()); } -TEST(TestOpImageCopyToTensor, SingleTensorShouldFail) +TEST(TestOpCopyImageToTensor, SingleTensorShouldFail) { kp::Manager mgr; @@ -156,45 +156,11 @@ TEST(TestOpImageCopyToTensor, SingleTensorShouldFail) EXPECT_TRUE(tensorA->isInit()); - EXPECT_THROW(mgr.sequence()->eval({ tensorA }), + EXPECT_THROW(mgr.sequence()->eval({ tensorA }), std::runtime_error); } -TEST(TestOpImageCopyToTensor, TensorsShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr> tensorA = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr> tensorB = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW( - mgr.sequence()->eval({ tensorA, tensorB }), - std::runtime_error); -} - -TEST(TestOpImageCopyToTensor, ImagesShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr> imageA = mgr.image( - testVecA, testVecA.size(), 1, 1, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr> imageB = mgr.image( - testVecA, testVecA.size(), 1, 1, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW( - mgr.sequence()->eval({ imageA, imageB }), - std::runtime_error); -} - -TEST(TestOpImageCopyToTensor, CopyThroughStorageTensor) +TEST(TestOpCopyImageToTensor, CopyThroughStorageTensor) { kp::Manager mgr; @@ -211,8 +177,8 @@ TEST(TestOpImageCopyToTensor, CopyThroughStorageTensor) mgr.sequence() ->eval({ imageIn }) ->eval({ tensorOut }) - ->eval({ imageIn, imageStorage }) - ->eval({ imageStorage, tensorOut }) + ->eval({ imageIn, imageStorage }) + ->eval({ imageStorage, tensorOut }) ->eval({ imageIn }) ->eval({ tensorOut }); @@ -220,7 +186,7 @@ TEST(TestOpImageCopyToTensor, CopyThroughStorageTensor) EXPECT_EQ(imageIn->vector(), tensorOut->vector()); } -TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceImageUninitialised) +TEST(TestOpCopyImageToTensor, CopyDeviceToDeviceImageUninitialised) { kp::Manager mgr; @@ -236,7 +202,7 @@ TEST(TestOpImageCopyToTensor, CopyDeviceToDeviceImageUninitialised) mgr.sequence() ->eval({ imageA }) ->eval({ tensorB }) - ->eval({ imageA, tensorB }) + ->eval({ imageA, tensorB }) ->eval({ imageA, }) diff --git a/test/TestOpTensorCopy.cpp b/test/TestOpCopyTensor.cpp similarity index 85% rename from test/TestOpTensorCopy.cpp rename to test/TestOpCopyTensor.cpp index ea15425a..794d122f 100644 --- a/test/TestOpTensorCopy.cpp +++ b/test/TestOpCopyTensor.cpp @@ -7,7 +7,7 @@ #include "shaders/Utils.hpp" -TEST(TestOpTensorCopy, CopyDeviceToDeviceTensor) +TEST(TestOpCopyTensor, CopyDeviceToDeviceTensor) { kp::Manager mgr; @@ -23,14 +23,14 @@ TEST(TestOpTensorCopy, CopyDeviceToDeviceTensor) mgr.sequence() ->eval({ tensorA, tensorB }) - ->eval({ tensorA, tensorB }) + ->eval({ tensorA, tensorB }) ->eval({ tensorA, tensorB }); // Making sure the GPU holds the same vector EXPECT_EQ(tensorA->vector(), tensorB->vector()); } -TEST(TestOpTensorCopy, CopyDeviceToDeviceTensorMulti) +TEST(TestOpCopyTensor, CopyDeviceToDeviceTensorMulti) { kp::Manager mgr; @@ -49,7 +49,7 @@ TEST(TestOpTensorCopy, CopyDeviceToDeviceTensorMulti) mgr.sequence() ->eval({ tensorA }) - ->eval({ tensorA, tensorB, tensorC }) + ->eval({ tensorA, tensorB, tensorC }) ->eval({ tensorB, tensorC }); EXPECT_EQ(testVecA, tensorA->vector()); @@ -57,7 +57,7 @@ TEST(TestOpTensorCopy, CopyDeviceToDeviceTensorMulti) EXPECT_EQ(testVecA, tensorC->vector()); } -TEST(TestOpTensorCopy, CopyDeviceToHostTensor) +TEST(TestOpCopyTensor, CopyDeviceToHostTensor) { kp::Manager mgr; @@ -75,7 +75,7 @@ TEST(TestOpTensorCopy, CopyDeviceToHostTensor) EXPECT_TRUE(tensorA->isInit()); EXPECT_TRUE(tensorB->isInit()); - mgr.sequence()->eval({ tensorA, tensorB }); + mgr.sequence()->eval({ tensorA, tensorB }); EXPECT_EQ(tensorA->vector(), tensorB->vector()); @@ -84,7 +84,7 @@ TEST(TestOpTensorCopy, CopyDeviceToHostTensor) EXPECT_EQ(tensorA->vector(), tensorB->vector()); } -TEST(TestOpTensorCopy, CopyHostToDeviceTensor) +TEST(TestOpCopyTensor, CopyHostToDeviceTensor) { kp::Manager mgr; @@ -102,7 +102,7 @@ TEST(TestOpTensorCopy, CopyHostToDeviceTensor) EXPECT_TRUE(tensorA->isInit()); EXPECT_TRUE(tensorB->isInit()); - mgr.sequence()->eval({ tensorA, tensorB }); + mgr.sequence()->eval({ tensorA, tensorB }); EXPECT_EQ(tensorA->vector(), tensorB->vector()); @@ -111,7 +111,7 @@ TEST(TestOpTensorCopy, CopyHostToDeviceTensor) EXPECT_EQ(tensorA->vector(), tensorB->vector()); } -TEST(TestOpTensorCopy, CopyHostToHostTensor) +TEST(TestOpCopyTensor, CopyHostToHostTensor) { kp::Manager mgr; @@ -129,7 +129,7 @@ TEST(TestOpTensorCopy, CopyHostToHostTensor) mgr.sequence() ->eval({ tensorA }) - ->eval({ tensorA, tensorB }); + ->eval({ tensorA, tensorB }); EXPECT_EQ(tensorA->vector(), tensorB->vector()); @@ -138,7 +138,7 @@ TEST(TestOpTensorCopy, CopyHostToHostTensor) EXPECT_EQ(tensorA->vector(), tensorB->vector()); } -TEST(TestOpTensorCopy, SingleTensorShouldFail) +TEST(TestOpCopyTensor, SingleTensorShouldFail) { kp::Manager mgr; @@ -150,27 +150,11 @@ TEST(TestOpTensorCopy, SingleTensorShouldFail) EXPECT_TRUE(tensorA->isInit()); - EXPECT_THROW(mgr.sequence()->eval({ tensorA }), + EXPECT_THROW(mgr.sequence()->eval({ tensorA }), std::runtime_error); } -TEST(TestOpTensorCopy, ImageShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr image = - mgr.image(testVecA, 3, 1, 1, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr tensor = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW(mgr.sequence()->eval({ tensor, image }), - std::runtime_error); -} - -TEST(TestOpTensorCopy, CopyThroughStorageTensor) +TEST(TestOpCopyTensor, CopyThroughStorageTensor) { kp::Manager mgr; @@ -185,15 +169,15 @@ TEST(TestOpTensorCopy, CopyThroughStorageTensor) mgr.sequence() ->eval({ tensorIn, tensorOut }) - ->eval({ tensorIn, tensorStorage }) - ->eval({ tensorStorage, tensorOut }) + ->eval({ tensorIn, tensorStorage }) + ->eval({ tensorStorage, tensorOut }) ->eval({ tensorIn, tensorOut }); // Making sure the GPU holds the same vector EXPECT_EQ(tensorIn->vector(), tensorOut->vector()); } -TEST(TestOpTensorCopy, CopyTensorThroughStorageViaAlgorithms) +TEST(TestOpCopyTensor, CopyTensorThroughStorageViaAlgorithms) { kp::Manager mgr; @@ -256,7 +240,7 @@ TEST(TestOpTensorCopy, CopyTensorThroughStorageViaAlgorithms) EXPECT_EQ(tensorIn->vector(), tensorOut->vector()); } -TEST(TestOpTensorCopy, CopyDeviceToDeviceTensorUninitialised) +TEST(TestOpCopyTensor, CopyDeviceToDeviceTensorUninitialised) { kp::Manager mgr; @@ -270,14 +254,14 @@ TEST(TestOpTensorCopy, CopyDeviceToDeviceTensorUninitialised) mgr.sequence() ->eval({ tensorA, tensorB }) - ->eval({ tensorA, tensorB }) + ->eval({ tensorA, tensorB }) ->eval({ tensorA, tensorB }); // Making sure the GPU holds the same vector EXPECT_EQ(tensorA->vector(), tensorB->vector()); } -TEST(TestOpTensorCopy, CopyTensorThroughStorageViaAlgorithmsUninitialisedOutput) +TEST(TestOpCopyTensor, CopyTensorThroughStorageViaAlgorithmsUninitialisedOutput) { kp::Manager mgr; diff --git a/test/TestOpTensorCopyToImage.cpp b/test/TestOpCopyTensorToImage.cpp similarity index 73% rename from test/TestOpTensorCopyToImage.cpp rename to test/TestOpCopyTensorToImage.cpp index 4dd9af88..85ca86ed 100644 --- a/test/TestOpTensorCopyToImage.cpp +++ b/test/TestOpCopyTensorToImage.cpp @@ -7,7 +7,7 @@ #include "shaders/Utils.hpp" -TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensor) +TEST(TestOpCopyTensorToImage, CopyDeviceToDeviceTensor) { kp::Manager mgr; @@ -25,7 +25,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensor) mgr.sequence() ->eval({ tensor }) ->eval({ image }) - ->eval({ tensor, image }) + ->eval({ tensor, image }) ->eval({ tensor }) ->eval({ image }); @@ -33,7 +33,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensor) EXPECT_EQ(tensor->vector(), image->vector()); } -TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensorMulti) +TEST(TestOpCopyTensorToImage, CopyDeviceToDeviceTensorMulti) { kp::Manager mgr; @@ -54,7 +54,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensorMulti) mgr.sequence() ->eval({ tensorA }) - ->eval({ tensorA, imageB, imageC }) + ->eval({ tensorA, imageB, imageC }) ->eval({ imageB, imageC }); // Making sure the GPU holds the same vector @@ -63,7 +63,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceTensorMulti) EXPECT_EQ(testVecA, imageC->vector()); } -TEST(TestOpTensorCopyToImage, CopyDeviceToHostTensor) +TEST(TestOpCopyTensorToImage, CopyDeviceToHostTensor) { kp::Manager mgr; @@ -81,7 +81,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToHostTensor) EXPECT_TRUE(tensorA->isInit()); EXPECT_TRUE(imageB->isInit()); - mgr.sequence()->eval({ tensorA, imageB }); + mgr.sequence()->eval({ tensorA, imageB }); EXPECT_EQ(tensorA->vector(), imageB->vector()); @@ -90,7 +90,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToHostTensor) EXPECT_EQ(tensorA->vector(), imageB->vector()); } -TEST(TestOpTensorCopyToImage, CopyHostToDeviceTensor) +TEST(TestOpCopyTensorToImage, CopyHostToDeviceTensor) { kp::Manager mgr; @@ -110,7 +110,7 @@ TEST(TestOpTensorCopyToImage, CopyHostToDeviceTensor) EXPECT_TRUE(tensorA->isInit()); EXPECT_TRUE(imageB->isInit()); - mgr.sequence()->eval({ tensorA, imageB }); + mgr.sequence()->eval({ tensorA, imageB }); EXPECT_EQ(tensorA->vector(), imageB->vector()); @@ -119,7 +119,7 @@ TEST(TestOpTensorCopyToImage, CopyHostToDeviceTensor) EXPECT_EQ(tensorA->vector(), imageB->vector()); } -TEST(TestOpTensorCopyToImage, CopyHostToHostTensor) +TEST(TestOpCopyTensorToImage, CopyHostToHostTensor) { kp::Manager mgr; @@ -137,7 +137,7 @@ TEST(TestOpTensorCopyToImage, CopyHostToHostTensor) mgr.sequence() ->eval({ tensorA }) - ->eval({ tensorA, imageB }); + ->eval({ tensorA, imageB }); EXPECT_EQ(tensorA->vector(), imageB->vector()); @@ -146,7 +146,7 @@ TEST(TestOpTensorCopyToImage, CopyHostToHostTensor) EXPECT_EQ(tensorA->vector(), imageB->vector()); } -TEST(TestOpTensorCopyToImage, SingleTensorShouldFail) +TEST(TestOpCopyTensorToImage, SingleTensorShouldFail) { kp::Manager mgr; @@ -158,45 +158,11 @@ TEST(TestOpTensorCopyToImage, SingleTensorShouldFail) EXPECT_TRUE(tensorA->isInit()); - EXPECT_THROW(mgr.sequence()->eval({ tensorA }), + EXPECT_THROW(mgr.sequence()->eval({ tensorA }), std::runtime_error); } -TEST(TestOpTensorCopyToImage, TensorsShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr> tensorA = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr> tensorB = - mgr.tensor(testVecA, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW( - mgr.sequence()->eval({ tensorA, tensorB }), - std::runtime_error); -} - -TEST(TestOpTensorCopyToImage, ImagesShouldFail) -{ - kp::Manager mgr; - - std::vector testVecA{ 6, 7, 8 }; - - std::shared_ptr> imageA = mgr.image( - testVecA, testVecA.size(), 1, 1, kp::Memory::MemoryTypes::eHost); - - std::shared_ptr> imageB = mgr.image( - testVecA, testVecA.size(), 1, 1, kp::Memory::MemoryTypes::eHost); - - EXPECT_THROW( - mgr.sequence()->eval({ imageA, imageB }), - std::runtime_error); -} - -TEST(TestOpTensorCopyToImage, CopyThroughStorageTensor) +TEST(TestOpCopyTensorToImage, CopyThroughStorageTensor) { kp::Manager mgr; @@ -213,8 +179,8 @@ TEST(TestOpTensorCopyToImage, CopyThroughStorageTensor) mgr.sequence() ->eval({ tensorIn }) ->eval({ imageOut }) - ->eval({ tensorIn, tensorStorage }) - ->eval({ tensorStorage, imageOut }) + ->eval({ tensorIn, tensorStorage }) + ->eval({ tensorStorage, imageOut }) ->eval({ tensorIn }) ->eval({ imageOut }); @@ -222,7 +188,7 @@ TEST(TestOpTensorCopyToImage, CopyThroughStorageTensor) EXPECT_EQ(tensorIn->vector(), imageOut->vector()); } -TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceImageUninitialised) +TEST(TestOpCopyTensorToImage, CopyDeviceToDeviceImageUninitialised) { kp::Manager mgr; @@ -238,7 +204,7 @@ TEST(TestOpTensorCopyToImage, CopyDeviceToDeviceImageUninitialised) mgr.sequence() ->eval({ tensorA }) ->eval({ imageB }) - ->eval({ tensorA, imageB }) + ->eval({ tensorA, imageB }) ->eval({ tensorA, }) diff --git a/test/TestOpSync.cpp b/test/TestOpSync.cpp index f3c1561c..9718fb26 100644 --- a/test/TestOpSync.cpp +++ b/test/TestOpSync.cpp @@ -43,7 +43,7 @@ TEST(TestOpSync, SyncToDeviceMemoryMultiTensor) mgr.sequence()->eval({ tensorA }); - mgr.sequence()->eval({ tensorA, tensorB, tensorC }); + mgr.sequence()->eval({ tensorA, tensorB, tensorC }); mgr.sequence()->eval({ tensorA, tensorB, tensorC }); @@ -90,7 +90,7 @@ TEST(TestOpSync, SyncToDeviceMemoryMultiImage) mgr.sequence()->eval({ imageA }); - mgr.sequence()->eval({ imageA, imageB, imageC }); + mgr.sequence()->eval({ imageA, imageB, imageC }); mgr.sequence()->eval({ imageA, imageB, imageC });