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 : #ifndef HCCLV2_TEMPLATE_UTILS
12 : #define HCCLV2_TEMPLATE_UTILS
13 :
14 : #include <algorithm>
15 : #include <map>
16 : #include <vector>
17 :
18 : #include "data_type.h"
19 : #include "coll_operator.h"
20 : #include "coll_alg_params.h"
21 : #include "op_mode.h"
22 : #include "virtual_topo.h"
23 : #include "connected_link_mgr.h"
24 : #include "dev_capability.h"
25 : #include "primitive.h"
26 : #include "prim_queue.h"
27 : #include "instruction.h"
28 : #include "ins_queue.h"
29 :
30 : namespace Hccl {
31 : constexpr int NUM_TWO = 2;
32 : constexpr uint64_t UB_MAX_DATA_SIZE = 256 * 1024 * 1024; // Byte, UB协议一次传输的最大size
33 :
34 : // log2 for HD
35 : inline u32 Log2(u32 antilogarithm)
36 : {
37 : u32 logarithm = 0;
38 : while ((antilogarithm >> (logarithm + 1)) != 0) {
39 : logarithm++;
40 : }
41 :
42 : return logarithm;
43 : }
44 :
45 : // judge if both odd or even
46 : inline bool IsSameParity(RankId rank, u32 portId) { return ((static_cast<u32>(rank) % NUM_TWO) == (portId % NUM_TWO)); }
47 :
48 : // roundup func for uint
49 0 : inline u64 RoundUp(u64 dividend, u64 divisor) { return dividend / divisor + ((dividend % divisor != 0) ? 1 : 0); }
50 :
51 : using BuffInfo = struct BufferInformation {
52 : BufferType inBuffType;
53 : BufferType outBuffType;
54 : BufferType scratBuffType;
55 : u64 scratchBuffSize = 0;
56 : u64 inBuffBaseOff = 0;
57 : u64 outBuffBaseOff = 0;
58 : u64 scratchBuffBaseOff = 0;
59 : };
60 :
61 : using SliceInfo = struct SliceInformation {
62 : u64 offset;
63 : u64 size;
64 : };
65 :
66 : struct SendRecvSliceInfo {
67 : SliceInfo sendSlice;
68 : SliceInfo recvSlice;
69 : };
70 :
71 : using RankSliceInfo = std::vector<std::vector<SliceInfo>>;
72 :
73 : // for DMA Copy Elimination
74 : using UsrData = struct UserDataInformation {
75 : std::vector<DataSlice> usrInSlices;
76 : std::vector<DataSlice> scratchInSlices;
77 : std::vector<DataSlice> scratchOutSlices;
78 : std::vector<DataSlice> usrOutSlices;
79 : };
80 :
81 : using A2ASendRecvInfo = struct A2ASendRecvInfoDef {
82 : // 存放数据长度和偏移长度
83 : std::vector<u64> sendLength;
84 : std::vector<u64> sendOffset;
85 : std::vector<u64> recvLength;
86 : std::vector<u64> recvOffset;
87 : // 存放数据个数和偏移个数
88 : std::vector<u64> sendCounts;
89 : std::vector<u64> sendDispls;
90 : std::vector<u64> recvCounts;
91 : std::vector<u64> recvDispls;
92 : };
93 :
94 : // 针对标准的RS temp,准备所需的信息
95 : // rankSize=n 的RS的标准行为:N个Input,Reduce成1个Output;
96 : struct TemplateInfo {
97 : uint64_t inputAddr;
98 : uint64_t outputAddr;
99 : uint64_t scratchAddr;
100 :
101 : BufferType inBuffType;
102 : BufferType outBuffType;
103 :
104 : uint64_t dataCount;
105 : DataType inDataType;
106 : DataType outDataType;
107 : uint64_t dataSize;
108 :
109 : uint64_t inStride = 0; // in Count
110 : };
111 :
112 : struct ParamPool {
113 0 : ParamPool(
114 : const CollAlgOperator& op, const CollAlgParams& params, const u64 scratchSize = 0,
115 : const AlgTopoInfo* topoInfo = nullptr, const RankGraph* rankGraph = nullptr)
116 0 : : op(op),
117 0 : params(params),
118 0 : scratchSize(scratchSize),
119 0 : topoInfo(topoInfo),
120 0 : rankGraph(rankGraph)
121 0 : {}
122 : const CollAlgOperator& op;
123 : const CollAlgParams& params;
124 : const u64 scratchSize = 0;
125 : const AlgTopoInfo* topoInfo = nullptr;
126 : const RankGraph* rankGraph = nullptr;
127 : };
128 :
129 : using TempFuncs = struct TemplateFunctionality {
130 : OpMode opMode;
131 : bool enableCounterNotify = false;
132 : bool forAllReduce = false;
133 : bool forAlgSeqComb = false;
134 : bool isForepart = false;
135 : bool isBottom = false;
136 : bool forAlgConcurrComb = false; // concurrent combination not supported yet, 2024/1/30
137 : bool forAlgPipeComb = false; // pipeline combination not supported yet, 2024/1/30
138 : UsrData usrData; // pass user memory info for DMA copy elimination
139 : };
140 :
141 : using AllignInfo = struct AllignInformation {
142 : bool enableAllign;
143 : u64 allignSize;
144 : DataType dataType;
145 : };
146 :
147 : using LinkReq = std::map<RankId, u32>;
148 :
149 : using AlgTempResReq = struct AlgTemplateResRequirement {
150 : std::vector<std::tuple<QId, QId, u32>> queNotifys;
151 : u32 queNum = 0;
152 : u32 streamNum = 0;
153 : LinkReq links; // link requirements
154 : std::vector<std::pair<QId, u32>> localWaitGroupCntNotify{};
155 : std::vector<std::pair<QId, u32>> localBcastPostCntNotify{};
156 : // u64 scratchBufferSize;
157 : };
158 :
159 : using ResLinks = std::map<RankId, std::vector<LinkData>>;
160 : using LinkDataIterator = std::vector<LinkData>::const_iterator;
161 :
162 : u32 GetNHRStepNum(u32 rankSize);
163 :
164 : HcclResult GetUnitAllignSize(const AllignInfo& allignInfo, u64& unitAllignSize);
165 :
166 : // convert virtualRank (rankIdx of virtual Topo) to algRank (rankIdx of Alg Template)
167 : HcclResult GetAlgRank(const RankId virtRank, const std::vector<RankId>& tempVTopo, u32& algRank);
168 :
169 : // slice calculation shared by ar and rs/ag
170 : HcclResult CalcRsAgSliceInfoConcurrMesh(
171 : const RankId myRank, const std::vector<std::vector<RankId>>& tempVTopo, const AllignInfo& allignInfo,
172 : const u64 dataSize, RankSliceInfo& sliceInfoVec);
173 : HcclResult CalcRsAgSliceInfoMesh(
174 : const RankId myRank, const u32 tempRankSize, const AllignInfo& allignInfo, const u64 dataSize,
175 : RankSliceInfo& sliceInfoVec);
176 : HcclResult CalcRsAgSliceInfoRing(
177 : const RankId myRank, const std::vector<std::vector<RankId>>& tempVTopo, const AllignInfo& allignInfo,
178 : const u64 dataSize, RankSliceInfo& sliceInfoVec);
179 : HcclResult CalcRsAgSliceInfoNHR(
180 : const RankId myRank, const u32 tempRankSize, const AllignInfo& allignInfo, const u64 dataSize,
181 : RankSliceInfo& sliceInfoVec);
182 : // slice calculation allreduce
183 : HcclResult CalcSliceInfoAllReduce(
184 : const AllignInfo& allignInfo, const u32 rankSize, const u64 dataSize, RankSliceInfo& sliceInfoVec);
185 : // res calculation
186 : HcclResult CalcResLinksMesh(
187 : const RankId myRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
188 : const u32 linkNumBtwPeers, AlgTempResReq& tempResReq);
189 : HcclResult CalcResLinksMesh2D(
190 : const RankId myRank, const std::vector<std::vector<RankId>>& tempVTopo, const u32 linkNumBtwPeers,
191 : AlgTempResReq& tempResReq);
192 : HcclResult CalcResLinksRing(
193 : const RankId myRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
194 : AlgTempResReq& tempResReq);
195 : HcclResult CalcResLinksNHR(
196 : const RankId myRank, const u32 tempRankSize, const std::vector<std::vector<RankId>>& tempVTopo,
197 : AlgTempResReq& tempResReq);
198 :
199 : // get detour send recv links in 4P mesh
200 : HcclResult GetDetourSendRecvLinksIn4P(
201 : const RankId myRank, const RankId neighborRank, const ResLinks& tempLinks,
202 : std::vector<std::vector<LinkDataIterator>>& sendRecvLinks);
203 :
204 : u32 GetLinkNum(const RankGraph* rankGraph, RankId srcRank, RankId dstRank);
205 :
206 : HcclResult GetLocalSendRecvInfoforAlltoall(
207 : const CollAlgOperator& opParam, const u32 userRank, const u32 userRankSize, A2ASendRecvInfo& localSendRecvInfo);
208 : HcclResult GetLocalSendRecvInfoforAlltoallV(
209 : const CollAlgOperator& opParam, const u32 userRank, const u32 userRankSize, A2ASendRecvInfo& localSendRecvInfo);
210 : HcclResult GetLocalSendRecvInfoforAlltoallVC(
211 : const CollAlgOperator& opParam, const u32 userRank, const u32 userRankSize, A2ASendRecvInfo& localSendRecvInfo);
212 : HcclResult GetAlltoAllLocalSendRecvInfo(
213 : const CollAlgOperator& opParam, const u32 userRank, const u32 userRankSize, A2ASendRecvInfo& localSendRecvInfo);
214 : HcclResult BufferTypeToAddr(const BufferType& bufferType, CollAlgOperator& op, uint64_t& addr);
215 : HcclResult CalcDataSplitRateForLinks(const std::vector<LinkData>& links, std::vector<float>& dataSplitRate);
216 : DataSlice CalcDataSliceForLinks(
217 : const DataSlice& recvSrcSliceAllLinks, std::vector<float> dataSplitRate, u32 j, DataType dataType_);
218 : } // namespace Hccl
219 :
220 : #endif // HCCLV2_COLL_ALG_BASE
|