Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "ccu_context_all_to_all_v_mesh2die.h"
12 : #include "ccu_instruction_all_to_all_v_mesh2die.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int CKE_IDX_0 = 0;
17 : constexpr int CKE_IDX_1 = 1;
18 : constexpr int CKE_IDX_2 = 2;
19 :
20 0 : CcuContextAllToAllVMesh2Die::CcuContextAllToAllVMesh2Die(
21 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
22 0 : : CcuContextAlgBase(arg, transports, group)
23 : {
24 0 : if (transports.empty()) {
25 0 : THROW<InvalidParamsException>(StringFormat("CcuContextAllToAllVMesh2Die transports is empty"));
26 : }
27 :
28 0 : const CcuCtxArgAllToAllVMesh2Die* ctxArg = dynamic_cast<const CcuCtxArgAllToAllVMesh2Die*>(&arg);
29 0 : if (ctxArg == nullptr) {
30 0 : THROW<NullPtrException>(StringFormat("CcuContextAllToAllVMesh2Die::ctxArg ptr is null"));
31 : }
32 :
33 0 : auto dimSize = ctxArg->dimSize;
34 0 : if (dimSize.size() != 1) { // 2Die场景dimSize为1
35 0 : THROW<InvalidParamsException>(
36 0 : StringFormat("CcuContextAllToAllVMesh2Die::dimSize[%u] is invalid", dimSize.size()));
37 : }
38 :
39 0 : rankSize_ = dimSize[0];
40 0 : if (rankSize_ <= 1 || rankSize_ % RANK_EVEN != 0) {
41 0 : THROW<InvalidParamsException>(StringFormat("CcuContextAllToAllVMesh2Die::rankSize[%u] is invalid", rankSize_));
42 : }
43 :
44 0 : rankId_ = ctxArg->rankId;
45 0 : withMyRank_ = ctxArg->withMyRank;
46 0 : rankGroup_ = ctxArg->rankGroup;
47 :
48 0 : localSize_ = transports.size() + 1;
49 0 : localId_ = localSize_ - 1; // 本rank所在DIE的编号,固定放在末尾
50 :
51 0 : peerSize_ = transports.size() + (withMyRank_ ? 1 : 0);
52 0 : logicId_ = rankId_ % peerSize_;
53 :
54 0 : selfBit_ = 1 << logicId_;
55 0 : allBit_ = ((1 << peerSize_) - 1) & (~(withMyRank_ ? selfBit_ : 0));
56 :
57 0 : HCCL_INFO(
58 : "[CcuContextAllToAllVMesh2Die] RankId[%u], rankSize[%llu], localSize[%u], peerSize[%u], logicId[%u], "
59 : "withMyRank[%u]",
60 : rankId_, rankSize_, localSize_, peerSize_, logicId_, withMyRank_);
61 0 : }
62 :
63 0 : void CcuContextAllToAllVMesh2Die::InitResources()
64 : {
65 0 : locSignal_ = CreateMaskSignal();
66 :
67 0 : input_ = CreateVariable();
68 :
69 0 : for (uint32_t peerId = 0; peerId < transports.size(); peerId++) {
70 0 : HCCL_DEBUG("[CcuContextAllToAllVMesh2Die]RankId[%u], PeerId[%u]", rankId_, peerId);
71 0 : output_.emplace_back(CreateVariable(*(transports[peerId]), CKE_IDX_1));
72 0 : token_.emplace_back(CreateVariable(*(transports[peerId]), CKE_IDX_2));
73 : }
74 : // 本rank固定放在末尾
75 0 : output_.emplace_back(CreateVariable());
76 0 : token_.emplace_back(CreateVariable());
77 :
78 0 : xnMaxTransportSize_ = CreateVariable();
79 0 : xnMaxTransportGoSize_ = CreateGroupOpSize();
80 :
81 0 : xnMaxTransportSize_ = MAX_TRANSPORT_SIZE;
82 0 : auto xnMaxTransportGoSize = CalGoSize(MAX_TRANSPORT_SIZE);
83 0 : xnMaxTransportGoSize_.addrOffset = xnMaxTransportGoSize[GO_ADDR_OFFSET_IDX];
84 0 : xnMaxTransportGoSize_.loopParam = xnMaxTransportGoSize[GO_LOOP_PARAM_IDX];
85 0 : xnMaxTransportGoSize_.parallelParam = xnMaxTransportGoSize[GO_PARALLEL_PARAM_IDX];
86 0 : xnMaxTransportGoSize_.residual = xnMaxTransportGoSize[GO_RESIDUAL_IDX];
87 :
88 0 : sendRecvInfo_.resize(localSize_);
89 0 : for (uint64_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
90 0 : sendRecvInfo_[rankIdx].sendOffset = CreateVariable();
91 0 : sendRecvInfo_[rankIdx].recvOffset = CreateVariable();
92 0 : sendRecvInfo_[rankIdx].sendTailSize = CreateVariable();
93 0 : sendRecvInfo_[rankIdx].sendTailGoSize = CreateGroupOpSize();
94 0 : sendRecvInfo_[rankIdx].sendLoopNum = CreateVariable();
95 : }
96 :
97 0 : for (uint16_t i = 0; i < localSize_; i++) {
98 0 : src_.emplace_back(CreateMemory());
99 0 : dst_.emplace_back(CreateMemory());
100 : }
101 :
102 0 : curSendTailSize_ = CreateVariable();
103 0 : curSendTailGoSize_ = CreateGroupOpSize();
104 :
105 0 : xnConst1_ = CreateVariable();
106 0 : completedRankCount_ = CreateVariable();
107 0 : }
108 :
109 0 : void CcuContextAllToAllVMesh2Die::LoadArgs()
110 : {
111 0 : Load(input_);
112 0 : Load(output_[localId_]);
113 0 : Load(token_[localId_]);
114 :
115 0 : for (uint64_t rankIdx = 0; rankIdx < localSize_; rankIdx++) {
116 0 : Load(sendRecvInfo_[rankIdx].sendOffset);
117 0 : Load(sendRecvInfo_[rankIdx].recvOffset);
118 0 : Load(sendRecvInfo_[rankIdx].sendTailSize);
119 0 : Load(sendRecvInfo_[rankIdx].sendTailGoSize);
120 0 : Load(sendRecvInfo_[rankIdx].sendLoopNum);
121 : }
122 0 : }
123 :
124 0 : void CcuContextAllToAllVMesh2Die::ExchangeInfoAndSync()
125 : {
126 : // 交换信息并做同步,前同步固定用1,2,3号信号
127 0 : CcuRep::Variable tempDst = CreateVariable();
128 0 : for (u32 peerId = 0; peerId < transports.size(); peerId++) {
129 0 : uint32_t dst = CalcDstRank(peerId);
130 0 : tempDst = output_[localId_];
131 0 : tempDst += sendRecvInfo_[dst].recvOffset;
132 :
133 0 : WriteVariableWithSignal(*transports[peerId], tempDst, CKE_IDX_1, CKE_IDX_1, selfBit_);
134 0 : WriteVariableWithSignal(*transports[peerId], token_[localId_], CKE_IDX_2, CKE_IDX_2, selfBit_);
135 : }
136 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit_);
137 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit_);
138 0 : }
139 :
140 0 : void CcuContextAllToAllVMesh2Die::PostSync()
141 : {
142 0 : for (const auto& t : transports) {
143 0 : if (t == nullptr) {
144 0 : THROW<NullPtrException>(StringFormat("CcuContextAllToAllVMesh2Die::PostSync transport ptr is null"));
145 : }
146 0 : RemotePost(*t, CKE_IDX_0, selfBit_);
147 : }
148 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit_);
149 0 : }
150 :
151 0 : uint32_t CcuContextAllToAllVMesh2Die::CalcDstRank(uint32_t peerId) const { return peerId; }
152 :
153 0 : uint32_t CcuContextAllToAllVMesh2Die::CalcTransIdx(uint32_t peerId) const { return peerId; }
154 :
155 0 : void CcuContextAllToAllVMesh2Die::DoAll2AllVMultiLoop()
156 : {
157 0 : completedRankCount_ = 0;
158 0 : xnConst1_ = 1;
159 0 : CCU_WHILE(completedRankCount_ != peerSize_)
160 : {
161 0 : HCCL_DEBUG("[CcuContextAllToAllVMesh2Die] Algorithm loops[%u].", peerSize_);
162 0 : LoopStep();
163 0 : }
164 0 : }
165 :
166 0 : void CcuContextAllToAllVMesh2Die::WriteToDstOutput(uint32_t peerId)
167 : {
168 0 : uint32_t dstRank = CalcDstRank(peerId);
169 0 : uint32_t transIdx = CalcTransIdx(peerId);
170 :
171 0 : HCCL_DEBUG(
172 : "[CcuContextAllToAllVMesh2Die] WriteToDstOutput[%u] Start. RankId[%u] dstRank[%u] transIdx[%u]", peerId,
173 : rankId_, dstRank, transIdx);
174 :
175 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum == UINT64_MAX) // 已经搬完了,仅同步
176 : {
177 0 : LocalPost(locSignal_, (1 << peerId));
178 0 : }
179 :
180 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum != UINT64_MAX) // 还没有搬完
181 : {
182 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum == UINT64_MAX - 1) // 最后一次搬运, 发送尾块数据
183 : {
184 0 : curSendTailSize_ = sendRecvInfo_[dstRank].sendTailSize;
185 0 : CCU_IF(curSendTailSize_ == 0) { LocalPost(locSignal_, (1 << peerId)); }
186 0 : CCU_IF(curSendTailSize_ != 0)
187 : {
188 0 : Write(*(transports[transIdx]), dst_[peerId], src_[peerId], curSendTailSize_, locSignal_, (1 << peerId));
189 0 : }
190 0 : completedRankCount_ += xnConst1_;
191 0 : }
192 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum != UINT64_MAX - 1) // 正常搬运
193 : {
194 0 : Write(*(transports[transIdx]), dst_[peerId], src_[peerId], xnMaxTransportSize_, locSignal_, (1 << peerId));
195 0 : dst_[peerId].addr += xnMaxTransportSize_;
196 0 : src_[peerId].addr += xnMaxTransportSize_;
197 0 : }
198 0 : sendRecvInfo_[dstRank].sendLoopNum += xnConst1_;
199 0 : }
200 0 : HCCL_DEBUG("[CcuContextAllToAllVMesh2Die] WriteToDstOutput end.");
201 0 : }
202 :
203 0 : void CcuContextAllToAllVMesh2Die::GroupCopyToDstOutput(uint32_t peerId)
204 : {
205 0 : uint32_t dstRank = CalcDstRank(peerId);
206 :
207 0 : HCCL_DEBUG(
208 : "[CcuContextAllToAllVMesh2Die] GroupCopyToDstOutput[%u] Start. RankId[%u] dstRank[%u]", peerId, rankId_,
209 : dstRank);
210 :
211 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum == UINT64_MAX) // 已经搬完了,仅同步
212 : {
213 0 : LocalPost(locSignal_, (1 << peerId));
214 0 : }
215 :
216 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum != UINT64_MAX) // 还没有搬完
217 : {
218 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum == UINT64_MAX - 1) // 最后一次搬运, 发送尾块数据
219 : {
220 0 : curSendTailSize_ = sendRecvInfo_[dstRank].sendTailSize;
221 0 : curSendTailGoSize_ = sendRecvInfo_[dstRank].sendTailGoSize;
222 0 : CCU_IF(curSendTailSize_ == 0) { LocalPost(locSignal_, (1 << peerId)); }
223 0 : CCU_IF(curSendTailSize_ != 0)
224 : {
225 0 : GroupCopy(dst_[peerId], src_[peerId], curSendTailGoSize_);
226 0 : LocalPost(locSignal_, (1 << peerId));
227 0 : }
228 0 : completedRankCount_ += xnConst1_;
229 0 : }
230 0 : CCU_IF(sendRecvInfo_[dstRank].sendLoopNum != UINT64_MAX - 1) // 正常搬运
231 : {
232 0 : GroupCopy(dst_[peerId], src_[peerId], xnMaxTransportGoSize_);
233 0 : dst_[peerId].addr += xnMaxTransportSize_;
234 0 : src_[peerId].addr += xnMaxTransportSize_;
235 0 : LocalPost(locSignal_, (1 << peerId));
236 0 : }
237 0 : sendRecvInfo_[dstRank].sendLoopNum += xnConst1_;
238 0 : }
239 0 : HCCL_DEBUG("[CcuContextAllToAllVMesh2Die] GroupCopyToDstOutput end.");
240 0 : }
241 :
242 0 : void CcuContextAllToAllVMesh2Die::CalcGroupSrcDst()
243 : {
244 0 : for (uint32_t peerId = 0; peerId < transports.size(); peerId++) {
245 0 : const u32 dstRank = CalcDstRank(peerId);
246 :
247 0 : src_[peerId].addr = input_;
248 0 : src_[peerId].addr += sendRecvInfo_[dstRank].sendOffset;
249 0 : src_[peerId].token = token_[peerId];
250 :
251 0 : dst_[peerId].addr = output_[peerId]; // recvOffset在前同步时已经计算
252 0 : dst_[peerId].token = token_[peerId];
253 : }
254 :
255 0 : if (withMyRank_) {
256 0 : src_[localId_].addr = input_;
257 0 : src_[localId_].addr += sendRecvInfo_[localId_].sendOffset;
258 0 : src_[localId_].token = token_[localId_];
259 0 : dst_[localId_].addr = output_[localId_];
260 0 : dst_[localId_].addr += sendRecvInfo_[localId_].recvOffset;
261 0 : dst_[localId_].token = token_[localId_];
262 : }
263 0 : }
264 :
265 0 : void CcuContextAllToAllVMesh2Die::LoopStep()
266 : {
267 0 : for (uint32_t peerId = 0; peerId < transports.size(); peerId++) {
268 0 : WriteToDstOutput(peerId);
269 : }
270 :
271 0 : if (withMyRank_) {
272 0 : GroupCopyToDstOutput(localId_);
273 : }
274 :
275 0 : LocalWait(locSignal_, (1 << peerSize_) - 1);
276 0 : }
277 :
278 0 : void CcuContextAllToAllVMesh2Die::Algorithm()
279 : {
280 : // 初始化寄存器资源 & 加载外部输入参数
281 0 : HCCL_INFO("[CcuContextAllToAllVMesh2Die] Algorithm Init Begins.");
282 0 : InitResources();
283 0 : LoadArgs();
284 :
285 0 : HCCL_INFO("[CcuContextAllToAllVMesh2Die] Algorithm begins.");
286 :
287 : // 框架已经默认做了前后轴同步,算法不需要再重复做
288 0 : ExchangeInfoAndSync();
289 :
290 0 : CalcGroupSrcDst();
291 0 : DoAll2AllVMultiLoop();
292 :
293 0 : PostSync();
294 :
295 0 : HCCL_INFO("[CcuContextAllToAllVMesh2Die] Algorithm Ends.");
296 0 : }
297 :
298 0 : std::vector<uint64_t> CcuContextAllToAllVMesh2Die::GeneArgs(const CcuTaskArg& arg)
299 : {
300 0 : const CcuTaskArgAllToAllVMesh2Die* taskArg = dynamic_cast<const CcuTaskArgAllToAllVMesh2Die*>(&arg);
301 0 : if (taskArg == nullptr) {
302 0 : THROW<NullPtrException>(StringFormat("CcuContextAllToAllVMesh2Die::taskArg ptr is null"));
303 : }
304 :
305 0 : uint64_t inputAddr = taskArg->inputAddr;
306 0 : uint64_t outputAddr = taskArg->outputAddr;
307 0 : uint64_t tokenInfo = taskArg->token;
308 :
309 0 : std::vector<uint64_t> taskParams = {inputAddr, outputAddr, tokenInfo}; // 不需要ScratchMem
310 :
311 0 : for (auto peerId : rankGroup_) {
312 0 : const uint64_t floorLoopNum = taskArg->localSendRecvInfo.sendLength[peerId] / MAX_TRANSPORT_SIZE;
313 0 : uint64_t sendLoopNum = UINT64_MAX - 1 - floorLoopNum;
314 0 : uint64_t sendTailSize = taskArg->localSendRecvInfo.sendLength[peerId] - floorLoopNum * MAX_TRANSPORT_SIZE;
315 0 : auto sendTailGoSize = CalGoSize(sendTailSize);
316 0 : uint64_t sendOffset = taskArg->localSendRecvInfo.sendOffset[peerId];
317 0 : uint64_t recvOffset = taskArg->localSendRecvInfo.recvOffset[peerId];
318 0 : taskParams.push_back(sendOffset);
319 0 : taskParams.push_back(recvOffset);
320 0 : taskParams.push_back(sendTailSize);
321 0 : taskParams.insert(taskParams.cend(), sendTailGoSize.cbegin(), sendTailGoSize.cend());
322 0 : taskParams.push_back(sendLoopNum);
323 0 : HCCL_DEBUG(
324 : "[CcuContextAllToAllVMesh2Die][sliceInfo] RankId[%u], dstRank[%d]: sendOffset[%llu], "
325 : "recvOffset[%llu], sendLength[%llu], sendTailSize[%llu], sendLoopNum[%llu]",
326 : rankId_, peerId, sendOffset, recvOffset, taskArg->localSendRecvInfo.sendLength[peerId], sendTailSize,
327 : sendLoopNum);
328 0 : }
329 :
330 0 : HCCL_DEBUG(
331 : "[CcuContextAllToAllVMesh2Die][GeneArgs] RankId[%u], inputAddr[%#llx], outputAddr[%#llx], "
332 : "xnMaxTransportSize[%llu], args[%zu]",
333 : rankId_, inputAddr, outputAddr, MAX_TRANSPORT_SIZE, taskParams.size());
334 :
335 0 : return taskParams;
336 0 : }
337 :
338 : } // namespace Hccl
|