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_broadcast_mesh_aiv_executor.h"
12 :
13 : namespace hccl {
14 :
15 0 : CollBroadcastMeshAivExecutor::CollBroadcastMeshAivExecutor(
16 0 : const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
17 0 : : CollBroadcastExecutor(dispatcher, topoMatcher)
18 : {
19 0 : DMAReduceFlag_ = false;
20 0 : desc_.isAivMode = true;
21 0 : }
22 :
23 0 : HcclResult CollBroadcastMeshAivExecutor::CalcStreamNum(u32& streamNum)
24 : {
25 0 : streamNum = 0; // AIV通信不需要申请从流
26 0 : HCCL_INFO("[CollBroadcastMeshAivExecutor][CalcStreamNum] tag[%s] streamNum[%u].", tag_.c_str(), streamNum);
27 0 : return HCCL_SUCCESS;
28 : }
29 :
30 0 : HcclResult CollBroadcastMeshAivExecutor::CalcCommInfo(std::vector<LevelNSubCommTransport>& opTransport)
31 : {
32 : TransportMemType inputType;
33 0 : if (GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
34 0 : inputType = TransportMemType::AIV_INPUT;
35 : } else {
36 0 : inputType = TransportMemType::CCL_INPUT;
37 : }
38 0 : TransportMemType outputType = TransportMemType::AIV_OUTPUT;
39 0 : HCCL_INFO(
40 : "[CollBroadcastMeshAivExecutor][CalcTransportMemType] tag[%s] inputType[%d],"
41 : " outputType[%d].",
42 : tag_.c_str(), inputType, outputType);
43 0 : CHK_RET(CalcLevel0CommInfo(inputType, outputType, opTransport));
44 0 : return HCCL_SUCCESS;
45 : }
46 :
47 0 : HcclResult CollBroadcastMeshAivExecutor::CalNumBlocks(
48 : u32& numBlocks, u32 rankSize, [[maybe_unused]] u64 dataSize, [[maybe_unused]] HcclCMDType cmdType)
49 : {
50 0 : numBlocks = rankSize; // 默认情况使用rankSize个AIV
51 0 : CHK_PRT_RET(
52 : numBlocks_ < numBlocks,
53 : HCCL_ERROR(
54 : "[CollBroadcastMeshAivExecutor][CalNumBlocks]aivCore[%u] is invalid, at least need [%u].", numBlocks_,
55 : numBlocks),
56 : HCCL_E_PARA);
57 :
58 0 : HCCL_INFO(
59 : "[CollBroadcastMeshAivExecutor][CalNumBlocks] numBlocks is set to [%u], limit[%u], recommanded[%u]", numBlocks,
60 : numBlocks_, numBlocks);
61 0 : return HCCL_SUCCESS;
62 : }
63 :
64 0 : HcclResult CollBroadcastMeshAivExecutor::CalcLevel0CommInfo(
65 : TransportMemType inputType, TransportMemType outputType, std::vector<LevelNSubCommTransport>& opTransport)
66 : {
67 0 : CommParaInfo commParaLevel0(COMM_LEVEL0, CommType::COMM_TAG_MESH);
68 0 : commParaLevel0.meshSinglePlane = true;
69 0 : CHK_RET(CalcCommPlaneInfo(tag_, commParaLevel0, opTransport[COMM_LEVEL0], inputType, outputType));
70 0 : return HCCL_SUCCESS;
71 0 : }
72 :
73 0 : HcclResult CollBroadcastMeshAivExecutor::Orchestrate(OpParam& param, AlgResourceResponse& algRes)
74 : {
75 0 : HcclUs startut = TIME_NOW();
76 0 : tag_ = param.tag;
77 0 : algResResp_ = &algRes;
78 :
79 0 : HcclResult ret = HCCL_SUCCESS;
80 0 : ExecMem execMem;
81 0 : execMem.count = param.DataDes.count;
82 0 : execMem.inputPtr = param.inputPtr;
83 0 : execMem.outputPtr = param.inputPtr; // broadcast使用一块内存
84 :
85 0 : if (GetWorkflowMode() != HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE) {
86 0 : execMem.inputMem = algRes.aivInputMem;
87 : } else {
88 0 : execMem.inputMem = algRes.cclInputMem;
89 : }
90 0 : execMem.outputMem = algRes.aivOutputMem;
91 :
92 0 : ret = KernelRun(param, execMem);
93 :
94 0 : CHK_PRT_RET(
95 : ret != HCCL_SUCCESS,
96 : HCCL_ERROR(
97 : "[CollBroadcastMeshAivExecutor][Orchestrate]errNo[0x%016llx] tag[%s] executor kernel run failed",
98 : HCCL_ERROR_CODE(ret), param.tag.c_str()),
99 : ret);
100 :
101 0 : HCCL_INFO(
102 : "tag[%s], Broadcast executor orchestrate success, take time [%lld]us", param.tag.c_str(),
103 : DURATION_US(TIME_NOW() - startut));
104 0 : return HCCL_SUCCESS;
105 0 : }
106 :
107 0 : HcclResult CollBroadcastMeshAivExecutor::KernelRun(const OpParam& param, ExecMem& execMem)
108 : {
109 0 : HCCL_INFO("[CollBroadcastMeshAivExecutor][KernelRun]broadcast aiv enter.");
110 :
111 0 : CHK_RET(CheckCommSize(COMM_LEVEL0, COMM_INDEX_0 + 1));
112 0 : SubCommInfo level0CommInfo = GetSubCommInfo(COMM_LEVEL0, COMM_INDEX_0);
113 :
114 : void* buffersIn[MAX_RANK_SIZE];
115 : void* buffersOut[MAX_RANK_SIZE];
116 :
117 0 : u32 rootRank = 0;
118 0 : HcclResult ret = GetRankByUserRank(COMM_LEVEL0, COMM_INDEX_0, param.root, rootRank);
119 0 : CHK_PRT_RET(
120 : ret != HCCL_SUCCESS,
121 : HCCL_ERROR("[BroadCastOperator][CollBroadcastMeshAivExecutor]invalid root[%u] to get userrank", param.root),
122 : ret);
123 :
124 0 : u32 localRank = level0CommInfo.localRank;
125 0 : u32 localRankSize = level0CommInfo.localRankSize;
126 0 : HCCL_DEBUG("[CollBroadcastMeshAivExecutor][KernelRun] userRank [%u] localRank [%u]", topoAttr_.userRank, localRank);
127 0 : for (u32 i = 0; i < localRankSize; i++) {
128 0 : if (i != localRank) {
129 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::INPUT_MEM, &(buffersIn[i])));
130 0 : CHK_RET(level0CommInfo.links[i]->GetRemoteMem(UserMemType::OUTPUT_MEM, &(buffersOut[i])));
131 : } else {
132 0 : buffersIn[i] = execMem.inputMem.ptr(); // ccl buffer
133 0 : buffersOut[i] = execMem.outputMem.ptr();
134 : }
135 : }
136 :
137 0 : bool isOpbase = (workflowMode_ == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE);
138 0 : AivOpArgs opArgs{HcclCMDType::HCCL_CMD_BROADCAST, execMem.inputPtr, execMem.outputPtr, execMem.count,
139 0 : param.DataDes.dataType, param.reduceType, rootRank, isOpbase};
140 0 : AivTopoArgs topoArgs{localRank, localRankSize};
141 0 : topoArgs.identify = algoAttr_.identifier;
142 : u32 numBlocks;
143 0 : CHK_PRT_RET(
144 : CalNumBlocks(numBlocks, localRankSize) != HCCL_SUCCESS, HCCL_ERROR("[%s] CalNumBlocks failed", __func__),
145 : HCCL_E_PARA);
146 0 : numBlocks_ = numBlocks;
147 0 : AivResourceArgs resourceArgs{param.tag, param.stream.ptr(), buffersIn, buffersOut, execMem.inputMem.size(),
148 0 : numBlocks_, param.aivTag};
149 0 : AivAlgArgs algArgs{};
150 0 : algArgs.execTimeOut = topoMatcher_->GetExecTimeOutConfig();
151 0 : algArgs.execTimeOutSet = true;
152 0 : struct AivProfilingInfo aivProfilingInfo;
153 0 : aivProfilingInfo.counter = opCounter_;
154 0 : ret = ExecuteKernelLaunch(opArgs, topoArgs, resourceArgs, algArgs, aivProfilingInfo); // 执行kernelLaunch
155 0 : CHK_PRT_RET(
156 : ret != HCCL_SUCCESS,
157 : HCCL_ERROR("[CollBroadcastMeshAivExecutor][KernelRun]broadcast aiv failed, return[%d]", ret), ret);
158 :
159 0 : ExtraArgs extraArgs;
160 0 : CHK_RET(SetOpCache(opArgs, topoArgs, resourceArgs, algArgs, extraArgs, aivProfilingInfo, false));
161 :
162 0 : HCCL_INFO("[CollBroadcastMeshAivExecutor][KernelRun]broadcast aiv run success.");
163 :
164 0 : return HCCL_SUCCESS;
165 0 : }
166 :
167 : REGISTER_EXEC("BroadcastMeshAivExecutor", BroadcastMeshAiv, CollBroadcastMeshAivExecutor);
168 :
169 : } // namespace hccl
|