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(PrimType, POST_TO, WAIT_FROM, WAIT_GROUP, LOCAL_COPY, LOCAL_REDUCE, SEND, RECV, GROUP, SEND_REDUCE,
32 : 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
42 : {
43 158 : return type;
44 : }
45 :
46 : protected:
47 : PrimType type;
48 : };
49 :
50 : class PrimQueue;
51 : constexpr u32 INVALID_PRIM_QID = 0xffffff; // 无效的原语队列
52 : class PrimPostTo : public Primitive {
53 : public:
54 5 : PrimPostTo(const weak_ptr<PrimQueue> queue, NotifyType notifyType = NotifyType::NORMAL, u32 topicId = 0);
55 :
56 : string Describe() const override;
57 :
58 : void SetParent(const weak_ptr<PrimQueue> &que);
59 :
60 : QId GetQid() const;
61 : QId GetParentQid() const;
62 3 : u32 GetTopicId() const
63 : {
64 3 : return topicId;
65 : }
66 1 : NotifyType GetNotifyType() const
67 : {
68 1 : return notifyType;
69 : }
70 :
71 : private:
72 : weak_ptr<PrimQueue> queue;
73 : NotifyType notifyType;
74 : u32 topicId;
75 : weak_ptr<PrimQueue> parent;
76 : };
77 :
78 : class PrimWaitFrom : public Primitive {
79 : public:
80 : PrimWaitFrom(const weak_ptr<PrimQueue> queue, u32 topicId = 0);
81 :
82 : string Describe() const override;
83 :
84 : void SetParent(const weak_ptr<PrimQueue> &que);
85 :
86 : QId GetQid() const;
87 : QId GetParentQid() const;
88 3 : u32 GetTopicId() const
89 : {
90 3 : return topicId;
91 : }
92 :
93 : private:
94 : weak_ptr<PrimQueue> queue;
95 : u32 topicId;
96 : weak_ptr<PrimQueue> parent;
97 : };
98 :
99 : class PrimWaitGroup : public Primitive {
100 : public:
101 : PrimWaitGroup(u32 topicId = 0);
102 :
103 : using Iterator = BaseConstIterator<vector, QId>;
104 :
105 : void Append(const weak_ptr<PrimQueue> queue);
106 :
107 : string Describe() const override;
108 :
109 : void SetParent(const weak_ptr<PrimQueue> &que);
110 :
111 : QId GetParentQid() const;
112 :
113 3 : u32 GetTopicId() const
114 : {
115 3 : return topicId;
116 : }
117 :
118 2 : Iterator Iter() const
119 : {
120 2 : return Iterator(qids);
121 : }
122 :
123 : private:
124 : vector<QId> qids;
125 : u32 topicId;
126 : weak_ptr<PrimQueue> parent;
127 : };
128 :
129 : class PrimLocalCopy : public Primitive {
130 : public:
131 : PrimLocalCopy(const DataSlice &srcSlice, const DataSlice &dstSlice);
132 :
133 : string Describe() const override;
134 :
135 5 : const DataSlice &GetSrcSlice() const
136 : {
137 5 : return srcSlice;
138 : }
139 5 : const DataSlice &GetDstSlice() const
140 : {
141 5 : return dstSlice;
142 : }
143 :
144 : private:
145 : DataSlice srcSlice;
146 : DataSlice dstSlice;
147 : };
148 :
149 : class PrimLocalReduce : public Primitive {
150 : public:
151 : PrimLocalReduce(const DataSlice &srcSlice, const DataSlice &dstSlice, DataType dataType, ReduceOp reduceOp);
152 :
153 : string Describe() const override;
154 :
155 0 : const DataType &GetDataType() const
156 : {
157 0 : return dataType;
158 : }
159 0 : const ReduceOp &GetReduceOp() const
160 : {
161 0 : return reduceOp;
162 : }
163 0 : const DataSlice &GetSrcSlice() const
164 : {
165 0 : return srcSlice;
166 : }
167 0 : const DataSlice &GetDstSlice() const
168 : {
169 0 : return dstSlice;
170 : }
171 :
172 : private:
173 : DataSlice srcSlice;
174 : DataSlice dstSlice;
175 : DataType dataType;
176 : ReduceOp reduceOp;
177 : };
178 :
179 : class PrimSend : public Primitive {
180 : public:
181 : PrimSend(RankId remoteRank, const LinkData &link, const DataSlice &localSlice, const DataSlice &remoteSlice,
182 24 : DmaMode dmaMode = DmaMode::DEFAULT);
183 :
184 : string Describe() const override;
185 : void Append(const DataSlice &localSlice, const DataSlice &remoteSlice);
186 :
187 : void SetRemoteRank(RankId remote)
188 : {
189 : remoteRank = remote;
190 : }
191 : void SetLink(const LinkData &l)
192 : {
193 : this->link = l;
194 : }
195 13 : RankId GetRemoteRank() const
196 : {
197 13 : return remoteRank;
198 : }
199 46 : const LinkData &GetLink() const
200 : {
201 46 : return link;
202 : }
203 8 : DmaMode GetDmaMode() const
204 : {
205 8 : return dmaMode;
206 : }
207 29 : u32 Size() const
208 : {
209 29 : return localSlices.size();
210 : }
211 5 : const DataSlice &GetLocalSlice(u32 pos) const
212 : {
213 5 : if (pos >= localSlices.size()) {
214 0 : throw InvalidParamsException("pos is out of range of localSlices");
215 : }
216 5 : return localSlices[pos];
217 : }
218 5 : const DataSlice &GetRemoteSlice(u32 pos) const
219 : {
220 5 : if (pos >= remoteSlices.size()) {
221 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
222 : }
223 5 : return remoteSlices[pos];
224 : }
225 :
226 : private:
227 : RankId remoteRank;
228 : LinkData link;
229 : vector<DataSlice> localSlices;
230 : vector<DataSlice> remoteSlices;
231 : DmaMode dmaMode{DmaMode::DEFAULT};
232 : };
233 :
234 : class PrimRecv : public Primitive {
235 : public:
236 : PrimRecv(RankId remoteRank, const LinkData &link, const DataSlice &localSlice, const DataSlice &remoteSlice,
237 26 : DmaMode dmaMode = DmaMode::DEFAULT);
238 :
239 : string Describe() const override;
240 : void Append(const DataSlice &localSlice, const DataSlice &remoteSlice);
241 :
242 : void SetRemoteRank(RankId remote)
243 : {
244 : remoteRank = remote;
245 : }
246 : void SetLink(const LinkData &l)
247 : {
248 : this->link = l;
249 : }
250 :
251 12 : RankId GetRemoteRank() const
252 : {
253 12 : return remoteRank;
254 : }
255 38 : const LinkData &GetLink() const
256 : {
257 38 : return link;
258 : }
259 8 : DmaMode GetDmaMode() const
260 : {
261 8 : return dmaMode;
262 : }
263 24 : u32 Size() const
264 : {
265 24 : return localSlices.size();
266 : }
267 5 : const DataSlice &GetLocalSlice(u32 pos) const
268 : {
269 5 : if (pos >= localSlices.size()) {
270 0 : throw InvalidParamsException("pos is out of range of localSlices");
271 : }
272 5 : return localSlices[pos];
273 : }
274 5 : const DataSlice &GetRemoteSlice(u32 pos) const
275 : {
276 5 : if (pos >= remoteSlices.size()) {
277 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
278 : }
279 5 : return remoteSlices[pos];
280 : }
281 :
282 : private:
283 : RankId remoteRank;
284 : LinkData link;
285 : vector<DataSlice> localSlices;
286 : vector<DataSlice> remoteSlices;
287 : DmaMode dmaMode{DmaMode::DEFAULT};
288 : };
289 :
290 : class PrimSendReduce : public Primitive {
291 : public:
292 : PrimSendReduce(RankId remoteRank, const LinkData &link, const DataSlice &localSlice,
293 : const DataSlice &remoteSrcSlice, const DataSlice &remoteDstSlice, const DataType &dataType,
294 8 : const ReduceOp &reduceOp, DmaMode dmaMode = DmaMode::DEFAULT);
295 :
296 : string Describe() const override;
297 : void Append(const DataSlice &localSlice, const DataSlice &remoteSrcSlice, const DataSlice &remoteDstSlice);
298 :
299 : void SetRemoteRank(RankId remote)
300 : {
301 : remoteRank = remote;
302 : }
303 : void SetLink(const LinkData &l)
304 : {
305 : this->link = l;
306 : }
307 :
308 14 : RankId GetRemoteRank() const
309 : {
310 14 : return remoteRank;
311 : }
312 44 : const LinkData &GetLink() const
313 : {
314 44 : return link;
315 : }
316 8 : DmaMode GetDmaMode() const
317 : {
318 8 : return dmaMode;
319 : }
320 10 : const DataType &GetDataType() const
321 : {
322 10 : return dataType;
323 : }
324 10 : const ReduceOp &GetReduceOp() const
325 : {
326 10 : return reduceOp;
327 : }
328 18 : u32 Size() const
329 : {
330 18 : return localSlices.size();
331 : }
332 3 : const DataSlice &GetLocalSlice(u32 pos) const
333 : {
334 3 : if (pos >= localSlices.size()) {
335 0 : throw InvalidParamsException("pos is out of range of localSlices");
336 : }
337 3 : return localSlices[pos];
338 : }
339 1 : const DataSlice &GetRemoteSrcSlice(u32 pos) const
340 : {
341 1 : if (pos >= remoteSrcSlices.size()) {
342 0 : throw InvalidParamsException("pos is out of range of remoteSrcSlices");
343 : }
344 1 : return remoteSrcSlices[pos];
345 : }
346 2 : const DataSlice &GetRemoteDstSlice(u32 pos) const
347 : {
348 2 : if (pos >= remoteDstSlices.size()) {
349 0 : throw InvalidParamsException("pos is out of range of remoteDstSlices");
350 : }
351 2 : return remoteDstSlices[pos];
352 : }
353 :
354 : private:
355 : RankId remoteRank;
356 : LinkData link;
357 : vector<DataSlice> localSlices;
358 : vector<DataSlice> remoteSrcSlices;
359 : vector<DataSlice> remoteDstSlices;
360 : DataType dataType;
361 : ReduceOp reduceOp;
362 : DmaMode dmaMode{DmaMode::DEFAULT};
363 : };
364 :
365 : class PrimRecvReduce : public Primitive {
366 : public:
367 : PrimRecvReduce(RankId remoteRank, const LinkData &link, const DataSlice &remoteSlice,
368 : const DataSlice &localSrcSlice, const DataSlice &localDstSlice, const DataType &dataType,
369 9 : const ReduceOp &reduceOp, DmaMode dmaMode = DmaMode::DEFAULT);
370 :
371 : string Describe() const override;
372 : void Append(const DataSlice &remoteSlice, const DataSlice &localSrcSlice, const DataSlice &localDstSlice);
373 :
374 : void SetRemoteRank(RankId remote)
375 : {
376 : remoteRank = remote;
377 : }
378 : void SetLink(const LinkData &l)
379 : {
380 : this->link = l;
381 : }
382 :
383 11 : RankId GetRemoteRank() const
384 : {
385 11 : return remoteRank;
386 : }
387 40 : const LinkData &GetLink() const
388 : {
389 40 : return link;
390 : }
391 8 : DmaMode GetDmaMode() const
392 : {
393 8 : return dmaMode;
394 : }
395 18 : const DataType &GetDataType() const
396 : {
397 18 : return dataType;
398 : }
399 18 : const ReduceOp &GetReduceOp() const
400 : {
401 18 : return reduceOp;
402 : }
403 39 : u32 Size() const
404 : {
405 39 : return remoteSlices.size();
406 : }
407 8 : const DataSlice &GetRemoteSlice(u32 pos) const
408 : {
409 8 : if (pos >= remoteSlices.size()) {
410 0 : throw InvalidParamsException("pos is out of range of remoteSlices");
411 : }
412 8 : return remoteSlices[pos];
413 : }
414 8 : const DataSlice &GetLocalSrcSlice(u32 pos) const
415 : {
416 8 : if (pos >= localSrcSlices.size()) {
417 0 : throw InvalidParamsException("pos is out of range of localSrcSlices");
418 : }
419 8 : return localSrcSlices[pos];
420 : }
421 10 : const DataSlice &GetLocalDstSlice(u32 pos) const
422 : {
423 10 : if (pos >= localDstSlices.size()) {
424 0 : throw InvalidParamsException("pos is out of range of localDstSlices");
425 : }
426 10 : return localDstSlices[pos];
427 : }
428 :
429 : private:
430 : RankId remoteRank;
431 : LinkData link;
432 : vector<DataSlice> remoteSlices;
433 : vector<DataSlice> localSrcSlices;
434 : vector<DataSlice> localDstSlices;
435 : DataType dataType;
436 : ReduceOp reduceOp;
437 : DmaMode dmaMode{DmaMode::DEFAULT};
438 : };
439 :
440 : class PrimGroup : public Primitive {
441 : public:
442 26 : PrimGroup() : Primitive(PrimType::GROUP)
443 : {
444 26 : }
445 :
446 : using Iterator = BaseConstIterator<vector, unique_ptr<Primitive>>;
447 :
448 : string Describe() const override;
449 : void CheckValid() const;
450 : void Append(unique_ptr<Primitive> prim);
451 :
452 16 : Iterator Iter() const
453 : {
454 16 : return Iterator(prims);
455 : }
456 :
457 5 : u32 GetSize() const
458 : {
459 5 : return prims.size();
460 : }
461 :
462 : private:
463 : vector<unique_ptr<Primitive>> prims;
464 : };
465 : } // namespace Hccl
466 : #endif
|