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 "coll_all_gather_v_mesh_opbase_pipeline_executor.h"
12 :
13 : namespace hccl {
14 0 : CollAllGatherVMeshOpbasePipelineExecutor::CollAllGatherVMeshOpbasePipelineExecutor(
15 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
16 0 : : CollAllGatherVExecutor(dispatcher, topoMatcher)
17 : {
18 0 : DMAReduceFlag_ = true;
19 0 : }
20 :
21 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcStreamNum(u32& streamNum)
22 : {
23 0 : u32 totalStreamNum = topoAttr_.deviceNumPerAggregation + 1U;
24 0 : streamNum = totalStreamNum - 1U;
25 0 : HCCL_INFO(
26 : "[CollAllGatherVMeshOpbasePipelineExecutor][CalcStreamNum] tag[%s] streamNum[%u]", tag_.c_str(), streamNum);
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcCurCountsAndCurDispls(
31 : const u64 maxTotalCount, std::vector<u64>& countsLeft, std::vector<u64>& displs, std::vector<u64>& curCounts,
32 : std::vector<u64>& curDispls, bool& finished)
33 : {
34 0 : finished = true;
35 :
36 0 : curCounts.resize(countsLeft.size(), 0);
37 0 : curDispls.resize(displs.size(), 0);
38 :
39 : // 先设置本轮的displacements,等于入参displs
40 0 : std::copy(displs.begin(), displs.end(), curDispls.begin());
41 :
42 : // 分配好每个rank的counts
43 0 : for (auto i = 0U; i < countsLeft.size(); ++i) {
44 0 : const auto curCount = countsLeft[i] < maxTotalCount ? countsLeft[i] : maxTotalCount;
45 0 : curCounts[i] = curCount;
46 0 : countsLeft[i] -= curCount;
47 0 : displs[i] += curCount;
48 :
49 0 : if (countsLeft[i] != 0) {
50 0 : finished = false;
51 : }
52 : }
53 :
54 0 : return HCCL_SUCCESS;
55 : }
56 :
57 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
58 : {
59 0 : TransportMemType inputType = TransportMemType::RESERVED;
60 0 : TransportMemType outputType = TransportMemType::RESERVED;
61 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
62 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
63 0 : CHK_RET(CalcLevel1CommInfo(inputType, outputType, opTransport));
64 0 : return HCCL_SUCCESS;
65 : }
66 :
67 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcTransportMemType(
68 : TransportMemType& inputType, TransportMemType& outputType)
69 : {
70 0 : inputType = TransportMemType::CCL_INPUT;
71 0 : outputType = TransportMemType::CCL_OUTPUT;
72 0 : HCCL_INFO(
73 : "[CollAllGatherVMeshOpbasePipelineExecutor][CalcTransportMemType]"
74 : "tag[%s] inputType[%d], outputType[%d]",
75 : tag_.c_str(), inputType, outputType);
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcLevel0CommInfo(
80 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
81 : {
82 0 : CommParaInfo commParaInfo(COMM_LEVEL0, CommType::COMM_TAG_MESH);
83 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL0], inputType, outputType));
84 0 : return HCCL_SUCCESS;
85 0 : }
86 :
87 : // PipeLine模式下使用Ring算法
88 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::CalcLevel1CommInfo(
89 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
90 : {
91 0 : CommParaInfo commParaInfo(COMM_LEVEL1, CommType::COMM_TAG_RING_INNER);
92 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL1], inputType, outputType));
93 0 : return HCCL_SUCCESS;
94 0 : }
95 :
96 0 : u64 CollAllGatherVMeshOpbasePipelineExecutor::CalcLoopMaxCount(const u64 cclBuffSize, const u32 unitSize)
97 : {
98 0 : u64 maxCountPerLoop
99 0 : = (cclBuffSize - HCCL_MIN_SLICE_ALIGN_910B) / HCCL_MIN_SLICE_ALIGN * HCCL_MIN_SLICE_ALIGN / unitSize;
100 0 : return maxCountPerLoop;
101 : }
102 :
103 0 : bool CollAllGatherVMeshOpbasePipelineExecutor::IsHugeData(const u64 curSize)
104 : {
105 0 : bool hugeData = curSize > RDMA_SEND_MAX_SIZE || curSize > SDMA_SEND_MAX_SIZE;
106 0 : return hugeData;
107 : }
108 :
109 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
110 : {
111 0 : HCCL_CONFIG_INFO(
112 : HCCL_ALG, "[CollAllGatherVMeshOpbasePipelineExecutor][KernelRun]AllGatherVMeshOpbasePipelineExecutor begins.");
113 :
114 : // step 1 先获取 comm level0 \ comm level1 的value
115 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
116 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
117 0 : u32 commIndex = level0CommInfo.localRank;
118 0 : CHK_RET(CheckCommSize(COMM_LEVEL1, commIndex + 1));
119 0 : SubCommInfo level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
120 :
121 : // allgatherv 计算slice,数据分成ranksize份,每份的起始偏移和大小
122 0 : u32 perDataSize = SIZE_TABLE[param.VDataDes.dataType];
123 0 : std::vector<Slice> outputSlices;
124 0 : const auto counts = static_cast<u64*>(param.VDataDes.counts);
125 0 : const auto displs = static_cast<u64*>(param.VDataDes.displs);
126 0 : for (u32 rank = 0; rank < topoAttr_.userRankSize; ++rank) {
127 0 : Slice userslice;
128 0 : userslice.offset = displs[rank] * perDataSize;
129 0 : userslice.size = counts[rank] * perDataSize;
130 0 : outputSlices.emplace_back(std::move(userslice));
131 : }
132 :
133 : // DMA消减场景,打包opInfo
134 0 : HcomCollOpInfo opInfo
135 0 : = {"", execMem.inputPtr, execMem.outputPtr, 0, param.VDataDes.dataType, 0, HCCL_REDUCE_RESERVED, 0};
136 :
137 : std::unique_ptr<AlgTemplateBase> tempAlg
138 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_GATHER_V_PIPELINE, dispatcher_);
139 0 : CHK_SMART_PTR_NULL(tempAlg);
140 0 : Stream stream = param.stream;
141 0 : CHK_RET(tempAlg->Prepare(
142 : &opInfo, topoAttr_.userRank, execMem.count, execMem.inputMem, execMem.outputMem, level0CommInfo, level1CommInfo,
143 : stream, algResResp_->slaveStreams, algResResp_->notifiesMain, algResResp_->notifiesAux, outputSlices));
144 0 : CHK_RET(tempAlg->RunAsync());
145 0 : return HCCL_SUCCESS;
146 0 : }
147 :
148 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::Getlevel1CommRank(SubCommInfo& level1CommInfo)
149 : {
150 0 : if (CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1) != HCCL_SUCCESS) {
151 0 : return HCCL_E_UNAVAIL;
152 : }
153 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
154 0 : u32 ringNum
155 0 : = (topoType_ == TopoType::TOPO_TYPE_8P_RING) ? LEVEL0_PLANE_NUM_IN_8PRING : LEVEL0_PLANE_NUM_IN_NPRING_SINGLE;
156 0 : u32 commIndex = (ringNum == LEVEL0_PLANE_NUM_IN_8PRING) ? topoAttr_.devicePhyId : level0CommInfo.localRank;
157 :
158 0 : if (CheckCommSize(COMM_LEVEL1, commIndex + 1) != HCCL_SUCCESS) {
159 0 : return HCCL_E_UNAVAIL;
160 : }
161 0 : level1CommInfo = GetSubCommInfo(COMM_LEVEL1, commIndex);
162 :
163 0 : return HCCL_SUCCESS;
164 0 : }
165 :
166 0 : HcclResult CollAllGatherVMeshOpbasePipelineExecutor::SelectTempAlg(
167 : std::unique_ptr<AlgTemplateBase>& level1TempAlg, u32 level1RankSize)
168 : {
169 0 : if (level1RankSize > 1) {
170 : level1TempAlg
171 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_ALL_GATHER_V_PIPELINE, dispatcher_);
172 0 : CHK_SMART_PTR_NULL(level1TempAlg);
173 0 : return HCCL_SUCCESS;
174 : }
175 0 : return HCCL_E_UNAVAIL;
176 : }
177 : REGISTER_EXEC(
178 : "AllGatherVMeshOpbasePipelineExecutor", AllGatherVOpbasePipeline, CollAllGatherVMeshOpbasePipelineExecutor);
179 :
180 : } // namespace hccl
|