1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
/*
* Copyright (C) 2023-2026 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/memory_manager/unified_memory_pooling.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/device/device.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/ptr_math.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/memory_manager/memory_operations_handler.h"
#include "shared/source/memory_manager/unified_memory_manager.h"
#include "shared/source/memory_manager/usm_pool_params.h"
#include "shared/source/utilities/heap_allocator.h"
namespace NEO {
bool UsmMemAllocPool::initialize(SVMAllocsManager *svmMemoryManager, const UnifiedMemoryProperties &memoryProperties, size_t poolSize, size_t minServicedSize, size_t maxServicedSize) {
void *poolAllocation = nullptr;
if (memoryProperties.memoryType == InternalMemoryType::hostUnifiedMemory) {
poolAllocation = svmMemoryManager->createHostUnifiedMemoryAllocation(poolSize, memoryProperties);
} else {
poolAllocation = svmMemoryManager->createUnifiedMemoryAllocation(poolSize, memoryProperties);
}
if (nullptr == poolAllocation) {
return false;
}
auto svmData = svmMemoryManager->getSVMAlloc(poolAllocation);
return initialize(svmMemoryManager, poolAllocation, svmData, minServicedSize, maxServicedSize);
}
bool UsmMemAllocPool::initialize(SVMAllocsManager *svmMemoryManager, void *ptr, SvmAllocationData *svmData, size_t minServicedSize, size_t maxServicedSize) {
DEBUG_BREAK_IF(nullptr == ptr);
this->pool = ptr;
this->svmMemoryManager = svmMemoryManager;
this->allocationData = svmData;
this->poolEnd = ptrOffset(this->pool, svmData->size);
this->chunkAllocator.reset(new HeapAllocator(castToUint64(this->pool),
svmData->size,
chunkAlignment,
maxServicedSize / 2));
this->poolMemoryType = svmData->memoryType;
this->poolInfo.minServicedSize = minServicedSize;
this->poolInfo.maxServicedSize = maxServicedSize;
this->poolInfo.poolSize = svmData->size;
this->device = svmData->device;
if (nullptr != device) {
allocation = svmData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex());
memoryOperationsIface = device->getRootDeviceEnvironment().memoryOperationsInterface.get();
}
return true;
}
bool UsmMemAllocPool::isInitialized() const {
return this->pool;
}
size_t UsmMemAllocPool::getPoolSize() const {
return this->poolInfo.poolSize;
}
void UsmMemAllocPool::cleanup() {
if (isInitialized()) {
if (this->customCleanup) {
this->customCleanup(this->pool);
}
[[maybe_unused]] const auto status = this->svmMemoryManager->freeSVMAlloc(this->pool, false);
DEBUG_BREAK_IF(false == status);
this->svmMemoryManager = nullptr;
this->pool = nullptr;
this->poolEnd = nullptr;
this->poolInfo.poolSize = 0u;
this->poolMemoryType = InternalMemoryType::notSpecified;
}
}
bool UsmMemAllocPool::alignmentIsAllowed(size_t alignment) {
return alignment <= poolAlignment;
}
bool UsmMemAllocPool::sizeIsAllowed(size_t size) {
return size >= poolInfo.minServicedSize && size <= poolInfo.maxServicedSize;
}
bool UsmMemAllocPool::flagsAreAllowed(const UnifiedMemoryProperties &memoryProperties) {
auto flagsWithoutCompression = memoryProperties.allocationFlags;
flagsWithoutCompression.flags.compressedHint = 0u;
flagsWithoutCompression.flags.uncompressedHint = 0u;
return flagsWithoutCompression.allFlags == 0u &&
memoryProperties.allocationFlags.allAllocFlags == 0u &&
memoryProperties.allocationFlags.hostptr == 0u;
}
double UsmMemAllocPool::getPercentOfFreeMemoryForRecycling(InternalMemoryType memoryType) {
if (InternalMemoryType::deviceUnifiedMemory == memoryType) {
return 0.08;
}
if (InternalMemoryType::hostUnifiedMemory == memoryType) {
return 0.02;
}
return 0.0;
}
bool UsmMemAllocPool::canBePooled(size_t size, const UnifiedMemoryProperties &memoryProperties) {
return sizeIsAllowed(size) &&
alignmentIsAllowed(memoryProperties.alignment) &&
flagsAreAllowed(memoryProperties) &&
memoryProperties.memoryType == this->poolMemoryType;
}
void *UsmMemAllocPool::createUnifiedMemoryAllocation(size_t requestedSize, const UnifiedMemoryProperties &memoryProperties) {
void *pooledPtr = nullptr;
if (isInitialized()) {
if (false == canBePooled(requestedSize, memoryProperties)) {
return nullptr;
}
std::unique_lock<std::mutex> lock(mtx);
auto actualSize = requestedSize;
auto pooledAddress = this->chunkAllocator->allocateWithCustomAlignment(actualSize, memoryProperties.alignment);
if (!pooledAddress) {
return nullptr;
}
pooledPtr = addrToPtr(pooledAddress);
this->allocations.insert(pooledPtr, AllocationInfo{pooledAddress, actualSize, requestedSize});
++this->svmMemoryManager->allocationsCounter;
}
return pooledPtr;
}
bool UsmMemAllocPool::isInPool(const void *ptr) const {
return ptr >= this->pool && ptr < this->poolEnd;
}
bool UsmMemAllocPool::isEmpty() const {
return 0u == this->allocations.getNumAllocs();
}
bool UsmMemAllocPool::freeSVMAlloc(const void *ptr, bool blocking) {
if (isInitialized() && isInPool(ptr)) {
std::unique_lock<std::mutex> lock(mtx);
auto allocationInfo = allocations.extract(ptr);
if (allocationInfo) {
DEBUG_BREAK_IF(allocationInfo->size == 0 || allocationInfo->address == 0);
if (blocking) {
svmMemoryManager->waitForEnginesCompletion(allocationData);
}
this->chunkAllocator->free(allocationInfo->address, allocationInfo->size);
if (trackResidency) {
OPTIONAL_UNRECOVERABLE_IF(nullptr == device || nullptr == allocation);
if (allocationInfo->isResident && 1u == this->residencyCount--) {
DEBUG_BREAK_IF(!isEmpty());
evictPool();
}
}
return true;
}
}
return false;
}
size_t UsmMemAllocPool::getPooledAllocationSize(const void *ptr) {
if (isInitialized() && isInPool(ptr)) {
std::unique_lock<std::mutex> lock(mtx);
auto allocationInfo = allocations.get(ptr);
if (allocationInfo) {
return allocationInfo->requestedSize;
}
}
return 0u;
}
void *UsmMemAllocPool::getPooledAllocationBasePtr(const void *ptr) {
if (isInitialized() && isInPool(ptr)) {
std::unique_lock<std::mutex> lock(mtx);
auto allocationInfo = allocations.get(ptr);
if (allocationInfo) {
return addrToPtr(allocationInfo->address);
}
}
return nullptr;
}
size_t UsmMemAllocPool::getOffsetInPool(const void *ptr) const {
if (isInitialized() && isInPool(ptr)) {
return ptrDiff(ptr, this->pool);
}
return 0u;
}
uint64_t UsmMemAllocPool::getPoolAddress() const {
return castToUint64(this->pool);
}
PoolInfo UsmMemAllocPool::getPoolInfo() const {
return poolInfo;
}
MemoryOperationsStatus UsmMemAllocPool::evictPool() {
return memoryOperationsIface->evict(device, *allocation);
}
MemoryOperationsStatus UsmMemAllocPool::makePoolResident() {
return memoryOperationsIface->makeResident(device, ArrayRef<NEO::GraphicsAllocation *>(&allocation, 1), true, true);
}
const std::array<const PoolInfo, 3> UsmMemAllocPoolsManager::getPoolInfos() {
return (device ? PoolInfo::getPoolInfos(device->getGfxCoreHelper()) : PoolInfo::getHostPoolInfos());
}
size_t UsmMemAllocPoolsManager::getMaxPoolableSize() {
return (device ? PoolInfo::getMaxPoolableSize(device->getGfxCoreHelper()) : PoolInfo::getHostMaxPoolableSize());
}
bool UsmMemAllocPoolsManager::initialize(SVMAllocsManager *svmMemoryManager) {
DEBUG_BREAK_IF(poolMemoryType != InternalMemoryType::deviceUnifiedMemory &&
poolMemoryType != InternalMemoryType::hostUnifiedMemory);
DEBUG_BREAK_IF(device == nullptr && poolMemoryType == InternalMemoryType::deviceUnifiedMemory);
this->svmMemoryManager = svmMemoryManager;
for (const auto &poolInfo : getPoolInfos()) {
this->pools[poolInfo] = std::vector<std::unique_ptr<UsmMemAllocPool>>();
auto pool = tryAddPool(poolInfo);
if (nullptr == pool) {
cleanup();
return false;
}
}
return true;
}
bool UsmMemAllocPoolsManager::isInitialized() const {
return nullptr != this->svmMemoryManager;
}
void UsmMemAllocPoolsManager::cleanup() {
for (auto &[_, bucket] : this->pools) {
for (const auto &pool : bucket) {
pool->cleanup();
}
}
this->pools.clear();
this->svmMemoryManager = nullptr;
}
void *UsmMemAllocPoolsManager::createUnifiedMemoryAllocation(size_t size, const UnifiedMemoryProperties &memoryProperties) {
DEBUG_BREAK_IF(false == isInitialized());
if (!canBePooled(size, memoryProperties)) {
return nullptr;
}
std::unique_lock<std::mutex> lock(mtx);
void *ptr = nullptr;
for (const auto &poolInfo : getPoolInfos()) {
if (size <= poolInfo.maxServicedSize) {
for (auto &pool : this->pools[poolInfo]) {
if (nullptr != (ptr = pool->createUnifiedMemoryAllocation(size, memoryProperties))) {
break;
}
}
if (nullptr == ptr) {
if (auto pool = tryAddPool(poolInfo)) {
ptr = pool->createUnifiedMemoryAllocation(size, memoryProperties);
DEBUG_BREAK_IF(nullptr == ptr);
}
}
break;
}
}
return ptr;
}
UsmMemAllocPool *UsmMemAllocPoolsManager::tryAddPool(PoolInfo poolInfo) {
UsmMemAllocPool *poolPtr = nullptr;
if (canAddPool(poolInfo)) {
auto pool = std::make_unique<UsmMemAllocPool>();
if (pool->initialize(svmMemoryManager, poolMemoryProperties, poolInfo.poolSize, poolInfo.minServicedSize, poolInfo.maxServicedSize)) {
poolPtr = pool.get();
this->totalSize += pool->getPoolSize();
if (trackResidency) {
pool->enableResidencyTracking();
}
pool->setCustomCleanup(this->customCleanup);
this->pools[poolInfo].push_back(std::move(pool));
}
}
return poolPtr;
}
bool UsmMemAllocPoolsManager::canAddPool(PoolInfo poolInfo) {
return true;
}
bool UsmMemAllocPoolsManager::canBePooled(size_t size, const UnifiedMemoryProperties &memoryProperties) {
return size <= getMaxPoolableSize() &&
UsmMemAllocPool::alignmentIsAllowed(memoryProperties.alignment) &&
UsmMemAllocPool::flagsAreAllowed(memoryProperties);
}
void UsmMemAllocPoolsManager::trimEmptyPools(PoolInfo poolInfo) {
std::lock_guard lock(mtx);
auto &bucket = pools[poolInfo];
auto firstEmptyPoolIt = std::partition(bucket.begin(), bucket.end(), [](std::unique_ptr<UsmMemAllocPool> &pool) {
return !pool->isEmpty();
});
const auto emptyPoolsCount = static_cast<size_t>(std::distance(firstEmptyPoolIt, bucket.end()));
if (emptyPoolsCount > maxEmptyPoolsPerBucket) {
std::advance(firstEmptyPoolIt, maxEmptyPoolsPerBucket);
for (auto it = firstEmptyPoolIt; it != bucket.end(); ++it) {
(*it)->cleanup();
}
bucket.erase(firstEmptyPoolIt, bucket.end());
}
}
bool UsmMemAllocPoolsManager::freeSVMAlloc(const void *ptr, bool blocking) {
bool allocFreed = false;
if (auto pool = this->getPoolContainingAlloc(ptr); pool) {
allocFreed = pool->freeSVMAlloc(ptr, blocking);
if (allocFreed && pool->isEmpty()) {
trimEmptyPools(pool->getPoolInfo());
}
}
return allocFreed;
}
size_t UsmMemAllocPoolsManager::getPooledAllocationSize(const void *ptr) {
if (auto pool = this->getPoolContainingAlloc(ptr); pool) {
return pool->getPooledAllocationSize(ptr);
}
return 0u;
}
void *UsmMemAllocPoolsManager::getPooledAllocationBasePtr(const void *ptr) {
if (auto pool = this->getPoolContainingAlloc(ptr); pool) {
return pool->getPooledAllocationBasePtr(ptr);
}
return nullptr;
}
size_t UsmMemAllocPoolsManager::getOffsetInPool(const void *ptr) {
if (auto pool = this->getPoolContainingAlloc(ptr); pool) {
return pool->getOffsetInPool(ptr);
}
return 0u;
}
UsmMemAllocPool *UsmMemAllocPoolsManager::getPoolContainingAlloc(const void *ptr) {
std::unique_lock<std::mutex> lock(mtx);
for (const auto &poolInfo : getPoolInfos()) {
for (auto &pool : this->pools[poolInfo]) {
if (pool->isInPool(ptr)) {
return pool.get();
}
}
}
return nullptr;
}
bool UsmMemAllocPoolsFacade::initialize(InternalMemoryType memoryType, const RootDeviceIndicesContainer &rootDeviceIndices, const std::map<uint32_t, DeviceBitfield> &subdeviceBitfields, Device *device, SVMAllocsManager *svmMemoryManager) {
bool poolManagerEnabled = false;
if (NEO::debugManager.flags.EnableUsmAllocationPoolManager.get() != -1) {
poolManagerEnabled = NEO::debugManager.flags.EnableUsmAllocationPoolManager.get() != 0;
}
if (poolManagerEnabled) {
this->poolManager = std::make_unique<UsmMemAllocPoolsManager>(memoryType, rootDeviceIndices, subdeviceBitfields, device);
return this->poolManager->initialize(svmMemoryManager);
} else {
this->pool = std::make_unique<UsmMemAllocPool>();
UnifiedMemoryProperties memoryProperties(memoryType, MemoryConstants::pageSize2M,
rootDeviceIndices, subdeviceBitfields);
auto usmPoolParams = UsmPoolParams::getUsmPoolParams(device->getGfxCoreHelper());
if (memoryType == InternalMemoryType::deviceUnifiedMemory && debugManager.flags.EnableDeviceUsmAllocationPool.get() != -1) {
usmPoolParams.poolSize = debugManager.flags.EnableDeviceUsmAllocationPool.get() * MemoryConstants::megaByte;
} else if (memoryType == InternalMemoryType::hostUnifiedMemory && debugManager.flags.EnableHostUsmAllocationPool.get() != -1) {
usmPoolParams.poolSize = debugManager.flags.EnableHostUsmAllocationPool.get() * MemoryConstants::megaByte;
}
if (memoryType == InternalMemoryType::deviceUnifiedMemory) {
memoryProperties.device = device;
}
return this->pool->initialize(svmMemoryManager, memoryProperties, usmPoolParams.poolSize, usmPoolParams.minServicedSize, usmPoolParams.maxServicedSize);
}
return false;
}
bool UsmMemAllocPoolsFacade::isInitialized() const {
if (this->poolManager) {
return this->poolManager->isInitialized();
} else if (this->pool) {
return this->pool->isInitialized();
}
return false;
}
void UsmMemAllocPoolsFacade::cleanup() {
if (this->poolManager) {
this->poolManager->cleanup();
this->poolManager.reset();
} else if (this->pool) {
this->pool->cleanup();
this->pool.reset();
}
}
void *UsmMemAllocPoolsFacade::createUnifiedMemoryAllocation(size_t size, const UnifiedMemoryProperties &memoryProperties) {
if (this->poolManager) {
return this->poolManager->createUnifiedMemoryAllocation(size, memoryProperties);
} else if (this->pool) {
return this->pool->createUnifiedMemoryAllocation(size, memoryProperties);
}
return nullptr;
}
bool UsmMemAllocPoolsFacade::freeSVMAlloc(const void *ptr, bool blocking) {
if (this->poolManager) {
return this->poolManager->freeSVMAlloc(ptr, blocking);
} else if (this->pool) {
return this->pool->freeSVMAlloc(ptr, blocking);
}
return false;
}
size_t UsmMemAllocPoolsFacade::getPooledAllocationSize(const void *ptr) {
if (this->poolManager) {
return this->poolManager->getPooledAllocationSize(ptr);
} else if (this->pool) {
return this->pool->getPooledAllocationSize(ptr);
}
return 0u;
}
void *UsmMemAllocPoolsFacade::getPooledAllocationBasePtr(const void *ptr) {
if (this->poolManager) {
return this->poolManager->getPooledAllocationBasePtr(ptr);
} else if (this->pool) {
return this->pool->getPooledAllocationBasePtr(ptr);
}
return nullptr;
}
UsmMemAllocPool *UsmMemAllocPoolsFacade::getPoolContainingAlloc(const void *ptr) {
if (this->poolManager) {
if (auto poolPtr = this->poolManager->getPoolContainingAlloc(ptr)) {
return poolPtr;
}
} else if (this->pool && this->pool->isInPool(ptr)) {
return this->pool.get();
}
return nullptr;
}
} // namespace NEO
|