Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "hcomm_res_mgr.h"
12 :
13 : #include <mutex>
14 :
15 : #include "hccl_common.h"
16 : #include "comm_engine_utils.h"
17 : #include "launch_device.h"
18 : #include "launch_aicpu.h"
19 :
20 : // orion 通用平台层单例
21 : #include "hccp_hdc_manager.h"
22 : #include "hccp_peer_manager.h"
23 : #include "hccp_tlv_hdc_manager.h"
24 : #include "rdma_handle_manager.h"
25 : #include "inner_net_dev_manager.h"
26 : #include "socket_handle_manager.h"
27 : #include "host_socket_handle_manager.h"
28 : #include "tp_manager.h"
29 : #include "endpoint_monitor.h"
30 : // legacy ccu单例
31 : #include "ccu_component.h"
32 : #include "ccu_res_batch_allocator_legacy.h"
33 : #include "../../../legacy/ascend950/unified_platform/ccu/ccu_context/ccu_context_mgr_imp.h"
34 : // 开源开放 ccu单例
35 : #include "hccp_tlv_hdc_mgr.h"
36 : #include "tp_mgr.h"
37 : #include "ccu_comp.h"
38 : #include "resources/ccu/ccu_device/ccu_res_batch_allocator.h"
39 : #include "ccu_kernel_mgr.h"
40 : #include "ccu_instance_mgr.h"
41 : #include "../endpoint_pairs/sockets/socket_process.h"
42 : #include "dpu_notify/dpu_notify_manager.h"
43 : #include "server_socket_mgr.h"
44 : #include "server_socket_manager.h"
45 : #include "adapter_rts_common.h"
46 :
47 : namespace hcomm {
48 :
49 : static std::mutex g_deviceResetRegMutex;
50 : static bool g_deviceResetCallbackRegistered = false;
51 :
52 : aclrtBinHandle HcommResMgr::binHandle_ = nullptr;
53 : std::mutex HcommResMgr::binHandleMtx_;
54 :
55 1 : HcclResult HcommResMgr::EnsureKernelBinLoaded(CommEngine engine)
56 : {
57 1 : if (engine != COMM_ENGINE_AICPU && engine != COMM_ENGINE_AICPU_TS) {
58 0 : HCCL_INFO(
59 : "[%s] engine[%s] kernel loading not required", __func__,
60 : GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str());
61 0 : return HCCL_SUCCESS;
62 : }
63 1 : std::lock_guard<std::mutex> lock(binHandleMtx_);
64 1 : if (binHandle_ != nullptr) {
65 0 : return HCCL_SUCCESS;
66 : }
67 1 : std::string jsonPath;
68 1 : CHK_RET(hccl::GetKernelFilePath(jsonPath));
69 1 : jsonPath += "ccl_kernel.json";
70 :
71 1 : HcclResult ret = hccl::LoadBinaryFromFile(jsonPath.c_str(), ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE, 0, binHandle_);
72 1 : CHK_PRT_RET(
73 : ret != HCCL_SUCCESS, HCCL_ERROR("[%s] load aicpu file fail, path[%s]", __func__, jsonPath.c_str()), ret);
74 1 : return HCCL_SUCCESS;
75 1 : }
76 :
77 1 : aclrtBinHandle HcommResMgr::GetBinHandle() { return binHandle_; }
78 :
79 : // HcommBaseResMgr
80 :
81 1 : void HcommBaseResMgr::Init()
82 : {
83 : // 临时方案:只声明单例对象做生命周期控制,不执行业务动作
84 : // 未来需要将各种单例转为该数据结构的成员变量
85 : // devicePhyId 目前不影响流程,只是触发静态对象声明
86 1 : DpuNotifyManager::GetInstance();
87 1 : Hccl::HccpHdcManager::GetInstance();
88 1 : Hccl::HccpPeerManager::GetInstance();
89 1 : Hccl::HccpTlvHdcManager::GetInstance();
90 1 : Hccl::RdmaHandleManager::GetInstance();
91 1 : Hccl::InnerNetDevManager::GetInstance();
92 1 : Hccl::SocketHandleManager::GetInstance();
93 1 : Hccl::HostSocketHandleManager::GetInstance();
94 1 : SocketMgr::GetInstance(devPhyId_);
95 1 : Hccl::TpManager::GetInstance(devPhyId_);
96 1 : (void)EndpointMonitor::GetHolder(devPhyId_);
97 :
98 1 : Hccl::CcuComponent::GetInstance(devPhyId_);
99 1 : Hccl::CcuResBatchAllocator::GetInstance(devPhyId_);
100 1 : Hccl::CtxMgrImp::GetInstance(devPhyId_);
101 :
102 : // 开源开放架构下CCU模式新增类型单例,当前混跑时不使用
103 1 : HccpTlvHdcMgr::GetInstance(devPhyId_);
104 1 : TpMgr::GetInstance(devPhyId_);
105 1 : CcuComponent::GetInstance(devPhyId_);
106 1 : CcuResBatchAllocator::GetInstance(devPhyId_);
107 1 : CcuKernelMgr::GetInstance(devPhyId_);
108 1 : CcuInstanceMgr::GetInstance(devPhyId_);
109 1 : SocketProcess::GetInstance(devPhyId_);
110 1 : }
111 :
112 : // HcommResMgr
113 :
114 2 : HcommResMgr::HcommResMgr() = default;
115 :
116 245 : HcommResMgr& HcommResMgr::GetInstance()
117 : {
118 245 : static HcommResMgr instance;
119 245 : return instance;
120 : }
121 :
122 196 : void HcommResMgr::InitDevice(uint32_t devicePhyId)
123 : {
124 196 : uint32_t devPhyId = devicePhyId;
125 196 : if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
126 0 : HCCL_WARNING(
127 : "[HcommResMgr][%s] use the backup device, devPhyId[%u] should be "
128 : "less than %u.",
129 : __func__, devPhyId, MAX_MODULE_DEVICE_NUM);
130 0 : devPhyId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
131 : }
132 196 : if (!isInitialized_[devPhyId]) {
133 1 : deviceResMgrs_[devPhyId].SetDevPhyId(devPhyId);
134 1 : deviceResMgrs_[devPhyId].Init();
135 1 : isInitialized_[devPhyId] = true;
136 : }
137 196 : }
138 :
139 196 : HcommBaseResMgr& HcommResMgr::GetDeviceResMgr(uint32_t devicePhyId)
140 : {
141 196 : InitDevice(devicePhyId);
142 196 : uint32_t devPhyId = devicePhyId;
143 196 : if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
144 0 : devPhyId = MAX_MODULE_DEVICE_NUM;
145 : }
146 196 : return deviceResMgrs_[devPhyId];
147 : }
148 :
149 49 : ConfigMgr& HcommResMgr::GetConfigMgr() { return configMgr_; }
150 :
151 0 : static void OnDeviceResetPre(int32_t deviceId, aclrtDeviceState state, [[maybe_unused]] void* args)
152 : {
153 : try {
154 0 : if (state != ACL_RT_DEVICE_STATE_RESET_PRE) {
155 0 : return;
156 : }
157 0 : HCCL_INFO("[OnDeviceResetPre] deviceId[%d] state[%d] ", deviceId, static_cast<int>(state));
158 :
159 0 : u32 devPhyId = 0;
160 0 : HcclResult ret = hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceId), devPhyId);
161 0 : if (ret != HCCL_SUCCESS) {
162 0 : HCCL_WARNING("[OnDeviceResetPre] hrtGetDevicePhyIdByIndex failed, deviceId[%d] ret[%d]", deviceId, ret);
163 0 : return;
164 : }
165 0 : SocketMgr::DeInit(devPhyId);
166 0 : ServerSocketMgr::DeInit(devPhyId);
167 0 : ServerSocketManager::GetInstance().DeInit(devPhyId);
168 0 : Hccl::RdmaHandleManager::GetInstance().DeInit(devPhyId);
169 0 : Hccl::SocketHandleManager::GetInstance().DeInit(devPhyId);
170 0 : Hccl::HccpHdcManager::GetInstance().DeInit(deviceId);
171 0 : } catch (const std::exception& e) {
172 0 : HCCL_WARNING("[OnDeviceResetPre][%s] exception caught:%s", __func__, e.what());
173 0 : } catch (...) {
174 0 : HCCL_WARNING("[OnDeviceResetPre][%s] unknown exception caught", __func__);
175 0 : }
176 : }
177 :
178 87 : void HcommResMgr::RegisterDeviceResetCallback()
179 : {
180 87 : std::lock_guard<std::mutex> lock(g_deviceResetRegMutex);
181 87 : if (g_deviceResetCallbackRegistered) {
182 83 : return;
183 : }
184 4 : aclError ret = aclrtRegDeviceStateCallback("hcomm_res_mgr", OnDeviceResetPre, nullptr);
185 4 : if (ret != ACL_SUCCESS) {
186 0 : HCCL_WARNING("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback failed, ret[%d]", ret);
187 0 : return;
188 : }
189 4 : g_deviceResetCallbackRegistered = true;
190 4 : HCCL_INFO("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback success");
191 87 : }
192 :
193 : } // namespace hcomm
|