Skip to content

Commit

Permalink
vtcm inherits from ddr allocation on cpu; add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adstraw committed Nov 18, 2021
1 parent 00a04b8 commit e099787
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 35 deletions.
20 changes: 8 additions & 12 deletions src/runtime/hexagon/hexagon/hexagon_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ struct DDRAllocation : public Allocation {
~DDRAllocation() { free(data_); }
};

#ifdef BUILD_FOR_HEXAGON
struct VTCMAllocation : public Allocation {
VTCMAllocation(size_t nbytes, size_t alignment) : Allocation(nbytes, alignment) {
#ifdef BUILD_FOR_HEXAGON
// TODO(Straw): Alignment not used when allocating VTCM
compute_res_attr_t res_info;
HEXAGON_SAFE_CALL(HAP_compute_res_attr_init(&res_info));
Expand All @@ -79,28 +79,23 @@ struct VTCMAllocation : public Allocation {
return;
}
// HEXAGON_PRINT(ALWAYS, "VTCMAllocation() - Context ID: %u, VTCM ptr: %p", context_id_, data_);
#else
int ret = posix_memalign(&data_, alignment, nbytes);
CHECK_EQ(ret, 0);
#endif
}
~VTCMAllocation() {
#ifdef BUILD_FOR_HEXAGON
// HEXAGON_PRINT(ALWAYS, "~VTCMAllocation() - Context ID: %u, VTCM ptr: %p", context_id_,
// data_);
// TODO(Straw): Need to handle the else case(s) here
if (context_id_ && data_) {
HEXAGON_SAFE_CALL(HAP_compute_res_release(context_id_));
data_ = nullptr;
}
#else
free(data_);
#endif
}
#ifdef BUILD_FOR_HEXAGON
unsigned int context_id_{0};
#endif
};
#else
struct VTCMAllocation : public DDRAllocation {
VTCMAllocation(size_t nbytes, size_t alignment) : DDRAllocation(nbytes, alignment) {}
};
#endif

