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