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 "hccl/hccl_ex.h"
12 : #include "hccl_types.h"
13 : #include "hccl_comm_conn.h"
14 : #include "hccl_comm_conn_mgr.h"
15 :
16 : using namespace std;
17 : using namespace hccl;
18 :
19 0 : HcclResult HcclRawOpen(HcclConn* conn)
20 : {
21 0 : CHK_PTR_NULL(conn);
22 :
23 0 : HCCL_RUN_INFO("Entry %s start conn[%llu]", __func__, hash<HcclConn*>{}(conn));
24 0 : HcclCommConn** comm = reinterpret_cast<HcclCommConn**>(conn);
25 0 : CHK_RET(HcclCommConnMgr::GetInstance().AddAndGetCommConn(*comm));
26 0 : HCCL_RUN_INFO("%s success conn[%llu]", __func__, hash<void*>{}(*comm));
27 :
28 0 : return HCCL_SUCCESS;
29 : }
30 :
31 0 : HcclResult HcclRawClose(HcclConn conn)
32 : {
33 0 : CHK_PTR_NULL(conn);
34 :
35 0 : HCCL_RUN_INFO("Entry %s start conn[%llu]", __func__, hash<void*>{}(conn));
36 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
37 0 : CHK_RET(HcclCommConnMgr::GetInstance().DelCommConn(comm));
38 0 : HCCL_RUN_INFO("%s success conn[%llu]", __func__, hash<void*>{}(conn));
39 :
40 0 : return HCCL_SUCCESS;
41 : }
42 :
43 0 : HcclResult HcclRawForceClose(HcclConn conn)
44 : {
45 0 : CHK_PTR_NULL(conn);
46 :
47 0 : HCCL_RUN_INFO("Entry %s start conn[%llu]", __func__, hash<void*>{}(conn));
48 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
49 0 : comm->SetForceClose();
50 0 : CHK_RET(HcclCommConnMgr::GetInstance().DelCommConn(comm));
51 0 : HCCL_RUN_INFO("%s success conn[%llu]", __func__, hash<void*>{}(conn));
52 :
53 0 : return HCCL_SUCCESS;
54 : }
55 :
56 0 : HcclResult HcclRawConnect(HcclConn conn, HcclAddr* connectAddr)
57 : {
58 0 : CHK_PTR_NULL(conn);
59 0 : CHK_PTR_NULL(connectAddr);
60 :
61 0 : HCCL_DEBUG("Entry %s start", __func__);
62 0 : if (HcclCommConnMgr::GetInstance().IsExistCommConn(*connectAddr)) {
63 0 : HCCL_ERROR(
64 : "cur client to remote ip[%s] port[%u] comm conn is exist.",
65 : HcclIpAddress((*connectAddr).info.tcp.ipv4Addr).GetReadableIP(), (*connectAddr).info.tcp.port);
66 0 : return HCCL_E_UNAVAIL;
67 : }
68 :
69 0 : if (HcclCommConnMgr::GetInstance().IsExceedMaxLinkNum(CLIENT_ROLE_SOCKET)) {
70 0 : HCCL_ERROR("The maximum number of communication connections that can be created is %u.", MAX_CONN_LINK_NUM);
71 0 : return HCCL_E_UNAVAIL;
72 : }
73 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
74 0 : CHK_RET(comm->Connect(*connectAddr));
75 0 : HcclCommConnMgr::GetInstance().InsertConnectCommMap(*connectAddr, conn);
76 0 : HCCL_RUN_INFO(
77 : "%s success conn[%llu] connectAddr[%llu]", __func__, hash<void*>{}(conn), hash<HcclAddr*>{}(connectAddr));
78 :
79 0 : return HCCL_SUCCESS;
80 : }
81 :
82 0 : HcclResult HcclRawBind(HcclConn conn, HcclAddr* bindAddr)
83 : {
84 0 : CHK_PTR_NULL(conn);
85 0 : CHK_PTR_NULL(bindAddr);
86 :
87 0 : HCCL_RUN_INFO("Entry %s start", __func__);
88 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
89 0 : CHK_RET(comm->Bind(*bindAddr));
90 0 : HCCL_RUN_INFO("%s success conn[%llu] bindAddr[%llu]", __func__, hash<void*>{}(conn), hash<HcclAddr*>{}(bindAddr));
91 :
92 0 : return HCCL_SUCCESS;
93 : }
94 :
95 0 : HcclResult HcclRawListen(HcclConn conn, int backLog)
96 : {
97 0 : CHK_PTR_NULL(conn);
98 :
99 0 : HCCL_RUN_INFO("Entry %s start conn[%llu]", __func__, hash<void*>{}(conn));
100 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
101 0 : CHK_RET(comm->Listen(backLog));
102 0 : HCCL_RUN_INFO("%s success", __func__);
103 :
104 0 : return HCCL_SUCCESS;
105 : }
106 :
107 0 : HcclResult HcclRawAccept(HcclConn conn, HcclAddr* acceptAddr, HcclConn* acceptConn)
108 : {
109 0 : CHK_PTR_NULL(conn);
110 0 : CHK_PTR_NULL(acceptAddr);
111 0 : CHK_PTR_NULL(acceptConn);
112 :
113 0 : HCCL_DEBUG("Entry %s start", __func__);
114 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
115 0 : HcclCommConn** newConn = reinterpret_cast<HcclCommConn**>(acceptConn);
116 :
117 0 : CHK_RET(comm->Accept(*acceptAddr, *newConn));
118 0 : CHK_RET(HcclCommConnMgr::GetInstance().AddCommConn(*newConn));
119 0 : HCCL_RUN_INFO(
120 : "%s success conn[%llu] acceptAddr[%llu] acceptConn[%llu]", __func__, hash<void*>{}(conn),
121 : hash<HcclAddr*>{}(acceptAddr), hash<void*>{}(*newConn));
122 :
123 0 : return HCCL_SUCCESS;
124 : }
125 :
126 0 : HcclResult HcclRawIsend(const void* buf, int count, HcclDataType dataType, HcclConn conn, HcclRequest* request)
127 : {
128 0 : CHK_PTR_NULL(conn);
129 0 : CHK_PTR_NULL(request);
130 :
131 0 : HCCL_DEBUG("Entry %s start", __func__);
132 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
133 0 : CHK_RET(comm->Isend(buf, count, dataType, *request));
134 0 : HcclRequestInfo* hcclReq = static_cast<HcclRequestInfo*>(*request);
135 0 : hcclReq->commHandle = comm;
136 0 : HCCL_DEBUG("%s success", __func__);
137 :
138 0 : return HCCL_SUCCESS;
139 : }
140 :
141 0 : HcclResult HcclRawImprobe(HcclConn conn, int* flag, HcclMessage* msg, HcclStatus* status)
142 : {
143 0 : CHK_PTR_NULL(conn);
144 0 : CHK_PTR_NULL(flag);
145 0 : CHK_PTR_NULL(msg);
146 0 : CHK_PTR_NULL(status);
147 :
148 0 : HCCL_DEBUG("Entry %s start", __func__);
149 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(conn);
150 0 : CHK_RET(comm->Improbe(*flag, *msg, *status));
151 0 : HcclMessageInfo* hcclMsg = static_cast<HcclMessageInfo*>(*msg);
152 0 : if (*flag == HCCL_IMPROBE_COMPLETED) {
153 0 : hcclMsg->commHandle = comm;
154 : }
155 0 : HCCL_DEBUG("%s success", __func__);
156 :
157 0 : return HCCL_SUCCESS;
158 : }
159 0 : HcclResult HcclRawImrecv(void* buf, int count, HcclDataType datatype, HcclMessage* msg, HcclRequest* request)
160 : {
161 0 : CHK_PTR_NULL(msg);
162 0 : CHK_PTR_NULL(*msg);
163 0 : CHK_PTR_NULL(request);
164 :
165 0 : HCCL_DEBUG("Entry %s start", __func__);
166 0 : HcclMessageInfo* hcclMsg = static_cast<HcclMessageInfo*>(*msg);
167 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(hcclMsg->commHandle);
168 0 : CHK_RET(comm->Imrecv(buf, count, datatype, *msg, *request));
169 0 : HcclRequestInfo* hcclReq = static_cast<HcclRequestInfo*>(*request);
170 0 : hcclReq->commHandle = comm;
171 0 : *msg = nullptr;
172 0 : HCCL_DEBUG("%s success", __func__);
173 :
174 0 : return HCCL_SUCCESS;
175 : }
176 :
177 0 : HcclResult HcclRawImrecvScatter(
178 : void* buf[], int count[], int bufCount, HcclDataType datatype, HcclMessage* msg, HcclRequest* request)
179 : {
180 0 : CHK_PTR_NULL(buf);
181 0 : CHK_PTR_NULL(count);
182 0 : CHK_PTR_NULL(msg);
183 0 : CHK_PTR_NULL(*msg);
184 0 : CHK_PTR_NULL(request);
185 :
186 0 : if (bufCount > MAX_SCATTER_BUF_NUM) {
187 0 : HCCL_ERROR("bufCount[%d] should less than %d", bufCount, MAX_SCATTER_BUF_NUM);
188 0 : return HCCL_E_PARA;
189 : }
190 :
191 0 : HCCL_DEBUG("Entry %s start", __func__);
192 0 : HcclMessageInfo* hcclMsg = static_cast<HcclMessageInfo*>(*msg);
193 0 : HcclCommConn* comm = static_cast<HcclCommConn*>(hcclMsg->commHandle);
194 0 : CHK_RET(comm->ImrecvScatter(buf, count, bufCount, datatype, *msg, *request));
195 0 : HcclRequestInfo* hcclReq = static_cast<HcclRequestInfo*>(*request);
196 0 : hcclReq->commHandle = comm;
197 0 : *msg = nullptr;
198 :
199 0 : return HCCL_SUCCESS;
200 : }
201 :
202 0 : HcclResult HcclRawGetCount(const HcclStatus* status, HcclDataType dataType, int* count)
203 : {
204 : // 入参校验
205 0 : CHK_PTR_NULL(status);
206 0 : CHK_PTR_NULL(count);
207 :
208 0 : HCCL_DEBUG("Entry %s start", __func__);
209 0 : if (status->error != 0) {
210 0 : HCCL_WARNING("Failed to obtain the count status[%d].", status->error);
211 0 : return HCCL_E_PARA;
212 : }
213 :
214 0 : *count = status->count;
215 0 : HCCL_DEBUG(
216 : "%s success. peerRank[%d] tag[%d] status[%d] dataType[%s] count[%d].", __func__, status->srcRank, status->tag,
217 : status->error, GetDataTypeEnumStr(dataType).c_str(), *count);
218 :
219 0 : return HCCL_SUCCESS;
220 : }
221 :
222 : HcclResult
223 0 : HcclRawTestSome(int count, HcclRequest requestArray[], int* compCount, int compIndices[], HcclStatus compStatus[])
224 : {
225 : // 入参校验
226 0 : CHK_PTR_NULL(compCount);
227 0 : CHK_PTR_NULL(requestArray);
228 0 : CHK_PTR_NULL(compIndices);
229 0 : CHK_PTR_NULL(compStatus);
230 :
231 0 : HCCL_DEBUG("Entry %s start", __func__);
232 0 : *compCount = 0;
233 0 : bool errorFlag = false;
234 0 : HcclResult ret = HCCL_SUCCESS;
235 0 : for (int i = 0; i < count; ++i) {
236 0 : HcclRequestInfo* hcclReq = reinterpret_cast<HcclRequestInfo*>(requestArray[i]);
237 0 : if (hcclReq == nullptr) {
238 0 : HCCL_INFO("[%d]th hcclRequest is nullptr, no need to testSome", i);
239 0 : continue;
240 : }
241 :
242 0 : HcclCommConn* comm = reinterpret_cast<hccl::HcclCommConn*>(hcclReq->commHandle);
243 0 : CHK_PTR_NULL(comm);
244 :
245 0 : s32 comp = HCCL_TEST_INCOMPLETED;
246 0 : ret = comm->Test(requestArray[i], comp, compStatus[*compCount]);
247 0 : if (ret != HCCL_SUCCESS) {
248 0 : compStatus[*compCount].error = HCCL_E_ROCE_TRANSFER;
249 0 : compIndices[*compCount] = i;
250 0 : errorFlag = true;
251 0 : (*compCount)++;
252 0 : } else if (comp == HCCL_TEST_COMPLETED) {
253 0 : requestArray[i] = nullptr;
254 0 : compIndices[*compCount] = i;
255 0 : compStatus[*compCount].error = HCCL_SUCCESS;
256 0 : (*compCount)++;
257 : }
258 :
259 0 : HCCL_INFO(
260 : "HcclRawTestSome: array[%d/%d] type[%u] flag[%d] compCount[%d] status[%d]", i + 1, count,
261 : hcclReq->transportRequest.requestType, comp, *compCount, hcclReq->transportRequest.status);
262 : }
263 :
264 0 : if (errorFlag) {
265 0 : HCCL_ERROR("HcclRawTestSome: some request link is exception");
266 0 : return HCCL_E_INTERNAL;
267 : }
268 :
269 0 : HCCL_DEBUG("%s success", __func__);
270 :
271 0 : return HCCL_SUCCESS;
272 : }
|