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 "hccp_hdc_manager.h"
12 : #include "orion_adapter_tsd.h"
13 : #include "orion_adapter_rts.h"
14 :
15 : namespace Hccl {
16 :
17 134 : HccpHdcManager& HccpHdcManager::GetInstance()
18 : {
19 134 : static HccpHdcManager hccpHdcManager;
20 134 : return hccpHdcManager;
21 : }
22 :
23 95 : void HccpHdcManager::Init(u32 deviceLogicId)
24 : {
25 95 : std::unique_lock<std::recursive_mutex> lock(managerMutex);
26 95 : if (instances.count(deviceLogicId) != 0) {
27 76 : return;
28 : }
29 :
30 19 : HrtOpenTsdProcess(deviceLogicId);
31 :
32 19 : HRaInitConfig cfg;
33 19 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
34 19 : cfg.mode = HrtNetworkMode::HDC;
35 19 : HrtRaInit(cfg);
36 :
37 19 : instances.insert(deviceLogicId);
38 95 : }
39 :
40 6 : void HccpHdcManager::DeInit(u32 deviceLogicId)
41 : {
42 6 : if (destroyed.load()) {
43 4 : return;
44 : }
45 5 : HCCL_INFO("[HccpHdcManager::%s] deviceLogicId [%u]", __func__, deviceLogicId);
46 :
47 5 : std::lock_guard<std::recursive_mutex> lock(managerMutex);
48 5 : if (instances.count(deviceLogicId) == 0) {
49 3 : HCCL_WARNING("[HccpHdcManager::%s] deviceLogicId[%u] not ra init", __func__, deviceLogicId);
50 3 : return;
51 : }
52 2 : instances.erase(deviceLogicId);
53 :
54 2 : HRaInitConfig cfg;
55 2 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
56 2 : cfg.mode = HrtNetworkMode::HDC;
57 2 : DECTOR_TRY_CATCH("HccpHdcManager", HrtRaDeInit(cfg));
58 2 : HCCL_INFO("[HccpHdcManager::%s] devLogicId [%u] ra deinit success.", __func__, deviceLogicId);
59 5 : }
60 :
61 3 : void HccpHdcManager::DestroyAll()
62 : {
63 3 : if (destroyed.load()) {
64 0 : return;
65 : }
66 3 : destroyed.store(true);
67 :
68 : // 析构前先注销reset devcie的回调,防止嵌套调用
69 3 : UnregisterDeviceResetCallback();
70 :
71 3 : std::lock_guard<std::recursive_mutex> lock(managerMutex);
72 20 : for (auto deviceLogicId : instances) {
73 23 : HCCL_INFO("HccpHdcManager deinit");
74 :
75 17 : HRaInitConfig cfg;
76 17 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
77 17 : cfg.mode = HrtNetworkMode::HDC;
78 17 : DECTOR_TRY_CATCH("HccpHdcManager", HrtRaDeInit(cfg));
79 17 : DECTOR_TRY_CATCH("HccpHdcManager", HrtResetDevice(deviceLogicId));
80 : }
81 3 : instances.clear();
82 3 : }
83 :
84 3 : void HccpHdcManager::UnregisterDeviceResetCallback() const
85 : {
86 3 : aclError ret = aclrtRegDeviceStateCallback("hcomm_res_mgr", nullptr, nullptr);
87 3 : if (ret != ACL_SUCCESS) {
88 0 : HCCL_WARNING("[HccpHdcManager] aclrtRegDeviceStateCallback unregister failed, ret[%d]", ret);
89 0 : return;
90 : }
91 5 : HCCL_INFO("[HccpHdcManager] aclrtRegDeviceStateCallback unregister success");
92 : }
93 :
94 3 : HccpHdcManager::~HccpHdcManager() { DECTOR_TRY_CATCH("HccpHdcManager", DestroyAll()); }
95 :
96 : } // namespace Hccl
|