Skip to content

Commit 6c6e368

Browse files
LunderbergLucien0
authored andcommitted
[Hexagon] Use single allocation to back 2-d arrays (apache#10903)
* [Hexagon] Use single allocation to back 2-d arrays Currently, each allocation allocates an entire page, so even a relatively small number of allocations can use very large amounts of VTCM. This commit changes calls to `AllocVtcmWorkspace` of shape `[N,M]` from performing `N` allocations of size `M`, to 1 allocation of size `N*M`. Since `N` is usually much smaller than a page, this reduces the total amount of memory required. This is an intermediate step, where the long-term solution is to use static planning for VTCM allocations. This returns the same `void**` type as the static planning eventually will, but avoids excess memory use in the meantime. * [Hexagon] Maintain alignment of allocations Previously, when a single monolithic allocation is used to back a 2-d Hexagon buffer of shape `[nallocs, nbytes_per_allocation]`, the allocation itself is aligned, but each individual region is not. This commit ensures that each individual region also followed the alignment specified.
1 parent 0542a69 commit 6c6e368

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/runtime/hexagon/hexagon/hexagon_buffer.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,24 @@ HexagonBuffer::HexagonBuffer(size_t nallocs, size_t nbytes, size_t alignment,
150150
Optional<String> scope)
151151
: ndim_(2), nbytes_per_allocation_(nbytes) {
152152
SetStorageScope(scope);
153+
154+
size_t nbytes_aligned = ((nbytes + (alignment - 1)) / alignment) * alignment;
155+
size_t nbytes_monolithic = nallocs * nbytes_aligned;
156+
157+
std::unique_ptr<Allocation> alloca = nullptr;
158+
if (GetStorageScope() == StorageScope::kDDR) {
159+
alloca = Allocator<StorageScope::kDDR>(nbytes_monolithic, alignment);
160+
} else if (GetStorageScope() == StorageScope::kVTCM) {
161+
alloca = Allocator<StorageScope::kVTCM>(nbytes_monolithic, alignment);
162+
}
163+
CHECK(alloca) << "could not create allocation";
164+
153165
for (size_t i = 0; i < nallocs; ++i) {
154-
std::unique_ptr<Allocation> alloca = nullptr;
155-
if (GetStorageScope() == StorageScope::kDDR) {
156-
alloca = Allocator<StorageScope::kDDR>(nbytes, alignment);
157-
} else if (GetStorageScope() == StorageScope::kVTCM) {
158-
alloca = Allocator<StorageScope::kVTCM>(nbytes, alignment);
159-
}
160-
CHECK(alloca != nullptr);
161-
allocations_.push_back(alloca->data_);
162-
managed_allocations_.push_back(std::move(alloca));
166+
void* alloc_offset = static_cast<unsigned char*>(alloca->data_) + i * nbytes_aligned;
167+
allocations_.push_back(alloc_offset);
163168
}
169+
170+
managed_allocations_.push_back(std::move(alloca));
164171
}
165172

166173
HexagonBuffer::HexagonBuffer(void* data, size_t nbytes, Optional<String> scope)

0 commit comments

Comments
 (0)