Skip to content

[SYCL] Usm allocator refactor #7785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sycl/plugins/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ add_sycl_plugin(level_zero
"../unified_runtime/ur/ur.cpp"
"../unified_runtime/ur/usm_allocator.cpp"
"../unified_runtime/ur/usm_allocator.hpp"
"../unified_runtime/ur/usm_allocator_config.cpp"
"../unified_runtime/ur/usm_allocator_config.hpp"
"../unified_runtime/ur/adapters/level_zero/ur_level_zero.hpp"
"../unified_runtime/ur/adapters/level_zero/ur_level_zero.cpp"
# Following are the PI Level-Zero Plugin only codes.
Expand Down
36 changes: 19 additions & 17 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <zet_api.h>

#include "ur/usm_allocator_config.hpp"
#include "ur_bindings.hpp"

extern "C" {
Expand Down Expand Up @@ -96,6 +97,8 @@ static const bool IndirectAccessTrackingEnabled = [] {
nullptr;
}();

static usm_settings::USMAllocatorConfig USMAllocatorConfigInstance;

// Map from L0 to PI result.
static inline pi_result mapError(ze_result_t Result) {
return ur2piResult(ze2urResult(Result));
Expand Down Expand Up @@ -775,18 +778,24 @@ pi_result _pi_context::initialize() {
auto createUSMAllocators = [this](pi_device Device) {
SharedMemAllocContexts.emplace(
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
std::make_tuple(std::unique_ptr<SystemMemory>(
new USMSharedMemoryAlloc(this, Device))));
std::make_tuple(
std::unique_ptr<SystemMemory>(
new USMSharedMemoryAlloc(this, Device)),
USMAllocatorConfigInstance.Configs[usm_settings::MemType::Shared]));

SharedReadOnlyMemAllocContexts.emplace(
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
std::make_tuple(std::unique_ptr<SystemMemory>(
new USMSharedReadOnlyMemoryAlloc(this, Device))));
new USMSharedReadOnlyMemoryAlloc(this, Device)),
USMAllocatorConfigInstance
.Configs[usm_settings::MemType::SharedReadOnly]));

DeviceMemAllocContexts.emplace(
std::piecewise_construct, std::make_tuple(Device->ZeDevice),
std::make_tuple(std::unique_ptr<SystemMemory>(
new USMDeviceMemoryAlloc(this, Device))));
std::make_tuple(
std::unique_ptr<SystemMemory>(
new USMDeviceMemoryAlloc(this, Device)),
USMAllocatorConfigInstance.Configs[usm_settings::MemType::Device]));
};

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

// We may allocate memory to this root device so create allocators.
if (SingleRootDevice &&
Expand Down Expand Up @@ -8111,12 +8121,6 @@ pi_result USMHostMemoryAlloc::allocateImpl(void **ResultPtr, size_t Size,
return USMHostAllocImpl(ResultPtr, Context, nullptr, Size, Alignment);
}

MemType USMSharedMemoryAlloc::getMemTypeImpl() { return MemType::Shared; }

MemType USMDeviceMemoryAlloc::getMemTypeImpl() { return MemType::Device; }

MemType USMHostMemoryAlloc::getMemTypeImpl() { return MemType::Host; }

void *USMMemoryAllocBase::allocate(size_t Size) {
void *Ptr = nullptr;

Expand Down Expand Up @@ -8145,8 +8149,6 @@ void USMMemoryAllocBase::deallocate(void *Ptr, bool OwnZeMemHandle) {
}
}

MemType USMMemoryAllocBase::getMemType() { return getMemTypeImpl(); }

pi_result piextUSMDeviceAlloc(void **ResultPtr, pi_context Context,
pi_device Device,
pi_usm_mem_properties *Properties, size_t Size,
Expand Down Expand Up @@ -9222,7 +9224,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
// The host allocation may already exists, e.g. with imported
// host ptr, or in case of interop buffer.
if (!HostAllocation.ZeHandle) {
if (enableBufferPooling()) {
if (USMAllocatorConfigInstance.EnableBuffers) {
HostAllocation.ReleaseAction = allocation_t::free;
PI_CALL(piextUSMHostAlloc(pi_cast<void **>(&ZeHandle), Context, nullptr,
Size, getAlignment()));
Expand Down Expand Up @@ -9275,7 +9277,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
Allocation.Valid = true;
return PI_SUCCESS;
} else { // Create device allocation
if (enableBufferPooling()) {
if (USMAllocatorConfigInstance.EnableBuffers) {
Allocation.ReleaseAction = allocation_t::free;
PI_CALL(piextUSMDeviceAlloc(pi_cast<void **>(&ZeHandle), Context,
Device, nullptr, Size, getAlignment()));
Expand Down Expand Up @@ -9336,7 +9338,7 @@ pi_result _pi_buffer::getZeHandle(char *&ZeHandle, access_mode_t AccessMode,
// host ptr, or in case of interop buffer.
if (!HostAllocation.ZeHandle) {
void *ZeHandleHost;
if (enableBufferPooling()) {
if (USMAllocatorConfigInstance.EnableBuffers) {
HostAllocation.ReleaseAction = allocation_t::free;
PI_CALL(piextUSMHostAlloc(&ZeHandleHost, Context, nullptr, Size,
getAlignment()));
Expand Down
6 changes: 0 additions & 6 deletions sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,20 @@ class USMMemoryAllocBase : public SystemMemory {
// type
virtual pi_result allocateImpl(void **ResultPtr, size_t Size,
pi_uint32 Alignment) = 0;
virtual MemType getMemTypeImpl() = 0;

public:
USMMemoryAllocBase(pi_context Ctx, pi_device Dev)
: Context{Ctx}, Device{Dev} {}
void *allocate(size_t Size) override final;
void *allocate(size_t Size, size_t Alignment) override final;
void deallocate(void *Ptr, bool OwnZeMemHandle) override final;
MemType getMemType() override final;
};

// Allocation routines for shared memory type
class USMSharedMemoryAlloc : public USMMemoryAllocBase {
protected:
pi_result allocateImpl(void **ResultPtr, size_t Size,
pi_uint32 Alignment) override;
MemType getMemTypeImpl() override;

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

public:
USMSharedReadOnlyMemoryAlloc(pi_context Ctx, pi_device Dev)
Expand All @@ -158,7 +154,6 @@ class USMDeviceMemoryAlloc : public USMMemoryAllocBase {
protected:
pi_result allocateImpl(void **ResultPtr, size_t Size,
pi_uint32 Alignment) override;
MemType getMemTypeImpl() override;

public:
USMDeviceMemoryAlloc(pi_context Ctx, pi_device Dev)
Expand All @@ -170,7 +165,6 @@ class USMHostMemoryAlloc : public USMMemoryAllocBase {
protected:
pi_result allocateImpl(void **ResultPtr, size_t Size,
pi_uint32 Alignment) override;
MemType getMemTypeImpl() override;

public:
USMHostMemoryAlloc(pi_context Ctx) : USMMemoryAllocBase(Ctx, nullptr) {}
Expand Down
Loading