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