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 ;
3533constexpr std::size_t kSlotSizeAlignment = 32 ;
3634constexpr std::size_t kSlotPadding = 32 ;
3735constexpr std::size_t kSlotAddressAlignment = 64 ;
3836
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-
5537bool GetSlotStride (std::size_t capacity, std::size_t & stride)
5638{
5739 // Keep one layout for every memory type: reserve ALIGN_UP(capacity, 32) + 32
@@ -77,16 +59,30 @@ Status BufferManager::BufferRegion::Create(MemoryType type, std::size_t size, Bu
7759 switch (type) {
7860 case MemoryType::HOST : {
7961 auto owner = ascendBuffer.MakeHostBuffer (size);
80- if (!owner) { return AllocationFailed (" host" ); }
62+ if (!owner) {
63+ return Status::Error (StatusCode::INTERNAL_ERROR , " failed to allocate host memory" );
64+ }
8165 // HOST has one CPU-visible address, which is also passed to the
8266 // provider when it registers the region as MEM_HOST.
8367 region = {owner, owner.get (), owner.get (), TransProvider::MemType::MEM_HOST };
8468 return Status::OK ();
8569 }
86- case MemoryType::HOST_PINNED : return MakeHostPinned (size, region);
70+ case MemoryType::HOST_PINNED : {
71+ void * deviceAddr = nullptr ;
72+ auto owner = ascendBuffer.MakeHostPinnedBuffer (size, &deviceAddr);
73+ if (!owner) {
74+ return Status::Error (StatusCode::INTERNAL_ERROR ,
75+ " failed to allocate host-pinned memory" );
76+ }
77+ region = {owner, owner.get (), deviceAddr, TransProvider::MemType::MEM_DEVICE };
78+ return Status::OK ();
79+ }
8780 case MemoryType::ASCEND_DEVICE : {
8881 auto owner = ascendBuffer.MakeDeviceBuffer (size);
89- if (!owner) { return AllocationFailed (" device" ); }
82+ if (!owner) {
83+ return Status::Error (StatusCode::INTERNAL_ERROR ,
84+ " failed to allocate device memory" );
85+ }
9086 region = {owner, owner.get (), owner.get (), TransProvider::MemType::MEM_DEVICE };
9187 return Status::OK ();
9288 }
@@ -102,51 +98,17 @@ void BufferManager::BufferRegion::Reset()
10298 providerMemType = TransProvider::MemType::MEM_HOST ;
10399}
104100
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-
144101bool IsTransportBufferReady (const ScatterGatherEntry& sge)
145102{
146103 return sge.local_addr != 0 && sge.device_addr != 0 && sge.length != 0 &&
147104 sge.slot_index != UINT32_MAX ;
148105}
149106
107+ bool IsCpuAccessible (MemoryType type)
108+ {
109+ return type == MemoryType::HOST || type == MemoryType::HOST_PINNED ;
110+ }
111+
150112BufferManager::~BufferManager ()
151113{
152114 if (provider_ && memHandle_) {
@@ -262,6 +224,7 @@ Status BufferManager::Allocate(std::size_t size, ScatterGatherEntry& sge)
262224 sge.length = static_cast <std::uint32_t >(size);
263225 sge.tokenId = tokenId_;
264226 sge.slot_index = idx;
227+ sge.memory_type = memory_type_;
265228 return Status::OK ();
266229}
267230
0 commit comments