Skip to content

Commit edd9002

Browse files
authored
[SYCL] Usm allocator refactor (#7785)
This commit makes USMAllocator unaware of the memory type on which it is operating. The user of USMAllocator is responsible for setting appropriate options for each instance of USMAllocator. This is the first step to making USMAllocator generic. In the next step, I'd like to start integration with Unified Memory Allocation (oneMemory). USMAllocator would be used as a heap manager by UMA.
1 parent 65d6144 commit edd9002

File tree

7 files changed

+430
-371
lines changed

7 files changed

+430
-371
lines changed

sycl/plugins/level_zero/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ add_sycl_plugin(level_zero
9090
"../unified_runtime/ur/ur.cpp"
9191
"../unified_runtime/ur/usm_allocator.cpp"
9292
"../unified_runtime/ur/usm_allocator.hpp"
93+
"../unified_runtime/ur/usm_allocator_config.cpp"
94+
"../unified_runtime/ur/usm_allocator_config.hpp"
9395
"../unified_runtime/ur/adapters/level_zero/ur_level_zero.hpp"
9496
"../unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp"
9597
# Following are the PI Level-Zero Plugin only codes.

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <zet_api.h>
2828

29+
#include "ur/usm_allocator_config.hpp"
2930
#include "ur_bindings.hpp"
3031

3132
extern "C" {
@@ -96,6 +97,8 @@ static const bool IndirectAccessTrackingEnabled = [] {
9697
nullptr;
9798
}();
9899

100+
static usm_settings::USMAllocatorConfig USMAllocatorConfigInstance;
101+
99102
// Map from L0 to PI result.
100103
static inline pi_result mapError(ze_result_t Result) {
101104
return ur2piResult(ze2urResult(Result));
@@ -775,18 +778,24 @@ pi_result _pi_context::initialize() {
775778
auto createUSMAllocators = [this](pi_device Device) {
776779
SharedMemAllocContexts.emplace(
777780
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
778-
std::make_tuple(std::unique_ptr<SystemMemory>(
779-
new USMSharedMemoryAlloc(this, Device))));
781+
std::make_tuple(
782+
std::unique_ptr<SystemMemory>(
783+
new USMSharedMemoryAlloc(this, Device)),
784+
USMAllocatorConfigInstance.Configs[usm_settings::MemType::Shared]));
780785

781786
SharedReadOnlyMemAllocContexts.emplace(
782787
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
783788
std::make_tuple(std::unique_ptr<SystemMemory>(
784-
new USMSharedReadOnlyMemoryAlloc(this, Device))));
789+
new USMSharedReadOnlyMemoryAlloc(this, Device)),
790+
USMAllocatorConfigInstance
791+
.Configs[usm_settings::MemType::SharedReadOnly]));
785792

786793
DeviceMemAllocContexts.emplace(
787794
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
788-
std::make_tuple(std::unique_ptr<SystemMemory>(
789-
new USMDeviceMemoryAlloc(this, Device))));
795+
std::make_tuple(
796+
std::unique_ptr<SystemMemory>(
797+
new USMDeviceMemoryAlloc(this, Device)),
798+
USMAllocatorConfigInstance.Configs[usm_settings::MemType::Device]));
790799
};
791800

792801
// Recursive helper to call createUSMAllocators for all sub-devices
@@ -808,7 +817,8 @@ pi_result _pi_context::initialize() {
808817
// are device-specific. Host allocations are not device-dependent therefore
809818
// we don't need a map with device as key.
810819
HostMemAllocContext = std::make_unique<USMAllocContext>(
811-
std::unique_ptr<SystemMemory>(new USMHostMemoryAlloc(this)));
820+
std::unique_ptr<SystemMemory>(new USMHostMemoryAlloc(this)),
821+
USMAllocatorConfigInstance.Configs[usm_settings::MemType::Host]);
812822

