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 : #include "prim_rules.h"
12 : #include "not_support_exception.h"
13 : #include "dev_capability.h"
14 : namespace Hccl {
15 : constexpr u32 INSTRUCTION_PRI_LOCAL_POST_TO = 90;
16 : constexpr u32 INSTRUCTION_PRI_LOCAL_WAIT_FROM = 85;
17 : constexpr u32 INSTRUCTION_PRI_LOCAL_COPY = 100;
18 : constexpr u32 INSTRUCTION_PRI_LOCAL_REDUCE = 30;
19 : constexpr u32 INSTRUCTION_PRI_POST_READY = 80;
20 : constexpr u32 INSTRUCTION_PRI_WAIT_READY = 70;
21 : constexpr u32 INSTRUCTION_PRI_READ = 60;
22 : constexpr u32 INSTRUCTION_PRI_READ_REDUCE = 60;
23 : constexpr u32 INSTRUCTION_PRI_WRITE = 60;
24 : constexpr u32 INSTRUCTION_PRI_WRITE_REDUCE = 60;
25 : constexpr u32 INSTRUCTION_PRI_WRITE_WITH_FIN = 60;
26 : constexpr u32 INSTRUCTION_PRI_WRITE_REDUCE_WITH_FIN = 60;
27 : constexpr u32 INSTRUCTION_PRI_POST_FIN = 60;
28 : constexpr u32 INSTRUCTION_PRI_WAIT_FIN = 50;
29 : constexpr u32 INSTRUCTION_PRI_POST_FIN_ACK = 50;
30 : constexpr u32 INSTRUCTION_PRI_WAIT_FIN_ACK = 40;
31 :
32 : const std::map<InstructionType, u32> INSTRUCTION_PRI_MAP
33 : = {{InstructionType::LOCAL_COPY, INSTRUCTION_PRI_LOCAL_COPY},
34 : {InstructionType::LOCAL_REDUCE, INSTRUCTION_PRI_LOCAL_REDUCE},
35 : {InstructionType::LOCAL_POST_TO, INSTRUCTION_PRI_LOCAL_POST_TO},
36 : {InstructionType::LOCAL_WAIT_FROM, INSTRUCTION_PRI_LOCAL_WAIT_FROM},
37 : {InstructionType::POST_READY, INSTRUCTION_PRI_POST_READY},
38 : {InstructionType::WAIT_READY, INSTRUCTION_PRI_WAIT_READY},
39 : {InstructionType::POST_FIN, INSTRUCTION_PRI_POST_FIN},
40 : {InstructionType::WAIT_FIN, INSTRUCTION_PRI_WAIT_FIN},
41 : {InstructionType::POST_FIN_ACK, INSTRUCTION_PRI_POST_FIN_ACK},
42 : {InstructionType::WAIT_FIN_ACK, INSTRUCTION_PRI_WAIT_FIN_ACK},
43 : {InstructionType::READ, INSTRUCTION_PRI_READ},
44 : {InstructionType::READ_REDUCE, INSTRUCTION_PRI_READ_REDUCE},
45 : {InstructionType::WRITE, INSTRUCTION_PRI_WRITE},
46 : {InstructionType::WRITE_REDUCE, INSTRUCTION_PRI_WRITE_REDUCE},
47 : {InstructionType::WRITE_WITH_FIN, INSTRUCTION_PRI_WRITE_WITH_FIN},
48 : {InstructionType::WRITE_REDUCE_WITH_FIN, INSTRUCTION_PRI_WRITE_REDUCE_WITH_FIN}};
49 :
50 33 : inline void CheckLinkIsValid(const LinkData &link, const string &desc)
51 : {
52 : // only support P2P, dev_net+RDMA now
53 33 : if (link.GetType() == PortDeploymentType::P2P) {
54 22 : return;
55 11 : } else if (link.GetType() == PortDeploymentType::DEV_NET) {
56 10 : auto linkProtocol = link.GetLinkProtocol();
57 30 : HCCL_INFO("[CheckLinkIsValid] linkProtocol is[%s]", linkProtocol.Describe().c_str());
58 15 : if (linkProtocol == LinkProtocol::ROCE ||
59 5 : linkProtocol == LinkProtocol::UB_CTP ||
60 0 : linkProtocol == LinkProtocol::UB_TP ||
61 15 : linkProtocol == LinkProtocol::UBOE ||
62 0 : linkProtocol == LinkProtocol::UBG
63 : ) {
64 10 : return;
65 : }
66 : }
67 1 : string msg = StringFormat("type=%s is not support in %s", link.Describe().c_str(), desc.c_str());
68 1 : throw NotSupportException(msg);
69 1 : }
70 :
71 16 : inline bool IsSupportInlineReduce(const DataType &datatype, const ReduceOp &reduceOp, const LinkData &link)
72 : {
73 16 : bool isDataType = DevCapability::GetInstance().GetInlineReduceDataTypeMap().at(datatype);
74 16 : bool isReduceOp = DevCapability::GetInstance().GetInlineReduceOpMap().at(reduceOp);
75 :
76 16 : bool result = isDataType && isReduceOp;
77 16 : if (link.GetType() == PortDeploymentType::P2P) {
78 12 : return result;
79 : } else {
80 : // here is DevNet
81 4 : bool isSupportDevNetInlineReduce = DevCapability::GetInstance().IsSupportDevNetInlineReduce();
82 4 : return result && isSupportDevNetInlineReduce;
83 : }
84 : }
85 :
86 16 : inline void AppendInsPostFinAck(RankId remote, const LinkData &link, vector<unique_ptr<Instruction>> &instructions)
87 : {
88 16 : if (link.GetType() == PortDeploymentType::DEV_NET) {
89 5 : if (!DevCapability::GetInstance().IsSupportStarsPollNetCq()) {
90 0 : instructions.push_back(make_unique<InsPostFinAck>(remote, link));
91 : }
92 5 : return;
93 : }
94 11 : if (link.GetType() == PortDeploymentType::P2P) {
95 : // do nothing
96 11 : return;
97 : }
98 :
99 : // not support, throw exception
100 0 : string msg = StringFormat("link=%s does not need or not support AppendInsPostFinAck", link.Describe().c_str());
101 0 : THROW<NotSupportException>(msg);
102 0 : }
103 :
104 4 : inline vector<unique_ptr<Instruction>> PrimSendInReadMode(const PrimSend &send)
105 : {
106 4 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO);
107 4 : RankId remote = send.GetRemoteRank();
108 4 : const LinkData link = send.GetLink();
109 4 : u32 index = 0;
110 :
111 4 : instructions[index++] = make_unique<InsPostReady>(remote, link);
112 4 : instructions[index++] = make_unique<InsWaitFin>(remote, link);
113 4 : AppendInsPostFinAck(remote, link, instructions);
114 :
115 4 : return instructions;
116 0 : }
117 :
118 16 : inline void AppendInsWaitFinAck(RankId remote, const LinkData &link, vector<unique_ptr<Instruction>> &instructions)
119 : {
120 16 : if (link.GetType() == PortDeploymentType::DEV_NET) {
121 5 : if (!DevCapability::GetInstance().IsSupportStarsPollNetCq()) {
122 0 : instructions.push_back(make_unique<InsWaitFinAck>(remote, link));
123 : }
124 5 : return;
125 : }
126 11 : if (link.GetType() == PortDeploymentType::P2P) {
127 : // do nothing
128 11 : return;
129 : }
130 : // not support, throw exception
131 0 : string msg = StringFormat("link=%s does not need or not support AppendInsWaitFinAck", link.Describe().c_str());
132 0 : THROW<NotSupportException>(msg);
133 0 : }
134 :
135 3 : inline vector<unique_ptr<Instruction>> PrimSendInWriteWithNotifyMode(const PrimSend &send)
136 : {
137 3 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE + send.Size());
138 3 : RankId remote = send.GetRemoteRank();
139 3 : const LinkData link = send.GetLink();
140 3 : u32 index = 0;
141 :
142 3 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
143 3 : for (u32 pos = 0; pos < send.Size() - 1; pos++) {
144 0 : instructions[index++] = make_unique<InsWrite>(remote, link, send.GetLocalSlice(pos), send.GetRemoteSlice(pos));
145 : }
146 6 : instructions[index++] = make_unique<InsWriteWithFin>(remote, link, send.GetLocalSlice(send.Size() - 1),
147 6 : send.GetRemoteSlice(send.Size() - 1), NotifyType::NORMAL);
148 3 : AppendInsWaitFinAck(remote, link, instructions);
149 :
150 3 : return instructions;
151 0 : }
152 :
153 1 : inline vector<unique_ptr<Instruction>> PrimSendInNormalWriteMode(const PrimSend &send)
154 : {
155 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO + send.Size());
156 1 : RankId remote = send.GetRemoteRank();
157 1 : const LinkData link = send.GetLink();
158 1 : u32 index = 0;
159 :
160 1 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
161 3 : for (u32 pos = 0; pos < send.Size(); pos++) {
162 2 : instructions[index++] = make_unique<InsWrite>(remote, link, send.GetLocalSlice(pos), send.GetRemoteSlice(pos));
163 : }
164 1 : instructions[index++] = make_unique<InsPostFin>(remote, link);
165 1 : AppendInsWaitFinAck(remote, link, instructions);
166 :
167 1 : return instructions;
168 0 : }
169 :
170 4 : inline vector<unique_ptr<Instruction>> PrimSendInWriteMode(const PrimSend &send)
171 : {
172 4 : if (send.GetLink().GetType() == PortDeploymentType::P2P) {
173 1 : return PrimSendInNormalWriteMode(send);
174 3 : } else if (send.GetLink().GetType() == PortDeploymentType::DEV_NET) {
175 3 : if (DevCapability::GetInstance().IsSupportWriteWithNotify()) {
176 3 : return PrimSendInWriteWithNotifyMode(send);
177 : }
178 0 : return PrimSendInNormalWriteMode(send);
179 : }
180 : // not support, throw exception
181 0 : string msg = StringFormat("link=%s does not support PrimSendInWriteMode", send.GetLink().Describe().c_str());
182 0 : MACRO_THROW(NotSupportException, msg);
183 0 : }
184 :
185 4 : inline vector<unique_ptr<Instruction>> PrimRecvInReadMode(const PrimRecv &recv)
186 : {
187 4 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO + recv.Size());
188 4 : RankId remote = recv.GetRemoteRank();
189 4 : const LinkData link = recv.GetLink();
190 4 : u32 index = 0;
191 :
192 4 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
193 9 : for (u32 pos = 0; pos < recv.Size(); pos++) {
194 5 : instructions[index++] = make_unique<InsRead>(remote, link, recv.GetLocalSlice(pos), recv.GetRemoteSlice(pos));
195 : }
196 4 : instructions[index++] = make_unique<InsPostFin>(remote, link);
197 4 : AppendInsWaitFinAck(remote, link, instructions);
198 :
199 4 : return instructions;
200 0 : }
201 :
202 4 : inline vector<unique_ptr<Instruction>> PrimRecvInWriteMode(const PrimRecv &recv)
203 : {
204 4 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO);
205 4 : RankId remote = recv.GetRemoteRank();
206 4 : const LinkData link = recv.GetLink();
207 4 : u32 index = 0;
208 :
209 4 : instructions[index++] = make_unique<InsPostReady>(remote, link);
210 4 : instructions[index++] = make_unique<InsWaitFin>(remote, link);
211 4 : AppendInsPostFinAck(remote, link, instructions);
212 :
213 4 : return instructions;
214 0 : }
215 :
216 4 : inline vector<unique_ptr<Instruction>> PrimSendReduceInReadModeWithInlineReduce(const PrimSendReduce &sendReduce)
217 : {
218 4 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO);
219 4 : RankId remote = sendReduce.GetRemoteRank();
220 4 : const LinkData link = sendReduce.GetLink();
221 4 : u32 index = 0;
222 :
223 4 : instructions[index++] = make_unique<InsPostReady>(remote, link);
224 4 : instructions[index++] = make_unique<InsWaitFin>(remote, link);
225 4 : AppendInsPostFinAck(remote, link, instructions);
226 :
227 4 : return instructions;
228 0 : }
229 :
230 1 : inline vector<unique_ptr<Instruction>> PrimSendReduceInWriteWithNotifyModeWithInlineReduce(
231 : const PrimSendReduce &sendReduce)
232 : {
233 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE + sendReduce.Size());
234 1 : RankId remote = sendReduce.GetRemoteRank();
235 1 : const LinkData link = sendReduce.GetLink();
236 1 : u32 index = 0;
237 :
238 1 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
239 2 : for (u32 pos = 0; pos < sendReduce.Size() - 1; pos++) {
240 2 : instructions[index++] = make_unique<InsWriteReduce>(remote, link, sendReduce.GetLocalSlice(pos),
241 : sendReduce.GetRemoteDstSlice(pos), sendReduce.GetDataType(),
242 1 : sendReduce.GetReduceOp());
243 : }
244 4 : instructions[index++] = make_unique<InsWriteReduceWithFin>(
245 1 : remote, link, sendReduce.GetLocalSlice(sendReduce.Size() - 1),
246 1 : sendReduce.GetRemoteDstSlice(sendReduce.Size() - 1), sendReduce.GetDataType(), sendReduce.GetReduceOp(),
247 2 : NotifyType::NORMAL);
248 1 : AppendInsWaitFinAck(remote, link, instructions);
249 :
250 1 : return instructions;
251 0 : }
252 :
253 0 : inline vector<unique_ptr<Instruction>> PrimSendReduceInNormalWriteModeWithInlineReduce(const PrimSendReduce &sendReduce)
254 : {
255 0 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO + sendReduce.Size());
256 0 : RankId remote = sendReduce.GetRemoteRank();
257 0 : const LinkData link = sendReduce.GetLink();
258 0 : u32 index = 0;
259 :
260 0 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
261 0 : for (u32 pos = 0; pos < sendReduce.Size(); pos++) {
262 0 : instructions[index++] = make_unique<InsWriteReduce>(remote, link, sendReduce.GetLocalSlice(pos),
263 : sendReduce.GetRemoteDstSlice(pos), sendReduce.GetDataType(),
264 0 : sendReduce.GetReduceOp());
265 : }
266 0 : instructions[index++] = make_unique<InsPostFin>(remote, link);
267 0 : AppendInsWaitFinAck(remote, link, instructions);
268 :
269 0 : return instructions;
270 0 : }
271 :
272 1 : inline vector<unique_ptr<Instruction>> PrimSendReduceInWriteModeWithInlineReduce(const PrimSendReduce &sendReduce)
273 : {
274 1 : if (sendReduce.GetLink().GetType() == PortDeploymentType::P2P) {
275 0 : return PrimSendReduceInNormalWriteModeWithInlineReduce(sendReduce);
276 1 : } else if (sendReduce.GetLink().GetType() == PortDeploymentType::DEV_NET) {
277 1 : if (DevCapability::GetInstance().IsSupportWriteWithNotify()) {
278 1 : return PrimSendReduceInWriteWithNotifyModeWithInlineReduce(sendReduce);
279 : }
280 0 : return PrimSendReduceInNormalWriteModeWithInlineReduce(sendReduce);
281 : }
282 :
283 : string msg = StringFormat("link=%s does not support PrimSendReduceInWriteModeWithInlineReduce",
284 0 : sendReduce.GetLink().Describe().c_str());
285 0 : MACRO_THROW(NotSupportException, msg);
286 0 : }
287 :
288 2 : inline vector<unique_ptr<Instruction>> PrimSendReduceInReadModeWithoutInlineReduce(const PrimSendReduce &sendReduce)
289 : {
290 2 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO);
291 2 : RankId remote = sendReduce.GetRemoteRank();
292 2 : const LinkData link = sendReduce.GetLink();
293 2 : u32 index = 0;
294 :
295 2 : instructions[index++] = make_unique<InsPostReady>(remote, link);
296 2 : instructions[index++] = make_unique<InsWaitFin>(remote, link);
297 2 : AppendInsPostFinAck(remote, link, instructions);
298 :
299 2 : return instructions;
300 0 : }
301 :
302 1 : inline vector<unique_ptr<Instruction>> PrimSendReduceInWriteModeWithoutInlineReduce(const PrimSendReduce &sendReduce)
303 : {
304 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO + sendReduce.Size());
305 1 : RankId remote = sendReduce.GetRemoteRank();
306 1 : const LinkData link = sendReduce.GetLink();
307 1 : u32 index = 0;
308 :
309 1 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
310 2 : for (u32 pos = 0; pos < sendReduce.Size(); pos++) {
311 1 : instructions[index++]
312 2 : = make_unique<InsWrite>(remote, link, sendReduce.GetLocalSlice(pos), sendReduce.GetRemoteSrcSlice(pos));
313 : }
314 1 : instructions[index++] = make_unique<InsPostFin>(remote, link);
315 1 : AppendInsWaitFinAck(remote, link, instructions);
316 :
317 1 : return instructions;
318 0 : }
319 :
320 4 : inline vector<unique_ptr<Instruction>> PrimRecvReduceInReadModeWithInlineReduce(const PrimRecvReduce &recvReduce)
321 : {
322 4 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO + recvReduce.Size());
323 4 : RankId remote = recvReduce.GetRemoteRank();
324 4 : const LinkData link = recvReduce.GetLink();
325 4 : u32 index = 0;
326 :
327 4 : instructions[index++] = make_unique<InsWaitReady>(remote, link);
328 9 : for (u32 pos = 0; pos < recvReduce.Size(); pos++) {
329 5 : instructions[index++]
330 10 : = make_unique<InsReadReduce>(remote, link, recvReduce.GetLocalDstSlice(pos), recvReduce.GetRemoteSlice(pos),
331 5 : recvReduce.GetDataType(), recvReduce.GetReduceOp());
332 : }
333 4 : instructions[index++] = make_unique<InsPostFin>(remote, link);
334 4 : AppendInsWaitFinAck(remote, link, instructions);
335 :
336 4 : return instructions;
337 0 : }
338 :
339 1 : inline vector<unique_ptr<Instruction>> PrimRecvReduceInWriteModeWithInlineReduce(const PrimRecvReduce &recvReduce)
340 : {
341 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::TWO);
342 1 : RankId remote = recvReduce.GetRemoteRank();
343 1 : const LinkData link = recvReduce.GetLink();
344 1 : u32 index = 0;
345 :
346 1 : instructions[index++] = make_unique<InsPostReady>(remote, link);
347 1 : instructions[index++] = make_unique<InsWaitFin>(remote, link);
348 1 : AppendInsPostFinAck(remote, link, instructions);
349 :
350 1 : return instructions;
351 0 : }
352 :
353 2 : inline vector<unique_ptr<Instruction>> PrimRecvReduceInReadModeWithoutInlineReduce(const PrimRecvReduce &recvReduce)
354 : {
355 2 : vector<unique_ptr<Instruction>> instructions(0);
356 2 : RankId remote = recvReduce.GetRemoteRank();
357 2 : const LinkData link = recvReduce.GetLink();
358 :
359 2 : instructions.push_back(make_unique<InsWaitReady>(remote, link));
360 :
361 5 : for (u32 pos = 0; pos < recvReduce.Size(); pos++) {
362 3 : instructions.push_back(
363 6 : make_unique<InsRead>(remote, link, recvReduce.GetLocalSrcSlice(pos), recvReduce.GetRemoteSlice(pos)));
364 : }
365 :
366 2 : instructions.push_back(make_unique<InsPostFin>(remote, link));
367 2 : AppendInsWaitFinAck(remote, link, instructions);
368 :
369 5 : for (u32 pos = 0; pos < recvReduce.Size(); pos++) {
370 3 : instructions.push_back(make_unique<InsLocalReduce>(recvReduce.GetLocalSrcSlice(pos),
371 : recvReduce.GetLocalDstSlice(pos), recvReduce.GetDataType(),
372 : recvReduce.GetReduceOp()));
373 : }
374 :
375 2 : return instructions;
376 0 : }
377 :
378 1 : inline vector<unique_ptr<Instruction>> PrimRecvReduceInWriteModeWithoutInlineReduce(const PrimRecvReduce &recvReduce)
379 : {
380 1 : vector<unique_ptr<Instruction>> instructions(0);
381 1 : RankId remote = recvReduce.GetRemoteRank();
382 1 : const LinkData link = recvReduce.GetLink();
383 :
384 1 : instructions.push_back(make_unique<InsPostReady>(remote, link));
385 1 : instructions.push_back(make_unique<InsWaitFin>(remote, link));
386 1 : AppendInsPostFinAck(remote, link, instructions);
387 :
388 3 : for (u32 pos = 0; pos < recvReduce.Size(); pos++) {
389 2 : instructions.push_back(make_unique<InsLocalReduce>(recvReduce.GetLocalSrcSlice(pos),
390 : recvReduce.GetLocalDstSlice(pos), recvReduce.GetDataType(),
391 : recvReduce.GetReduceOp()));
392 : }
393 1 : return instructions;
394 0 : }
395 :
396 1 : vector<unique_ptr<Instruction>> Translate(const PrimPostTo &postTo)
397 : {
398 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE);
399 1 : u32 waitQid = postTo.GetQid();
400 :
401 1 : instructions[InsArrayIndex::ZERO]
402 2 : = make_unique<InsLocalPostTo>(waitQid, postTo.GetNotifyType(), postTo.GetTopicId());
403 1 : return instructions;
404 0 : }
405 :
406 1 : vector<unique_ptr<Instruction>> Translate(const PrimWaitFrom &waitFrom)
407 : {
408 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE);
409 1 : u32 postQid = waitFrom.GetQid();
410 :
411 1 : instructions[InsArrayIndex::ZERO] = make_unique<InsLocalWaitFrom>(postQid, NotifyType::NORMAL, waitFrom.GetTopicId());
412 1 : return instructions;
413 0 : }
414 :
415 1 : vector<unique_ptr<Instruction>> Translate(const PrimWaitGroup &waitGroup)
416 : {
417 1 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE);
418 1 : auto insLocalWaitGroup = make_unique<InsLocalWaitGroup>(waitGroup.GetTopicId());
419 2 : for (auto iter = waitGroup.Iter(); iter.HasNext(); ++iter) {
420 1 : insLocalWaitGroup->Append(*iter);
421 : }
422 1 : instructions[InsArrayIndex::ZERO] = std::move(insLocalWaitGroup);
423 :
424 1 : return instructions;
425 1 : }
426 :
427 0 : vector<unique_ptr<Instruction>> Translate(const PrimLocalReduce &localReduce)
428 : {
429 0 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE);
430 0 : instructions[InsArrayIndex::ZERO] = make_unique<InsLocalReduce>(
431 0 : localReduce.GetSrcSlice(), localReduce.GetDstSlice(), localReduce.GetDataType(), localReduce.GetReduceOp());
432 0 : return instructions;
433 0 : }
434 :
435 5 : vector<unique_ptr<Instruction>> Translate(const PrimLocalCopy &localCopy)
436 : {
437 5 : vector<unique_ptr<Instruction>> instructions(InsArraySize::ONE);
438 :
439 5 : instructions[InsArrayIndex::ZERO] = make_unique<InsLocalCopy>(localCopy.GetSrcSlice(), localCopy.GetDstSlice());
440 5 : return instructions;
441 0 : }
442 :
443 9 : vector<unique_ptr<Instruction>> Translate(const PrimSend &send)
444 : {
445 9 : if (send.Size() == 0) {
446 0 : vector<unique_ptr<Instruction>> instructions(0);
447 0 : return instructions;
448 0 : }
449 10 : CheckLinkIsValid(send.GetLink(), send.Describe());
450 8 : auto dmaMode = send.GetDmaMode();
451 8 : if (dmaMode == DmaMode::PUT) {
452 2 : return PrimSendInWriteMode(send);
453 6 : } else if (dmaMode == DmaMode::GET) {
454 2 : return PrimSendInReadMode(send);
455 : } else {
456 4 : if (send.GetLink().GetType() == PortDeploymentType::P2P) {
457 2 : return PrimSendInReadMode(send);
458 : } else {
459 2 : return PrimSendInWriteMode(send);
460 : }
461 : }
462 : }
463 :
464 8 : vector<unique_ptr<Instruction>> Translate(const PrimRecv &recv)
465 : {
466 8 : if (recv.Size() == 0) {
467 0 : vector<unique_ptr<Instruction>> instructions(0);
468 0 : return instructions;
469 0 : }
470 8 : CheckLinkIsValid(recv.GetLink(), recv.Describe());
471 8 : auto dmaMode = recv.GetDmaMode();
472 8 : if (dmaMode == DmaMode::PUT) {
473 2 : return PrimRecvInWriteMode(recv);
474 6 : } else if (dmaMode == DmaMode::GET) {
475 2 : return PrimRecvInReadMode(recv);
476 : } else {
477 4 : if (recv.GetLink().GetType() == PortDeploymentType::P2P) {
478 2 : return PrimRecvInReadMode(recv);
479 : } else {
480 2 : return PrimRecvInWriteMode(recv);
481 : }
482 : }
483 : }
484 :
485 5 : vector<unique_ptr<Instruction>> TranslateWithInlineReduce(const PrimSendReduce &sendReduce)
486 : {
487 5 : auto dmaMode = sendReduce.GetDmaMode();
488 5 : if (dmaMode == DmaMode::PUT) {
489 0 : return PrimSendReduceInWriteModeWithInlineReduce(sendReduce);
490 5 : } else if (dmaMode == DmaMode::GET) {
491 2 : return PrimSendReduceInReadModeWithInlineReduce(sendReduce);
492 : } else {
493 3 : if (sendReduce.GetLink().GetType() == PortDeploymentType::P2P) {
494 2 : return PrimSendReduceInReadModeWithInlineReduce(sendReduce);
495 : } else {
496 1 : return PrimSendReduceInWriteModeWithInlineReduce(sendReduce);
497 : }
498 : }
499 : }
500 :
501 3 : vector<unique_ptr<Instruction>> TranslateWithoutInlineReduce(const PrimSendReduce &sendReduce)
502 : {
503 3 : auto dmaMode = sendReduce.GetDmaMode();
504 3 : if (dmaMode == DmaMode::PUT) {
505 0 : return PrimSendReduceInWriteModeWithoutInlineReduce(sendReduce);
506 3 : } else if (dmaMode == DmaMode::GET) {
507 1 : return PrimSendReduceInReadModeWithoutInlineReduce(sendReduce);
508 : } else {
509 2 : if (sendReduce.GetLink().GetType() == PortDeploymentType::P2P) {
510 1 : return PrimSendReduceInReadModeWithoutInlineReduce(sendReduce);
511 : } else {
512 1 : return PrimSendReduceInWriteModeWithoutInlineReduce(sendReduce);
513 : }
514 : }
515 : }
516 :
517 8 : vector<unique_ptr<Instruction>> Translate(const PrimSendReduce &sendReduce)
518 : {
519 8 : if (sendReduce.Size() == 0) {
520 0 : vector<unique_ptr<Instruction>> instructions(0);
521 0 : return instructions;
522 0 : }
523 8 : CheckLinkIsValid(sendReduce.GetLink(), sendReduce.Describe());
524 8 : if (IsSupportInlineReduce(sendReduce.GetDataType(), sendReduce.GetReduceOp(), sendReduce.GetLink())) {
525 5 : return TranslateWithInlineReduce(sendReduce);
526 : } else {
527 3 : return TranslateWithoutInlineReduce(sendReduce);
528 : }
529 : }
530 :
531 5 : vector<unique_ptr<Instruction>> TranslateWithInlineReduce(const PrimRecvReduce &recvReduce)
532 : {
533 5 : auto dmaMode = recvReduce.GetDmaMode();
534 5 : if (dmaMode == DmaMode::PUT) {
535 0 : return PrimRecvReduceInWriteModeWithInlineReduce(recvReduce);
536 5 : } else if (dmaMode == DmaMode::GET) {
537 2 : return PrimRecvReduceInReadModeWithInlineReduce(recvReduce);
538 : } else {
539 3 : if (recvReduce.GetLink().GetType() == PortDeploymentType::P2P) {
540 2 : return PrimRecvReduceInReadModeWithInlineReduce(recvReduce);
541 : } else {
542 1 : return PrimRecvReduceInWriteModeWithInlineReduce(recvReduce);
543 : }
544 : }
545 : }
546 :
547 3 : vector<unique_ptr<Instruction>> TranslateWithoutInlineReduce(const PrimRecvReduce &recvReduce)
548 : {
549 3 : auto dmaMode = recvReduce.GetDmaMode();
550 3 : if (dmaMode == DmaMode::PUT) {
551 0 : return PrimRecvReduceInWriteModeWithoutInlineReduce(recvReduce);
552 3 : } else if (dmaMode == DmaMode::GET) {
553 0 : return PrimRecvReduceInReadModeWithoutInlineReduce(recvReduce);
554 : } else {
555 3 : if (recvReduce.GetLink().GetType() == PortDeploymentType::P2P) {
556 2 : return PrimRecvReduceInReadModeWithoutInlineReduce(recvReduce);
557 : } else {
558 1 : return PrimRecvReduceInWriteModeWithoutInlineReduce(recvReduce);
559 : }
560 : }
561 : }
562 :
563 8 : vector<unique_ptr<Instruction>> Translate(const PrimRecvReduce &recvReduce)
564 : {
565 8 : if (recvReduce.Size() == 0) {
566 0 : vector<unique_ptr<Instruction>> instructions(0);
567 0 : return instructions;
568 0 : }
569 8 : CheckLinkIsValid(recvReduce.GetLink(), recvReduce.Describe());
570 8 : if (IsSupportInlineReduce(recvReduce.GetDataType(), recvReduce.GetReduceOp(), recvReduce.GetLink())) {
571 5 : return TranslateWithInlineReduce(recvReduce);
572 : } else {
573 3 : return TranslateWithoutInlineReduce(recvReduce);
574 : }
575 : }
576 :
577 49 : bool CompareInsRule(pair<unique_ptr<Instruction>, int> &insA, pair<unique_ptr<Instruction>, int> &insB)
578 : {
579 49 : if (INSTRUCTION_PRI_MAP.at(insA.first->GetType()) == INSTRUCTION_PRI_MAP.at(insB.first->GetType())) {
580 4 : return insA.second < insB.second;
581 : } else {
582 45 : return INSTRUCTION_PRI_MAP.at(insA.first->GetType()) > INSTRUCTION_PRI_MAP.at(insB.first->GetType());
583 : }
584 : }
585 :
586 5 : vector<unique_ptr<Instruction>> GenerateTempInstruction(const PrimGroup &group)
587 : {
588 5 : vector<unique_ptr<Instruction>> instructions;
589 5 : vector<unique_ptr<Instruction>> generateVec;
590 5 : group.CheckValid();
591 15 : for (auto iter = group.Iter(); iter.HasNext(); ++iter) {
592 10 : if (iter->GetType() == PrimType::SEND) {
593 3 : generateVec = Translate(static_cast<const PrimSend &>(*iter));
594 3 : instructions.insert(instructions.end(), make_move_iterator(generateVec.begin()),
595 : make_move_iterator(generateVec.end()));
596 7 : } else if (iter->GetType() == PrimType::RECV) {
597 3 : generateVec = Translate(static_cast<const PrimRecv &>(*iter));
598 3 : instructions.insert(instructions.end(), make_move_iterator(generateVec.begin()),
599 : make_move_iterator(generateVec.end()));
600 4 : } else if (iter->GetType() == PrimType::SEND_REDUCE) {
601 2 : generateVec = Translate(static_cast<const PrimSendReduce &>(*iter));
602 2 : instructions.insert(instructions.end(), make_move_iterator(generateVec.begin()),
603 : make_move_iterator(generateVec.end()));
604 2 : } else if (iter->GetType() == PrimType::RECV_REDUCE) {
605 2 : generateVec = Translate(static_cast<const PrimRecvReduce &>(*iter));
606 2 : instructions.insert(instructions.end(), make_move_iterator(generateVec.begin()),
607 : make_move_iterator(generateVec.end()));
608 : }
609 5 : }
610 5 : return instructions;
611 5 : }
612 :
613 5 : vector<unique_ptr<Instruction>> Translate(const PrimGroup &group)
614 : {
615 5 : vector<unique_ptr<Instruction>> tempInstruction = GenerateTempInstruction(group);
616 5 : vector<pair<unique_ptr<Instruction>, int>> pairInstructions;
617 29 : for (size_t i = 0; i < tempInstruction.size(); i++) {
618 24 : pairInstructions.emplace_back(std::move(tempInstruction[i]), i);
619 : }
620 5 : sort(pairInstructions.begin(), pairInstructions.end(), CompareInsRule);
621 5 : vector<unique_ptr<Instruction>> instructions;
622 29 : for (auto &pairInstruction : pairInstructions) {
623 24 : instructions.push_back(std::move(pairInstruction.first));
624 : }
625 5 : return instructions;
626 5 : }
627 : } // namespace Hccl
|