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_reduce_scatter_v_for_310p_ring_executor.h"
12 :
13 : #include <algorithm>
14 :
15 : namespace hccl {
16 0 : CollReduceScatterVFor310PRingExecutor::CollReduceScatterVFor310PRingExecutor(
17 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
18 0 : : CollReduceScatterVExecutor(dispatcher, topoMatcher)
19 : {
20 0 : DMAReduceFlag_ = true;
21 0 : CCLMemSlice_ = true;
22 0 : }
23 :
24 0 : HcclResult CollReduceScatterVFor310PRingExecutor::CalcStreamNum(u32& streamNum)
25 : {
26 0 : streamNum = DMAReduceFlag_ ? 1 : 0;
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult CollReduceScatterVFor310PRingExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
31 : {
32 0 : TransportMemType inputType = TransportMemType::RESERVED;
33 0 : TransportMemType outputType = TransportMemType::RESERVED;
34 0 : CHK_RET(CalcTransportMemType(inputType, outputType));
35 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
36 0 : return HCCL_SUCCESS;
37 : }
38 :
39 : HcclResult
40 0 : CollReduceScatterVFor310PRingExecutor::CalcTransportMemType(TransportMemType& inputType, TransportMemType& outputType)
41 : {
42 0 : if (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
43 0 : inputType = TransportMemType::CCL_INPUT;
44 0 : outputType = TransportMemType::CCL_OUTPUT;
45 : } else {
46 0 : inputType = TransportMemType::PARAM_INPUT;
47 0 : outputType = TransportMemType::PARAM_OUTPUT;
48 : }
49 0 : HCCL_INFO(
50 : "[CollReduceScatterVFor310PRingExecutor][CalcTransportMemType] tag[%s] inputType[%d], outputType[%d]",
51 : tag_.c_str(), inputType, outputType);
52 0 : return HCCL_SUCCESS;
53 : }
54 :
55 0 : HcclResult CollReduceScatterVFor310PRingExecutor::CalcLevel0CommInfo(
56 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
57 : {
58 0 : HCCL_INFO("[CollReduceScatterVFor310PRingExecutor][CalcLevel0CommInfo]tag[%s] start", tag_.c_str());
59 0 : CommParaInfo commParaInfo(COMM_LEVEL0, CommType::COMM_TAG_RING_INNER);
60 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaInfo, opTransport[COMM_LEVEL0], inputType, outputType));
61 0 : HCCL_INFO("[CollReduceScatterVFor310PRingExecutor][CalcLevel0CommInfo]tag[%s] Calc RingComm finish", tag_.c_str());
62 0 : return HCCL_SUCCESS;
63 0 : }
64 :
65 0 : HcclResult CollReduceScatterVFor310PRingExecutor::CalcCurCountsAndCurDispls(
66 : const u64 maxTotalCount, std::vector<u64>& countsLeft, std::vector<u64>& displs, std::vector<u64>& curCounts,
67 : std::vector<u64>& curDispls, bool& finished)
68 : {
69 0 : curCounts = std::vector<u64>(countsLeft.size(), 0);
70 0 : curDispls = std::vector<u64>(displs.size(), 0);
71 0 : auto allocatableCount = maxTotalCount;
72 :
73 : // 先设置本轮的displacements,等于入参displs
74 0 : std::copy(displs.begin(), displs.end(), curDispls.begin());
75 :
76 : // 分配本轮的counts,如果CCLbuffer空间还没完全利用,则再进行分配
77 0 : while (allocatableCount > 0) {
78 : // 计算现在还有几个rank还有数据需要去通信(countsLeft不为0)
79 0 : const auto nonZeroCount = std::count_if(countsLeft.begin(), countsLeft.end(), [](const u64 count) {
80 0 : return count != 0;
81 : });
82 0 : if (nonZeroCount == 0) {
83 0 : finished = true;
84 0 : break;
85 : } else {
86 : // 计算每个rank可以分到多少count
87 0 : const auto perRankCount = allocatableCount / nonZeroCount;
88 0 : if (perRankCount == 0) {
89 0 : break;
90 : }
91 : // 分配好每个rank的counts
92 0 : for (auto i = 0U; i < countsLeft.size(); ++i) {
93 0 : const auto curCount = countsLeft[i] < perRankCount ? countsLeft[i] : perRankCount;
94 0 : allocatableCount -= curCount;
95 0 : curCounts[i] += curCount;
96 0 : countsLeft[i] -= curCount;
97 0 : displs[i] += curCount;
98 : }
99 : }
100 : }
101 0 : return HCCL_SUCCESS;
102 : }
103 :
104 0 : HcclResult CollReduceScatterVFor310PRingExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
105 : {
106 0 : HCCL_CONFIG_INFO(HCCL_ALG, "[CollReduceScatterVFor310PRingExecutor][KernelRun] 310p aiv ReduceScatterV start");
107 0 : const auto* displsPtr = static_cast<const u64*>(param.VDataDes.displs);
108 0 : HcclDataType dataType = param.VDataDes.dataType;
109 0 : const u32 unitSize = SIZE_TABLE[dataType];
110 :
111 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
112 0 : SubCommInfo outerCommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
113 0 : const u32 level0RankSize = outerCommInfo.localRankSize;
114 :
115 : bool isInlineReduce
116 0 : = IsSupportSDMAReduce(execMem.inputMem.ptr(), execMem.outputMem.ptr(), dataType, param.reduceType);
117 :
118 0 : u64 reduceAttr = 0;
119 0 : if (isInlineReduce) {
120 0 : SalSetBitOne(reduceAttr, ATTR_POS_INLINE_REDUCE);
121 : } else {
122 0 : HCCL_ERROR("[CollReduceScatterVFor310PRingExecutor][KernelRun] ReduceScatterV only support InlineReduce!");
123 :
124 0 : return HCCL_E_NOT_SUPPORT;
125 : }
126 :
127 : // 根据counts和displace计算每个rank的数据范围
128 : // 两个slices: dataSlices里的offset是cclBuffer范围内的偏移,就地计算得出
129 : // outputSlices里的offset是user output的偏移,使用传入的displs算得
130 0 : std::vector<Slice> dataSlices;
131 0 : std::vector<Slice> outputSlices;
132 0 : const auto counts = static_cast<u64*>(param.VDataDes.counts);
133 0 : auto displace = 0ULL;
134 0 : for (auto rank = 0U; rank < level0RankSize; ++rank) {
135 0 : Slice slice;
136 0 : slice.offset = displace * unitSize;
137 0 : slice.size = counts[rank] * unitSize;
138 0 : dataSlices.emplace_back(slice);
139 :
140 0 : slice.offset = displsPtr[rank] * unitSize;
141 0 : outputSlices.emplace_back(std::move(slice));
142 :
143 0 : displace += counts[rank];
144 : }
145 :
146 : // opInfo这里主要填对inputPtr和outputPtr就好
147 0 : HcomCollOpInfo opInfo = {"", execMem.inputPtr, execMem.outputPtr, 0, dataType, 0, param.reduceType, 0};
148 0 : HcomCollOpInfo* opInfoPtr = nullptr;
149 0 : if (DMAReduceFlag_) {
150 0 : opInfoPtr = &opInfo;
151 : }
152 :
153 0 : std::vector<u32> rankOrder(level0RankSize, 0);
154 : std::unique_ptr<AlgTemplateBase> tempAlg
155 0 : = AlgTemplateRegistry::Instance().GetAlgTemplate(TemplateType::TEMPLATE_REDUCESCATTER_RING_DIRECT, dispatcher_);
156 0 : CHK_SMART_PTR_NULL(tempAlg);
157 0 : CHK_RET(tempAlg->Prepare(
158 : reduceAttr, opInfoPtr, topoAttr_.userRank, algResResp_->slaveStreams, algResResp_->notifiesMain,
159 : algResResp_->notifiesAux, rankOrder, outputSlices, true));
160 :
161 0 : CHK_RET(tempAlg->Prepare(
162 : execMem.inputMem, execMem.outputMem, execMem.outputMem, execMem.count, dataType, param.stream, param.reduceType,
163 : 0, dataSlices));
164 :
165 0 : CHK_RET(tempAlg->RegisterProfiler(
166 : (outerCommInfo.localRankSize << PROF_RANKSIZE_OFFSET_OF_PLANEID) + outerCommInfo.localRank, PROF_STAGE_0,
167 : HCCL_EXEC_STEP_NOT_SET, param.stream));
168 :
169 : // 执行Ring算法
170 0 : CHK_RET(RunTemplate(tempAlg, outerCommInfo));
171 :
172 0 : return HCCL_SUCCESS;
173 0 : }
174 :
175 : REGISTER_EXEC("ReduceScatterVFor310PRing", ReduceScatterVFor310PRing, CollReduceScatterVFor310PRingExecutor);
176 : } // namespace hccl
|