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 "log.h"
12 :
13 : #include "ins_coll_alg_registry.h"
14 : #include "ins_recv_executor.h"
15 : #include "alg_data_trans_wrapper.h"
16 :
17 : using namespace std;
18 :
19 : namespace Hccl {
20 0 : InsRecvExecutor::InsRecvExecutor() : InsCollAlgBase()
21 : {
22 0 : }
23 :
24 0 : InsRecvExecutor::~InsRecvExecutor()
25 : {
26 0 : }
27 :
28 0 : HcclResult InsRecvExecutor::Orchestrate(const RankGraph *rankGraph,
29 : const CollAlgOperator &op,
30 : const CollAlgParams ¶ms,
31 : InsQuePtr insQue)
32 : {
33 0 : dmaMode_ = DmaMode::DEFAULT;
34 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Begin to Generate Instruction Queue for RECV.");
35 0 : CHK_RET(Init(op, params, insQue));
36 : // 从集合通信算子op中获得remote rank和data type/count相关信息
37 0 : RankId remoteRank = op.sendRecvRemoteRank;
38 0 : u32 dataElemSize = DATA_TYPE_SIZE_MAP.at(op.dataType);
39 0 : u64 totalDataSize = static_cast<u64>(dataElemSize) * op.dataCount;
40 0 : if (totalDataSize == 0) {
41 0 : HCCL_WARNING("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] totalDataSize is 0, do nothing.");
42 0 : return HcclResult::HCCL_SUCCESS;
43 : }
44 : // 判断是不是自发自收这种情况,若是,则什么都不做,直接返回
45 0 : if (myRank_ == remoteRank) {
46 0 : HCCL_WARNING("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Self send, Self recv, Do nothing");
47 0 : return HcclResult::HCCL_SUCCESS;
48 : }
49 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Other send, Self recv");
50 :
51 : // 从virtualTopo里面拿到 link,转成LinkData格式
52 0 : const std::vector<NetInstance::Path> recvPath = GetPathsFromRankGraph(rankGraph, myRank_, remoteRank);
53 0 : CHK_PRT_RET(recvPath.size() == 0,
54 : HCCL_ERROR("[InsCollAlgFactory] Unable to obtain valid link, srcRank [%d], dstRank [%d].", myRank_,
55 : remoteRank), HcclResult::HCCL_E_INTERNAL);
56 0 : LinkData recvLinkData(recvPath[0]);
57 0 : if (recvLinkData.GetType() == PortDeploymentType::P2P
58 0 : && recvLinkData.GetLinkProtocol() == LinkProtocol::PCIE) {
59 0 : dmaMode_ = DmaMode::GET;
60 : }
61 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Total transfer data size [%llu], Max scratch buffer size [%u].", totalDataSize, params.maxTmpMemSize);
62 :
63 : // 初始化循环参数
64 0 : u64 resDataSize = totalDataSize;
65 0 : u64 currentOffset = 0;
66 0 : u32 roundIdx = 0;
67 : // 模式判断
68 0 : if (opMode_ == OpMode::OFFLOAD) {
69 0 : HCCL_DEBUG("[InsCollAlgFactory] Rank[%d], Generating Instruction Queues in OFFLOAD Mode for HOST.", myRank_);
70 0 : u64 transferSize = resDataSize;
71 : // 根据本轮数据搬运量声明相关DataSlice--图模式一次搬运全部数据 从inputbuffer直接send到对面outputbuffer
72 0 : DataSlice outputBuffer(BufferType::OUTPUT, currentOffset, transferSize);
73 0 : DataSlice remoteInputBuffer(BufferType::INPUT, currentOffset, transferSize);
74 0 : SlicesList recvSlicesList({remoteInputBuffer}, {outputBuffer});
75 0 : DataInfo recvInfo(recvLinkData, recvSlicesList);
76 0 : CHK_RET(Recv(recvInfo, insQue, 0, true, dmaMode_));
77 0 : }else {
78 0 : HCCL_DEBUG("[InsCollAlgFactory] Rank[%d], Generating Instruction Queues in OPBASE Mode for HOST.", myRank_);
79 : // 当需要多轮搬运时,需保证一次数据的搬运量需为单个数据size的整数倍
80 0 : u64 maxRoundTransferSize = params.maxTmpMemSize - params.maxTmpMemSize % dataElemSize;
81 0 : while(resDataSize > 0) {
82 : // 判断本轮需搬运的数据量
83 0 : u64 transferSize = resDataSize>params.maxTmpMemSize?maxRoundTransferSize:resDataSize;
84 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Recv round [%u], transfer data size [%llu]", roundIdx, transferSize);
85 : // 根据本轮数据搬运量创建相关DataSlice
86 0 : DataSlice outputBuffer(BufferType::OUTPUT, currentOffset, transferSize);
87 0 : DataSlice scratchBuffer(BufferType::SCRATCH, 0, transferSize);
88 0 : DataSlice remoteScratchBuffer(BufferType::SCRATCH, 0, transferSize);
89 0 : SlicesList recvSlicesList({remoteScratchBuffer}, {scratchBuffer});
90 0 : DataInfo recvInfo(recvLinkData, recvSlicesList);
91 0 : CHK_RET(Recv(recvInfo, insQue, 0, true, dmaMode_));
92 : // local copy
93 0 : CHK_RET(LocalCopy(insQue, scratchBuffer, outputBuffer));
94 : // 更新循环参数
95 0 : currentOffset = currentOffset + transferSize;
96 0 : resDataSize = resDataSize - transferSize;
97 0 : roundIdx = roundIdx + 1;
98 0 : }
99 : }
100 0 : return HcclResult::HCCL_SUCCESS;
101 0 : }
102 :
103 0 : HcclResult InsRecvExecutor::CalcResOffload(const RankGraph *rankGraph,
104 : const u64 &dataSize,
105 : CollOffloadOpResReq &resReq)
106 : {
107 : (void)rankGraph;
108 : (void)dataSize;
109 0 : resReq.requiredScratchMemSize = 0; //图模式不用scratchmemory
110 0 : resReq.requiredSubQueNum = 0;
111 :
112 0 : return HcclResult::HCCL_SUCCESS;
113 : }
114 :
115 0 : HcclResult InsRecvExecutor::CalcRes(const RankGraph *rankGraph,
116 : CollAlgResReq &algResReq)
117 : {
118 0 : u32 linkNumBtwPeers = 1;
119 0 : algResReq.primQueueNum = 1;
120 0 : AlgTempResReq tempResReq;
121 0 : tempResReq.queNum = 1;
122 0 : tempResReq.streamNum = tempResReq.streamNum;
123 0 : if (static_cast<u32>(sendRecvRemoteRank_) > rankSize_ - 1) {
124 0 : HCCL_ERROR("[InsCollAlgFactory][InsRecvExecutor][CalcRes] Rank[%d] get dest[%d] is invalid", myRank_, sendRecvRemoteRank_);
125 0 : return HcclResult::HCCL_E_PARA;
126 : }
127 0 : tempResReq.links[sendRecvRemoteRank_] = linkNumBtwPeers;
128 0 : uint32_t linkNum = GetPathsFromRankGraph(rankGraph, myRank_, sendRecvRemoteRank_).size();
129 0 : if (linkNum == 0) {
130 0 : HCCL_ERROR("[InsCollAlgFactory][InsRecvExecutor][CalcRes] Rank[%d] get path num to dest[%d] is zero", myRank_, sendRecvRemoteRank_);
131 : }
132 0 : CHK_RET(CalcResLinks(myRank_, rankGraph, linkPriority_, tempResReq.links, algResReq.links));
133 0 : CHK_RET(CalcLinkInfo(myRank_, rankGraph, tempResReq.links, algResReq.levelRankPairs));
134 0 : return HcclResult::HCCL_SUCCESS;
135 0 : }
136 :
137 :
138 0 : HcclResult InsRecvExecutor::Orchestrate(const AlgTopoInfo &topoInfo,
139 : const CollAlgOperator &op,
140 : const CollAlgParams ¶ms,
141 : ConnectedLinkMgr *linkMgr,
142 : InsQuePtr insQue)
143 : {
144 0 : dmaMode_ = DmaMode::DEFAULT;
145 : (void)topoInfo;
146 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Begin to Generate Instruction Queue for RECV AICPU mode.");
147 0 : CHK_RET(Init(op, params, insQue));
148 : // 从集合通信算子op中获得remote rank和data type/count相关信息
149 0 : RankId remoteRank = op.sendRecvRemoteRank;
150 0 : u32 dataElemSize = DATA_TYPE_SIZE_MAP.at(op.dataType);
151 0 : u64 totalDataSize = static_cast<u64>(dataElemSize) * op.dataCount;
152 0 : if (totalDataSize == 0) {
153 0 : HCCL_WARNING("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] totalDataSize is 0, do nothing.");
154 0 : return HcclResult::HCCL_SUCCESS;
155 : }
156 : // 判断是不是自发自收这种情况,若是,则什么都不做,直接返回
157 0 : if (myRank_ == remoteRank) {
158 0 : HCCL_WARNING("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Self send, Self recv, Do nothing");
159 0 : return HcclResult::HCCL_SUCCESS;
160 : }
161 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Other send, Self recv.");
162 :
163 : // 从linkMgr里面拿到 linkData
164 0 : const vector<LinkData> recvPath = linkMgr->GetLinks(remoteRank);
165 0 : CHK_PRT_RET(recvPath.size() == 0,
166 : HCCL_ERROR("[InsCollAlgFactory] Unable to obtain valid link, srcRank [%d], dstRank [%d].", myRank_,
167 : remoteRank), HcclResult::HCCL_E_INTERNAL);
168 0 : LinkData recvLinkData(recvPath[0]);
169 0 : if (recvLinkData.GetType() == PortDeploymentType::P2P
170 0 : && recvLinkData.GetLinkProtocol() == LinkProtocol::PCIE) {
171 0 : dmaMode_ = DmaMode::GET;
172 : }
173 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Total transfer data size [%llu], Max scratch buffer size [%u].", totalDataSize, params.maxTmpMemSize);
174 :
175 : // 初始化循环参数
176 0 : u64 resDataSize = totalDataSize;
177 0 : u64 currentOffset = 0;
178 0 : u32 roundIdx = 0;
179 : // 模式判断
180 0 : if (opMode_ == OpMode::OFFLOAD) {
181 0 : u64 maxLoopOutputSize = 256 * 1024 * 1024; // 256m为一轮
182 0 : HCCL_DEBUG("[InsCollAlgFactory] Rank[%d], Generating Instruction Queues in OFFLOAD Mode for HOST.", myRank_);
183 0 : while(resDataSize > 0) {
184 0 : u64 transferSize = resDataSize > maxLoopOutputSize ? maxLoopOutputSize : resDataSize;
185 0 : DataSlice outputBuffer(BufferType::OUTPUT, currentOffset, transferSize);
186 0 : DataSlice remoteInputBuffer(BufferType::INPUT, currentOffset, transferSize);
187 0 : SlicesList recvSlicesList({remoteInputBuffer}, {outputBuffer});
188 0 : DataInfo recvInfo(recvLinkData, recvSlicesList);
189 0 : CHK_RET(Recv(recvInfo, insQue, 0, true, dmaMode_));
190 0 : currentOffset = currentOffset + transferSize;
191 0 : resDataSize = resDataSize - transferSize;
192 0 : roundIdx = roundIdx + 1;
193 0 : }
194 : } else {
195 0 : HCCL_DEBUG("[InsCollAlgFactory] Rank[%d], Generating Instruction Queues in OPBASE Mode for HOST.", myRank_);
196 : // 当需要多轮搬运时,需保证一次数据的搬运量需为单个数据size的整数倍
197 0 : u64 maxRoundTransferSize = params.maxTmpMemSize - params.maxTmpMemSize % dataElemSize;
198 0 : while(resDataSize > 0) {
199 : // 判断本轮需搬运的数据量
200 0 : u64 transferSize = resDataSize>params.maxTmpMemSize?maxRoundTransferSize:resDataSize;
201 0 : HCCL_DEBUG("[InsCollAlgFactory][InsRecvExecutor][Orchestrate] Recv round [%u], transfer data size [%llu]", roundIdx, transferSize);
202 : // 根据本轮数据搬运量创建相关DataSlice
203 0 : DataSlice outputBuffer(BufferType::OUTPUT, currentOffset, transferSize);
204 0 : DataSlice scratchBuffer(BufferType::SCRATCH, 0, transferSize);
205 0 : DataSlice remoteScratchBuffer(BufferType::SCRATCH, 0, transferSize);
206 :
207 0 : SlicesList recvSlicesList({remoteScratchBuffer}, {scratchBuffer});
208 0 : DataInfo recvInfo(recvLinkData, recvSlicesList);
209 0 : CHK_RET(Recv(recvInfo, insQue, 0, true, dmaMode_));
210 : // local copy
211 0 : CHK_RET(LocalCopy(insQue, scratchBuffer, outputBuffer));
212 : // 更新循环参数
213 0 : currentOffset = currentOffset + transferSize;
214 0 : resDataSize = resDataSize - transferSize;
215 0 : roundIdx = roundIdx + 1;
216 0 : }
217 : }
218 0 : return HcclResult::HCCL_SUCCESS;
219 0 : }
220 :
221 : // 注册
222 : INS_REGISTER_IMPL(OpType::RECV, InsRecv, InsRecvExecutor);
223 :
224 : } // namespace Hccl
|