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_broadcast_mesh1d.h"
12 : #include "ccu_instruction_broadcast_mesh1d.h"
13 :
14 : namespace Hccl {
15 :
16 : constexpr int CKE_IDX_0 = 0;
17 : constexpr int CKE_IDX_1 = 1;
18 : constexpr int CKE_IDX_2 = 2;
19 :
20 0 : CcuContextBroadcastMesh1D::CcuContextBroadcastMesh1D(
21 0 : const CcuCtxArg& arg, const std::vector<CcuTransport*>& transports, const CcuTransportGroup& group)
22 0 : : CcuContextAlgBase(arg, transports, group)
23 : {
24 0 : const CcuCtxArgBroadcastMesh1D* ctxArg = dynamic_cast<const CcuCtxArgBroadcastMesh1D*>(&arg);
25 0 : if (ctxArg == nullptr) {
26 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1D::ctxArg ptr is null"));
27 : }
28 0 : rankId_ = ctxArg->rankId_;
29 0 : rootId_ = ctxArg->rootId_;
30 0 : rankSize_ = ctxArg->dimSize_[0];
31 0 : dataType_ = ctxArg->op_.dataType;
32 0 : outputDataType_ = ctxArg->op_.outputDataType;
33 0 : if (outputDataType_ == DataType::INVALID) {
34 0 : outputDataType_ = dataType_;
35 0 : HCCL_INFO(
36 : "[CcuContextBroadcastMesh1D] outputDataType is [INVALID], set outputDataType to[%s]",
37 : outputDataType_.Describe().c_str());
38 : }
39 0 : HCCL_INFO(
40 : "[CcuContextBroadcastMesh1D] init end, ctxArg->dimSize size[%zu] rankSize[%llu]", ctxArg->dimSize_.size(),
41 : rankSize_);
42 0 : }
43 :
44 0 : void CcuContextBroadcastMesh1D::CreateAllVariables()
45 : {
46 0 : HCCL_INFO("[CcuContextBroadcastMesh1D]transports.size: [%zu]", transports.size());
47 0 : uint16_t transportIdx = 0;
48 0 : input_ = CreateVariable();
49 0 : if (transports.size() == 0) {
50 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1D transports is empty"));
51 : }
52 0 : HCCL_DEBUG("[CcuContextBroadcastMesh1D]transports.size: [%zu]", transports.size());
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 : "[CcuContextBroadcastMesh1D] MyRank[%u], PeerId[%llu], TransportId[%hu]", rankId_, peerId,
60 : transportIdx);
61 0 : CHK_PRT_RET(
62 : transports[transportIdx] == nullptr || transportIdx >= transports.size(),
63 : HCCL_ERROR(
64 : "[CcuContextBroadcastMesh1D] Algorithm transport ptr is null or transportIdx is out of bounds"), );
65 0 : output_.push_back(
66 0 : CreateVariable((*transports[transportIdx]), CKE_IDX_1)); // 获取transport中id=2的Var来传递output
67 0 : token_.push_back(CreateVariable((*transports[transportIdx]), CKE_IDX_2));
68 0 : transportIdx++;
69 : }
70 : }
71 0 : offset_ = CreateVariable();
72 0 : slicesize_ = CreateVariable();
73 0 : groupOpSize_ = CreateGroupOpSize();
74 0 : return;
75 : }
76 :
77 0 : void CcuContextBroadcastMesh1D::LoadAndExchangeData()
78 : {
79 0 : uint16_t selfBit = 1 << rankId_;
80 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
81 0 : HCCL_DEBUG("[CcuContextBroadcastMesh1D] BroadcastMesh1D LoadAndExchanageData: rankId[%u]", rankId_);
82 0 : Load(input_);
83 0 : Load(output_[rankId_]);
84 0 : Load(token_[rankId_]);
85 0 : Load(offset_);
86 0 : Load(slicesize_);
87 0 : Load(groupOpSize_);
88 0 : for (auto t : transports) {
89 0 : WriteVariableWithSignal(*t, output_[rankId_], CKE_IDX_1, CKE_IDX_1, selfBit);
90 0 : WriteVariableWithSignal(*t, token_[rankId_], CKE_IDX_2, CKE_IDX_2, selfBit);
91 : }
92 0 : GroupWait(*transportGroup, CKE_IDX_1, allBit);
93 0 : GroupWait(*transportGroup, CKE_IDX_2, allBit);
94 0 : return;
95 : }
96 :
97 0 : void CcuContextBroadcastMesh1D::BroadcastFromRootToAll()
98 : {
99 0 : std::vector<CcuRep::Memory> dst;
100 0 : for (uint32_t index = 0; index < rankSize_; index++) {
101 0 : dst.emplace_back(CreateMemory());
102 : }
103 0 : CcuRep::Memory src = CreateMemory();
104 0 : src.addr = input_;
105 0 : src.token = token_[rankId_];
106 0 : uint32_t curId = 0;
107 0 : for (uint32_t rankIdx = 0; rankIdx < rankSize_; rankIdx++) {
108 0 : if (rankIdx != rootId_) {
109 0 : dst[curId].addr = output_[rankIdx];
110 0 : dst[curId].token = token_[rankIdx];
111 0 : curId++;
112 : }
113 : }
114 0 : dst[rankSize_ - 1].addr = output_[rankId_];
115 0 : dst[rankSize_ - 1].token = token_[rankId_];
116 0 : GroupBroadcast(transports, dst, src, groupOpSize_);
117 0 : HCCL_INFO("[CcuContextBroadcastMesh1D] BroadcastMesh1D GroupBroadcast end");
118 0 : return;
119 0 : }
120 :
121 0 : void CcuContextBroadcastMesh1D::Algorithm()
122 : {
123 0 : HCCL_INFO("[CcuContextBroadcastMesh1D] BroadcastMesh1D run");
124 0 : uint16_t selfBit = 1 << rankId_;
125 0 : uint16_t allBit = ((1 << rankSize_) - 1) & (~(1 << rankId_));
126 :
127 0 : CreateAllVariables();
128 :
129 0 : LoadAndExchangeData();
130 :
131 0 : if (rankId_ == rootId_) {
132 0 : BroadcastFromRootToAll();
133 : }
134 :
135 0 : for (auto t : transports) {
136 0 : RemotePost(*t, CKE_IDX_0, selfBit);
137 : }
138 :
139 0 : GroupWait(*transportGroup, CKE_IDX_0, allBit);
140 0 : HCCL_INFO("[CcuContextBroadcastMesh1D] BroadcastMesh1D Broadcast groupwait end");
141 0 : return;
142 : }
143 :
144 0 : std::vector<uint64_t> CcuContextBroadcastMesh1D::GeneArgs(const CcuTaskArg& arg)
145 : {
146 0 : const CcuTaskArgBroadcastMesh1D* taskArg = dynamic_cast<const CcuTaskArgBroadcastMesh1D*>(&arg);
147 0 : if (taskArg == nullptr) {
148 0 : THROW<NullPtrException>(StringFormat("CcuContextBroadcastMesh1D::taskArg ptr is null"));
149 : }
150 0 : uint64_t inputAddr = taskArg->inputAddr_;
151 0 : uint64_t outputAddr = taskArg->outputAddr_;
152 0 : uint64_t tokenInfo = taskArg->token_;
153 0 : uint64_t offset = taskArg->offset_;
154 0 : uint64_t sliceSize = taskArg->sliceSize_;
155 0 : auto goSize = CalGoSize(sliceSize);
156 :
157 0 : return {inputAddr, outputAddr, tokenInfo, offset, sliceSize, goSize[0], goSize[1], goSize[2], goSize[3]};
158 0 : }
159 : } // namespace Hccl
|