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 195 : HcommResMgr& HcommResMgr::GetInstance(const uint32_t devicePhyId)
80 : {
81 : static std::array<bool, MAX_MODULE_DEVICE_NUM + 1> isInitialized{false};
82 :
83 195 : uint32_t devPhyId = devicePhyId;
84 195 : if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
85 0 : HCCL_WARNING(
86 : "[HcommResMgr][%s] use the backup device, devPhyId[%u] should be "
87 : "less than %u.",
88 : __func__, devPhyId, MAX_MODULE_DEVICE_NUM);
89 0 : devPhyId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
90 : }
91 195 : if (!isInitialized[devPhyId]) {
92 : // 临时方案:只声明单例对象做生命周期控制,不执行业务动作
93 : // 未来需要将各种单例转为该数据结构的成员变量
94 : // devicePhyId 目前不影响流程,只是触发静态对象声明
95 1 : DpuNotifyManager::GetInstance();
96 1 : Hccl::HccpHdcManager::GetInstance();
97 1 : Hccl::HccpPeerManager::GetInstance();
98 1 : Hccl::HccpTlvHdcManager::GetInstance();
99 1 : Hccl::RdmaHandleManager::GetInstance();
100 1 : Hccl::InnerNetDevManager::GetInstance();
101 1 : Hccl::SocketHandleManager::GetInstance();
102 1 : Hccl::HostSocketHandleManager::GetInstance();
103 1 : SocketMgr::GetInstance(devicePhyId);
104 1 : Hccl::TpManager::GetInstance(devicePhyId);
105 1 : EndpointMonitor::GetInstance(devicePhyId);
106 :
107 1 : Hccl::CcuComponent::GetInstance(devicePhyId);
108 1 : Hccl::CcuResBatchAllocator::GetInstance(devicePhyId);
109 1 : Hccl::CtxMgrImp::GetInstance(devicePhyId);
110 :
111 : // 开源开放架构下CCU模式新增类型单例,当前混跑时不使用
112 1 : HccpTlvHdcMgr::GetInstance(devicePhyId);
113 1 : TpMgr::GetInstance(devicePhyId);
114 1 : CcuComponent::GetInstance(devicePhyId);
115 1 : CcuResBatchAllocator::GetInstance(devicePhyId);
116 1 : CcuKernelMgr::GetInstance(devicePhyId);
117 1 : CcuInstanceMgr::GetInstance(devicePhyId);
118 1 : SocketProcess::GetInstance(devicePhyId);
119 : }
120 :
121 261 : static HcommResMgr hcommResMgrs[MAX_MODULE_DEVICE_NUM + 1];
122 195 : hcommResMgrs[devPhyId].devPhyId_ = devPhyId;
123 195 : isInitialized[devPhyId] = true;
124 :
125 195 : return hcommResMgrs[devPhyId];
126 : }
127 :
128 66 : HcommResMgr::HcommResMgr()
129 : {
130 : // 临时方案:最小化修改不做处理
131 : // 未来需在构造函数中触发各类成员变量声明
132 66 : }
133 :
134 66 : HcommResMgr::~HcommResMgr()
135 : {
136 : // 临时方案:最小化修改不做处理
137 : // 未来需在析构函数中主动调用各种单例销毁流程,保证销毁时序
138 66 : }
139 :
140 0 : static void OnDeviceResetPre(int32_t deviceId, aclrtDeviceState state, [[maybe_unused]] void* args)
141 : {
142 : try {
143 0 : if (state != ACL_RT_DEVICE_STATE_RESET_PRE) {
144 0 : return;
145 : }
146 0 : HCCL_INFO("[OnDeviceResetPre] deviceId[%d] state[%d] ", deviceId, static_cast<int>(state));
147 :
148 0 : u32 devPhyId = 0;
149 0 : HcclResult ret = hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceId), devPhyId);
150 0 : if (ret != HCCL_SUCCESS) {
151 0 : HCCL_WARNING("[OnDeviceResetPre] hrtGetDevicePhyIdByIndex failed, deviceId[%d] ret[%d]", deviceId, ret);
152 0 : return;
153 : }
154 0 : SocketMgr::DeInit(devPhyId);
155 0 : ServerSocketMgr::DeInit(devPhyId);
156 0 : ServerSocketManager::GetInstance().DeInit(devPhyId);
157 0 : Hccl::RdmaHandleManager::GetInstance().DeInit(devPhyId);
158 0 : Hccl::SocketHandleManager::GetInstance().DeInit(devPhyId);
159 0 : Hccl::HccpHdcManager::GetInstance().DeInit(deviceId);
160 0 : } catch (const std::exception& e) {
161 0 : HCCL_WARNING("[OnDeviceResetPre][%s] exception caught:%s", __func__, e.what());
162 0 : } catch (...) {
163 0 : HCCL_WARNING("[OnDeviceResetPre][%s] unknown exception caught", __func__);
164 0 : }
165 : }
166 :
167 87 : void HcommResMgr::RegisterDeviceResetCallback()
168 : {
169 87 : std::lock_guard<std::mutex> lock(g_deviceResetRegMutex);
170 87 : if (g_deviceResetCallbackRegistered) {
171 83 : return;
172 : }
173 4 : aclError ret = aclrtRegDeviceStateCallback("hcomm_res_mgr", OnDeviceResetPre, nullptr);
174 4 : if (ret != ACL_SUCCESS) {
175 0 : HCCL_WARNING("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback failed, ret[%d]", ret);
176 0 : return;
177 : }
178 4 : g_deviceResetCallbackRegistered = true;
179 4 : HCCL_INFO("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback success");
180 87 : }
181 :
182 : } // namespace hcomm
|