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 :
17 : // orion 通用平台层单例
18 : #include "hccp_hdc_manager.h"
19 : #include "hccp_peer_manager.h"
20 : #include "hccp_tlv_hdc_manager.h"
21 : #include "rdma_handle_manager.h"
22 : #include "inner_net_dev_manager.h"
23 : #include "socket_handle_manager.h"
24 : #include "host_socket_handle_manager.h"
25 : #include "tp_manager.h"
26 : #include "endpoint_monitor.h"
27 : // legacy ccu单例
28 : #include "ccu_component.h"
29 : #include "../../../legacy/ascend950/unified_platform/ccu/ccu_device/ccu_res_batch_allocator.h"
30 : #include "../../../legacy/ascend950/unified_platform/ccu/ccu_context/ccu_context_mgr_imp.h"
31 : // 开源开放 ccu单例
32 : #include "hccp_tlv_hdc_mgr.h"
33 : #include "tp_mgr.h"
34 : #include "ccu_comp.h"
35 : #include "resources/ccu/ccu_device/ccu_res_batch_allocator.h"
36 : #include "ccu_kernel_mgr.h"
37 : #include "ccu_instance_mgr.h"
38 : #include "../endpoint_pairs/sockets/socket_process.h"
39 : #include "dpu_notify/dpu_notify_manager.h"
40 : #include "server_socket_mgr.h"
41 : #include "server_socket_manager.h"
42 : #include "adapter_rts_common.h"
43 :
44 : namespace hcomm {
45 :
46 : static std::mutex g_deviceResetRegMutex;
47 : static bool g_deviceResetCallbackRegistered = false;
48 :
49 150 : HcommResMgr& HcommResMgr::GetInstance(const uint32_t devicePhyId)
50 : {
51 : static std::array<bool, MAX_MODULE_DEVICE_NUM + 1> isInitialized{false};
52 :
53 150 : uint32_t devPhyId = devicePhyId;
54 150 : if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
55 0 : HCCL_WARNING("[HcommResMgr][%s] use the backup device, devPhyId[%u] should be "
56 : "less than %u.", __func__, devPhyId, MAX_MODULE_DEVICE_NUM);
57 0 : devPhyId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
58 : }
59 150 : if (!isInitialized[devPhyId]) {
60 : // 临时方案:只声明单例对象做生命周期控制,不执行业务动作
61 : // 未来需要将各种单例转为该数据结构的成员变量
62 : // devicePhyId 目前不影响流程,只是触发静态对象声明
63 1 : DpuNotifyManager::GetInstance();
64 1 : Hccl::HccpHdcManager::GetInstance();
65 1 : Hccl::HccpPeerManager::GetInstance();
66 1 : Hccl::HccpTlvHdcManager::GetInstance();
67 1 : Hccl::RdmaHandleManager::GetInstance();
68 1 : Hccl::InnerNetDevManager::GetInstance();
69 1 : Hccl::SocketHandleManager::GetInstance();
70 1 : Hccl::HostSocketHandleManager::GetInstance();
71 1 : SocketMgr::GetInstance(devicePhyId);
72 1 : Hccl::TpManager::GetInstance(devicePhyId);
73 1 : EndpointMonitor::GetInstance(devicePhyId);
74 :
75 1 : Hccl::CcuComponent::GetInstance(devicePhyId);
76 1 : Hccl::CcuResBatchAllocator::GetInstance(devicePhyId);
77 1 : Hccl::CtxMgrImp::GetInstance(devicePhyId);
78 :
79 : // 开源开放架构下CCU模式新增类型单例,当前混跑时不使用
80 1 : HccpTlvHdcMgr::GetInstance(devicePhyId);
81 1 : TpMgr::GetInstance(devicePhyId);
82 1 : CcuComponent::GetInstance(devicePhyId);
83 1 : CcuResBatchAllocator::GetInstance(devicePhyId);
84 1 : CcuKernelMgr::GetInstance(devicePhyId);
85 1 : CcuInstanceMgr::GetInstance(devicePhyId);
86 1 : SocketProcess::GetInstance(devicePhyId);
87 : }
88 :
89 216 : static HcommResMgr hcommResMgrs[MAX_MODULE_DEVICE_NUM + 1];
90 150 : hcommResMgrs[devPhyId].devPhyId_ = devPhyId;
91 150 : isInitialized[devPhyId] = true;
92 :
93 150 : return hcommResMgrs[devPhyId];
94 : }
95 :
96 66 : HcommResMgr::HcommResMgr()
97 : {
98 : // 临时方案:最小化修改不做处理
99 : // 未来需在构造函数中触发各类成员变量声明
100 66 : }
101 :
102 66 : HcommResMgr::~HcommResMgr()
103 : {
104 : // 临时方案:最小化修改不做处理
105 : // 未来需在析构函数中主动调用各种单例销毁流程,保证销毁时序
106 66 : }
107 :
108 0 : static void OnDeviceResetPre(int32_t deviceId, aclrtDeviceState state, void *args)
109 : {
110 : try {
111 0 : if (state != ACL_RT_DEVICE_STATE_RESET_PRE) {
112 0 : return;
113 : }
114 0 : HCCL_INFO("[OnDeviceResetPre] deviceId[%d] state[%d] ", deviceId, static_cast<int>(state));
115 :
116 0 : u32 devPhyId = 0;
117 0 : HcclResult ret = hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceId), devPhyId);
118 0 : if (ret != HCCL_SUCCESS) {
119 0 : HCCL_WARNING("[OnDeviceResetPre] hrtGetDevicePhyIdByIndex failed, deviceId[%d] ret[%d]", deviceId, ret);
120 0 : return;
121 : }
122 0 : SocketMgr::DeInit(devPhyId);
123 0 : ServerSocketMgr::DeInit(devPhyId);
124 0 : ServerSocketManager::GetInstance().DeInit(devPhyId);
125 0 : Hccl::RdmaHandleManager::GetInstance().DeInit(devPhyId);
126 0 : Hccl::SocketHandleManager::GetInstance().DeInit(devPhyId);
127 0 : Hccl::HccpHdcManager::GetInstance().DeInit(deviceId);
128 0 : } catch (const std::exception &e) {
129 0 : HCCL_WARNING("[OnDeviceResetPre][%s] exception caught:%s", __func__, e.what());
130 0 : } catch (...) {
131 0 : HCCL_WARNING("[OnDeviceResetPre][%s] unknown exception caught", __func__);
132 0 : }
133 : }
134 :
135 58 : void HcommResMgr::RegisterDeviceResetCallback()
136 : {
137 58 : std::lock_guard<std::mutex> lock(g_deviceResetRegMutex);
138 58 : if (g_deviceResetCallbackRegistered) {
139 55 : return;
140 : }
141 3 : aclError ret = aclrtRegDeviceStateCallback("hcomm_res_mgr", OnDeviceResetPre, nullptr);
142 3 : if (ret != ACL_SUCCESS) {
143 0 : HCCL_WARNING("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback failed, ret[%d]", ret);
144 0 : return;
145 : }
146 3 : g_deviceResetCallbackRegistered = true;
147 3 : HCCL_INFO("[RegisterDeviceResetCallback] aclrtRegDeviceStateCallback success");
148 58 : }
149 :
150 : } // namespace hcomm
|