template <HexagonBuffer::StorageScope S>
std::unique_ptr<Allocation> Allocator(size_t nbytes, size_t alignment);
Expand Down Expand Up @@ -150,8 +145,9 @@ HexagonBuffer::HexagonBuffer(size_t ndim, size_t nbytes, size_t alignment, Optio

HexagonBuffer::HexagonBuffer(void* data, size_t nbytes, Optional<String> scope)
: ndim_(1), nbytes_(nbytes) {
// TODO(Straw): disallow VTCM scope?
SetStorageScope(scope);
// disallow external VTCM allocations
CHECK(GetStorageScope() != HexagonBuffer::StorageScope::kVTCM);
allocations_.push_back(data);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/hexagon/hexagon/hexagon_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ class HexagonBuffer {
/*! \brief The underlying storage type in which the allocation
* resides.
*/
StorageScope storage_scope_;
size_t ndim_;
size_t nbytes_;
StorageScope storage_scope_;
};

} // namespace hexagon
Expand Down
115 changes: 93 additions & 22 deletions tests/cpp/hexagon_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,20 @@ TEST(HexagonBuffer, copy_from_invalid_size) {
}

TEST(HexagonBuffer, nd) {
Optional<String> scope("global");
HexagonBuffer hb(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);
EXPECT_EQ(hb.GetStorageScope(), HexagonBuffer::StorageScope::kDDR);
Optional<String> def;
HexagonBuffer hb_default(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, def);
EXPECT_EQ(hb_default.GetStorageScope(), HexagonBuffer::StorageScope::kDDR);

Optional<String> global("global");
HexagonBuffer hb_global(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, global);
EXPECT_EQ(hb_global.GetStorageScope(), HexagonBuffer::StorageScope::kDDR);

Optional<String> vtcm("global.vtcm");
HexagonBuffer hb_vtcm(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, vtcm);
EXPECT_EQ(hb_vtcm.GetStorageScope(), HexagonBuffer::StorageScope::kVTCM);

Optional<String> invalid("invalid");
EXPECT_THROW(HexagonBuffer hb_invalid(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, invalid), InternalError);
}

TEST(HexagonBuffer, nd_copy_from) {
Expand All @@ -100,29 +111,31 @@ TEST(HexagonBuffer, nd_copy_from) {
}

TEST(HexagonBuffer, 1d_copy_from_1d) {
Optional<String> scope("global");
HexagonBuffer hb1(8 /* nbytes */, 8 /* alignment */, scope);
Optional<String> global("global");
HexagonBuffer from(8 /* nbytes */, 8 /* alignment */, global);

std::vector<uint8_t> data{0, 1, 2, 3, 4, 5, 6, 7};
hb1.CopyFrom(data.data(), data.size());
Optional<String> vtcm("global.vtcm");
HexagonBuffer to(8 /* nbytes */, 8 /* alignment */, vtcm);

HexagonBuffer hb2(8 /* nbytes */, 8 /* alignment */, scope);
hb2.CopyFrom(hb1);
std::vector<uint8_t> data{0, 1, 2, 3, 4, 5, 6, 7};
from.CopyFrom(data.data(), data.size());
to.CopyFrom(from);

uint8_t* ptr = static_cast<uint8_t*>(hb2.GetPointer()[0]);
uint8_t* ptr = static_cast<uint8_t*>(to.GetPointer()[0]);
for (size_t i = 0; i < data.size(); ++i) {
EXPECT_EQ(ptr[i], data[i]);
}
}

TEST(HexagonBuffer, 2d_copy_from_1d) {
Optional<String> scope("global");
HexagonBuffer hb1d(8 /* nbytes */, 8 /* alignment */, scope);
Optional<String> vtcm("global.vtcm");
HexagonBuffer hb1d(8 /* nbytes */, 8 /* alignment */, vtcm);

Optional<String> global("global");
HexagonBuffer hb2d(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, global);

std::vector<uint8_t> data{0, 1, 2, 3, 4, 5, 6, 7};
hb1d.CopyFrom(data.data(), data.size());

HexagonBuffer hb2d(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);
hb2d.CopyFrom(hb1d);

uint8_t* ptr = static_cast<uint8_t*>(hb2d.GetPointer()[0]);
Expand All @@ -139,13 +152,14 @@ TEST(HexagonBuffer, 2d_copy_from_1d) {
}

TEST(HexagonBuffer, 1d_copy_from_2d) {
Optional<String> scope("global");
HexagonBuffer hb2d(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);
Optional<String> vtcm("global.vtcm");
HexagonBuffer hb2d(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, vtcm);

Optional<String> global("global.vtcm");
HexagonBuffer hb1d(8 /* nbytes */, 8 /* alignment */, global);

std::vector<uint8_t> data{0, 1, 2, 3, 4, 5, 6, 7};
hb2d.CopyFrom(data.data(), data.size());

HexagonBuffer hb1d(8 /* nbytes */, 8 /* alignment */, scope);
hb1d.CopyFrom(hb2d);

uint8_t* ptr = static_cast<uint8_t*>(hb1d.GetPointer()[0]);
Expand All @@ -154,12 +168,35 @@ TEST(HexagonBuffer, 1d_copy_from_2d) {
}
}

TEST(HexagonBuffer, nd_copy_from_nd_invalid_size)
{
Optional<String> scope("global");
HexagonBuffer hb1d(8 /* nbytes */, 8 /* alignment */, scope);
HexagonBuffer hb2d(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);

HexagonBuffer toosmall1d(4 /* nbytes */, 8 /* alignment */, scope);
EXPECT_THROW(hb1d.CopyFrom(toosmall1d), InternalError);
EXPECT_THROW(hb2d.CopyFrom(toosmall1d), InternalError);

HexagonBuffer toosbig1d(16 /* nbytes */, 16 /* alignment */, scope);
EXPECT_THROW(hb1d.CopyFrom(toosbig1d), InternalError);
EXPECT_THROW(hb2d.CopyFrom(toosbig1d), InternalError);

HexagonBuffer toosmall2d(2 /* ndim */, 2 /* nbytes */, 8 /* alignment */, scope);
EXPECT_THROW(hb1d.CopyFrom(toosmall2d), InternalError);
EXPECT_THROW(hb2d.CopyFrom(toosmall2d), InternalError);

HexagonBuffer toobig2d(2 /* ndim */, 16 /* nbytes */, 16 /* alignment */, scope);
EXPECT_THROW(hb1d.CopyFrom(toobig2d), InternalError);
EXPECT_THROW(hb2d.CopyFrom(toobig2d), InternalError);
}

TEST(HexagonBuffer, md_copy_from_nd) {
Optional<String> scope("global");
HexagonBuffer hb2d(3 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);
HexagonBuffer hb3d(4 /* ndim */, 3 /* nbytes */, 8 /* alignment */, scope);
EXPECT_THROW(hb2d.CopyFrom(hb3d), InternalError);
EXPECT_THROW(hb3d.CopyFrom(hb2d), InternalError);
HexagonBuffer hb3d(3 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);
HexagonBuffer hb4d(4 /* ndim */, 3 /* nbytes */, 8 /* alignment */, scope);
EXPECT_THROW(hb3d.CopyFrom(hb4d), InternalError);
EXPECT_THROW(hb4d.CopyFrom(hb3d), InternalError);
}

TEST(HexagonBuffer, copy_to) {
Expand All @@ -176,3 +213,37 @@ TEST(HexagonBuffer, copy_to) {
EXPECT_EQ(data_in[i], data_out[i]);
}
}

TEST(HexagonBuffer, nd_copy_to) {
Optional<String> scope("global");
HexagonBuffer hb(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, scope);

std::vector<uint8_t> data_in{0, 1, 2, 3, 4, 5, 6, 7};
hb.CopyFrom(data_in.data(), data_in.size());

std::vector<uint8_t> data_out{7, 6, 5, 4, 3, 2, 1, 0};
hb.CopyTo(data_out.data(), data_out.size());

for (size_t i = 0; i < data_in.size(); ++i) {
EXPECT_EQ(data_in[i], data_out[i]);
}
}

TEST(HexagonBuffer, external) {
std::vector<uint8_t> data{0, 1, 2, 3, 4, 5, 6, 7};

Optional<String> def;
HexagonBuffer hb_default(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, def);
EXPECT_EQ(hb_default.GetStorageScope(), HexagonBuffer::StorageScope::kDDR);

Optional<String> global("global");
HexagonBuffer hb_global(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, global);
EXPECT_EQ(hb_global.GetStorageScope(), HexagonBuffer::StorageScope::kDDR);

Optional<String> vtcm("global.vtcm");
HexagonBuffer hb_vtcm(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, vtcm);
EXPECT_EQ(hb_vtcm.GetStorageScope(), HexagonBuffer::StorageScope::kVTCM);

Optional<String> invalid("invalid");
EXPECT_THROW(HexagonBuffer hb_invalid(2 /* ndim */, 4 /* nbytes */, 8 /* alignment */, invalid), InternalError);
}

0 comments on commit e099787

Please sign in to comment.