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 : #include "conn_local_notify_manager.h"
11 :
12 : #include "rdma_handle_manager.h"
13 : #include "communicator_impl.h"
14 : #include "invalid_params_exception.h"
15 :
16 : #include "ipc_local_notify.h"
17 : #include "rdma_local_notify.h"
18 : #include "ub_local_notify.h"
19 :
20 : namespace Hccl {
21 :
22 488 : ConnLocalNotifyManager::ConnLocalNotifyManager(CommunicatorImpl* communicator) : comm(communicator) {}
23 :
24 488 : ConnLocalNotifyManager::~ConnLocalNotifyManager() { DECTOR_TRY_CATCH("ConnLocalNotifyManager", Destroy()); }
25 :
26 12 : bool ConnLocalNotifyManager::IsExist(RankId remoteRankId, const LinkData& linkData)
27 : {
28 12 : return notifyPool.count(remoteRankId) != 0 && notifyPool[remoteRankId].count(linkData) != 0;
29 : }
30 :
31 6 : void ConnLocalNotifyManager::ApplyFor(RankId remoteRankId, const LinkData& linkData)
32 : {
33 18 : HCCL_INFO("Local notify for remoteRankId[%d] and linkData[%s] alloc.", remoteRankId, linkData.Describe().c_str());
34 6 : if (IsExist(remoteRankId, linkData)) {
35 3 : HCCL_WARNING(
36 : "Local notify for remoteRankId[%d] and linkData[%s] already exists, no need to alloc.", remoteRankId,
37 : linkData.Describe().c_str());
38 1 : return;
39 : }
40 :
41 5 : u32 count = 3; // 待修改: 需要定义GetCount()
42 5 : notifyPool[remoteRankId][linkData].resize(count);
43 :
44 20 : for (u32 i = 0; i < count; ++i) {
45 15 : if (linkData.GetType() == PortDeploymentType::P2P) {
46 12 : notifyPool[remoteRankId][linkData][i]
47 24 : = make_unique<IpcLocalNotify>(comm->GetOpAiCpuTSFeatureFlag()); // 算子粒度
48 12 : continue;
49 3 : } else if (linkData.GetType() == PortDeploymentType::DEV_NET) {
50 3 : auto linkProtocol = linkData.GetLinkProtocol();
51 3 : if (linkProtocol == LinkProtocol::ROCE) {
52 0 : RdmaHandle rdmaHandle = RdmaHandleManager::GetInstance().Get(
53 0 : comm->GetDevicePhyId(), linkData.GetLocalPort(), linkProtocol);
54 0 : notifyPool[remoteRankId][linkData][i]
55 0 : = make_unique<RdmaLocalNotify>(rdmaHandle, comm->GetOpAiCpuTSFeatureFlag()); // 算子粒度
56 0 : continue;
57 0 : } else if (
58 3 : linkProtocol == LinkProtocol::UB_CTP || linkProtocol == LinkProtocol::UB_TP
59 3 : || linkProtocol == LinkProtocol::UBOE || linkProtocol == LinkProtocol::UBG) {
60 6 : RdmaHandle rdmaHandle = RdmaHandleManager::GetInstance().Get(
61 3 : comm->GetDevicePhyId(), linkData.GetLocalPort(), linkProtocol);
62 3 : notifyPool[remoteRankId][linkData][i]
63 6 : = make_unique<UbLocalNotify>(rdmaHandle, comm->GetOpAiCpuTSFeatureFlag()); // 算子粒度
64 : } else {
65 : // 待修改: 仅支持 P2P 和 RDMA 申请 notify
66 : string msg = StringFormat(
67 0 : "Unsupported %s of link %s", linkProtocol.Describe().c_str(), linkData.Describe().c_str());
68 0 : THROW<InvalidParamsException>(msg);
69 0 : }
70 : } else {
71 : // 待修改: 仅支持 P2P 和 RDMA 申请 notify
72 : string msg = StringFormat(
73 0 : "Unsupported %s of link %s", linkData.GetType().Describe().c_str(), linkData.Describe().c_str());
74 0 : THROW<InvalidParamsException>(msg);
75 0 : }
76 : };
77 : }
78 :
79 1 : bool ConnLocalNotifyManager::Release(RankId remoteRankId, const LinkData& linkData)
80 : {
81 1 : if (!IsExist(remoteRankId, linkData)) {
82 0 : HCCL_WARNING(
83 : "Notify for remoteRankId[%d] and linkData[%p] does not exist, no need to release.", remoteRankId,
84 : &linkData);
85 0 : return true;
86 : }
87 :
88 1 : notifyPool[remoteRankId].erase(linkData);
89 1 : if (notifyPool[remoteRankId].empty()) {
90 1 : notifyPool.erase(remoteRankId);
91 : }
92 1 : return true;
93 : }
94 :
95 5 : vector<BaseLocalNotify*> ConnLocalNotifyManager::Get(RankId remoteRankId, const LinkData& linkData)
96 : {
97 5 : vector<BaseLocalNotify*> v;
98 :
99 5 : if (!IsExist(remoteRankId, linkData)) {
100 15 : HCCL_WARNING(
101 : "Get Local notify for remoteRankId[%d] and linkData[%s] does not exist", remoteRankId,
102 : linkData.Describe().c_str());
103 5 : return v;
104 : }
105 :
106 0 : for (const auto& i : notifyPool[remoteRankId][linkData]) {
107 0 : v.emplace_back(i.get());
108 : }
109 :
110 0 : return v;
111 0 : }
112 :
113 489 : bool ConnLocalNotifyManager::Destroy()
114 : {
115 489 : notifyPool.clear();
116 489 : return true;
117 : }
118 :
119 : } // namespace Hccl
|