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 "transport_mem.h"
12 : #include "log.h"
13 : #include "transport_ipc_mem.h"
14 : #include "transport_roce_mem.h"
15 : #ifdef CCL_KERNEL
16 : #include "transport_device_roce_mem.h"
17 : #endif
18 :
19 : namespace hccl {
20 : constexpr u32 INVALID_REMOTE_RANK_ID = 0xFFFFFFFF;
21 0 : TransportMem::TransportMem(const std::unique_ptr<NotifyPool> ¬ifyPool, const HcclNetDevCtx &netDevCtx,
22 0 : const HcclDispatcher &dispatcher, AttrInfo &attrInfo)
23 0 : : TransportMem(notifyPool, netDevCtx, dispatcher, attrInfo, false)
24 0 : {}
25 :
26 0 : TransportMem::TransportMem(const std::unique_ptr<NotifyPool> ¬ifyPool, const HcclNetDevCtx &netDevCtx,
27 0 : const HcclDispatcher &dispatcher, AttrInfo &attrInfo, bool aicpuUnfoldMode)
28 0 : : notifyPool_(notifyPool), netDevCtx_(netDevCtx), dispatcher_(dispatcher), localRankId_(attrInfo.localRankId),
29 0 : remoteRankId_(attrInfo.remoteRankId), aicpuUnfoldMode_(aicpuUnfoldMode)
30 0 : {}
31 :
32 0 : TransportMem::~TransportMem()
33 0 : {}
34 :
35 : // static
36 0 : std::shared_ptr<TransportMem> TransportMem::Create(TpType tpType, const std::unique_ptr<NotifyPool> ¬ifyPool,
37 : const HcclNetDevCtx &netDevCtx, const HcclDispatcher &dispatcher, AttrInfo &attrInfo)
38 : {
39 0 : return Create(tpType, notifyPool, netDevCtx, dispatcher, attrInfo, false);
40 : }
41 :
42 0 : std::shared_ptr<TransportMem> TransportMem::Create(TpType tpType, const std::unique_ptr<NotifyPool> ¬ifyPool,
43 : const HcclNetDevCtx &netDevCtx, const HcclDispatcher &dispatcher, AttrInfo &attrInfo, bool aicpuUnfoldMode)
44 : {
45 0 : std::shared_ptr<TransportMem> transportMemPtr;
46 : #if !defined(CCL_KERNEL) || defined(CCL_LLT)
47 0 : CHK_PRT_RET((netDevCtx == nullptr), HCCL_ERROR("[TransportMem][Create]netDevCtx is null"), nullptr);
48 0 : HCCL_DEBUG("transportMem create tpType:%u netDevCtx:%p dispatcher:%p localRankId:%u remoteRankId:%u sdid:%u " \
49 : "serverId:%u trafficClass:%u serviceLevel:%u", tpType, netDevCtx, dispatcher, attrInfo.localRankId,
50 : attrInfo.remoteRankId, attrInfo.sdid, attrInfo.serverId, attrInfo.trafficClass, attrInfo.serviceLevel);
51 0 : switch (tpType) {
52 0 : case TpType::ROCE:
53 0 : transportMemPtr = std::make_unique<TransportRoceMem>(notifyPool, netDevCtx, dispatcher, attrInfo,
54 0 : aicpuUnfoldMode);
55 0 : break;
56 0 : case TpType::IPC:
57 0 : transportMemPtr = std::make_unique<TransportIpcMem>(notifyPool, netDevCtx, dispatcher, attrInfo,
58 0 : aicpuUnfoldMode);
59 0 : break;
60 0 : default:
61 0 : break;
62 : }
63 : #else
64 : HCCL_ERROR("[TransportMem] The Create interface with qpInfo should be used on the AICPU, tpType[%u]", tpType);
65 : #endif
66 0 : return transportMemPtr;
67 0 : }
68 :
69 0 : std::shared_ptr<TransportMem> TransportMem::Create(TpType tpType, const HcclQpInfoV2 &qpInfo,
70 : const HcclDispatcher &dispatcher, AttrInfo &attrInfo)
71 : {
72 0 : const std::unique_ptr<NotifyPool> notifyPool = nullptr; // dummy for device ibv transport
73 0 : const HcclNetDevCtx netDevCtx = nullptr; // dummy
74 0 : HCCL_DEBUG("[TransportMem] create tpType:%u netDevCtx:%p dispatcher:%p localRankId:%u remoteRankId:%u", tpType,
75 : netDevCtx, dispatcher, attrInfo.localRankId, attrInfo.remoteRankId);
76 0 : std::shared_ptr<TransportMem> transportMemPtr;
77 0 : switch (tpType) {
78 0 : case TpType::ROCE_DEVICE:
79 : #ifdef CCL_KERNEL
80 0 : transportMemPtr = std::make_unique<TransportDeviceRoceMem>(notifyPool, netDevCtx, dispatcher, attrInfo,
81 0 : false, qpInfo); // aicpuUnfoldMode is set by host
82 : #else
83 : HCCL_ERROR("[TransportMem] ROCE_DEVICE Only running on the AICPU");
84 : #endif
85 0 : break;
86 0 : default:
87 0 : HCCL_ERROR("[TransportMem] unsupported TpType[%u] on the AICPU", tpType);
88 0 : break;
89 : }
90 0 : return transportMemPtr;
91 0 : }
92 :
93 0 : HcclResult TransportMem::SetDataSocket(const std::shared_ptr<HcclSocket> &socket)
94 : {
95 0 : dataSocket_ = socket;
96 0 : return HCCL_SUCCESS;
97 : }
98 :
99 0 : HcclResult TransportMem::DoExchangeMemDesc(
100 : const RmaMemDescs &localMemDescs, RmaMemDescs &remoteMemDescs, u32 &actualNumOfRemote)
101 : {
102 0 : HCCL_INFO("[HcclOneSidedConn][ExchangeMemDesc]localRank[%u] exchange memDesc begin, role[%u]", localRankId_, dataSocket_->GetLocalRole());
103 :
104 0 : if (dataSocket_->GetLocalRole() == HcclSocketRole::SOCKET_ROLE_CLIENT) {
105 : // 先收后发
106 0 : CHK_RET(ReceiveRemoteMemDesc(remoteMemDescs, actualNumOfRemote));
107 0 : CHK_RET(SendLocalMemDesc(localMemDescs));
108 : } else {
109 : // 先发后收
110 0 : CHK_RET(SendLocalMemDesc(localMemDescs));
111 0 : CHK_RET(ReceiveRemoteMemDesc(remoteMemDescs, actualNumOfRemote));
112 : }
113 0 : HCCL_INFO("[HcclOneSidedConn][ExchangeMemDesc]get actualNumOfRemotee[%u]", actualNumOfRemote);
114 : // 校验remoteDescs中的remoteRankId和conn对象中保存的localRankId是否一样
115 0 : for (u32 i = 0; i < actualNumOfRemote; i++) {
116 0 : CHK_PTR_NULL((remoteMemDescs.array) + i);
117 0 : u32 tempRankId = remoteMemDescs.array[i].remoteRankId;
118 0 : HCCL_DEBUG("[TransportMem][ExchangeMemDesc]tempRankId:%u, userRank:%u", tempRankId, localRankId_);
119 0 : if (tempRankId == INVALID_REMOTE_RANK_ID) {
120 0 : HCCL_INFO("[DoExchangeMemDesc] It's unnecessary to check remoteID.");
121 0 : continue;
122 : }
123 0 : if (tempRankId != localRankId_) {
124 0 : HCCL_ERROR("[TransportMem][ExchangeMemDesc]localRank[%u] receive remoteMemDesc from wrong localRank[%u], "\
125 : "connection is for localRank[%u]", localRankId_, tempRankId, localRankId_);
126 0 : return HCCL_E_INTERNAL;
127 : }
128 : }
129 0 : return HCCL_SUCCESS;
130 : }
131 :
132 0 : HcclResult TransportMem::SendLocalMemDesc(const RmaMemDescs &localMemDescs)
133 : {
134 0 : HcclResult ret = dataSocket_->Send(&localMemDescs.arrayLength, sizeof(u32));
135 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
136 : HCCL_ERROR("errNo[0x%016llx] localRank[%u] send localMemDesc.arrayLength to remote "\
137 : "failed, ret[%u]", HCCL_ERROR_CODE(ret), localRankId_, ret), ret);
138 0 : HCCL_DEBUG("send localMemDescs.arrayLength:%u", localMemDescs.arrayLength);
139 :
140 0 : if (localMemDescs.arrayLength == 0) {
141 0 : HCCL_INFO("localMemDescs.arrayLength[%u], no need to send data", localMemDescs.arrayLength);
142 : } else {
143 0 : HCCL_DEBUG("send descSize:%u", localMemDescs.arrayLength * sizeof(RmaMemDesc));
144 0 : ret = dataSocket_->Send(localMemDescs.array, localMemDescs.arrayLength * sizeof(RmaMemDesc));
145 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
146 : HCCL_ERROR("errNo[0x%016llx] localRank[%u] send localMemDesc to remote "\
147 : "failed, ret[%u]", HCCL_ERROR_CODE(ret), localRankId_, ret), ret);
148 : }
149 0 : return HCCL_SUCCESS;
150 : }
151 :
152 0 : HcclResult TransportMem::ReceiveRemoteMemDesc(RmaMemDescs &remoteMemDescs, u32 &actualNumOfRemote)
153 : {
154 0 : HcclResult ret = dataSocket_->Recv(&actualNumOfRemote, sizeof(u32));
155 0 : remoteMemDescs.arrayLength = actualNumOfRemote;
156 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
157 : HCCL_ERROR("errNo[0x%016llx] localRank[%u] receive actualNumOfRemote to remote "\
158 : "failed, ret[%u]", HCCL_ERROR_CODE(ret), localRankId_, ret), ret);
159 0 : HCCL_DEBUG("receive actualNumOfRemote:%u", actualNumOfRemote);
160 0 : if (actualNumOfRemote == 0) {
161 0 : HCCL_INFO("actualNumOfRemote[%u], no need to receive data", actualNumOfRemote);
162 : } else {
163 0 : HCCL_DEBUG("receive descSize:%u", actualNumOfRemote * sizeof(RmaMemDesc));
164 0 : ret = dataSocket_->Recv(remoteMemDescs.array, actualNumOfRemote * sizeof(RmaMemDesc));
165 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
166 : HCCL_ERROR("errNo[0x%016llx] localRank[%u] receive remoteMemDesc from remote "\
167 : "failed, ret[%u]", HCCL_ERROR_CODE(ret), localRankId_, ret), ret);
168 : }
169 0 : return HCCL_SUCCESS;
170 : }
171 : } // namespace hccl
|