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