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 "log.h"
12 :
13 : #include "ins_coll_alg_base.h"
14 :
15 : namespace Hccl {
16 :
17 1 : InsCollAlgBase::InsCollAlgBase() {}
18 :
19 1 : InsCollAlgBase::~InsCollAlgBase() {}
20 :
21 1 : void InsCollAlgBase::SetMyRank(RankId myRank)
22 : {
23 1 : myRank_ = myRank;
24 1 : return;
25 : }
26 :
27 1 : void InsCollAlgBase::SetRankSize(u32 rankSize)
28 : {
29 1 : rankSize_ = rankSize;
30 1 : return;
31 : }
32 :
33 1 : void InsCollAlgBase::SetDevType(DevType devType)
34 : {
35 1 : devType_ = devType;
36 1 : return;
37 : }
38 :
39 0 : void InsCollAlgBase::SetSendRecvRemoteRank(RankId sendRecvRemoteRank)
40 : {
41 0 : sendRecvRemoteRank_ = sendRecvRemoteRank;
42 0 : return;
43 : }
44 :
45 0 : void InsCollAlgBase::SetOp(const CollAlgOperator& op)
46 : {
47 0 : op_ = op;
48 0 : return;
49 : }
50 :
51 0 : void InsCollAlgBase::SetAllignSize(u64 allignSize)
52 : {
53 0 : allignSize_ = allignSize;
54 0 : return;
55 : }
56 :
57 1 : void InsCollAlgBase::EnableDataAllign(bool enableAllign)
58 : {
59 1 : enableAllign_ = enableAllign;
60 1 : return;
61 : }
62 :
63 1 : void InsCollAlgBase::EnableDetour(bool enableDetour)
64 : {
65 1 : enableDetour_ = enableDetour;
66 1 : return;
67 : }
68 :
69 0 : void InsCollAlgBase::SetDmaMode(const DmaMode dmaMode)
70 : {
71 0 : dmaMode_ = dmaMode;
72 0 : return;
73 : }
74 :
75 0 : void InsCollAlgBase::SetRmaDataBufferMgr(const RmtDataBufferMgr* rmaDataBufferMgr)
76 : {
77 : (void)rmaDataBufferMgr;
78 0 : return;
79 : }
80 :
81 0 : bool InsCollAlgBase::IsEnableCounterNotify() const
82 : {
83 0 : HCCL_DEBUG("[InsCollAlgBase][%s] start.", __func__);
84 0 : return true;
85 : }
86 :
87 1 : HcclResult InsCollAlgBase::Init(const CollAlgOperator& op, const CollAlgParams& params, InsQuePtr insQue)
88 : {
89 : // init params
90 1 : CHK_PRT_RET(
91 : InitParams(op, params) != HcclResult::HCCL_SUCCESS,
92 : HCCL_ERROR("[InsCollAlgFactory] Rank [%d], Fail to init params.", myRank_), HcclResult::HCCL_E_PARA);
93 :
94 : // init queMap
95 1 : CHK_PRT_RET(
96 : GenInsQueMap(insQue) != HcclResult::HCCL_SUCCESS,
97 : HCCL_ERROR("[InsCollAlgFactory] Rank [%d], Fail to init insQueMap.", myRank_), HcclResult::HCCL_E_PARA);
98 :
99 1 : return HcclResult::HCCL_SUCCESS;
100 : }
101 :
102 0 : HcclResult InsCollAlgBase::InitParams(const CollAlgOperator& op, const CollAlgParams& params)
103 : {
104 0 : op_ = op;
105 0 : opMode_ = params.opMode;
106 0 : maxTmpMemSize_ = params.maxTmpMemSize;
107 :
108 0 : CHK_PRT_RET(
109 : (maxTmpMemSize_ == 0) && (opMode_ == OpMode::OPBASE),
110 : HCCL_ERROR("[InsCollAlgFactory] maxTmpMemSize equals to zero for OPBASE."), HcclResult::HCCL_E_PARA);
111 :
112 0 : CHK_PRT_RET(
113 : InitDataInfo(op, dataType_, outputDataType_, dataCount_),
114 : HCCL_ERROR("[InsCollAlgFactory] unable to init DataInfo."), HcclResult::HCCL_E_PARA);
115 0 : dataTypeSize_ = DataTypeSizeGet(dataType_);
116 0 : dataSize_ = dataCount_ * dataTypeSize_;
117 :
118 0 : CHK_PRT_RET(
119 : InitOpInfo(op, opType_, redOp_, root_), HCCL_ERROR("[InsCollAlgFactory] unable to init OpInfo."),
120 : HcclResult::HCCL_E_PARA);
121 :
122 0 : return HcclResult::HCCL_SUCCESS;
123 : }
124 :
125 1 : HcclResult InsCollAlgBase::GenInsQueMap(InsQuePtr insQue)
126 : {
127 1 : if (insQue == nullptr) {
128 0 : HCCL_ERROR("[InsCollAlgBase] insQue is nullptr.");
129 0 : return HcclResult::HCCL_E_PTR;
130 : }
131 1 : CHK_PRT_RET(
132 : !insQue->IsMaster(),
133 : HCCL_ERROR("[InsCollAlgFactory] Rank [%d], Input Primitive Queue is not a master queue.", myRank_),
134 : HcclResult::HCCL_E_PARA);
135 1 : queId2InsQue_.insert(std::make_pair(insQue->GetId(), insQue));
136 1 : return HcclResult::HCCL_SUCCESS;
137 : }
138 :
139 1 : HcclResult InsCollAlgBase::InitQueue(const u32& requiredQueNum, std::vector<InsQuePtr>& requiredQue)
140 : {
141 1 : CHK_PRT_RET(
142 : !static_cast<bool>(queId2InsQue_.count(0)),
143 : HCCL_ERROR("[InsCollAlgFactory] Rank [%d], Invalid queId2InsQue Map.", myRank_), HcclResult::HCCL_E_INTERNAL);
144 1 : InsQuePtr insQue = queId2InsQue_[0];
145 :
146 2 : for (u32 queIdx = 0; queIdx < requiredQueNum; queIdx++) {
147 1 : if (!static_cast<bool>(queId2InsQue_.count(queIdx))) {
148 0 : queId2InsQue_.insert(std::make_pair(queIdx, insQue->Fork()));
149 : }
150 1 : requiredQue.push_back(queId2InsQue_[queIdx]);
151 : }
152 :
153 1 : return HcclResult::HCCL_SUCCESS;
154 1 : }
155 :
156 0 : HcclResult InsCollAlgBase::SetLinkPrty(const std::vector<BasePortType>& linkPriority)
157 : {
158 0 : CHK_PRT_RET(
159 : linkPriority.size() == 0, HCCL_ERROR("[InsCollAlgFactory] Invalid given link priority."),
160 : HcclResult::HCCL_E_PARA);
161 0 : linkPriority_.assign(linkPriority.begin(), linkPriority.end());
162 :
163 0 : return HcclResult::HCCL_SUCCESS;
164 : }
165 :
166 0 : HcclResult InsCollAlgBase::CalcParallelNotifyReq(
167 : const u32 primQueueNum, const u32 IntraqueNum, std::vector<std::tuple<QId, QId, u32>>& queueNotifys) const
168 : {
169 0 : u32 slaveNum = primQueueNum - 1;
170 0 : std::vector<std::tuple<QId, QId, u32>> notifyRequests;
171 :
172 0 : notifyRequests.reserve(slaveNum); // 每个从流需要1个
173 0 : for (QId q = 1; q < primQueueNum; q++) {
174 0 : notifyRequests.emplace_back(std::make_tuple(0, q, 0));
175 0 : notifyRequests.emplace_back(std::make_tuple(q, 0, 0));
176 : }
177 0 : for (QId q = IntraqueNum; q < primQueueNum; q++) {
178 0 : if (IntraqueNum == q) {
179 0 : continue;
180 : }
181 0 : notifyRequests.emplace_back(std::make_tuple(IntraqueNum, q, 0));
182 0 : notifyRequests.emplace_back(std::make_tuple(q, IntraqueNum, 0));
183 : }
184 0 : queueNotifys = notifyRequests;
185 0 : return HcclResult::HCCL_SUCCESS;
186 0 : }
187 :
188 0 : HcclResult InsCollAlgBase::CalcLocalRankSize(
189 : const RankId myRank, const std::vector<std::vector<RankId>>& virtRanks, u32& rankSizeLevel0,
190 : u32& rankSizeLevel1) const
191 : {
192 0 : constexpr uint64_t virtRanks_2 = 2;
193 0 : CHK_PRT_RET(
194 : virtRanks.size() < virtRanks_2, HCCL_ERROR("[CalcLocalRankSize] virtRanks level num is smaller than 2."),
195 : HcclResult::HCCL_E_INTERNAL);
196 :
197 0 : rankSizeLevel0 = virtRanks.at(0).size();
198 0 : rankSizeLevel1 = virtRanks.at(1).size();
199 :
200 0 : HCCL_INFO(
201 : "[CalcLocalRankSize] localRankSize: myRank[%d] rankSizeLevel0_[%u] rankSizeLevel1_[%u]", myRank, rankSizeLevel0,
202 : rankSizeLevel1);
203 0 : return HcclResult::HCCL_SUCCESS;
204 : };
205 :
206 0 : LinkReq InsCollAlgBase::GetSeqLinksUnion(const LinkReq& linkReq0, const LinkReq& linkReq1) const
207 : {
208 0 : LinkReq retLinkReq = linkReq0;
209 0 : for (auto linkReqIter = linkReq1.begin(); linkReqIter != linkReq1.end(); linkReqIter++) {
210 0 : if (retLinkReq.find(linkReqIter->first) == retLinkReq.end()) {
211 0 : retLinkReq.insert(std::pair<RankId, u32>(linkReqIter->first, linkReqIter->second));
212 : } else {
213 0 : u32 tmpLinkReq = retLinkReq[linkReqIter->first];
214 0 : retLinkReq[linkReqIter->first] = std::max(tmpLinkReq, linkReqIter->second);
215 : }
216 : }
217 0 : return retLinkReq;
218 0 : }
219 :
220 0 : HcclResult InsCollAlgBase::CalNumBlocks(u32& numBlocks, u64 dataSize, u32 numBlocksLimit)
221 : {
222 0 : numBlocks = 0;
223 : (void)dataSize;
224 : (void)numBlocksLimit;
225 0 : HCCL_INFO("[InsCollAlgFactory] current executor doesn't support controlling num of aiv cores.");
226 0 : return HcclResult::HCCL_SUCCESS;
227 : }
228 : } // namespace Hccl
|