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_gather_mesh1d_2die.h"
12 : #include "ccu_instruction_all_gather_mesh1d_2die.h"
13 : #include "ccu_datatype.h"
14 :
15 : namespace Hccl {
16 :
17 : constexpr int OUTPUT_XN_ID = 1;
18 : constexpr int TOKEN_XN_ID = 2;
19 : constexpr int CKE_IDX_0 = 0;
20 : constexpr int CKE_IDX_1 = 1;
21 : constexpr int CKE_IDX_2 = 2;
22 : constexpr int CKE_IDX_3 = 3;
23 :
24 0 : CcuContextAllGatherMesh1D2Die::CcuContextAllGatherMesh1D2Die(const CcuCtxArg &arg,
25 : const std::vector<CcuTransport *> &transports,
26 0 : const CcuTransportGroup &group)
27 0 : : CcuContextAlgBase(arg, transports, group)
28 : {
29 0 : HCCL_INFO("[CcuContextAllGatherMesh1D2Die] Enter Constructor.");
30 0 : const CcuCtxArgAllGatherMesh1D2Die *ctxArg = dynamic_cast<const CcuCtxArgAllGatherMesh1D2Die *>(&arg);
31 0 : if (ctxArg == nullptr) {
32 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D2Die::ctxArg ptr is null"));
33 : }
34 0 : rankId_ = ctxArg->rankId_;
35 0 : withMyRank_ = ctxArg->withMyRank_;
36 0 : HCCL_INFO("[CcuContextAllGatherMesh1D2Die] WithMyRank[%d]", withMyRank_);
37 0 : if (ctxArg->dimSize_.size() > 0) {
38 0 : rankSize_ = ctxArg->dimSize_[0];
39 : }
40 0 : }
41 :
42 0 : void CcuContextAllGatherMesh1D2Die::Algorithm()
43 : {
44 0 : HCCL_INFO("[CcuContextAllGatherMesh1D2Die] AllgatherMesh1D run.");
45 0 : uint16_t logicRankSize = withMyRank_ ? transports.size() + 1 : transports.size();
46 0 : uint16_t logicId = rankId_ % logicRankSize; // topo为 2 * n
47 0 : uint16_t selfBit = 1 << logicId;
48 0 : uint16_t allBit = withMyRank_ ? ((1 << logicRankSize) - 1) & (~(1 << logicId)) : (1 << logicRankSize) - 1;
49 :
50 0 : input_.push_back(CreateVariable());
51 0 : uint16_t transportIdx = 0;
52 0 : if (transports.size() == 0) {
53 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D2Die transports is empty"));
54 : }
55 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
56 0 : uint16_t virRankSize = transports.size() + 1;
57 :
58 0 : for (uint64_t peerId = 0; peerId < transports.size(); peerId++) {
59 0 : HCCL_INFO("[CcuContextAllGatherMesh1D2Die] MyRank[%u], PeerId[%llu], TransportId[%u]",
60 : rankId_, peerId, transportIdx);
61 0 : CHK_PRT_RET(transports[transportIdx] == nullptr,
62 : HCCL_ERROR("[CcuContextAllGatherMesh1D2Die] Algorithm transport ptr is null"),);
63 0 : output_.push_back(CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID)); // 获取transport中id=1的Var来传递output
64 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
65 0 : transportIdx++;
66 : }
67 :
68 : // 最后一个位置放自己地址
69 0 : output_.push_back(CreateVariable());
70 0 : token_.push_back(CreateVariable());
71 :
72 0 : offset_ = CreateVariable();
73 0 : groupOpSize_ = CreateGroupOpSize();
74 :
75 0 : Load(input_[0]);
76 0 : Load(output_[virRankSize-1]);
77 0 : Load(token_[virRankSize-1]);
78 0 : Load(offset_);
79 0 : Load(groupOpSize_);
80 :
81 0 : for (auto t : transports) {
82 0 : WriteVariableWithSignal(*t, output_[virRankSize-1], OUTPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
83 0 : WriteVariableWithSignal(*t, token_[virRankSize-1], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
84 : }
85 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
86 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
87 :
88 0 : CcuRep::Memory src = CreateMemory();
89 0 : std::vector<CcuRep::Memory> dst;
90 0 : for (uint64_t rankIdx = 0; rankIdx < virRankSize; rankIdx++) {
91 0 : dst.push_back(CreateMemory());
92 : }
93 0 : src.addr = input_[0];
94 0 : src.token = token_[virRankSize-1];
95 :
96 : // 最后一个固定为本rank地址
97 0 : for (uint64_t rankIdx = 0; rankIdx < virRankSize; rankIdx++) {
98 0 : dst[rankIdx].addr = output_[rankIdx];
99 0 : dst[rankIdx].addr += offset_;
100 0 : dst[rankIdx].token = token_[rankIdx];
101 : }
102 :
103 0 : if (withMyRank_) {
104 0 : GroupBroadcast(transports, dst, src, groupOpSize_);
105 : } else {
106 0 : GroupBroadcastWithoutMyRank(transports, dst, src, groupOpSize_);
107 : }
108 :
109 0 : for (auto t : transports) {
110 0 : RemotePost(*t, CKE_IDX_0, selfBit);
111 : }
112 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
113 0 : HCCL_INFO("[CcuContextAllGatherMesh1D2Die] AllgatherMesh1D end.");
114 0 : return;
115 0 : }
116 :
117 0 : std::vector<uint64_t> CcuContextAllGatherMesh1D2Die::GeneArgs(const CcuTaskArg &arg)
118 : {
119 0 : const CcuTaskArgAllGatherMesh1D2Die *taskArg = dynamic_cast<const CcuTaskArgAllGatherMesh1D2Die *>(&arg);
120 0 : if (taskArg == nullptr) {
121 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D2Die::taskArg ptr is null"));
122 : }
123 0 : uint64_t inputAddr = taskArg->inputAddr_;
124 0 : uint64_t outputAddr = taskArg->outputAddr_;
125 0 : uint64_t tokenInfo = taskArg->token_;
126 0 : uint64_t outputSliceStride_ = taskArg->outputSliceStride_;
127 0 : uint64_t offset = outputSliceStride_ * rankId_; // output 偏移 outputSliceStride_
128 0 : uint64_t sliceSize = taskArg->sliceSize_;
129 0 : auto goSize = CalGoSize(sliceSize);
130 0 : return {inputAddr, outputAddr, tokenInfo, offset, goSize[0], goSize[1], goSize[2], goSize[3]};
131 0 : }
132 :
133 : }
|