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 "ccu_context_all_gather_mesh1d.h"
12 : #include "ccu_instruction_all_gather_mesh1d.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int OUTPUT_XN_ID = 1;
17 : constexpr int TOKEN_XN_ID = 2;
18 : constexpr int CKE_IDX_0 = 0;
19 : constexpr int CKE_IDX_1 = 1;
20 : constexpr int CKE_IDX_2 = 2;
21 : constexpr int CKE_IDX_3 = 3;
22 :
23 2 : CcuContextAllGatherMesh1D::CcuContextAllGatherMesh1D(const CcuCtxArg &arg,
24 : const std::vector<CcuTransport *> &transports,
25 2 : const CcuTransportGroup &group)
26 2 : : CcuContextAlgBase(arg, transports, group)
27 : {
28 6 : HCCL_INFO("[CcuContextAllGatherMesh1D] Enter Constructor.");
29 2 : const CcuCtxArgAllGatherMesh1D *ctxArg = dynamic_cast<const CcuCtxArgAllGatherMesh1D *>(&arg);
30 2 : if (ctxArg == nullptr) {
31 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D::ctxArg ptr is null"));
32 : }
33 2 : rankId_ = ctxArg->rankId_;
34 2 : if (ctxArg->dimSize_.size() > 0) {
35 0 : rankSize_ = ctxArg->dimSize_[0];
36 : }
37 2 : }
38 :
39 0 : void CcuContextAllGatherMesh1D::Algorithm()
40 : {
41 0 : HCCL_INFO("[CcuContextAllGatherMesh1D] AllgatherMesh1D run.");
42 0 : uint16_t selfBit = 1 << rankId_;
43 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
44 :
45 0 : input_.push_back(CreateVariable());
46 0 : uint16_t transportIdx = 0;
47 0 : if (transports.size() == 0) {
48 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D transports is empty"));
49 : }
50 : // 按照rank号从小到大遍历transports,遇到本rank就填充本地资源,否则依次取远端资源,要求给框架返回的Link同样是按顺序排列的
51 0 : for (uint64_t peerId = 0; peerId < rankSize_; peerId++) {
52 0 : if (peerId == rankId_) {
53 0 : output_.push_back(CreateVariable());
54 0 : token_.push_back(CreateVariable());
55 : } else {
56 0 : HCCL_INFO("[CcuContextAllGatherMesh1D] MyRank[%u], PeerId[%llu], TransportId[%u]",
57 : rankId_, peerId, transportIdx);
58 0 : CHK_PRT_RET(transports[transportIdx] == nullptr,
59 : HCCL_ERROR("[CcuContextAllGatherMesh1D] Algorithm transport ptr is null"),);
60 0 : output_.push_back(CreateVariable((*transports[transportIdx]), OUTPUT_XN_ID)); // 获取transport中id=1的Var来传递output
61 0 : token_.push_back(CreateVariable((*transports[transportIdx]), TOKEN_XN_ID));
62 0 : transportIdx++;
63 : }
64 : }
65 0 : offset_ = CreateVariable();
66 0 : groupOpSize_ = CreateGroupOpSize();
67 :
68 0 : Load(input_[0]);
69 0 : Load(output_[rankId_]);
70 0 : Load(token_[rankId_]);
71 0 : Load(offset_);
72 0 : Load(groupOpSize_);
73 :
74 0 : for (auto t : transports) {
75 0 : WriteVariableWithSignal(*t, output_[rankId_], OUTPUT_XN_ID, CKE_IDX_1, selfBit); // index = 1,传递output信息
76 0 : WriteVariableWithSignal(*t, token_[rankId_], TOKEN_XN_ID, CKE_IDX_2, selfBit); // index = 2,传递token信息
77 : }
78 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit); // index = 1,传递output信息
79 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit); // index = 2,传递token信息
80 :
81 0 : CcuRep::Memory src = CreateMemory();
82 0 : std::vector<CcuRep::Memory> dst;
83 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
84 0 : dst.push_back(CreateMemory());
85 : }
86 0 : src.addr = input_[0];
87 0 : src.token = token_[rankId_];
88 0 : uint32_t curId = 0;
89 0 : uint32_t dstId = 0;
90 0 : for (uint64_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
91 0 : if (rankIdx != rankId_) {
92 0 : curId = dstId;
93 0 : dstId++;
94 : } else {
95 0 : curId = rankSize_ - 1;
96 : }
97 0 : dst[curId].addr = output_[rankIdx];
98 0 : dst[curId].addr += offset_;
99 0 : dst[curId].token = token_[rankIdx];
100 : }
101 0 : GroupBroadcast(transports, dst, src, groupOpSize_);
102 :
103 0 : for (auto t : transports) {
104 0 : RemotePost(*t, CKE_IDX_0, selfBit);
105 : }
106 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
107 0 : HCCL_INFO("[CcuContextAllGatherMesh1D] AllgatherMesh1D end.");
108 0 : return;
109 0 : }
110 :
111 0 : std::vector<uint64_t> CcuContextAllGatherMesh1D::GeneArgs(const CcuTaskArg &arg)
112 : {
113 0 : const CcuTaskArgAllGatherMesh1D *taskArg = dynamic_cast<const CcuTaskArgAllGatherMesh1D *>(&arg);
114 0 : if (taskArg == nullptr) {
115 0 : THROW<NullPtrException>(StringFormat("CcuContextAllGatherMesh1D::taskArg ptr is null"));
116 : }
117 0 : uint64_t outputAddr = taskArg->outputAddr_;
118 0 : uint64_t inputAddr = taskArg->inputAddr_;
119 0 : uint64_t tokenInfo = taskArg->token_;
120 0 : uint64_t offset = taskArg->offset_;
121 0 : uint64_t sliceSize = taskArg->sliceSize_;
122 0 : auto goSize = CalGoSize(sliceSize);
123 0 : return {inputAddr, outputAddr, tokenInfo, offset, goSize[0], goSize[1], goSize[2], goSize[3]};
124 0 : }
125 : }
|