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