|
5 | 5 |
|
6 | 6 | #include "command_list_cache.hpp" |
7 | 7 | #include "common.hpp" |
| 8 | +#include "v2/context.hpp" |
8 | 9 |
|
9 | 10 | #include "context.hpp" |
10 | 11 | #include "device.hpp" |
11 | 12 |
|
12 | 13 | #include "uur/fixtures.h" |
| 14 | +#include "uur/raii.h" |
13 | 15 |
|
14 | 16 | #include <gtest/gtest.h> |
15 | 17 | #include <map> |
@@ -160,3 +162,97 @@ TEST_P(CommandListCacheTest, ImmediateCommandListsHaveProperAttributes) { |
160 | 162 | } |
161 | 163 | } |
162 | 164 | } |
| 165 | + |
| 166 | +TEST_P(CommandListCacheTest, CommandListsAreReusedByQueues) { |
| 167 | + static constexpr int NumQueuesPerType = 5; |
| 168 | + size_t NumUniqueQueueTypes = 0; |
| 169 | + |
| 170 | + for (int I = 0; I < NumQueuesPerType; I++) { |
| 171 | + NumUniqueQueueTypes = 0; |
| 172 | + |
| 173 | + { // Queues scope |
| 174 | + std::vector<uur::raii::Queue> Queues; |
| 175 | + for (auto Priority : |
| 176 | + std::vector<uint32_t>{UR_QUEUE_FLAG_PRIORITY_LOW, |
| 177 | + UR_QUEUE_FLAG_PRIORITY_HIGH, 0}) { |
| 178 | + for (auto Index : |
| 179 | + std::vector<std::optional<int32_t>>{std::nullopt, 0}) { |
| 180 | + NumUniqueQueueTypes++; |
| 181 | + |
| 182 | + ur_queue_properties_t QueueProps{ |
| 183 | + UR_STRUCTURE_TYPE_QUEUE_PROPERTIES, nullptr, 0}; |
| 184 | + QueueProps.flags |= UR_QUEUE_FLAG_SUBMISSION_IMMEDIATE; |
| 185 | + if (Priority) { |
| 186 | + QueueProps.flags |= Priority; |
| 187 | + } |
| 188 | + |
| 189 | + ur_queue_index_properties_t IndexProps{ |
| 190 | + UR_STRUCTURE_TYPE_QUEUE_INDEX_PROPERTIES, nullptr, 0}; |
| 191 | + if (Index) { |
| 192 | + IndexProps.computeIndex = *Index; |
| 193 | + QueueProps.pNext = &IndexProps; |
| 194 | + } |
| 195 | + |
| 196 | + ur_queue_handle_t Queue; |
| 197 | + ASSERT_EQ( |
| 198 | + urQueueCreate(context, device, &QueueProps, &Queue), |
| 199 | + UR_RESULT_SUCCESS); |
| 200 | + |
| 201 | + Queues.emplace_back(Queue); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + ASSERT_EQ(static_cast<v2::ur_context_handle_t>(context) |
| 206 | + ->commandListCache.getNumImmediateCommandLists(), |
| 207 | + 0); |
| 208 | + ASSERT_EQ(static_cast<v2::ur_context_handle_t>(context) |
| 209 | + ->commandListCache.getNumRegularCommandLists(), |
| 210 | + 0); |
| 211 | + } // Queues scope |
| 212 | + |
| 213 | + ASSERT_EQ(static_cast<v2::ur_context_handle_t>(context) |
| 214 | + ->commandListCache.getNumImmediateCommandLists(), |
| 215 | + NumUniqueQueueTypes * 2); // * 2 for compute and copy |
| 216 | + ASSERT_EQ(static_cast<v2::ur_context_handle_t>(context) |
| 217 | + ->commandListCache.getNumRegularCommandLists(), |
| 218 | + 0); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +TEST_P(CommandListCacheTest, CommandListsCacheIsThreadSafe) { |
| 223 | + static constexpr int NumThreads = 10; |
| 224 | + static constexpr int NumIters = 10; |
| 225 | + |
| 226 | + std::vector<std::thread> Threads; |
| 227 | + for (int I = 0; I < NumThreads; I++) { |
| 228 | + Threads.emplace_back([I, this]() { |
| 229 | + for (int J = 0; J < NumIters; J++) { |
| 230 | + ur_queue_properties_t QueueProps{ |
| 231 | + UR_STRUCTURE_TYPE_QUEUE_PROPERTIES, nullptr, 0}; |
| 232 | + QueueProps.flags |= UR_QUEUE_FLAG_SUBMISSION_IMMEDIATE; |
| 233 | + if (I < NumThreads / 2) { |
| 234 | + QueueProps.flags |= UR_QUEUE_FLAG_PRIORITY_LOW; |
| 235 | + } else { |
| 236 | + QueueProps.flags |= UR_QUEUE_FLAG_PRIORITY_HIGH; |
| 237 | + } |
| 238 | + |
| 239 | + uur::raii::Queue Queue; |
| 240 | + ASSERT_EQ( |
| 241 | + urQueueCreate(context, device, &QueueProps, Queue.ptr()), |
| 242 | + UR_RESULT_SUCCESS); |
| 243 | + |
| 244 | + ASSERT_LE(static_cast<v2::ur_context_handle_t>(context) |
| 245 | + ->commandListCache.getNumImmediateCommandLists(), |
| 246 | + NumThreads * 2); // * 2 for compute and copy |
| 247 | + } |
| 248 | + }); |
| 249 | + } |
| 250 | + |
| 251 | + for (auto &Thread : Threads) { |
| 252 | + Thread.join(); |
| 253 | + } |
| 254 | + |
| 255 | + ASSERT_LE(static_cast<v2::ur_context_handle_t>(context) |
| 256 | + ->commandListCache.getNumImmediateCommandLists(), |
| 257 | + NumThreads * 2); |
| 258 | +} |
0 commit comments