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 "alg_data_trans_wrapper.h"
14 : #include "ins_temp_reduce_aicpu_reduce.h"
15 :
16 : namespace Hccl {
17 0 : InsTempReduceAicpuReduce::InsTempReduceAicpuReduce(const RankId virtualRank, const u32 tempRankSize,
18 : const std::vector<std::vector<RankId>> &tempVTopo,
19 0 : const std::map<RankId, u32> &tempVirtRankMap)
20 0 : : InsAlgTemplateBase(virtualRank, tempRankSize, tempVTopo, tempVirtRankMap)
21 : {
22 0 : }
23 :
24 0 : InsTempReduceAicpuReduce::~InsTempReduceAicpuReduce()
25 : {
26 0 : }
27 :
28 0 : HcclResult InsTempReduceAicpuReduce::CalcRes(AlgTempResReq &tempResReq)
29 : {
30 0 : tempResReq.queNum = tempVTopo_[0].size();
31 0 : tempResReq.streamNum = tempResReq.queNum;
32 0 : tempResReq.queNotifys = CreateMasterSlaveQueNotifiesRequest(tempResReq.queNum);
33 0 : QId centerQ = 0;
34 0 : tempResReq.localWaitGroupCntNotify.emplace_back(centerQ, 0);
35 0 : tempResReq.localBcastPostCntNotify.emplace_back(centerQ, 0);
36 0 : CHK_RET(CalcResLinksMesh(myRank_, tempRankSize_, tempVTopo_, linkNumBtwPeers_, tempResReq));
37 0 : HCCL_DEBUG("[InsTempReduceAicpuReduce]CalcRes: queNum[%u], myRank[%d], tempRankSize[%u]", tempResReq.queNum, myRank_, tempRankSize_);
38 0 : return HcclResult::HCCL_SUCCESS;
39 : }
40 :
41 0 : u32 InsTempReduceAicpuReduce::CalcScratchMultiple(BufferType inBuffType, BufferType outBuffType) const
42 : {
43 : (void) inBuffType;
44 : (void) outBuffType;
45 0 : return tempRankSize_;
46 : }
47 :
48 0 : HcclResult InsTempReduceAicpuReduce::RunGatherMesh(const TempFuncs &tempFuncs, const TemplateDataParams &templateDataParams,
49 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
50 : {
51 : (void) tempFuncs;
52 0 : if (u32(myRank_) == root_) {
53 : // 本端rank数据从本端input -> 本端scratch
54 0 : CHK_RET(PreSyncInterQueues(tempInsQues));
55 0 : u64 srcOffset = templateDataParams.buffInfo.inBuffBaseOff;
56 0 : u64 srcSize = templateDataParams.sliceSize;
57 0 : u64 dstOffset = templateDataParams.sliceSize * root_;
58 0 : DataSlice srcSlice = DataSlice(BufferType::INPUT, srcOffset, srcSize);
59 0 : DataSlice dstSlice = DataSlice(BufferType::SCRATCH, dstOffset, srcSize);
60 0 : std::unique_ptr<Instruction> insLocalCopy = std::make_unique<InsLocalCopy>(srcSlice, dstSlice);
61 0 : tempInsQues[0]->Append(std::move(insLocalCopy));
62 : // recv from other rank
63 : u32 myAlgRank;
64 0 : CHK_RET(GetAlgRank(myRank_, tempVTopo_[0], myAlgRank));
65 0 : for (u32 queIdx = 1; queIdx < tempVTopo_[0].size(); queIdx++) {
66 0 : RankId neighborRank = tempVTopo_[0][(myAlgRank + queIdx) % tempRankSize_];
67 0 : LinkData neighborLinkData = tempLinks.at(neighborRank)[0];
68 0 : std::vector<DataSlice> srcSlices;
69 0 : std::vector<DataSlice> dstSlices;
70 0 : srcSlices.emplace_back(BufferType::INPUT, srcOffset, srcSize);
71 0 : dstSlices.emplace_back(BufferType::SCRATCH, srcSize * neighborRank, srcSize);
72 0 : SlicesList rxSlicesList(srcSlices, dstSlices);
73 0 : DataInfo recvData(neighborLinkData, rxSlicesList);
74 0 : CHK_PRT_RET(Recv(recvData, tempInsQues[queIdx], 0, true, dmaMode_), HCCL_ERROR("[InsTempReduceAicpuReduce] BatchSend failed"),
75 : HcclResult::HCCL_E_INTERNAL);
76 0 : }
77 0 : CHK_RET(PostSyncInterQueues(tempInsQues));
78 0 : } else {
79 : // send to root rank
80 0 : u64 srcOffset = templateDataParams.buffInfo.inBuffBaseOff;
81 0 : u64 srcSize = templateDataParams.sliceSize;
82 0 : LinkData linkSend = tempLinks.at(root_)[0];
83 0 : std::vector<DataSlice> srcSlices;
84 0 : std::vector<DataSlice> dstSlices;
85 0 : srcSlices.emplace_back(BufferType::INPUT, srcOffset, srcSize);
86 0 : dstSlices.emplace_back(BufferType::SCRATCH, srcSize * u32(myRank_), srcSize);
87 0 : SlicesList txSlicesList(srcSlices, dstSlices);
88 0 : DataInfo sendData(linkSend, txSlicesList);
89 0 : CHK_PRT_RET(Send(sendData, tempInsQues[0], 0, true, dmaMode_), HCCL_ERROR("[InsTempReduceAicpuReduce] BatchSend failed"),
90 : HcclResult::HCCL_E_INTERNAL);
91 0 : }
92 0 : return HCCL_SUCCESS;
93 : }
94 :
95 0 : HcclResult InsTempReduceAicpuReduce::RunAicpuLocalReduce(const TemplateDataParams &templateDataParams, std::vector<InsQuePtr> &tempInsQues)
96 : {
97 0 : DataSlice dataSlice = DataSlice(BufferType::SCRATCH, 0, templateDataParams.sliceSize);
98 0 : for (u32 rankId = 1; rankId < tempRankSize_; rankId++) {
99 0 : DataSlice reduceSlice = DataSlice(BufferType::SCRATCH, templateDataParams.sliceSize * rankId, templateDataParams.sliceSize);
100 0 : AicpuReduce(tempInsQues[0], reduceSlice, dataSlice, dataType_, redOp_);
101 : }
102 0 : DataSlice outputSlice = DataSlice(BufferType::OUTPUT, templateDataParams.buffInfo.inBuffBaseOff, templateDataParams.sliceSize);
103 0 : LocalCopy(tempInsQues[0], dataSlice, outputSlice);
104 0 : return HCCL_SUCCESS;
105 : }
106 :
107 0 : HcclResult InsTempReduceAicpuReduce::GenExtIns(const TempFuncs &tempFuncs, const TemplateDataParams &templateDataParams,
108 : const ResLinks &tempLinks, std::vector<InsQuePtr> &tempInsQues)
109 : {
110 0 : HCCL_INFO("[InsTempReduceAicpuReduce] Run start");
111 :
112 0 : dmaMode_ = DmaMode::PUT;
113 0 : if (IsPcieLink(tempLinks)) {
114 0 : dmaMode_ = DmaMode::GET;
115 : }
116 0 : if (tempVTopo_[0].size() == 1) {
117 0 : return HcclResult::HCCL_SUCCESS;
118 : }
119 0 : opMode_ = tempFuncs.opMode;
120 0 : queNum_ = tempVTopo_[0].size();
121 0 : CHK_PRT_RET(queNum_ != tempInsQues.size(),
122 : HCCL_ERROR("[CollAlgFactory] [InsTempReduceAicpuReduce] Rank [%d], requiredQue Error.", myRank_),
123 : HcclResult::HCCL_E_INTERNAL);
124 :
125 0 : RunGatherMesh(tempFuncs, templateDataParams, tempLinks, tempInsQues);
126 0 : StreamSync(tempInsQues);
127 0 : RunAicpuLocalReduce(templateDataParams, tempInsQues);
128 0 : HCCL_INFO("[InsTempReduceAicpuReduce] Run finished");
129 0 : return HCCL_SUCCESS;
130 : }
131 :
132 : } // namespace Hccl
|