2626#include < cstdlib>
2727#include < cstring>
2828#include < limits>
29- #include " logger.h"
3029#include " trans/ascend/ascend_buffer.h"
3130
3231namespace UC ::ASU {
3332
34- constexpr std::uintptr_t kHostRegisterPageSize = 4096 ;
35- constexpr std::size_t kSlotSizeAlignment = 32 ;
36- constexpr std::size_t kSlotPadding = 32 ;
3733constexpr std::size_t kSlotAddressAlignment = 64 ;
3834
39- void FreeHostMemory (void * addr)
40- {
41- auto ret = aclrtFreeHost (addr);
42- if (ret != ACL_SUCCESS ) { UC_ERROR (" Failed to free host memory addr={} ret={}" , addr, ret); }
43- }
44-
45- void ReleaseHostPinnedMemory (void * addr)
46- {
47- auto ret = aclrtHostUnregister (addr);
48- if (ret != ACL_SUCCESS ) {
49- UC_ERROR (" Failed to unregister host-pinned memory addr={} ret={}" , addr, ret);
50- return ;
51- }
52- FreeHostMemory (addr);
53- }
54-
5535bool GetSlotStride (std::size_t capacity, std::size_t & stride)
5636{
57- // Keep one layout for every memory type: reserve ALIGN_UP(capacity, 32) + 32
58- // bytes, then align the next slot address to a 64-byte boundary.
37+ // NOTE: Ascend ACL documents an aclrtMallocHost large-block suballocation
38+ // layout of ALIGN_UP(len, 32) + 32 bytes with 64-byte-aligned segment
39+ // starts. Current HCOMM/RDMA validation did not reproduce failures without
40+ // the extra 32-byte tail room, so ASU keeps only the 64-byte slot-start
41+ // alignment for now.
42+ // Keep one layout for every memory type by aligning each slot start to a
43+ // 64-byte boundary.
5944 constexpr auto kMaxSize = std::numeric_limits<std::size_t >::max ();
60- if (capacity > kMaxSize - (kSlotSizeAlignment - 1 )) { return false ; }
61-
62- const auto alignedSize =
63- (capacity + kSlotSizeAlignment - 1 ) / kSlotSizeAlignment * kSlotSizeAlignment ;
64- if (alignedSize > kMaxSize - kSlotPadding ) { return false ; }
65-
66- const auto paddedSize = alignedSize + kSlotPadding ;
67- if (paddedSize > kMaxSize - (kSlotAddressAlignment - 1 )) { return false ; }
45+ if (capacity > kMaxSize - (kSlotAddressAlignment - 1 )) { return false ; }
6846
6947 stride =
70- (paddedSize + kSlotAddressAlignment - 1 ) / kSlotAddressAlignment * kSlotAddressAlignment ;
48+ (capacity + kSlotAddressAlignment - 1 ) / kSlotAddressAlignment * kSlotAddressAlignment ;
7149 return true ;
7250}
7351
@@ -77,16 +55,30 @@ Status BufferManager::BufferRegion::Create(MemoryType type, std::size_t size, Bu
7755 switch (type) {
7856 case MemoryType::HOST : {
7957 auto owner = ascendBuffer.MakeHostBuffer (size);
80- if (!owner) { return AllocationFailed (" host" ); }
58+ if (!owner) {
59+ return Status::Error (StatusCode::INTERNAL_ERROR , " failed to allocate host memory" );
60+ }
8161 // HOST has one CPU-visible address, which is also passed to the
8262 // provider when it registers the region as MEM_HOST.
8363 region = {owner, owner.get (), owner.get (), TransProvider::MemType::MEM_HOST };
8464 return Status::OK ();
8565 }
86- case MemoryType::HOST_PINNED : return MakeHostPinned (size, region);
66+ case MemoryType::HOST_PINNED : {
67+ void * deviceAddr = nullptr ;
68+ auto owner = ascendBuffer.MakeHostPinnedBuffer (size, &deviceAddr);
69+ if (!owner) {
70+ return Status::Error (StatusCode::INTERNAL_ERROR ,
71+ " failed to allocate host-pinned memory" );
72+ }
73+ region = {owner, owner.get (), deviceAddr, TransProvider::MemType::MEM_DEVICE };
74+ return Status::OK ();
75+ }
8776 case MemoryType::ASCEND_DEVICE : {
8877 auto owner = ascendBuffer.MakeDeviceBuffer (size);
89- if (!owner) { return AllocationFailed (" device" ); }
78+ if (!owner) {
79+ return Status::Error (StatusCode::INTERNAL_ERROR ,
80+ " failed to allocate device memory" );
81+ }
9082 region = {owner, owner.get (), owner.get (), TransProvider::MemType::MEM_DEVICE };
9183 return Status::OK ();
9284 }
@@ -102,51 +94,17 @@ void BufferManager::BufferRegion::Reset()
10294 providerMemType = TransProvider::MemType::MEM_HOST ;
10395}
10496
105- Status BufferManager::BufferRegion::AllocationFailed (const char * type)
106- {
107- return Status::Error (StatusCode::INTERNAL_ERROR ,
108- std::string (" failed to allocate " ) + type + " memory" );
109- }
110-
111- Status BufferManager::BufferRegion::MakeHostPinned (std::size_t size, BufferRegion& region)
112- {
113- void * hostAddr = nullptr ;
114- auto ret = aclrtMallocHost (&hostAddr, size);
115- if (ret != ACL_SUCCESS ) { return AllocationFailed (" host-pinned" ); }
116- if (reinterpret_cast <std::uintptr_t >(hostAddr) % kHostRegisterPageSize != 0 ) {
117- FreeHostMemory (hostAddr);
118- return Status::Error (StatusCode::INTERNAL_ERROR ,
119- " host-pinned memory is not 4K page aligned" );
120- }
121-
122- ret = aclrtHostRegisterV2 (hostAddr, size, ACL_HOST_REG_MAPPED | ACL_HOST_REG_PINNED );
123- if (ret != ACL_SUCCESS ) {
124- FreeHostMemory (hostAddr);
125- return Status::Error (StatusCode::INTERNAL_ERROR ,
126- " failed to register host-pinned memory with ACL" );
127- }
128-
129- void * deviceAddr = nullptr ;
130- ret = aclrtHostGetDevicePointer (hostAddr, &deviceAddr, 0 );
131- if (ret != ACL_SUCCESS ) {
132- ReleaseHostPinnedMemory (hostAddr);
133- return Status::Error (StatusCode::INTERNAL_ERROR ,
134- " failed to get host-pinned device address" );
135- }
136-
137- // The owner keeps the ACL registration alive until after HCOMM has
138- // unregistered the region in BufferManager's destructor.
139- auto owner = std::shared_ptr<void >(hostAddr, ReleaseHostPinnedMemory);
140- region = {owner, hostAddr, deviceAddr, TransProvider::MemType::MEM_DEVICE };
141- return Status::OK ();
142- }
143-
14497bool IsTransportBufferReady (const ScatterGatherEntry& sge)
14598{
14699 return sge.local_addr != 0 && sge.device_addr != 0 && sge.length != 0 &&
147100 sge.slot_index != UINT32_MAX ;
148101}
149102
103+ bool IsCpuAccessible (MemoryType type)
104+ {
105+ return type == MemoryType::HOST || type == MemoryType::HOST_PINNED ;
106+ }
107+
150108BufferManager::~BufferManager ()
151109{
152110 if (provider_ && memHandle_) {
@@ -262,6 +220,7 @@ Status BufferManager::Allocate(std::size_t size, ScatterGatherEntry& sge)
262220 sge.length = static_cast <std::uint32_t >(size);
263221 sge.tokenId = tokenId_;
264222 sge.slot_index = idx;
223+ sge.memory_type = memory_type_;
265224 return Status::OK ();
266225}
267226
0 commit comments