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 <iostream>
12 : #include <string>
13 : #include <map>
14 :
15 : #include "log.h"
16 : #include "coll_alg_component_lite.h"
17 :
18 : namespace Hccl {
19 :
20 0 : void CollAlgComponentLite::EnableDetour(bool enableDetour)
21 : {
22 0 : enableDetour_ = enableDetour;
23 0 : return;
24 : }
25 :
26 0 : void CollAlgComponentLite::EnableDataAllign(bool enableAllign)
27 : {
28 0 : enableAllign_ = enableAllign;
29 0 : return;
30 : }
31 :
32 0 : void CollAlgComponentLite::SetAllignSize(u64 allignSize)
33 : {
34 0 : allignSize_ = allignSize;
35 0 : return;
36 : }
37 :
38 0 : void CollAlgComponentLite::SetDmaMode(const DmaMode dmaMode)
39 : {
40 0 : dmaMode_ = dmaMode;
41 0 : return;
42 : }
43 :
44 0 : HcclResult CollAlgComponentLite::ParsePackedData(std::vector<char> packedData)
45 : {
46 0 : BinaryStream binaryStream(packedData);
47 0 : DmaMode dmaMode;
48 0 : binaryStream >> dmaMode;
49 0 : SetDmaMode(dmaMode);
50 0 : return HcclResult::HCCL_SUCCESS;
51 0 : }
52 :
53 0 : HcclResult CollAlgComponentLite::Orchestrate(
54 : const CollAlgOperator& op, const std::string& algName, const AlgTopoInfo& algTopoInfo, PrimQuePtr queue)
55 : {
56 0 : HCCL_DEBUG("[CollAlgComponentLite] Orchestrate Mode: Primitive.");
57 0 : if (rankSize_ == 1) {
58 0 : u64 dataSize = op.dataCount * DataTypeSizeGet(op.dataType);
59 0 : DataSlice usrInSlice = DataSlice(BufferType::INPUT, 0, dataSize);
60 0 : DataSlice usrOutSlice = DataSlice(BufferType::OUTPUT, 0, dataSize);
61 0 : std::unique_ptr<Primitive> primLocalCopy = std::make_unique<PrimLocalCopy>(usrInSlice, usrOutSlice);
62 0 : if (primLocalCopy == nullptr) {
63 0 : HCCL_ERROR("[CollAlgComponentLite] primLocalCopy is nullptr");
64 0 : return HcclResult::HCCL_E_PARA;
65 : }
66 0 : queue->Append(std::move(primLocalCopy));
67 :
68 0 : HCCL_DEBUG("[CollAlgComponentLite] rankSize = 1.");
69 0 : return HcclResult::HCCL_SUCCESS;
70 0 : }
71 :
72 0 : std::shared_ptr<CollAlgBase> primGenFunc = CollAlgRegistry::Global()->GetAlgImpl(op.opType, algName);
73 0 : CHK_PRT_RET(
74 : primGenFunc == nullptr, HCCL_ERROR("[CollAlgComponentLite] can not find collAlgName: [%s]", algName.c_str()),
75 : HcclResult::HCCL_E_PARA);
76 :
77 0 : CHK_PRT_RET(
78 : enableDetour_
79 : && ((algName != "AllGatherMesh") && (algName != "ReduceScatterMesh") && (algName != "AllReduceMesh")),
80 : HCCL_ERROR("[CollAlgComponentLite] Current algorithm can not support detouring, please check!"),
81 : HcclResult::HCCL_E_NOT_SUPPORT);
82 :
83 0 : primGenFunc->SetMyRank(myRank_);
84 0 : primGenFunc->SetRankSize(rankSize_);
85 0 : primGenFunc->SetDevType(devType_);
86 0 : primGenFunc->EnableDataAllign(enableAllign_);
87 0 : primGenFunc->SetAllignSize(allignSize_);
88 0 : primGenFunc->EnableDetour(enableDetour_);
89 0 : primGenFunc->SetDmaMode(dmaMode_);
90 :
91 0 : CollAlgParams params;
92 0 : params.maxTmpMemSize = scratchBufferSize_;
93 0 : params.opMode = OpMode::OPBASE;
94 0 : primGenFunc->GenPrimQuesAIC(algTopoInfo, op, params, linkMgr_, queue);
95 0 : return HcclResult::HCCL_SUCCESS;
96 0 : }
97 :
98 0 : HcclResult CollAlgComponentLite::Orchestrate(
99 : const CollAlgOperator& op, const std::string& algName, const AlgTopoInfo& algTopoInfo, InsQuePtr queue)
100 : {
101 0 : HCCL_DEBUG("[CollAlgComponentLite] Orchestrate Mode: Instruction.");
102 : bool isAlltoAll
103 0 : = (op.opType == OpType::ALLTOALL) || (op.opType == OpType::ALLTOALLV) || (op.opType == OpType::ALLTOALLVC);
104 0 : if ((rankSize_ == 1) && (!isAlltoAll)) {
105 0 : u64 dataSize = op.dataCount * DataTypeSizeGet(op.dataType);
106 0 : DataSlice usrInSlice = DataSlice(BufferType::INPUT, 0, dataSize);
107 0 : DataSlice usrOutSlice = DataSlice(BufferType::OUTPUT, 0, dataSize);
108 0 : std::unique_ptr<Instruction> insLocalCopy = std::make_unique<InsLocalCopy>(usrInSlice, usrOutSlice);
109 0 : if (insLocalCopy == nullptr) {
110 0 : HCCL_ERROR("[CollAlgComponentLite] insLocalCopy is nullptr");
111 0 : return HcclResult::HCCL_E_PARA;
112 : }
113 0 : queue->Append(std::move(insLocalCopy));
114 :
115 0 : HCCL_DEBUG("[CollAlgComponentLite] rankSize = 1.");
116 0 : HCCL_DEBUG("[CollAlgComponentLite] finish CollAlgComponentLite::Orchestrate.");
117 0 : return HcclResult::HCCL_SUCCESS;
118 0 : }
119 :
120 0 : std::shared_ptr<InsCollAlgBase> insGenFunc = InsCollAlgRegistry::Global()->GetAlgImpl(op.opType, algName);
121 0 : CHK_PRT_RET(
122 : insGenFunc == nullptr, HCCL_ERROR("[CollAlgComponentLite] can not find insCollAlgName: [%s]", algName.c_str()),
123 : HcclResult::HCCL_E_PARA);
124 :
125 0 : insGenFunc->SetMyRank(myRank_);
126 0 : insGenFunc->SetSendRecvRemoteRank(op.sendRecvRemoteRank);
127 0 : insGenFunc->SetRankSize(rankSize_);
128 0 : insGenFunc->SetDevType(devType_);
129 0 : insGenFunc->EnableDataAllign(enableAllign_);
130 0 : insGenFunc->SetAllignSize(allignSize_);
131 0 : insGenFunc->EnableDetour(enableDetour_);
132 0 : insGenFunc->SetDmaMode(dmaMode_);
133 0 : insGenFunc->SetRmaDataBufferMgr(rmaDataBufferMgr_);
134 :
135 0 : CollAlgParams params;
136 0 : params.maxTmpMemSize = scratchBufferSize_;
137 0 : params.opMode = op.opMode;
138 0 : insGenFunc->Orchestrate(algTopoInfo, op, params, linkMgr_, queue);
139 :
140 0 : HCCL_DEBUG("finish CollAlgComponentLite Orchestrate");
141 0 : return HcclResult::HCCL_SUCCESS;
142 0 : }
143 :
144 0 : void CollAlgComponentLite::UpdateScratchBufferSize(u64 bufferSize) { scratchBufferSize_ = bufferSize; }
145 :
146 : } // namespace Hccl
|