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