File: generic_pool_allocator.inl

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (121 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download
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
/*
 * Copyright (C) 2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/device/device.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/utilities/buffer_pool_allocator.inl"
#include "shared/source/utilities/generic_pool_allocator.h"
#include "shared/source/utilities/heap_allocator.h"

namespace NEO {

template <PoolTraits Traits>
GenericPool<Traits>::GenericPool(Device *device, size_t poolSize)
    : BaseType(device->getMemoryManager(), nullptr), device(device) {
    DEBUG_BREAK_IF(device->getProductHelper().is2MBLocalMemAlignmentEnabled() &&
                   !isAligned(poolSize, MemoryConstants::pageSize2M));

    auto properties = Traits::createAllocationProperties(device, poolSize);
    auto graphicsAllocation = this->memoryManager->allocateGraphicsMemoryWithProperties(properties);

    this->chunkAllocator.reset(new NEO::HeapAllocator(this->params.startingOffset,
                                                      graphicsAllocation ? graphicsAllocation->getUnderlyingBufferSize() : 0u,
                                                      MemoryConstants::pageSize,
                                                      0u));
    this->mainStorage.reset(graphicsAllocation);
    this->mtx = std::make_unique<std::mutex>();
    stackVec.push_back(graphicsAllocation);
}

template <PoolTraits Traits>
GenericPool<Traits>::GenericPool(GenericPool &&pool) noexcept : BaseType(std::move(pool)) {
    mtx.reset(pool.mtx.release());
    this->stackVec = std::move(pool.stackVec);
    this->device = pool.device;
}

template <PoolTraits Traits>
GenericPool<Traits>::~GenericPool() {
    if (this->mainStorage) {
        device->getMemoryManager()->freeGraphicsMemory(this->mainStorage.release());
    }
}

template <PoolTraits Traits>
SharedPoolAllocation *GenericPool<Traits>::allocate(size_t size) {
    auto offset = static_cast<size_t>(this->chunkAllocator->allocate(size));
    if (offset == 0) {
        return nullptr;
    }
    return new SharedPoolAllocation{this->mainStorage.get(),
                                    offset - this->params.startingOffset,
                                    size,
                                    mtx.get()};
}

template <PoolTraits Traits>
const StackVec<GraphicsAllocation *, 1> &GenericPool<Traits>::getAllocationsVector() {
    return stackVec;
}

template <PoolTraits Traits>
GenericPoolAllocator<Traits>::GenericPoolAllocator(Device *device) : device(device) {}

template <PoolTraits Traits>
SharedPoolAllocation *GenericPoolAllocator<Traits>::requestGraphicsAllocation(size_t size) {
    if (size > Traits::maxAllocationSize) {
        return nullptr;
    }

    std::lock_guard<std::mutex> lock(allocatorMtx);

    if (this->bufferPools.empty()) {
        this->addNewBufferPool(GenericPool<Traits>(device, alignToPoolSize(Traits::defaultPoolSize)));
    }

    auto allocFromPool = allocateFromPools(size);
    if (allocFromPool != nullptr) {
        return allocFromPool;
    }

    this->drain();

    allocFromPool = allocateFromPools(size);
    if (allocFromPool != nullptr) {
        return allocFromPool;
    }

    this->addNewBufferPool(GenericPool<Traits>(device, alignToPoolSize(Traits::defaultPoolSize)));
    return allocateFromPools(size);
}

template <PoolTraits Traits>
void GenericPoolAllocator<Traits>::freeSharedAllocation(SharedPoolAllocation *sharedPoolAllocation) {
    std::unique_lock lock(allocatorMtx);
    this->tryFreeFromPoolBuffer(sharedPoolAllocation->getGraphicsAllocation(),
                                sharedPoolAllocation->getOffset(),
                                sharedPoolAllocation->getSize());
    delete sharedPoolAllocation;
}

template <PoolTraits Traits>
SharedPoolAllocation *GenericPoolAllocator<Traits>::allocateFromPools(size_t size) {
    for (auto &pool : this->bufferPools) {
        if (auto allocation = pool.allocate(size)) {
            return allocation;
        }
    }
    return nullptr;
}

template <PoolTraits Traits>
size_t GenericPoolAllocator<Traits>::alignToPoolSize(size_t size) const {
    return alignUp(size, Traits::poolAlignment);
}

} // namespace NEO