813823
// We may allocate memory to this root device so create allocators.
814824
if (SingleRootDevice &&
@@ -8116,12 +8126,6 @@ pi_result USMHostMemoryAlloc::allocateImpl(void **ResultPtr, size_t Size,
81168126
return USMHostAllocImpl(ResultPtr, Context, nullptr, Size, Alignment);
81178127
}
81188128

8119-
MemType USMSharedMemoryAlloc::getMemTypeImpl() { return MemType::Shared; }
8120-
8121-
MemType USMDeviceMemoryAlloc::getMemTypeImpl() { return MemType::Device; }
8122-
8123-
MemType USMHostMemoryAlloc::getMemTypeImpl() { return MemType::Host; }
8124-
81258129
void *USMMemoryAllocBase::allocate(size_t Size) {
81268130
void *Ptr = nullptr;
81278131

@@ -8150,8 +8154,6 @@ void USMMemoryAllocBase::deallocate(void *Ptr, bool OwnZeMemHandle) {
81508154
}
81518155
}
81528156

8153-
MemType USMMemoryAllocBase::getMemType() { return getMemTypeImpl(); }
8154-
81558157
pi_result piextUSMDeviceAlloc(void **ResultPtr, pi_context Context,
81568158
pi_device Device,
81578159
pi_usm_mem_properties *Properties, size_t Size,
@@ -9227,7 +9229,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
92279229
// The host allocation may already exists, e.g. with imported
92289230
// host ptr, or in case of interop buffer.
92299231
if (!HostAllocation.ZeHandle) {
9230-
if (enableBufferPooling()) {
9232+
if (USMAllocatorConfigInstance.EnableBuffers) {
92319233
HostAllocation.ReleaseAction = allocation_t::free;
92329234
PI_CALL(piextUSMHostAlloc(pi_cast<void **>(&ZeHandle), Context, nullptr,
92339235
Size, getAlignment()));
@@ -9280,7 +9282,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
92809282
Allocation.Valid = true;
92819283
return PI_SUCCESS;
92829284
} else { // Create device allocation
9283-
if (enableBufferPooling()) {
9285+
if (USMAllocatorConfigInstance.EnableBuffers) {
92849286
Allocation.ReleaseAction = allocation_t::free;
92859287
PI_CALL(piextUSMDeviceAlloc(pi_cast<void **>(&ZeHandle), Context,
92869288
Device, nullptr, Size, getAlignment()));
@@ -9341,7 +9343,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
93419343
// host ptr, or in case of interop buffer.
93429344
if (!HostAllocation.ZeHandle) {
93439345
void *ZeHandleHost;
9344-
if (enableBufferPooling()) {
9346+
if (USMAllocatorConfigInstance.EnableBuffers) {
93459347
HostAllocation.ReleaseAction = allocation_t::free;
93469348
PI_CALL(piextUSMHostAlloc(&ZeHandleHost, Context, nullptr, Size,
93479349
getAlignment()));

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,20 @@ class USMMemoryAllocBase : public SystemMemory {
118118
// type
119119
virtual pi_result allocateImpl(void **ResultPtr, size_t Size,
120120
pi_uint32 Alignment) = 0;
121-
virtual MemType getMemTypeImpl() = 0;
122121

123122
public:
124123
USMMemoryAllocBase(pi_context Ctx, pi_device Dev)
125124
: Context{Ctx}, Device{Dev} {}
126125
void *allocate(size_t Size) override final;
127126
void *allocate(size_t Size, size_t Alignment) override final;
128127
void deallocate(void *Ptr, bool OwnZeMemHandle) override final;
129-
MemType getMemType() override final;
130128
};
131129

132130
// Allocation routines for shared memory type
133131
class USMSharedMemoryAlloc : public USMMemoryAllocBase {
134132
protected:
135133
pi_result allocateImpl(void **ResultPtr, size_t Size,
136134
pi_uint32 Alignment) override;
137-
MemType getMemTypeImpl() override;
138135

139136
public:
140137
USMSharedMemoryAlloc(pi_context Ctx, pi_device Dev)
@@ -146,7 +143,6 @@ class USMSharedReadOnlyMemoryAlloc : public USMMemoryAllocBase {
146143
protected:
147144
pi_result allocateImpl(void **ResultPtr, size_t Size,
148145
pi_uint32 Alignment) override;
149-
MemType getMemTypeImpl() override { return MemType::SharedReadOnly; }
150146

151147
public:
152148
USMSharedReadOnlyMemoryAlloc(pi_context Ctx, pi_device Dev)
@@ -158,7 +154,6 @@ class USMDeviceMemoryAlloc : public USMMemoryAllocBase {
158154
protected:
159155
pi_result allocateImpl(void **ResultPtr, size_t Size,
160156
pi_uint32 Alignment) override;
161-
MemType getMemTypeImpl() override;
162157

163158
public:
164159
USMDeviceMemoryAlloc(pi_context Ctx, pi_device Dev)
@@ -170,7 +165,6 @@ class USMHostMemoryAlloc : public USMMemoryAllocBase {
170165
protected:
171166
pi_result allocateImpl(void **ResultPtr, size_t Size,
172167
pi_uint32 Alignment) override;
173-
MemType getMemTypeImpl() override;
174168

175169
public:
176170
USMHostMemoryAlloc(pi_context Ctx) : USMMemoryAllocBase(Ctx, nullptr) {}

0 commit comments

Comments
 (0)