Skip to content

[SYCL][L0] Make cache of command-lists in the context per-device #5197

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 2 commits into from
Dec 23, 2021
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
43 changes: 24 additions & 19 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,21 +798,24 @@ pi_result _pi_context::finalize() {
// Destroy the command list used for initializations
ZE_CALL(zeCommandListDestroy, (ZeCommandListInit));

std::lock_guard<std::mutex> Lock(ZeCommandListCacheMutex);
for (ze_command_list_handle_t &ZeCommandList : ZeComputeCommandListCache) {
if (ZeCommandList)
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
}
for (ze_command_list_handle_t &ZeCommandList : ZeCopyCommandListCache) {
if (ZeCommandList)
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
}

// Adjust the number of command lists created on this platform.
auto Platform = Devices[0]->Platform;
Platform->ZeGlobalCommandListCount -= ZeComputeCommandListCache.size();
Platform->ZeGlobalCommandListCount -= ZeCopyCommandListCache.size();

std::lock_guard<std::mutex> Lock(ZeCommandListCacheMutex);
for (auto &List : ZeComputeCommandListCache) {
for (ze_command_list_handle_t &ZeCommandList : List.second) {
if (ZeCommandList)
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
}
Platform->ZeGlobalCommandListCount -= List.second.size();
}
for (auto &List : ZeCopyCommandListCache) {
for (ze_command_list_handle_t &ZeCommandList : List.second) {
if (ZeCommandList)
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
}
Platform->ZeGlobalCommandListCount -= List.second.size();
}
return PI_SUCCESS;
}

Expand All @@ -825,9 +828,10 @@ bool _pi_queue::isInOrderQueue() const {
pi_result _pi_queue::resetCommandList(pi_command_list_ptr_t CommandList,
bool MakeAvailable) {
bool UseCopyEngine = CommandList->second.isCopy();
auto &ZeCommandListCache = UseCopyEngine
? this->Context->ZeCopyCommandListCache
: this->Context->ZeComputeCommandListCache;
auto &ZeCommandListCache =
UseCopyEngine
? this->Context->ZeCopyCommandListCache[this->Device->ZeDevice]
: this->Context->ZeComputeCommandListCache[this->Device->ZeDevice];

// Fence had been signalled meaning the associated command-list completed.
// Reset the fence and put the command list into a cache for reuse in PI
Expand Down Expand Up @@ -1048,9 +1052,10 @@ _pi_context::getAvailableCommandList(pi_queue Queue,
_pi_result pi_result = PI_OUT_OF_RESOURCES;
ZeStruct<ze_fence_desc_t> ZeFenceDesc;

auto &ZeCommandListCache = UseCopyEngine
? Queue->Context->ZeCopyCommandListCache
: Queue->Context->ZeComputeCommandListCache;
auto &ZeCommandListCache =
UseCopyEngine
? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice]
: Queue->Context->ZeComputeCommandListCache[Queue->Device->ZeDevice];

// Initally, we need to check if a command list has already been created
// on this device that is available for use. If so, then reuse that
Expand All @@ -1068,7 +1073,7 @@ _pi_context::getAvailableCommandList(pi_queue Queue,
CommandList->second.InUse = true;
} else {
// If there is a command list available on this context, but it
// wasn't yet used in this queue then creat a new entry in this
// wasn't yet used in this queue then create a new entry in this
// queue's map to hold the fence and other associated command
// list information.

Expand Down
16 changes: 12 additions & 4 deletions sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,18 @@ struct _pi_context : _pi_object {
// Mutex Lock for the Command List Cache. This lock is used to control both
// compute and copy command list caches.
std::mutex ZeCommandListCacheMutex;
// Cache of all currently Available Command Lists for use by PI APIs
std::list<ze_command_list_handle_t> ZeComputeCommandListCache;
// Cache of all currently Available Copy Command Lists for use by PI APIs
std::list<ze_command_list_handle_t> ZeCopyCommandListCache;
// Cache of all currently available/completed command/copy lists.
// Note that command-list can only be re-used on the same device.
//
// TODO: explore if we should use root-device for creating command-lists
// as spec says that in that case any sub-device can re-use it: "The
// application must only use the command list for the device, or its
// sub-devices, which was provided during creation."
//
std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t>>
ZeComputeCommandListCache;
std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t>>
ZeCopyCommandListCache;

// Retrieves a command list for executing on this device along with
// a fence to be used in tracking the execution of this command list.
Expand Down