Skip to content

Commit

Permalink
Add a new memory property: eDeviceAndLocal (#396)
Browse files Browse the repository at this point in the history
This is memory which is device local, and also host visible and host
coherent.

Signed-off-by: Robert Quill <robert.quill@imgtec.com>
  • Loading branch information
robquill authored Sep 12, 2024
1 parent a242929 commit f190212
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ Image::getPrimaryImageUsageFlags()
switch (this->mMemoryType) {
case MemoryTypes::eDevice:
case MemoryTypes::eHost:
case MemoryTypes::eDeviceAndHost:
return vk::ImageUsageFlagBits::eStorage |
vk::ImageUsageFlagBits::eTransferSrc |
vk::ImageUsageFlagBits::eTransferDst;
Expand Down
10 changes: 8 additions & 2 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ Memory::mapRawData()

std::shared_ptr<vk::DeviceMemory> hostVisibleMemory = nullptr;

if (this->mMemoryType == MemoryTypes::eHost) {
if (this->mMemoryType == MemoryTypes::eHost ||
this->mMemoryType == MemoryTypes::eDeviceAndHost) {
hostVisibleMemory = this->mPrimaryMemory;
} else if (this->mMemoryType == MemoryTypes::eDevice) {
hostVisibleMemory = this->mStagingMemory;
Expand Down Expand Up @@ -187,7 +188,8 @@ Memory::unmapRawData()

std::shared_ptr<vk::DeviceMemory> hostVisibleMemory = nullptr;

if (this->mMemoryType == MemoryTypes::eHost) {
if (this->mMemoryType == MemoryTypes::eHost ||
this->mMemoryType == MemoryTypes::eDeviceAndHost) {
hostVisibleMemory = this->mPrimaryMemory;
} else if (this->mMemoryType == MemoryTypes::eDevice) {
hostVisibleMemory = this->mStagingMemory;
Expand Down Expand Up @@ -226,6 +228,10 @@ Memory::getPrimaryMemoryPropertyFlags()
return vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent;
break;
case MemoryTypes::eDeviceAndHost:
return vk::MemoryPropertyFlagBits::eDeviceLocal |
vk::MemoryPropertyFlagBits::eHostVisible |
vk::MemoryPropertyFlagBits::eHostCoherent;
case MemoryTypes::eStorage:
return vk::MemoryPropertyFlagBits::eDeviceLocal;
break;
Expand Down
2 changes: 2 additions & 0 deletions src/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,14 @@ Tensor::constructDescriptorSet(vk::DescriptorSet descriptorSet,

return writeDesciptorSet;
}

vk::BufferUsageFlags
Tensor::getPrimaryBufferUsageFlags()
{
switch (this->mMemoryType) {
case MemoryTypes::eDevice:
case MemoryTypes::eHost:
case MemoryTypes::eDeviceAndHost:
return vk::BufferUsageFlagBits::eStorageBuffer |
vk::BufferUsageFlagBits::eTransferSrc |
vk::BufferUsageFlagBits::eTransferDst;
Expand Down
3 changes: 2 additions & 1 deletion src/include/kompute/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class Image : public Memory
"Custom data types are not supported for Kompute Images");
}

if (memoryType == MemoryTypes::eHost) {
if (memoryType == MemoryTypes::eHost ||
memoryType == MemoryTypes::eDeviceAndHost) {
// Host-accessible memory must be linear-tiled.
tiling = vk::ImageTiling::eLinear;
} else if (memoryType == MemoryTypes::eDevice ||
Expand Down
2 changes: 2 additions & 0 deletions src/include/kompute/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Memory
eDevice = 0, ///< Type is device memory, source and destination
eHost = 1, ///< Type is host memory, source and destination
eStorage = 2, ///< Type is Device memory (only)
eDeviceAndHost =
3, ///< Type is host-visible and host-coherent device memory
};

enum class DataTypes
Expand Down
24 changes: 24 additions & 0 deletions test/TestOpCopyImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,27 @@ TEST(TestOpCopyImage, CopyDeviceToDeviceImage2DMismatchedSizes)
EXPECT_THROW(mgr.sequence()->eval<kp::OpCopy>({ imageA, imageB }),
std::runtime_error);
}

TEST(TestOpImageCopy, CopyDeviceAndHostToDeviceAndHostImage)
{
kp::Manager mgr;

std::vector<float> testVecA{ 1, 2, 3 };
std::vector<float> testVecB{ 0, 0, 0 };

std::shared_ptr<kp::ImageT<float>> imageA =
mgr.image(testVecA, 3, 1, 1, kp::Memory::MemoryTypes::eDeviceAndHost);
std::shared_ptr<kp::ImageT<float>> imageB =
mgr.image(testVecB, 3, 1, 1, kp::Memory::MemoryTypes::eDeviceAndHost);

EXPECT_TRUE(imageA->isInit());
EXPECT_TRUE(imageB->isInit());

mgr.sequence()
->eval<kp::OpSyncDevice>({ imageA, imageB })
->eval<kp::OpCopy>({ imageA, imageB })
->eval<kp::OpSyncLocal>({ imageA, imageB });

// Making sure the GPU holds the same vector
EXPECT_EQ(imageA->vector(), imageB->vector());
}
26 changes: 26 additions & 0 deletions test/TestOpCopyImageToTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,29 @@ TEST(TestOpCopyImageToTensor, CopyDeviceToDeviceImageUninitialised)
// Making sure the GPU holds the same vector
EXPECT_EQ(imageA->vector(), tensorB->vector());
}

TEST(TestOpImageCopyToTensor, CopyDeviceAndHostToDeviceAndHostTensor)
{
kp::Manager mgr;

std::vector<float> testVecA{ 1, 2, 3 };
std::vector<float> testVecB{ 0, 0, 0 };

std::shared_ptr<kp::ImageT<float>> imageA = mgr.image(
testVecA, testVecA.size(), 1, 1, kp::Memory::MemoryTypes::eDeviceAndHost);
std::shared_ptr<kp::TensorT<float>> tensorB =
mgr.tensor(testVecB, kp::Memory::MemoryTypes::eDeviceAndHost);

EXPECT_TRUE(imageA->isInit());
EXPECT_TRUE(tensorB->isInit());

mgr.sequence()
->eval<kp::OpSyncDevice>({ imageA })
->eval<kp::OpSyncDevice>({ tensorB })
->eval<kp::OpCopy>({ imageA, tensorB })
->eval<kp::OpSyncLocal>({ imageA })
->eval<kp::OpSyncLocal>({ tensorB });

// Making sure the GPU holds the same vector
EXPECT_EQ(imageA->vector(), tensorB->vector());
}
24 changes: 24 additions & 0 deletions test/TestOpCopyTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,27 @@ TEST(TestOpCopyTensor, CopyTensorThroughStorageViaAlgorithmsUninitialisedOutput)
// Making sure the GPU holds the same vector
EXPECT_EQ(tensorIn->vector(), tensorOut->vector());
}

TEST(TestOpTensorCopy, CopyDeviceAndHostToDeviceAndHostTensor)
{
kp::Manager mgr;

std::vector<float> testVecA{ 1, 2, 3 };
std::vector<float> testVecB{ 0, 0, 0 };

std::shared_ptr<kp::TensorT<float>> tensorA =
mgr.tensor(testVecA, kp::Memory::MemoryTypes::eDeviceAndHost);
std::shared_ptr<kp::TensorT<float>> tensorB =
mgr.tensor(testVecB, kp::Memory::MemoryTypes::eDeviceAndHost);

EXPECT_TRUE(tensorA->isInit());
EXPECT_TRUE(tensorB->isInit());

mgr.sequence()
->eval<kp::OpSyncDevice>({ tensorA, tensorB })
->eval<kp::OpCopy>({ tensorA, tensorB })
->eval<kp::OpSyncLocal>({ tensorA, tensorB });

// Making sure the GPU holds the same vector
EXPECT_EQ(tensorA->vector(), tensorB->vector());
}
26 changes: 26 additions & 0 deletions test/TestOpCopyTensorToImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,29 @@ TEST(TestOpCopyTensorToImage, CopyDeviceToDeviceImageUninitialised)
// Making sure the GPU holds the same vector
EXPECT_EQ(tensorA->vector(), imageB->vector());
}

TEST(TestOpTensorCopyToImage, CopyDeviceAndHostToDeviceAndHostTensor)
{
kp::Manager mgr;

std::vector<float> testVecA{ 1, 2, 3 };
std::vector<float> testVecB{ 0, 0, 0 };

std::shared_ptr<kp::TensorT<float>> tensorA =
mgr.tensor(testVecA, kp::Memory::MemoryTypes::eDeviceAndHost);
std::shared_ptr<kp::ImageT<float>> imageB = mgr.image(
testVecB, testVecB.size(), 1, 1, kp::Memory::MemoryTypes::eDeviceAndHost);

EXPECT_TRUE(tensorA->isInit());
EXPECT_TRUE(imageB->isInit());

mgr.sequence()
->eval<kp::OpSyncDevice>({ tensorA })
->eval<kp::OpSyncDevice>({ imageB })
->eval<kp::OpCopy>({ tensorA, imageB })
->eval<kp::OpSyncLocal>({ tensorA })
->eval<kp::OpSyncLocal>({ imageB });

// Making sure the GPU holds the same vector
EXPECT_EQ(tensorA->vector(), imageB->vector());
}
15 changes: 15 additions & 0 deletions test/TestOpImageCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ TEST(TestOpImageCreate, ExceptionOnInvalidTiledImage)

kp::Manager mgr;

try {
std::shared_ptr<kp::ImageT<float>> imageA =
mgr.image(testVecA,
1,
1,
1,
vk::ImageTiling::eOptimal,
kp::Memory::MemoryTypes::eDeviceAndHost);
} catch (const std::runtime_error& err) {
// check exception
ASSERT_TRUE(std::string(err.what())
.find("optimal tiling is only supported for") !=
std::string::npos);
}

try {
std::shared_ptr<kp::ImageT<float>> imageA =
mgr.image(testVecA,
Expand Down

0 comments on commit f190212

Please sign in to comment.