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_peer_manager.h"
12 : #include "orion_adapter_rts.h"
13 : #include "orion_adapter_hccp.h"
14 :
15 : namespace Hccl {
16 :
17 97 : HccpPeerManager &HccpPeerManager::GetInstance()
18 : {
19 97 : static HccpPeerManager hccpPeerManager;
20 97 : return hccpPeerManager;
21 : }
22 :
23 15 : void HccpPeerManager::Init(s32 deviceLogicId)
24 : {
25 15 : std::lock_guard<std::mutex> lock(managerMutex_);
26 :
27 15 : if (instances_.count(deviceLogicId) != 0) {
28 10 : instances_[deviceLogicId].Ref();
29 30 : HCCL_INFO("[HccpPeerManager::%s] deviceLogicId[%d] ra has initialized, ref[%d].",
30 : __func__, deviceLogicId, instances_[deviceLogicId].Count());
31 10 : return;
32 : }
33 5 : HRaInitConfig cfg;
34 5 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
35 5 : cfg.mode = HrtNetworkMode::PEER;
36 5 : HrtRaInit(cfg);
37 :
38 5 : instances_[deviceLogicId].Ref();
39 15 : HCCL_INFO("[HccpPeerManager::%s] deviceLogicId[%d] ra init success.", __func__, deviceLogicId);
40 15 : }
41 :
42 33 : void HccpPeerManager::DeInit(s32 deviceLogicId)
43 : {
44 33 : std::lock_guard<std::mutex> lock(managerMutex_);
45 :
46 33 : if (isDestroy) {
47 0 : HCCL_WARNING("[HccpPeerManager::%s] HccpPeerManager has been detroy", __func__);
48 0 : return;
49 : }
50 :
51 : // 校验是否存在
52 33 : if (instances_.count(deviceLogicId) == 0) {
53 93 : HCCL_WARNING("[HccpPeerManager::%s] deviceLogicId[%d] not ra init", __func__, deviceLogicId);
54 31 : return;
55 : }
56 :
57 : // 引用计数-1
58 2 : instances_[deviceLogicId].Unref();
59 2 : u32 count = instances_[deviceLogicId].Count();
60 6 : HCCL_INFO("[HccpPeerManager::%s] devLogicId[%d] release one, ref[%u].", __func__, deviceLogicId, count);
61 :
62 : // 若引用计数为0, 则释放资源
63 2 : if (count == 0){
64 1 : HRaInitConfig cfg;
65 1 : cfg.phyId = HrtGetDevicePhyIdByIndex(deviceLogicId);
66 1 : cfg.mode = HrtNetworkMode::PEER;
67 1 : HrtRaDeInit(cfg);
68 1 : instances_.erase(deviceLogicId);
69 3 : HCCL_INFO("[HccpPeerManager::%s] devLogicId [%d] ra deinit success.", __func__, deviceLogicId);
70 : }
71 33 : }
72 :
73 2 : void HccpPeerManager::DeInitAll()
74 : {
75 2 : std::lock_guard<std::mutex> lock(managerMutex_);
76 2 : isDestroy = true;
77 :
78 4 : for (auto const &instance : instances_) {
79 2 : u32 count = instance.second.Count();
80 6 : CHK_PRT_CONT(count != 0, HCCL_WARNING("[HccpPeerManager::%s] release is not as expected, "
81 : "devLogicId[%d] ref[%u]", __func__, instance.first, count));
82 2 : HRaInitConfig cfg;
83 2 : cfg.phyId = HrtGetDevicePhyIdByIndex(instance.first);
84 2 : cfg.mode = HrtNetworkMode::PEER;
85 2 : HrtRaDeInit(cfg);
86 6 : HCCL_INFO("[HccpPeerManager::%s] devLogicId [%d] ra deinit success.", __func__, instance.first);
87 : }
88 :
89 2 : instances_.clear();
90 2 : }
91 :
92 1 : HccpPeerManager::~HccpPeerManager()
93 : {
94 1 : DECTOR_TRY_CATCH("HccpPeerManager", DeInitAll());
95 1 : }
96 : } // namespace Hccl
|