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 "opretry_link_manage.h"
12 :
13 : namespace hccl {
14 :
15 12 : OpretryLinkManage& OpretryLinkManage::GetInstance(s32 deviceLogicID)
16 : {
17 28 : static OpretryLinkManage opretryLinkManage[MAX_DEV_NUM];
18 12 : if (static_cast<u32>(deviceLogicID) >= MAX_DEV_NUM) {
19 1 : HCCL_WARNING("[OpretryLinkManage][GetInstance] deviceLogicID[%d] is invalid", deviceLogicID);
20 1 : return opretryLinkManage[0];
21 : }
22 11 : return opretryLinkManage[deviceLogicID];
23 : }
24 :
25 16 : OpretryLinkManage::~OpretryLinkManage()
26 : {
27 16 : isDeInit_ = true;
28 16 : allRemoteRankList_.clear();
29 16 : groupAllRemoteRankList_.clear();
30 16 : }
31 :
32 8 : HcclResult OpretryLinkManage::AddLinkInfoByIdentifier(
33 : const std::string& identifier, const std::string& newTag, std::vector<u32>& remoteRankList, bool incre)
34 : {
35 8 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
36 8 : const auto& identifierIt = allRemoteRankList_.find(identifier);
37 8 : if (identifierIt != allRemoteRankList_.end()) {
38 3 : const auto& tagIt = identifierIt->second.find(newTag);
39 3 : if (tagIt == identifierIt->second.end()) {
40 1 : identifierIt->second.emplace(newTag, remoteRankList);
41 1 : groupAllRemoteRankList_[identifier].insert(remoteRankList.begin(), remoteRankList.end());
42 2 : } else if (incre) {
43 : // 增量建链场景
44 4 : for (auto remoteRank : remoteRankList) {
45 3 : if (std::find(tagIt->second.begin(), tagIt->second.end(), remoteRank) == tagIt->second.end()) {
46 2 : tagIt->second.push_back(remoteRank);
47 : }
48 : }
49 1 : groupAllRemoteRankList_[identifier].insert(remoteRankList.begin(), remoteRankList.end());
50 : } else {
51 : // tag已存在,则不重复添加
52 1 : HCCL_INFO(
53 : "[OpretryLinkManage][AddLinkInfoByIdentifier]identifier[%s] newTag[%s] is already add",
54 : identifier.c_str(), newTag.c_str());
55 1 : return HCCL_SUCCESS;
56 : }
57 : } else {
58 15 : std::unordered_map<std::string, std::vector<u32>> tmp = {{newTag, remoteRankList}};
59 5 : allRemoteRankList_.emplace(identifier, tmp);
60 5 : std::unordered_set<u32> remoteRankSet(remoteRankList.begin(), remoteRankList.end());
61 5 : groupAllRemoteRankList_.emplace(identifier, remoteRankSet);
62 5 : }
63 7 : return HCCL_SUCCESS;
64 13 : }
65 :
66 2 : HcclResult OpretryLinkManage::GetLinkInfoByIdentifier(
67 : const std::string& identifier, const std::string& newTag, std::vector<u32>& remoteRankList)
68 : {
69 2 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
70 2 : const auto& identifierIt = allRemoteRankList_.find(identifier);
71 2 : if (identifierIt != allRemoteRankList_.end()) {
72 1 : const auto& tagIt = identifierIt->second.find(newTag);
73 1 : if (tagIt != identifierIt->second.end()) {
74 1 : remoteRankList = tagIt->second;
75 1 : HCCL_RUN_INFO(
76 : "[OpretryLinkManage][GetLinkInfoByIdentifier]identifier[%s] newTag[%s] get success", identifier.c_str(),
77 : newTag.c_str());
78 1 : return HCCL_SUCCESS;
79 : } else {
80 0 : HCCL_ERROR("[OpretryLinkManage]newTag[%s] not found, please add it before", newTag.c_str());
81 0 : return HCCL_E_PARA;
82 : }
83 : } else {
84 1 : HCCL_ERROR("[OpretryLinkManage]identifier[%s] not found, please add it before", identifier.c_str());
85 1 : return HCCL_E_PARA;
86 : }
87 : return HCCL_SUCCESS;
88 2 : }
89 :
90 2 : HcclResult OpretryLinkManage::GetLinkInfoByIdentifier(const std::string& identifier, std::vector<u32>& remoteRankList)
91 : {
92 2 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
93 2 : const auto& identifierIt = groupAllRemoteRankList_.find(identifier);
94 2 : if (identifierIt != groupAllRemoteRankList_.end()) {
95 1 : remoteRankList.assign(identifierIt->second.begin(), identifierIt->second.end());
96 1 : HCCL_RUN_INFO("[OpretryLinkManage][GetLinkInfoByIdentifier]identifier[%s] get success", identifier.c_str());
97 1 : return HCCL_SUCCESS;
98 : } else {
99 1 : HCCL_ERROR("[OpretryLinkManage]identifier[%s] not found, please add it before", identifier.c_str());
100 1 : return HCCL_E_PARA;
101 : }
102 : return HCCL_SUCCESS;
103 2 : }
104 :
105 3 : HcclResult OpretryLinkManage::DeleteLinkInfoByIdentifier(const std::string& identifier)
106 : {
107 3 : CHK_PRT_RET(isDeInit_ == true, HCCL_WARNING("OpretryLinkManage has been destroyed"), HCCL_SUCCESS);
108 3 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
109 3 : if (allRemoteRankList_.find(identifier) != allRemoteRankList_.end()) {
110 1 : allRemoteRankList_.erase(identifier);
111 : }
112 3 : if (groupAllRemoteRankList_.find(identifier) != groupAllRemoteRankList_.end()) {
113 1 : groupAllRemoteRankList_.erase(identifier);
114 : }
115 3 : return HCCL_SUCCESS;
116 3 : }
117 : } // namespace hccl
|