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_PRIMITIVE_H
12 : #define HCCLV2_PRIMITIVE_H
13 :
14 : #include <string>
15 : #include <memory>
16 : #include <list>
17 : #include "types.h"
18 : #include "data_slice.h"
19 : #include "notify_type.h"
20 : #include "data_type.h"
21 : #include "dma_mode.h"
22 : #include "reduce_op.h"
23 : #include "string_util.h"
24 : #include "virtual_topo.h"
25 : #include "invalid_params_exception.h"
26 :
27 : namespace Hccl {
28 :
29 : using namespace std;
30 :
31 8298 : MAKE_ENUM(
32 : PrimType, POST_TO, WAIT_FROM, WAIT_GROUP, LOCAL_COPY, LOCAL_REDUCE, SEND, RECV, GROUP, SEND_REDUCE, RECV_REDUCE)
33 :
34 : class Primitive {
35 : public:
36 129 : explicit Primitive(PrimType type) : type(type) {};
37 :
38 129 : virtual ~Primitive() = default;
39 :
40 : virtual string Describe() const = 0;
41 158 : PrimType GetType() const { return type; }
42 :
43 : protected:
44 : PrimType type;
45 : };
46 :
47 : class PrimQueue;
48 : constexpr u32 INVALID_PRIM_QID = 0xffffff; // 无效的原语队列
49 : class PrimPostTo : public Primitive {
50 : public:
51 5 : PrimPostTo(const weak_ptr<PrimQueue> queue, NotifyType notifyType = NotifyType::NORMAL, u32 topicId = 0);
52 :
53 : string Describe() const override;
54 :
55 : void SetParent(const weak_ptr<PrimQueue>& que);
56 :
57 : QId GetQid() const;
58 : QId GetParentQid() const;
59 3 : u32 GetTopicId() const { return topicId; }
60 1 : NotifyType GetNotifyType() const { return notifyType; }
61 :
62 : private:
63 : weak_ptr<PrimQueue> queue;
64 : NotifyType notifyType;
65 : u32 topicId;
66 : weak_ptr<PrimQueue> parent;
67 : };
68 :
69 : class PrimWaitFrom : public Primitive {
70 : public:
71 : PrimWaitFrom(const weak_ptr<PrimQueue> queue, u32 topicId = 0);
72 :
73 : string Describe() const override;
74 :
75 : void SetParent(const weak_ptr<PrimQueue>& que);
76 :
77 : QId GetQid() const;
78 : QId GetParentQid() const;
79 3 : u32 GetTopicId() const { return topicId; }
80 :
81 : private:
82 : weak_ptr<PrimQueue> queue;
83 : u32 topicId;
84 : weak_ptr<PrimQueue> parent;
85 : };
86 :
87 : class PrimWaitGroup : public Primitive {
88 : public:
89 : PrimWaitGroup(u32 topicId = 0);
90 :
91 : using Iterator = BaseConstIterator<vector, QId>;
92 :
93 : void Append(const weak_ptr<PrimQueue> queue);
94 :
95 : string Describe() const override;
96 :
97 : void SetParent(const weak_ptr<PrimQueue>& que);
98 :
99 : QId GetParentQid() const;
100 :
101 3 : u32 GetTopicId() const { return topicId; }
102 :
103 2 : Iterator Iter() const { return Iterator(qids); }
104 :
105 : private:
106 : vector<QId> qids;
107 : u32 topicId;
108 : weak_ptr<PrimQueue> parent;
109 : };
110 :
111 : class PrimLocalCopy : public Primitive {
112 : public:
113 : PrimLocalCopy(const DataSlice& srcSlice, const DataSlice& dstSlice);
114 :
115 : string Describe() const override;
116 :
117 5 : const DataSlice& GetSrcSlice() const { return srcSlice; }
118 5 : const DataSlice& GetDstSlice() const { return dstSlice; }
119 :
120 : private:
121 : DataSlice srcSlice;
122 : DataSlice dstSlice;
123 : };
124 :
125 : class PrimLocalReduce : public Primitive {
126 : public:
127 : PrimLocalReduce(const DataSlice& srcSlice, const DataSlice& dstSlice, DataType dataType, ReduceOp reduceOp);
128 :
129 : string Describe() const override;
130 :
131 0 : const DataType& GetDataType() const { return dataType; }
132 0 : const ReduceOp& GetReduceOp() const { return reduceOp; }
133 0 : const DataSlice& GetSrcSlice() const { return srcSlice; }
134 0 : const DataSlice& GetDstSlice() const { return dstSlice; }
135 :
136 : private:
137 : DataSlice srcSlice;
138 : DataSlice dstSlice;
139 : DataType dataType;
140 : ReduceOp reduceOp;
141 : };
142 :
143 : class PrimSend : public Primitive {
144 : public:
145 : PrimSend(
146 : RankId remoteRank, const LinkData& link, const DataSlice& localSlice, const DataSlice& remoteSlice,
147 24 : DmaMode dmaMode = DmaMode::DEFAULT);
148 :
149 : string Describe() const override;
150 : void Append(const DataSlice& localSlice, const DataSlice& remoteSlice);
151 :
152 : void SetRemoteRank(RankId remote) { remoteRank = remote; }
153 : void SetLink(const LinkData& l) { this->link = l; }
154 13 : RankId GetRemoteRank() const { return remoteRank; }
155 46 : const LinkData& GetLink() const { return link; }
156 8 : DmaMode GetDmaMode() const { return dmaMode; }
157 29 : u32 Size() const { return localSlices.size(); }
158 5 : const DataSlice& GetLocalSlice(u32 pos) const
159 : {
160 5 : if (pos >= localSlices.size()) {
161 0 : throw InvalidParamsException("pos is out of range of localSlices");
162 : }
163 5 : return localSlices[pos];
164 : }
165 5 : const DataSlice& GetRemoteSlice(u32 pos) const
166 : {
167 5 : if (pos >= remoteSlices.size()) {
168 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
169 : }
170 5 : return remoteSlices[pos];
171 : }
172 :
173 : private:
174 : RankId remoteRank;
175 : LinkData link;
176 : vector<DataSlice> localSlices;
177 : vector<DataSlice> remoteSlices;
178 : DmaMode dmaMode{DmaMode::DEFAULT};
179 : };
180 :
181 : class PrimRecv : public Primitive {
182 : public:
183 : PrimRecv(
184 : RankId remoteRank, const LinkData& link, const DataSlice& localSlice, const DataSlice& remoteSlice,
185 26 : DmaMode dmaMode = DmaMode::DEFAULT);
186 :
187 : string Describe() const override;
188 : void Append(const DataSlice& localSlice, const DataSlice& remoteSlice);
189 :
190 : void SetRemoteRank(RankId remote) { remoteRank = remote; }
191 : void SetLink(const LinkData& l) { this->link = l; }
192 :
193 12 : RankId GetRemoteRank() const { return remoteRank; }
194 38 : const LinkData& GetLink() const { return link; }
195 8 : DmaMode GetDmaMode() const { return dmaMode; }
196 24 : u32 Size() const { return localSlices.size(); }
197 5 : const DataSlice& GetLocalSlice(u32 pos) const
198 : {
199 5 : if (pos >= localSlices.size()) {
200 0 : throw InvalidParamsException("pos is out of range of localSlices");
201 : }
202 5 : return localSlices[pos];
203 : }
204 5 : const DataSlice& GetRemoteSlice(u32 pos) const
205 : {
206 5 : if (pos >= remoteSlices.size()) {
207 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
208 : }
209 5 : return remoteSlices[pos];
210 : }
211 :
212 : private:
213 : RankId remoteRank;
214 : LinkData link;
215 : vector<DataSlice> localSlices;
216 : vector<DataSlice> remoteSlices;
217 : DmaMode dmaMode{DmaMode::DEFAULT};
218 : };
219 :
220 : class PrimSendReduce : public Primitive {
221 : public:
222 : PrimSendReduce(
223 : RankId remoteRank, const LinkData& link, const DataSlice& localSlice, const DataSlice& remoteSrcSlice,
224 : const DataSlice& remoteDstSlice, const DataType& dataType, const ReduceOp& reduceOp,
225 8 : DmaMode dmaMode = DmaMode::DEFAULT);
226 :
227 : string Describe() const override;
228 : void Append(const DataSlice& localSlice, const DataSlice& remoteSrcSlice, const DataSlice& remoteDstSlice);
229 :
230 : void SetRemoteRank(RankId remote) { remoteRank = remote; }
231 : void SetLink(const LinkData& l) { this->link = l; }
232 :
233 14 : RankId GetRemoteRank() const { return remoteRank; }
234 44 : const LinkData& GetLink() const { return link; }
235 8 : DmaMode GetDmaMode() const { return dmaMode; }
236 10 : const DataType& GetDataType() const { return dataType; }
237 10 : const ReduceOp& GetReduceOp() const { return reduceOp; }
238 18 : u32 Size() const { return localSlices.size(); }
239 3 : const DataSlice& GetLocalSlice(u32 pos) const
240 : {
241 3 : if (pos >= localSlices.size()) {
242 0 : throw InvalidParamsException("pos is out of range of localSlices");
243 : }
244 3 : return localSlices[pos];
245 : }
246 1 : const DataSlice& GetRemoteSrcSlice(u32 pos) const
247 : {
248 1 : if (pos >= remoteSrcSlices.size()) {
249 0 : throw InvalidParamsException("pos is out of range of remoteSrcSlices");
250 : }
251 1 : return remoteSrcSlices[pos];
252 : }
253 2 : const DataSlice& GetRemoteDstSlice(u32 pos) const
254 : {
255 2 : if (pos >= remoteDstSlices.size()) {
256 0 : throw InvalidParamsException("pos is out of range of remoteDstSlices");
257 : }
258 2 : return remoteDstSlices[pos];
259 : }
260 :
261 : private:
262 : RankId remoteRank;
263 : LinkData link;
264 : vector<DataSlice> localSlices;
265 : vector<DataSlice> remoteSrcSlices;
266 : vector<DataSlice> remoteDstSlices;
267 : DataType dataType;
268 : ReduceOp reduceOp;
269 : DmaMode dmaMode{DmaMode::DEFAULT};
270 : };
271 :
272 : class PrimRecvReduce : public Primitive {
273 : public:
274 : PrimRecvReduce(
275 : RankId remoteRank, const LinkData& link, const DataSlice& remoteSlice, const DataSlice& localSrcSlice,
276 : const DataSlice& localDstSlice, const DataType& dataType, const ReduceOp& reduceOp,
277 9 : DmaMode dmaMode = DmaMode::DEFAULT);
278 :
279 : string Describe() const override;
280 : void Append(const DataSlice& remoteSlice, const DataSlice& localSrcSlice, const DataSlice& localDstSlice);
281 :
282 : void SetRemoteRank(RankId remote) { remoteRank = remote; }
283 : void SetLink(const LinkData& l) { this->link = l; }
284 :
285 11 : RankId GetRemoteRank() const { return remoteRank; }
286 40 : const LinkData& GetLink() const { return link; }
287 8 : DmaMode GetDmaMode() const { return dmaMode; }
288 18 : const DataType& GetDataType() const { return dataType; }
289 18 : const ReduceOp& GetReduceOp() const { return reduceOp; }
290 39 : u32 Size() const { return remoteSlices.size(); }
291 8 : const DataSlice& GetRemoteSlice(u32 pos) const
292 : {
293 8 : if (pos >= remoteSlices.size()) {
294 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
295 : }
296 8 : return remoteSlices[pos];
297 : }
298 8 : const DataSlice& GetLocalSrcSlice(u32 pos) const
299 : {
300 8 : if (pos >= localSrcSlices.size()) {
301 0 : throw InvalidParamsException("pos is out of range of localSrcSlices");
302 : }
303 8 : return localSrcSlices[pos];
304 : }
305 10 : const DataSlice& GetLocalDstSlice(u32 pos) const
306 : {
307 10 : if (pos >= localDstSlices.size()) {
308 0 : throw InvalidParamsException("pos is out of range of localDstSlices");
309 : }
310 10 : return localDstSlices[pos];
311 : }
312 :
313 : private:
314 : RankId remoteRank;
315 : LinkData link;
316 : vector<DataSlice> remoteSlices;
317 : vector<DataSlice> localSrcSlices;
318 : vector<DataSlice> localDstSlices;
319 : DataType dataType;
320 : ReduceOp reduceOp;
321 : DmaMode dmaMode{DmaMode::DEFAULT};
322 : };
323 :
324 : class PrimGroup : public Primitive {
325 : public:
326 26 : PrimGroup() : Primitive(PrimType::GROUP) {}
327 :
328 : using Iterator = BaseConstIterator<vector, unique_ptr<Primitive>>;
329 :
330 : string Describe() const override;
331 : void CheckValid() const;
332 : void Append(unique_ptr<Primitive> prim);
333 :
334 16 : Iterator Iter() const { return Iterator(prims); }
335 :
336 5 : u32 GetSize() const { return prims.size(); }
337 :
338 : private:
339 : vector<unique_ptr<Primitive>> prims;
340 : };
341 : } // namespace Hccl
342 : #endif
|