| 1 | /* Copyright (c) 2025, Intel Corporation |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | * copy of this software and associated documentation files (the "Software"), |
| 5 | * to deal in the Software without restriction, including without limitation |
| 6 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | * and/or sell copies of the Software, and to permit persons to whom the |
| 8 | * Software is furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included |
| 11 | * in all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 17 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 18 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 19 | * OTHER DEALINGS IN THE SOFTWARE. |
| 20 | */ |
| 21 | //! |
| 22 | //! \file vp_graphset.cpp |
| 23 | //! \brief The header file of the base class of graph set |
| 24 | //! \details The graph set will include graph generation from binary. |
| 25 | //! It's responsible for setting up HW states and generating the SFC |
| 26 | //! commands. |
| 27 | //! |
| 28 | #include "vp_graphset.h" |
| 29 | #include "levelzero_npu_interface.h" |
| 30 | #include "vp_utils.h" |
| 31 | #include "vp_platform_interface.h" |
| 32 | |
| 33 | using namespace vp; |
| 34 | |
| 35 | VpGraphSet::VpGraphSet(PVP_MHWINTERFACE hwInterface, PVpAllocator allocator) : |
| 36 | m_hwInterface(hwInterface), |
| 37 | m_allocator(allocator) |
| 38 | { |
| 39 | if (hwInterface) |
| 40 | { |
| 41 | m_osInterface = hwInterface->m_osInterface; |
| 42 | if (hwInterface->m_vpPlatformInterface) |
| 43 | { |
| 44 | m_graphPool = &hwInterface->m_vpPlatformInterface->GetGraphPool(); |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | MOS_STATUS VpGraphSet::Clean() |
| 51 | { |
| 52 | VP_PUBLIC_CHK_NULL_RETURN(m_osInterface); |
| 53 | VP_PUBLIC_CHK_NULL_RETURN(m_osInterface->npuInterface); |
| 54 | VP_PUBLIC_CHK_NULL_RETURN(m_osInterface->npuInterface->pfnDestroyGraph); |
| 55 | |
| 56 | for (auto &handle : m_cachedGraphs) |
| 57 | { |
| 58 | std::lock_guard<std::mutex> lock(handle.second.Mutex()); |
| 59 | handle.second.Initialized() = false; |
| 60 | if (handle.second.Thread().joinable()) |
| 61 | { |
| 62 | handle.second.Thread().join(); |
| 63 | } |
| 64 | ze_graph_handle_t &graph = handle.second.Graph(); |
| 65 | if (MOS_STATUS_SUCCESS != m_osInterface->npuInterface->pfnDestroyGraph(m_osInterface->npuInterface,graph)) |
| 66 | { |
| 67 | VP_PUBLIC_ASSERTMESSAGE("Free graph fail"); |
| 68 | } |
| 69 | handle.second.Initialized() = false; |
| 70 | handle.second.Failed() = false; |
| 71 | } |
| 72 | m_cachedGraphs.clear(); |
| 73 | return MOS_STATUS_SUCCESS; |
| 74 | } |
| 75 | |
| 76 | MOS_STATUS VpGraphSet::GetGraphObject( |
| 77 | VP_GRAPH_ID graphId, |
| 78 | GraphHandle *&graphHandle) |
| 79 | { |
| 80 | VP_PUBLIC_CHK_NULL_RETURN(m_osInterface); |
| 81 | VP_PUBLIC_CHK_NULL_RETURN(m_osInterface->npuInterface); |
| 82 | VP_PUBLIC_CHK_NULL_RETURN(m_graphPool); |
| 83 | |
| 84 | auto handle = m_cachedGraphs.find(graphId); |
| 85 | if (handle == m_cachedGraphs.end()) |
| 86 | { |
| 87 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 88 | VP_PUBLIC_CHK_STATUS_RETURN(LoadGraphForDebug()); |
| 89 | #endif |
| 90 | auto blobHandle = m_graphPool->find(graphId); |
| 91 | VP_PUBLIC_CHK_NOT_FOUND_RETURN(blobHandle, m_graphPool); |
| 92 | handle = m_cachedGraphs.emplace(std::piecewise_construct, std::forward_as_tuple(graphId), std::forward_as_tuple()).first; |
| 93 | VP_PUBLIC_CHK_NOT_FOUND_RETURN(handle, &m_cachedGraphs); |
| 94 | InitGraphCmdPackage cmdPackage(blobHandle->second, m_osInterface->npuInterface, &handle->second); |
| 95 | VP_PUBLIC_CHK_STATUS_RETURN(m_osInterface->pfnSubmitPackage(m_osInterface, cmdPackage)); |
| 96 | } |
| 97 | graphHandle = &handle->second; |
| 98 | return MOS_STATUS_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | std::unique_ptr<CmdPackage> InitGraphCmdPackage::Clone() const |
| 102 | { |
| 103 | return std::make_unique<InitGraphCmdPackage>(*this); |
| 104 | } |
| 105 | |
| 106 | MOS_STATUS InitGraphCmdPackage::Submit() |
| 107 | { |
| 108 | VP_PUBLIC_CHK_NULL_RETURN(m_graphHandle); |
| 109 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface); |
| 110 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnBuildGraph); |
| 111 | |
| 112 | m_graphHandle->Thread() = std::thread([this]() { |
| 113 | { |
| 114 | MOS_STATUS status = m_npuInterface->pfnBuildGraph(m_npuInterface, m_graphBin.blob, m_graphBin.size, m_graphHandle->Graph()); |
| 115 | std::lock_guard<std::mutex> lock(m_graphHandle->Mutex()); |
| 116 | if (MOS_STATUS_SUCCESS == status) |
| 117 | { |
| 118 | m_graphHandle->Initialized() = true; |
| 119 | m_graphHandle->Failed() = false; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | m_graphHandle->Failed() = true; |
| 124 | m_graphHandle->Initialized() = false; |
| 125 | } |
| 126 | } |
| 127 | m_graphHandle->Condition().notify_all(); |
| 128 | m_releaseable = true; |
| 129 | }); |
| 130 | return MOS_STATUS_SUCCESS; |
| 131 | } |