|
| 1 | +//===--------- queue_immediate_in_order.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 "queue_immediate_out_of_order.hpp" |
| 12 | +#include "../common/latency_tracker.hpp" |
| 13 | +#include "ur.hpp" |
| 14 | + |
| 15 | +namespace v2 { |
| 16 | + |
| 17 | +// Helper function to intialize std::array of command list manager. |
| 18 | +// This is needed because command list manager does not have a default |
| 19 | +// constructor. |
| 20 | +template <size_t... Is> |
| 21 | +std::array<lockable<ur_command_list_manager>, sizeof...(Is)> |
| 22 | +createCommandListManagers(ur_context_handle_t hContext, |
| 23 | + ur_device_handle_t hDevice, uint32_t ordinal, |
| 24 | + ze_command_queue_priority_t priority, |
| 25 | + std::index_sequence<Is...>) { |
| 26 | + return { |
| 27 | + ((void)Is, lockable<ur_command_list_manager>( |
| 28 | + hContext, hDevice, |
| 29 | + hContext->getCommandListCache().getImmediateCommandList( |
| 30 | + hDevice->ZeDevice, |
| 31 | + {true, ordinal, true /* always enable copy offload */}, |
| 32 | + ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS, priority)))...}; |
| 33 | +} |
| 34 | + |
| 35 | +template <size_t N> |
| 36 | +std::array<lockable<ur_command_list_manager>, N> |
| 37 | +createCommandListManagers(ur_context_handle_t hContext, |
| 38 | + ur_device_handle_t hDevice, uint32_t ordinal, |
| 39 | + ze_command_queue_priority_t priority) { |
| 40 | + return createCommandListManagers(hContext, hDevice, ordinal, priority, |
| 41 | + std::make_index_sequence<N>{}); |
| 42 | +} |
| 43 | + |
| 44 | +void ur_queue_immediate_out_of_order_t::initializeSignalEvents() { |
| 45 | + // Counter-based events are not signaled by default. Use dummy operation |
| 46 | + // to ensure proper state. TODO: we can use zexCounterBasedEventCreate for |
| 47 | + // this. |
| 48 | + void *tmpMem = nullptr; |
| 49 | + uint32_t tmpPattern = 0; |
| 50 | + UR_CALL_THROWS(ur::level_zero::urUSMHostAlloc(hContext, nullptr, nullptr, |
| 51 | + sizeof(tmpPattern), &tmpMem)); |
| 52 | + |
| 53 | + for (size_t i = 0; i < numCommandLists; ++i) { |
| 54 | + internalSignalEvents[i] = eventPool->allocate(); |
| 55 | + commandListManagers[i].get_no_lock()->appendUSMFill( |
| 56 | + tmpMem, sizeof(tmpPattern), &tmpPattern, sizeof(tmpPattern), 0, nullptr, |
| 57 | + internalSignalEvents[i]); |
| 58 | + ZE2UR_CALL_THROWS( |
| 59 | + zeCommandListHostSynchronize, |
| 60 | + (commandListManagers[i].get_no_lock()->getZeCommandList(), UINT64_MAX)); |
| 61 | + |
| 62 | + signalEvents.assign(i, internalSignalEvents[i], false); |
| 63 | + } |
| 64 | + |
| 65 | + UR_CALL_THROWS(ur::level_zero::urUSMFree(hContext, tmpMem)); |
| 66 | +} |
| 67 | + |
| 68 | +ur_queue_immediate_out_of_order_t::ur_queue_immediate_out_of_order_t( |
| 69 | + ur_context_handle_t hContext, ur_device_handle_t hDevice, uint32_t ordinal, |
| 70 | + ze_command_queue_priority_t priority, event_flags_t eventFlags, |
| 71 | + ur_queue_flags_t flags) |
| 72 | + : hContext(hContext), hDevice(hDevice), |
| 73 | + commandListManagers(createCommandListManagers<numCommandLists>( |
| 74 | + hContext, hDevice, ordinal, priority)), |
| 75 | + eventPool(hContext->getEventPoolCache(PoolCacheType::Immediate) |
| 76 | + .borrow(hDevice->Id.value(), eventFlags)), |
| 77 | + flags(flags) { |
| 78 | + initializeSignalEvents(); |
| 79 | +} |
| 80 | + |
| 81 | +ur_result_t ur_queue_immediate_out_of_order_t::queueGetInfo( |
| 82 | + ur_queue_info_t propName, size_t propSize, void *pPropValue, |
| 83 | + size_t *pPropSizeRet) { |
| 84 | + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); |
| 85 | + // TODO: consider support for queue properties and size |
| 86 | + switch ((uint32_t)propName) { // cast to avoid warnings on EXT enum values |
| 87 | + case UR_QUEUE_INFO_CONTEXT: |
| 88 | + return ReturnValue(hContext); |
| 89 | + case UR_QUEUE_INFO_DEVICE: |
| 90 | + return ReturnValue(hDevice); |
| 91 | + case UR_QUEUE_INFO_REFERENCE_COUNT: |
| 92 | + return ReturnValue(uint32_t{RefCount.load()}); |
| 93 | + case UR_QUEUE_INFO_FLAGS: |
| 94 | + return ReturnValue(flags); |
| 95 | + case UR_QUEUE_INFO_SIZE: |
| 96 | + case UR_QUEUE_INFO_DEVICE_DEFAULT: |
| 97 | + return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION; |
| 98 | + case UR_QUEUE_INFO_EMPTY: { |
| 99 | + auto isCmdListEmpty = [](ze_command_list_handle_t cmdList) { |
| 100 | + auto status = ZE_CALL_NOCHECK(zeCommandListHostSynchronize, (cmdList, 0)); |
| 101 | + if (status == ZE_RESULT_SUCCESS) { |
| 102 | + return true; |
| 103 | + } else if (status == ZE_RESULT_NOT_READY) { |
| 104 | + return false; |
| 105 | + } else { |
| 106 | + throw ze2urResult(status); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + bool empty = std::all_of( |
| 111 | + commandListManagers.begin(), commandListManagers.end(), |
| 112 | + [&](auto &cmdListManager) { |
| 113 | + return isCmdListEmpty(cmdListManager.lock()->getZeCommandList()); |
| 114 | + }); |
| 115 | + |
| 116 | + return ReturnValue(empty); |
| 117 | + } |
| 118 | + default: |
| 119 | + UR_LOG(ERR, |
| 120 | + "Unsupported ParamName in urQueueGetInfo: " |
| 121 | + "ParamName=ParamName={}(0x{})", |
| 122 | + propName, logger::toHex(propName)); |
| 123 | + return UR_RESULT_ERROR_INVALID_VALUE; |
| 124 | + } |
| 125 | + |
| 126 | + return UR_RESULT_SUCCESS; |
| 127 | +} |
| 128 | + |
| 129 | +ur_result_t ur_queue_immediate_out_of_order_t::queueGetNativeHandle( |
| 130 | + ur_queue_native_desc_t * /*pDesc*/, ur_native_handle_t *phNativeQueue) { |
| 131 | + *phNativeQueue = reinterpret_cast<ur_native_handle_t>( |
| 132 | + commandListManagers[getNextCommandListId()] |
| 133 | + .get_no_lock() |
| 134 | + ->getZeCommandList()); |
| 135 | + return UR_RESULT_SUCCESS; |
| 136 | +} |
| 137 | + |
| 138 | +ur_result_t ur_queue_immediate_out_of_order_t::queueFinish() { |
| 139 | + TRACK_SCOPE_LATENCY("ur_queue_immediate_out_of_order_t::queueFinish"); |
| 140 | + |
| 141 | + auto lastCommandListId = |
| 142 | + commandListIndex.load(std::memory_order_relaxed) % numCommandLists; |
| 143 | + |
| 144 | + UR_CALL(commandListManagers[lastCommandListId].lock()->appendEventsWait( |
| 145 | + numCommandLists, signalEvents.events.data(), nullptr)); |
| 146 | + ZE2UR_CALL(zeCommandListHostSynchronize, |
| 147 | + (commandListManagers[lastCommandListId].lock()->getZeCommandList(), |
| 148 | + UINT64_MAX)); |
| 149 | + |
| 150 | + return UR_RESULT_SUCCESS; |
| 151 | +} |
| 152 | + |
| 153 | +ur_result_t ur_queue_immediate_out_of_order_t::queueFlush() { |
| 154 | + return UR_RESULT_SUCCESS; |
| 155 | +} |
| 156 | + |
| 157 | +ur_queue_immediate_out_of_order_t::~ur_queue_immediate_out_of_order_t() { |
| 158 | + try { |
| 159 | + UR_CALL_THROWS(queueFinish()); |
| 160 | + } catch (...) { |
| 161 | + // Ignore errors during destruction |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +ur_result_t ur_queue_immediate_out_of_order_t::enqueueEventsWaitWithBarrier( |
| 166 | + uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList, |
| 167 | + ur_event_handle_t *phEvent) { |
| 168 | + TRACK_SCOPE_LATENCY( |
| 169 | + "ur_queue_immediate_out_of_order_t::enqueueEventsWaitWithBarrier"); |
| 170 | + // For in-order queue we don't need a real L0 barrier, just wait for |
| 171 | + // requested events in potentially different queues and add a "barrier" |
| 172 | + // event signal because it is already guaranteed that previous commands |
| 173 | + // in this queue are completed when the signal is started. However, we do |
| 174 | + // need to use barrier if profiling is enabled: see |
| 175 | + // zeCommandListAppendWaitOnEvents |
| 176 | + if ((flags & UR_QUEUE_FLAG_PROFILING_ENABLE) != 0) { |
| 177 | + for (size_t id = 0; id < numCommandLists; id++) { |
| 178 | + UR_CALL(commandListManagers[id].lock()->appendEventsWaitWithBarrier( |
| 179 | + numEventsInWaitList, phEventWaitList, internalSignalEvents[id])); |
| 180 | + } |
| 181 | + } else { |
| 182 | + for (size_t id = 0; id < numCommandLists; id++) { |
| 183 | + UR_CALL(commandListManagers[id].lock()->appendEventsWait( |
| 184 | + numEventsInWaitList, phEventWaitList, internalSignalEvents[id])); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (phEvent) { |
| 189 | + UR_CALL(commandListManagers.front().lock()->appendEventsWait( |
| 190 | + numCommandLists, internalSignalEvents.data(), |
| 191 | + createOrUseInternalSignalEvent<false>(0, phEvent))); |
| 192 | + } |
| 193 | + |
| 194 | + return UR_RESULT_SUCCESS; |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace v2 |
0 commit comments