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 "aiv_ins_preprocessor.h"
12 : #include "aiv_ins.h"
13 : #include "env_config.h"
14 :
15 : namespace Hccl {
16 :
17 3 : void AivInsPreprocessor::SetProtocol(uint8_t protocol)
18 : {
19 3 : protocol_ = protocol;
20 3 : }
21 :
22 4 : uint8_t AivInsPreprocessor::GetProtocol() const
23 : {
24 4 : return protocol_;
25 : }
26 :
27 6 : void AivInsPreprocessor::Preprocess(std::shared_ptr<InsQueue> &insQueue) const
28 : {
29 18 : HCCL_INFO("[AivInsPreprocessor::%s] insQueue Preprocess start.", __func__);
30 :
31 : // 对每主queue中每个ins进行预处理
32 10 : for (auto ins = insQueue->Iter(); ins.HasNext(); ++ins) {
33 4 : if (ins->GetType() != InstructionType::AIV_INS) {
34 1 : continue;
35 : }
36 3 : InsPreprocess(ins);
37 6 : }
38 :
39 18 : HCCL_INFO("[AivInsPreprocessor::%s] insQueue Preprocess end.", __func__);
40 6 : }
41 :
42 3 : void AivInsPreprocessor::InsPreprocess(InsIterator &insIter) const
43 : {
44 9 : HCCL_INFO("[AivInsPreprocessor::%s] start.", __func__);
45 :
46 3 : const AivInstruction &aivIns = dynamic_cast<const AivInstruction &>(*insIter);
47 :
48 3 : auto links = aivIns.GetLinks();
49 :
50 3 : if (protocol_ == 0) { // ubmemory
51 3 : BatchBuildTransports(links);
52 0 : } else if (protocol_ == 1) { // urma
53 0 : BatchBuildUrmaTransports(links);
54 : } else {
55 0 : THROW<InvalidParamsException>(
56 0 : StringFormat("protocol[%u] not supported", protocol_));
57 : }
58 :
59 9 : HCCL_INFO("[AivInsPreprocessor::%s] end.", __func__);
60 3 : }
61 :
62 3 : void AivInsPreprocessor::BatchBuildTransports(const vector<LinkData> &links) const
63 : {
64 9 : HCCL_INFO("[AivInsPreprocessor::%s] start.", __func__);
65 :
66 : // 创建MemTransport并进行异步建链、交换
67 3 : comm->GetUbMemoryTransportMgr()->BatchCreateTransport(links);
68 :
69 3 : comm->GetUbMemoryTransportMgr()->TransportsConnect();
70 :
71 9 : HCCL_INFO("[AivInsPreprocessor::%s] end.", __func__);
72 3 : }
73 :
74 0 : void AivInsPreprocessor::BatchBuildUrmaTransports(const vector<LinkData> &links) const
75 : {
76 0 : HCCL_RUN_INFO("[AivInsPreprocessor::%s] start.", __func__);
77 :
78 0 : std::string opTag = comm->GetCurrentCollOperator()->opTag;
79 :
80 : // 创建RmaConnectiuon
81 0 : RmaConnManager& connManager = comm->GetRmaConnManager();
82 0 : for (auto &link : links) {
83 0 : auto conn = connManager.Create(opTag, link, HrtUbJfcMode::USER_CTL);
84 0 : CHECK_NULLPTR(conn, "[AivInsPreprocessor::BatchBuildUrmaTransports] conn is nullptr!");
85 : }
86 0 : HCCL_INFO("[AivInsPreprocessor::%s] end creating rma connection", __func__);
87 :
88 : // 创建UrmaDirectTransport并进行异步建链、交换
89 0 : auto transportMgr = comm->GetMemTransportManager();
90 0 : CHECK_NULLPTR(transportMgr, "[AivInsPreprocessor::BatchBuildUrmaTransports] transportMgr is nullptr!");
91 0 : transportMgr->BatchBuildUrmaDirectTransports(links);
92 :
93 0 : auto timeout = std::chrono::seconds(EnvConfig::GetInstance().GetSocketConfig().GetLinkTimeOut());
94 :
95 0 : HcclUs startTime = std::chrono::steady_clock::now();
96 0 : bool isReady = false;
97 0 : while (!isReady) {
98 0 : isReady = transportMgr->IsAllTransportReady();
99 0 : if (isReady) {
100 0 : break;
101 : }
102 0 : if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
103 0 : transportMgr->DumpNotReadyTransportsUrma();
104 0 : RPT_INPUT_ERR(true, "EI0006", std::vector<std::string>({"reason"}),
105 : std::vector<std::string>({"Aiv urma wait transports ready timeout."}));
106 0 : THROW<InternalException>("Aiv WaitTransportReady timeout, commId[%s].",
107 0 : comm->GetId().c_str());
108 : break;
109 : }
110 : }
111 :
112 0 : HCCL_INFO("[AivInsPreprocessor::%s] end.", __func__);
113 0 : }
114 :
115 0 : std::vector<HcclAiRMAWQ> AivInsPreprocessor::GetWqs() const
116 : {
117 0 : HCCL_INFO("[AivInsPreprocessor::%s] start.", __func__);
118 0 : if (protocol_ != 1) {
119 0 : THROW<InvalidParamsException>(
120 0 : StringFormat("can not get wq info when protocol is [%u]", protocol_));
121 : }
122 0 : auto memTransportMgr = comm->GetMemTransportManager();
123 0 : CHECK_NULLPTR(memTransportMgr, "[AivInsPreprocessor::GetWqs] memTransportMgr is nullptr!");
124 0 : return memTransportMgr->GetUrmaWqs();
125 : }
126 :
127 0 : std::vector<HcclAiRMACQ> AivInsPreprocessor::GetCqs() const
128 : {
129 0 : HCCL_INFO("[AivInsPreprocessor::%s] start.", __func__);
130 0 : if (protocol_ != 1) {
131 0 : THROW<InvalidParamsException>(
132 0 : StringFormat("can not get cq info when protocol is [%u]", protocol_));
133 : }
134 0 : auto memTransportMgr = comm->GetMemTransportManager();
135 0 : CHECK_NULLPTR(memTransportMgr, "[AivInsPreprocessor::GetCqs] memTransportMgr is nullptr!");
136 0 : return memTransportMgr->GetUrmaCqs();
137 : }
138 :
139 238 : AivInsPreprocessor::~AivInsPreprocessor()
140 : {
141 238 : }
142 :
143 : } // namespace Hccl
|