|
| 1 | +//===--------- command_list_cache.cpp - Level Zero Adapter ---------------===// |
| 2 | +// |
| 3 | +// Copyright (C) 2024 Intel Corporation |
| 4 | +// |
| 5 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See LICENSE.TXT |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include "command_list_cache.hpp" |
| 12 | + |
| 13 | +#include "context.hpp" |
| 14 | +#include "device.hpp" |
| 15 | + |
| 16 | +bool v2::immediate_command_list_descriptor_t::operator==( |
| 17 | + const immediate_command_list_descriptor_t &rhs) const { |
| 18 | + return ZeDevice == rhs.ZeDevice && IsInOrder == rhs.IsInOrder && |
| 19 | + Mode == rhs.Mode && Priority == rhs.Priority && Index == rhs.Index; |
| 20 | +} |
| 21 | + |
| 22 | +bool v2::regular_command_list_descriptor_t::operator==( |
| 23 | + const regular_command_list_descriptor_t &rhs) const { |
| 24 | + return ZeDevice == rhs.ZeDevice && Ordinal == rhs.Ordinal && |
| 25 | + IsInOrder == rhs.IsInOrder; |
| 26 | +} |
| 27 | + |
| 28 | +namespace v2 { |
| 29 | +inline size_t command_list_descriptor_hash_t::operator()( |
| 30 | + const command_list_descriptor_t &desc) const { |
| 31 | + if (auto ImmCmdDesc = |
| 32 | + std::get_if<immediate_command_list_descriptor_t>(&desc)) { |
| 33 | + return combine_hashes(0, ImmCmdDesc->ZeDevice, ImmCmdDesc->Ordinal, |
| 34 | + ImmCmdDesc->IsInOrder, ImmCmdDesc->Mode, |
| 35 | + ImmCmdDesc->Priority, ImmCmdDesc->Index); |
| 36 | + } else { |
| 37 | + auto RegCmdDesc = std::get<regular_command_list_descriptor_t>(desc); |
| 38 | + return combine_hashes(0, RegCmdDesc.ZeDevice, RegCmdDesc.IsInOrder, |
| 39 | + RegCmdDesc.Ordinal); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +command_list_cache_t::command_list_cache_t(ze_context_handle_t ZeContext) |
| 44 | + : ZeContext{ZeContext} {} |
| 45 | + |
| 46 | +raii::ze_command_list_t |
| 47 | +command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) { |
| 48 | + if (auto ImmCmdDesc = |
| 49 | + std::get_if<immediate_command_list_descriptor_t>(&desc)) { |
| 50 | + ze_command_list_handle_t ZeCommandList; |
| 51 | + ZeStruct<ze_command_queue_desc_t> QueueDesc; |
| 52 | + QueueDesc.ordinal = ImmCmdDesc->Ordinal; |
| 53 | + QueueDesc.mode = ImmCmdDesc->Mode; |
| 54 | + QueueDesc.priority = ImmCmdDesc->Priority; |
| 55 | + QueueDesc.flags = |
| 56 | + ImmCmdDesc->IsInOrder ? ZE_COMMAND_QUEUE_FLAG_IN_ORDER : 0; |
| 57 | + if (ImmCmdDesc->Index.has_value()) { |
| 58 | + QueueDesc.flags |= ZE_COMMAND_QUEUE_FLAG_EXPLICIT_ONLY; |
| 59 | + QueueDesc.index = ImmCmdDesc->Index.value(); |
| 60 | + } |
| 61 | + ZE2UR_CALL_THROWS( |
| 62 | + zeCommandListCreateImmediate, |
| 63 | + (ZeContext, ImmCmdDesc->ZeDevice, &QueueDesc, &ZeCommandList)); |
| 64 | + return raii::ze_command_list_t(ZeCommandList, &zeCommandListDestroy); |
| 65 | + } else { |
| 66 | + auto RegCmdDesc = std::get<regular_command_list_descriptor_t>(desc); |
| 67 | + ZeStruct<ze_command_list_desc_t> CmdListDesc; |
| 68 | + CmdListDesc.flags = |
| 69 | + RegCmdDesc.IsInOrder ? ZE_COMMAND_LIST_FLAG_IN_ORDER : 0; |
| 70 | + CmdListDesc.commandQueueGroupOrdinal = RegCmdDesc.Ordinal; |
| 71 | + |
| 72 | + ze_command_list_handle_t ZeCommandList; |
| 73 | + ZE2UR_CALL_THROWS(zeCommandListCreate, (ZeContext, RegCmdDesc.ZeDevice, |
| 74 | + &CmdListDesc, &ZeCommandList)); |
| 75 | + return raii::ze_command_list_t(ZeCommandList, &zeCommandListDestroy); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +raii::ze_command_list_t command_list_cache_t::getImmediateCommandList( |
| 80 | + ze_device_handle_t ZeDevice, bool IsInOrder, uint32_t Ordinal, |
| 81 | + ze_command_queue_mode_t Mode, ze_command_queue_priority_t Priority, |
| 82 | + std::optional<uint32_t> Index) { |
| 83 | + immediate_command_list_descriptor_t Desc; |
| 84 | + Desc.ZeDevice = ZeDevice; |
| 85 | + Desc.Ordinal = Ordinal; |
| 86 | + Desc.IsInOrder = IsInOrder; |
| 87 | + Desc.Mode = Mode; |
| 88 | + Desc.Priority = Priority; |
| 89 | + Desc.Index = Index; |
| 90 | + return getCommandList(Desc); |
| 91 | +} |
| 92 | + |
| 93 | +raii::ze_command_list_t |
| 94 | +command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice, |
| 95 | + bool IsInOrder, uint32_t Ordinal) { |
| 96 | + regular_command_list_descriptor_t Desc; |
| 97 | + Desc.ZeDevice = ZeDevice; |
| 98 | + Desc.IsInOrder = IsInOrder; |
| 99 | + Desc.Ordinal = Ordinal; |
| 100 | + return getCommandList(Desc); |
| 101 | +} |
| 102 | + |
| 103 | +void command_list_cache_t::addImmediateCommandList( |
| 104 | + raii::ze_command_list_t cmdList, ze_device_handle_t ZeDevice, |
| 105 | + bool IsInOrder, uint32_t Ordinal, ze_command_queue_mode_t Mode, |
| 106 | + ze_command_queue_priority_t Priority, std::optional<uint32_t> Index) { |
| 107 | + immediate_command_list_descriptor_t Desc; |
| 108 | + Desc.ZeDevice = ZeDevice; |
| 109 | + Desc.Ordinal = Ordinal; |
| 110 | + Desc.IsInOrder = IsInOrder; |
| 111 | + Desc.Mode = Mode; |
| 112 | + Desc.Priority = Priority; |
| 113 | + Desc.Index = Index; |
| 114 | + addCommandList(Desc, std::move(cmdList)); |
| 115 | +} |
| 116 | + |
| 117 | +void command_list_cache_t::addRegularCommandList( |
| 118 | + raii::ze_command_list_t cmdList, ze_device_handle_t ZeDevice, |
| 119 | + bool IsInOrder, uint32_t Ordinal) { |
| 120 | + regular_command_list_descriptor_t Desc; |
| 121 | + Desc.ZeDevice = ZeDevice; |
| 122 | + Desc.IsInOrder = IsInOrder; |
| 123 | + Desc.Ordinal = Ordinal; |
| 124 | + addCommandList(Desc, std::move(cmdList)); |
| 125 | +} |
| 126 | + |
| 127 | +raii::ze_command_list_t |
| 128 | +command_list_cache_t::getCommandList(const command_list_descriptor_t &desc) { |
| 129 | + std::unique_lock<ur_mutex> Lock(ZeCommandListCacheMutex); |
| 130 | + auto it = ZeCommandListCache.find(desc); |
| 131 | + if (it == ZeCommandListCache.end()) { |
| 132 | + Lock.unlock(); |
| 133 | + return createCommandList(desc); |
| 134 | + } |
| 135 | + |
| 136 | + assert(!it->second.empty()); |
| 137 | + |
| 138 | + raii::ze_command_list_t CommandListHandle = std::move(it->second.top()); |
| 139 | + it->second.pop(); |
| 140 | + |
| 141 | + if (it->second.empty()) |
| 142 | + ZeCommandListCache.erase(it); |
| 143 | + |
| 144 | + return CommandListHandle; |
| 145 | +} |
| 146 | + |
| 147 | +void command_list_cache_t::addCommandList(const command_list_descriptor_t &desc, |
| 148 | + raii::ze_command_list_t cmdList) { |
| 149 | + // TODO: add a limit? |
| 150 | + std::unique_lock<ur_mutex> Lock(ZeCommandListCacheMutex); |
| 151 | + auto [it, _] = ZeCommandListCache.try_emplace(desc); |
| 152 | + it->second.emplace(std::move(cmdList)); |
| 153 | +} |
| 154 | +} // namespace v2 |
0 commit comments