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