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_CCU_CONTEXT_ALL_TO_ALL_V_MESH_2D_H_
12 : #define HCCLV2_CCU_CONTEXT_ALL_TO_ALL_V_MESH_2D_H_
13 :
14 : #include <vector>
15 :
16 : #include <ios>
17 : #include "log.h"
18 : #include "ccu_context_alg_base.h"
19 : #include "ccu_datatype.h"
20 : #include "ccu_instruction_all_to_all_v_mesh2d.h"
21 :
22 : namespace Hccl {
23 : // a2a 对每个对端的发送接收信息
24 : struct A2AVsingleSendRecvInfo {
25 : CcuRep::Variable sendOffset;
26 : CcuRep::Variable recvOffset;
27 : CcuRep::Variable sendTailSizeA; // 本rank给其他rank要发的尾块
28 : CcuRep::Variable sendTailSizeB; // 本rank给其他rank要发的尾块
29 : CcuRep::Variable sendTailSize;
30 : CcuRep::Variable recvTailSizeA; // 本rank从其他所有rank要收的数据
31 : CcuRep::Variable recvTailSizeB; // 本rank从其他所有rank要收的数据
32 : CcuRep::Variable sendLoopNum; // 本rank给其他所有rank要发的轮数
33 : CcuRep::Variable recvLoopNum; // 本rank从其他所有rank要收的数据
34 : };
35 :
36 : class CcuContextAllToAllVMesh2D : public CcuContextAlgBase {
37 : public:
38 : CcuContextAllToAllVMesh2D(
39 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group);
40 0 : ~CcuContextAllToAllVMesh2D() override {}
41 :
42 : void Algorithm() override;
43 : std::vector<uint64_t> GeneArgs(const CcuTaskArg& arg) override;
44 :
45 : protected:
46 : // a2a 对每个对端的发送接收信息
47 : struct A2AVsingleSendRecvInfo {
48 : CcuRep::Variable sendOffset;
49 : CcuRep::Variable recvOffset;
50 : CcuRep::Variable sendTailSizeA; // 本rank给其他所有rank要发的数据
51 : CcuRep::Variable sendTailSizeB; // 本rank给其他所有rank要发的数据
52 : GroupOpSize sendTailGoSizeA;
53 : GroupOpSize sendTailGoSizeB;
54 : CcuRep::Variable sendTailSize;
55 : CcuRep::Variable recvTailSizeA; // 本rank从其他所有rank要收的数据
56 : CcuRep::Variable recvTailSizeB; // 本rank从其他所有rank要收的数据
57 : CcuRep::Variable sendLoopNum; // 本rank给其他所有rank要发的轮数
58 : CcuRep::Variable recvLoopNum; // 本rank从其他所有rank要收的数据
59 : };
60 : void GenAddrVariables(
61 : std::vector<CcuRep::Variable>& input, std::vector<CcuRep::Variable>& output,
62 : std::vector<CcuRep::Variable>& token);
63 :
64 : void CalcGroupSrcDst(std::vector<CcuRep::Memory>& src, std::vector<CcuRep::Memory>& dst);
65 : void LoadAll2allSendRecvInfo(A2AVsingleSendRecvInfo sendRecvInfo);
66 :
67 : private:
68 : void InitResources();
69 : void LoadArgs();
70 : void ExchangeInfoAndSync();
71 : void RankSync(uint32_t signalIndex);
72 : void PostSync();
73 : void AxisSync(uint32_t signalIndex);
74 : void FirstStep();
75 : void SecondStep();
76 : void CalculateArgs();
77 : void DoAll2AllVMultiLoop();
78 : void UpdateLoopRecorder(uint16_t flag);
79 : uint32_t CalcDstRank(uint32_t sliceId, uint32_t peerId) const;
80 : uint32_t CalcTransIdx(uint32_t peerId) const;
81 : void GroupCopyToDstOutput(uint16_t sliceId, uint16_t peerId);
82 : void WriteToDstOutput(uint16_t sliceId, uint16_t peerId);
83 : void WriteToDstScratch(uint16_t sliceId, uint16_t peerId);
84 : void ReadFromSrc(uint16_t sliceId, uint16_t peerId);
85 : void CopyLoopNumRecorder();
86 :
87 : CcuRep::Variable input_;
88 : std::vector<CcuRep::Variable> output_;
89 : std::vector<CcuRep::Variable> scratch_;
90 : std::vector<CcuRep::Variable> token_;
91 : std::vector<std::vector<CcuRep::Variable>> sendLoopNumRecorder_; // 记录同轴的卡a要给另一个轴的卡b发多少轮
92 : std::vector<std::vector<CcuRep::Variable>> recvLoopNumRecorder_; // 记录同轴的卡a要从另一个轴的卡b收多少轮
93 : std::vector<std::vector<CcuRep::Variable>> LocSendLoopNumRecorder_; // 记录同轴的卡a要给另一个轴的卡b发多少轮
94 : std::vector<std::vector<CcuRep::Variable>> LocRecvLoopNumRecorder_; // 记录同轴的卡a要从另一个轴的卡b收多少轮
95 : std::vector<CcuRep::Variable> sendRecorder_;
96 :
97 : std::vector<CcuRep::Variable> isPostFlag_;
98 : CcuRep::Variable xnConst1_;
99 : CcuRep::Variable completedRankCount_;
100 : CcuRep::Variable xnHalfTransportSize_;
101 : CcuRep::Variable xnMaxTransportSize_;
102 : CcuRep::Variable curSendTailSize_;
103 : GroupOpSize xnHalfTransportGoSize_;
104 : GroupOpSize curSendTailGoSize_;
105 : std::vector<A2AVsingleSendRecvInfo> sendRecvInfo_;
106 : // srcOffset_,dstOffset_用于记录已经操作的数据量,主要影响在input和output上的偏移
107 : CcuRep::Variable srcOffset_;
108 : CcuRep::Variable dstOffset_;
109 : uint32_t axisId_{0};
110 : std::vector<uint32_t> dimSize_; // 每个维度的大小
111 : std::vector<uint32_t> dimId_; // 本rank所在行或列的编号
112 : uint32_t localId_{0}; // 本rank所在行或列的编号
113 : uint32_t localSize_{0}; // 本rank所在行或列的总rank数
114 : uint32_t anotherId_{0}; // 本rank在另一个轴上的Id
115 : uint32_t anotherSize_{0}; // 本rank所在另一个轴上的总rank数
116 :
117 : // 中间步骤用的地址寄存器
118 : std::vector<CcuRep::Memory> inputAddrs_;
119 : std::vector<CcuRep::Memory> bufferAddrs_;
120 : std::vector<CcuRep::Memory> outputAddrs_;
121 :
122 : // firstScratchBaseOffset_,secondScratchBaseOffset_,大小为scratchmem的一半
123 : CcuRep::Variable firstScratchBaseOffset_;
124 : CcuRep::Variable secondScratchBaseOffset_;
125 : // localRank要 往/从 remoteRank的scratchmem上 写/读
126 : // offset指向localRank第一次在remoteRank上操作的地址
127 : // step指的是localRank在remoteRank上的每次操作的步长
128 : CcuRep::Variable firstScratchSliceOffset_;
129 : CcuRep::Variable firstScratchSliceStep_;
130 : CcuRep::Variable secondScratchSliceOffset_;
131 : CcuRep::Variable secondScratchSliceStep_;
132 :
133 : // 计算参数用
134 : uint64_t scratchSliceBias{0}; // scratchmem一半的大小
135 : uint64_t scratchSliceSize{0}; // scratchmem上每一格的大小
136 : uint64_t firstScratchBaseOffset{0};
137 : uint64_t secondScratchBaseOffset{0};
138 : uint64_t firstScratchSliceOffset{0};
139 : uint64_t firstScratchSliceStep{0};
140 : uint64_t secondScratchSliceOffset{0};
141 : uint64_t secondScratchSliceStep{0};
142 :
143 : // 在本地的搬运完成标记
144 : std::vector<CcuRep::MaskSignal> firstSignal_;
145 : std::vector<CcuRep::MaskSignal> secondSignal_;
146 :
147 : // 跨轴同步信号
148 : std::string localAxisSignalName_;
149 : std::string anotherAxisSignalName_;
150 : CcuRep::MaskSignal localAxisSignal_;
151 : CcuRep::MaskSignal anotherAxisSignal_;
152 : };
153 : } // namespace Hccl
154 :
155 : #endif // HCCLV2_CCU_CONTEXT_ALL_TO_ALL_V_MESH_2D_H_
|