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