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