Skip to content

Commit b7c2fd9

Browse files
committed
[L0v2] add submitted kernel vector compaction
L0v2 avoids internally tracking each kernel submission through an event for lifetime management. Instead, when a kernel is submitted to the queue, its handle is added to a vector, to be removed at the next queue synchronization point, urQueueFinish(). This is a much more efficient way of handling kernel tracking, since it avoids taking and storing an event. However, if the application never synchronizes the queue, this vector of submitted kernels will grow unbounded. This patch avoids this problem by dynamically compacting the submitted kernel vector at set intervals, deduplicating identical kernel handles. The larger the amount of unique kernels, the larger the vector will be.
1 parent 4fa9f71 commit b7c2fd9

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

unified-runtime/source/adapters/level_zero/v2/command_list_manager.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,56 @@ ur_result_t ur_command_list_manager::appendNativeCommandExp(
10591059
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
10601060
}
10611061

1062+
void ur_command_list_manager::compactSubmittedKernels() {
1063+
size_t beforeSize = submittedKernels.size();
1064+
1065+
std::sort(submittedKernels.begin(), submittedKernels.end());
1066+
1067+
// Remove all but one unique entry for each kernel. All removed entries
1068+
// need to have their refcounts decremented.
1069+
auto newEnd = std::unique(
1070+
submittedKernels.begin(), submittedKernels.end(), [](auto lhs, auto rhs) {
1071+
if (lhs == rhs) {
1072+
[[maybe_unused]] const bool lastEntry = rhs->RefCount.release();
1073+
assert(!lastEntry); // there should be at least one entry left.
1074+
return true; // duplicate.
1075+
}
1076+
return false;
1077+
});
1078+
1079+
submittedKernels.erase(newEnd, submittedKernels.end());
1080+
1081+
// Adjust compaction threshold.
1082+
size_t removed = beforeSize - submittedKernels.size();
1083+
size_t removedPct = beforeSize > 0 ? (removed * 100) / beforeSize : 0;
1084+
if (removedPct > 75) {
1085+
// We removed a lot of entries. Lower the threshold if possible.
1086+
compactionThreshold = std::max<std::size_t>(
1087+
SUBMITTED_KERNELS_DEFAULT_THRESHOLD, compactionThreshold / 2);
1088+
} else if (removedPct < 10 &&
1089+
compactionThreshold < SUBMITTED_KERNELS_MAX_THRESHOLD) {
1090+
// Increase the threshold if we removed very little entries. This means
1091+
// there are many unique kernels, and we need to allow the vector to grow
1092+
// more.
1093+
compactionThreshold *= 2;
1094+
}
1095+
}
1096+
10621097
void ur_command_list_manager::recordSubmittedKernel(
10631098
ur_kernel_handle_t hKernel) {
1099+
bool isDuplicate = std::any_of(
1100+
submittedKernels.end() -
1101+
std::min(SUBMITTED_KERNELS_DUPE_CHECK_DEPTH, submittedKernels.size()),
1102+
submittedKernels.end(), [hKernel](auto k) { return k == hKernel; });
1103+
1104+
if (isDuplicate) {
1105+
return;
1106+
}
1107+
1108+
if (submittedKernels.size() > compactionThreshold) {
1109+
compactSubmittedKernels();
1110+
}
1111+
10641112
submittedKernels.push_back(hKernel);
10651113
hKernel->RefCount.retain();
10661114
}

unified-runtime/source/adapters/level_zero/v2/command_list_manager.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ struct wait_list_view {
4545
}
4646
};
4747

48+
// When recording submitted kernels, we only care about unique kernels. It's not
49+
// important whether the kernel has been submitted to the kernel just once or
50+
// dozens of times. The number of unique kernels should be fairly low.
51+
// So, in order to reduce the number of entries in the submitted kernels vector,
52+
// we do a lookback at 4 previous entries (to try to keep within a cacheline),
53+
// and don't record a new kernel if it exists.
54+
static const size_t SUBMITTED_KERNELS_DUPE_CHECK_DEPTH = 4;
55+
56+
// In scenarios where queue synchronization happens rarely, the submitted kernel
57+
// vector can grow unbounded. In order to avoid that, we go through the entire
58+
// vector, eliminating any duplicates.
59+
static const size_t SUBMITTED_KERNELS_DEFAULT_THRESHOLD = 128;
60+
61+
// If we reach this many unique kernels, the application is probably doing
62+
// something incorrectly. The adapter will still function, just that compaction
63+
// will happen more frequently.
64+
static const size_t SUBMITTED_KERNELS_MAX_THRESHOLD = 65536;
65+
4866
struct ur_command_list_manager {
4967
ur_command_list_manager(ur_context_handle_t context,
5068
ur_device_handle_t device,
@@ -254,6 +272,7 @@ struct ur_command_list_manager {
254272
ur_command_t callerCommand);
255273

256274
void recordSubmittedKernel(ur_kernel_handle_t hKernel);
275+
void compactSubmittedKernels();
257276

258277
ze_event_handle_t getSignalEvent(ur_event_handle_t hUserEvent,
259278
ur_command_t commandType);
@@ -299,6 +318,8 @@ struct ur_command_list_manager {
299318
v2::raii::ur_device_handle_t hDevice;
300319

301320
std::vector<ur_kernel_handle_t> submittedKernels;
321+
std::size_t compactionThreshold = SUBMITTED_KERNELS_DEFAULT_THRESHOLD;
322+
302323
v2::raii::command_list_unique_handle zeCommandList;
303324
std::vector<ze_event_handle_t> waitList;
304325
};

0 commit comments

Comments
 (0)