Skip to content

Commit ca457d9

Browse files
[SYCL][L0] Make cache of command-lists in the context per-device (#5197)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent dc9bd3f commit ca457d9

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -798,21 +798,24 @@ pi_result _pi_context::finalize() {
798798
// Destroy the command list used for initializations
799799
ZE_CALL(zeCommandListDestroy, (ZeCommandListInit));
800800

801-
std::lock_guard<std::mutex> Lock(ZeCommandListCacheMutex);
802-
for (ze_command_list_handle_t &ZeCommandList : ZeComputeCommandListCache) {
803-
if (ZeCommandList)
804-
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
805-
}
806-
for (ze_command_list_handle_t &ZeCommandList : ZeCopyCommandListCache) {
807-
if (ZeCommandList)
808-
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
809-
}
810-
811801
// Adjust the number of command lists created on this platform.
812802
auto Platform = Devices[0]->Platform;
813-
Platform->ZeGlobalCommandListCount -= ZeComputeCommandListCache.size();
814-
Platform->ZeGlobalCommandListCount -= ZeCopyCommandListCache.size();
815803

804+
std::lock_guard<std::mutex> Lock(ZeCommandListCacheMutex);
805+
for (auto &List : ZeComputeCommandListCache) {
806+
for (ze_command_list_handle_t &ZeCommandList : List.second) {
807+
if (ZeCommandList)
808+
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
809+
}
810+
Platform->ZeGlobalCommandListCount -= List.second.size();
811+
}
812+
for (auto &List : ZeCopyCommandListCache) {
813+
for (ze_command_list_handle_t &ZeCommandList : List.second) {
814+
if (ZeCommandList)
815+
ZE_CALL(zeCommandListDestroy, (ZeCommandList));
816+
}
817+
Platform->ZeGlobalCommandListCount -= List.second.size();
818+
}
816819
return PI_SUCCESS;
817820
}
818821

@@ -825,9 +828,10 @@ bool _pi_queue::isInOrderQueue() const {
825828
pi_result _pi_queue::resetCommandList(pi_command_list_ptr_t CommandList,
826829
bool MakeAvailable) {
827830
bool UseCopyEngine = CommandList->second.isCopy();
828-
auto &ZeCommandListCache = UseCopyEngine
829-
? this->Context->ZeCopyCommandListCache
830-
: this->Context->ZeComputeCommandListCache;
831+
auto &ZeCommandListCache =
832+
UseCopyEngine
833+
? this->Context->ZeCopyCommandListCache[this->Device->ZeDevice]
834+
: this->Context->ZeComputeCommandListCache[this->Device->ZeDevice];
831835

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

1051-
auto &ZeCommandListCache = UseCopyEngine
1052-
? Queue->Context->ZeCopyCommandListCache
1053-
: Queue->Context->ZeComputeCommandListCache;
1055+
auto &ZeCommandListCache =
1056+
UseCopyEngine
1057+
? Queue->Context->ZeCopyCommandListCache[Queue->Device->ZeDevice]
1058+
: Queue->Context->ZeComputeCommandListCache[Queue->Device->ZeDevice];
10541059

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

sycl/plugins/level_zero/pi_level_zero.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,18 @@ struct _pi_context : _pi_object {
517517
// Mutex Lock for the Command List Cache. This lock is used to control both
518518
// compute and copy command list caches.
519519
std::mutex ZeCommandListCacheMutex;
520-
// Cache of all currently Available Command Lists for use by PI APIs
521-
std::list<ze_command_list_handle_t> ZeComputeCommandListCache;
522-
// Cache of all currently Available Copy Command Lists for use by PI APIs
523-
std::list<ze_command_list_handle_t> ZeCopyCommandListCache;
520+
// Cache of all currently available/completed command/copy lists.
521+
// Note that command-list can only be re-used on the same device.
522+
//
523+
// TODO: explore if we should use root-device for creating command-lists
524+
// as spec says that in that case any sub-device can re-use it: "The
525+
// application must only use the command list for the device, or its
526+
// sub-devices, which was provided during creation."
527+
//
528+
std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t>>
529+
ZeComputeCommandListCache;
530+
std::unordered_map<ze_device_handle_t, std::list<ze_command_list_handle_t>>
531+
ZeCopyCommandListCache;
524532

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

0 commit comments

Comments
 (0)