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(const std::string &identifier, const std::string &newTag,
33 : 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("[OpretryLinkManage][AddLinkInfoByIdentifier]identifier[%s] newTag[%s] is already add",
53 : identifier.c_str(), newTag.c_str());
54 1 : return HCCL_SUCCESS;
55 : }
56 : } else {
57 15 : std::unordered_map<std::string, std::vector<u32>> tmp = {{newTag, remoteRankList}};
58 5 : allRemoteRankList_.emplace(identifier, tmp);
59 5 : std::unordered_set<u32> remoteRankSet(remoteRankList.begin(), remoteRankList.end());
60 5 : groupAllRemoteRankList_.emplace(identifier, remoteRankSet);
61 5 : }
62 7 : return HCCL_SUCCESS;
63 13 : }
64 :
65 2 : HcclResult OpretryLinkManage::GetLinkInfoByIdentifier(const std::string &identifier, const std::string &newTag,
66 : std::vector<u32> &remoteRankList)
67 : {
68 2 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
69 2 : const auto &identifierIt = allRemoteRankList_.find(identifier);
70 2 : if (identifierIt != allRemoteRankList_.end()) {
71 1 : const auto &tagIt = identifierIt->second.find(newTag);
72 1 : if (tagIt != identifierIt->second.end()) {
73 1 : remoteRankList = tagIt->second;
74 1 : HCCL_RUN_INFO("[OpretryLinkManage][GetLinkInfoByIdentifier]identifier[%s] newTag[%s] get success",
75 : identifier.c_str(), newTag.c_str());
76 1 : return HCCL_SUCCESS;
77 : } else {
78 0 : HCCL_ERROR("[OpretryLinkManage]newTag[%s] not found, please add it before", newTag.c_str());
79 0 : return HCCL_E_PARA;
80 : }
81 : } else {
82 1 : HCCL_ERROR("[OpretryLinkManage]identifier[%s] not found, please add it before", identifier.c_str());
83 1 : return HCCL_E_PARA;
84 : }
85 : return HCCL_SUCCESS;
86 2 : }
87 :
88 2 : HcclResult OpretryLinkManage::GetLinkInfoByIdentifier(const std::string &identifier, std::vector<u32> &remoteRankList)
89 : {
90 2 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
91 2 : const auto &identifierIt = groupAllRemoteRankList_.find(identifier);
92 2 : if (identifierIt != groupAllRemoteRankList_.end()) {
93 1 : remoteRankList.assign(identifierIt->second.begin(), identifierIt->second.end());
94 1 : HCCL_RUN_INFO("[OpretryLinkManage][GetLinkInfoByIdentifier]identifier[%s] get success", identifier.c_str());
95 1 : return HCCL_SUCCESS;
96 : } else {
97 1 : HCCL_ERROR("[OpretryLinkManage]identifier[%s] not found, please add it before", identifier.c_str());
98 1 : return HCCL_E_PARA;
99 : }
100 : return HCCL_SUCCESS;
101 2 : }
102 :
103 3 : HcclResult OpretryLinkManage::DeleteLinkInfoByIdentifier(const std::string &identifier)
104 : {
105 3 : CHK_PRT_RET(isDeInit_ == true, HCCL_WARNING("OpretryLinkManage has been destroyed"), HCCL_SUCCESS);
106 3 : std::unique_lock<std::mutex> lock(opretryLinkMutex_);
107 3 : if (allRemoteRankList_.find(identifier) != allRemoteRankList_.end()) {
108 1 : allRemoteRankList_.erase(identifier);
109 : }
110 3 : if (groupAllRemoteRankList_.find(identifier) != groupAllRemoteRankList_.end()) {
111 1 : groupAllRemoteRankList_.erase(identifier);
112 : }
113 3 : return HCCL_SUCCESS;
114 3 : }
115 : }
|