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