Skip to content

Commit

Permalink
[Hexagon] Use single allocation to back 2-d arrays (apache#10903)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
Lunderberg authored Apr 6, 2022
1 parent fc736ed commit 591a000
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/runtime/hexagon/hexagon/hexagon_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,24 @@ HexagonBuffer::HexagonBuffer(size_t nallocs, size_t nbytes, size_t alignment,
Optional<String> scope)
: ndim_(2), nbytes_per_allocation_(nbytes) {
SetStorageScope(scope);

size_t nbytes_aligned = ((nbytes + (alignment - 1)) / alignment) * alignment;
size_t nbytes_monolithic = nallocs * nbytes_aligned;

std::unique_ptr<Allocation> alloca = nullptr;
if (GetStorageScope() == StorageScope::kDDR) {
alloca = Allocator<StorageScope::kDDR>(nbytes_monolithic, alignment);
} else if (GetStorageScope() == StorageScope::kVTCM) {
alloca = Allocator<StorageScope::kVTCM>(nbytes_monolithic, alignment);
}
CHECK(alloca) << "could not create allocation";

for (size_t i = 0; i < nallocs; ++i) {
std::unique_ptr<Allocation> alloca = nullptr;
if (GetStorageScope() == StorageScope::kDDR) {
alloca = Allocator<StorageScope::kDDR>(nbytes, alignment);
} else if (GetStorageScope() == StorageScope::kVTCM) {
alloca = Allocator<StorageScope::kVTCM>(nbytes, alignment);
}
CHECK(alloca != nullptr);
allocations_.push_back(alloca->data_);
managed_allocations_.push_back(std::move(alloca));
void* alloc_offset = static_cast<unsigned char*>(alloca->data_) + i * nbytes_aligned;
allocations_.push_back(alloc_offset);
}

managed_allocations_.push_back(std::move(alloca));
}

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

0 comments on commit 591a000

Please sign in to comment.