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