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