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_res.h"
12 : #include "log.h"
13 : #include "hccl_comm_pub.h"
14 : #include "independent_op.h"
15 : #include "channel_manager.h"
16 : #include "hcomm_c_adpt.h"
17 : #include "param_check_pub.h"
18 : #include "hccl_one_sided_conn.h"
19 : #include <array>
20 : #include <vector>
21 :
22 : using namespace hccl;
23 :
24 0 : HcclResult HcclChannelGetNotifyNum(HcclComm comm, ChannelHandle channel, uint32_t* notifyNum)
25 : {
26 0 : CHK_PTR_NULL(notifyNum);
27 0 : CHK_PTR_NULL(comm);
28 :
29 0 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
30 0 : HcclResult ret = HCCL_SUCCESS;
31 0 : if (hcclComm->IsCommunicatorV2()) {
32 0 : ret = static_cast<HcclResult>(HcommChannelGetNotifyNum(channel, notifyNum));
33 : } else {
34 0 : auto& channelMgr = hcclComm->GetIndependentOp().GetChannelManager();
35 0 : ret = channelMgr.ChannelCommGetNotifyNum(channel, notifyNum);
36 : }
37 :
38 0 : if (ret != HCCL_SUCCESS) {
39 0 : HCCL_ERROR(
40 : "[%s] Failed to get channel notifyNum, group[%s], channel[%llu], ret[%d]", __func__,
41 : hcclComm->GetIdentifier().c_str(), static_cast<unsigned long long>(channel), ret);
42 0 : return ret;
43 : }
44 :
45 0 : HCCL_RUN_INFO(
46 : "[%s] get channel notifyNum success, group[%s], channel[%llu], notifyNum[%u], ret[%d]", __func__,
47 : hcclComm->GetIdentifier().c_str(), static_cast<unsigned long long>(channel), *notifyNum, ret);
48 0 : return HCCL_SUCCESS;
49 : }
50 :
51 0 : HcclResult CommChannelDestroy(HcclComm comm, ChannelHandle* channelList, uint32_t channelNum)
52 : {
53 0 : CHK_PTR_NULL(comm);
54 0 : CHK_PTR_NULL(channelList);
55 0 : CHK_PRT_RET(
56 : channelNum == 0, HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
57 0 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
58 0 : HcclResult ret = HCCL_SUCCESS;
59 0 : if (hcclComm->IsCommunicatorV2()) {
60 0 : CollComm* collComm = hcclComm->GetCollComm();
61 0 : CHK_PTR_NULL(collComm);
62 0 : ChannelManager* channelMgr = collComm->GetChannelManager();
63 0 : CHK_PTR_NULL(channelMgr);
64 0 : ret = channelMgr->ChannelCommDestroy(channelList, channelNum);
65 : } else {
66 0 : auto& channelMgr = hcclComm->GetIndependentOp().GetChannelManager();
67 0 : ret = channelMgr.ChannelCommDestroy(channelList, channelNum);
68 : }
69 :
70 0 : if (ret != HCCL_SUCCESS) {
71 0 : HCCL_ERROR(
72 : "[%s] Failed to destroy channel, group[%s], channelList[%p], channelNum[%u], ret[%d]", __func__,
73 : hcclComm->GetIdentifier().c_str(), channelList, channelNum, ret);
74 0 : return ret;
75 : }
76 :
77 0 : HCCL_RUN_INFO(
78 : "[%s] destroy channel success, group[%s], channelList[%p], channelNum[%u], ret[%d]", __func__,
79 : hcclComm->GetIdentifier().c_str(), channelList, channelNum, ret);
80 0 : return HCCL_SUCCESS;
81 : }
82 :
83 3 : HcclResult HcclChannelGetHcclBuffer(HcclComm comm, ChannelHandle channel, void** buffer, uint64_t* size)
84 : {
85 3 : CHK_PTR_NULL(comm);
86 2 : CHK_PTR_NULL(buffer);
87 1 : CHK_PTR_NULL(size);
88 : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
89 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
90 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
91 : CollComm* collComm = hcclComm->GetCollComm();
92 : CHK_PTR_NULL(collComm);
93 : auto myRank = collComm->GetMyRank();
94 : CHK_PTR_NULL(myRank);
95 : CHK_RET(myRank->ChannelGetHcclBuffer(channel, buffer, size));
96 : return HCCL_SUCCESS;
97 : }());
98 : #endif
99 0 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
100 0 : CollComm* collComm = hcclComm->GetCollComm();
101 0 : hccl::MyRank* myRank = nullptr;
102 0 : if (collComm != nullptr) {
103 0 : myRank = collComm->GetMyRank();
104 : }
105 0 : if (collComm != nullptr && hcclComm->GetConnectMode() != 0 && myRank != nullptr) {
106 0 : CHK_RET(myRank->ChannelGetHcclBuffer(channel, buffer, size));
107 0 : return HCCL_SUCCESS;
108 : }
109 :
110 : CommBuffer commBuffer;
111 0 : auto& channelMgr = hcclComm->GetIndependentOp().GetChannelManager();
112 0 : HcclResult ret = channelMgr.ChannelCommGetHcclBuffer(channel, &commBuffer);
113 0 : if (ret != HCCL_SUCCESS) {
114 0 : HCCL_ERROR(
115 : "[%s] Failed to get channel hccl buffer, group[%s], channel[%llu], ret[%d]", __func__,
116 : hcclComm->GetIdentifier().c_str(), static_cast<unsigned long long>(channel), ret);
117 0 : return ret;
118 : }
119 0 : *buffer = commBuffer.addr;
120 0 : *size = commBuffer.size;
121 :
122 0 : HCCL_RUN_INFO(
123 : "[%s] get channel hccl buffer success, group[%s], channel[%llu], "
124 : "buffer[type:%d, addr:%p, size:%llu], ret[%d]",
125 : __func__, hcclComm->GetIdentifier().c_str(), static_cast<unsigned long long>(channel), commBuffer.type,
126 : commBuffer.addr, static_cast<unsigned long long>(commBuffer.size), ret);
127 0 : return HCCL_SUCCESS;
128 : }
129 :
130 : HcclResult
131 0 : HcclChannelGetRemoteMems(HcclComm comm, ChannelHandle channel, uint32_t* memNum, CommMem** remoteMems, char*** memTags)
132 : {
133 0 : CHK_PTR_NULL(comm);
134 0 : CHK_PTR_NULL(remoteMems);
135 0 : CHK_PTR_NULL(memTags);
136 0 : CHK_PTR_NULL(memNum);
137 :
138 : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
139 0 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
140 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
141 : CollComm* collComm = hcclComm->GetCollComm();
142 : CHK_PTR_NULL(collComm);
143 : auto myRank = collComm->GetMyRank();
144 : CHK_PTR_NULL(myRank);
145 : CHK_RET(myRank->ChannelGetRemoteMems(channel, memNum, remoteMems, memTags));
146 : return HCCL_SUCCESS;
147 : }());
148 : #endif
149 :
150 0 : hccl::hcclComm* hcclComm = static_cast<hccl::hcclComm*>(comm);
151 0 : std::string commId = hcclComm->GetIdentifier();
152 0 : HCCL_RUN_INFO("legacy Entry-%s:comm[%s]", __func__, commId.c_str());
153 0 : HcclMem* remoteMem = nullptr;
154 : HcclResult ret
155 0 : = hcclComm->GetIndependentOp().GetChannelManager().ChannelCommGetRemoteMem(channel, &remoteMem, memNum);
156 0 : CHK_PRT_RET(
157 : ret != HCCL_SUCCESS,
158 : HCCL_ERROR("[HcclChannelGetRemoteMems]legacy failed. channel[%llu], ret[%d]", channel, ret), ret);
159 0 : *remoteMems = reinterpret_cast<CommMem*>(remoteMem);
160 0 : if (*memNum > 0) {
161 : // A2/A3 tag为非真实tag,无法获取到,统一使用固定字符串。
162 : static const char* HCCL_BUFFER_TAG = "HcclBuffer";
163 : static thread_local std::array<char*, MAX_REMOTE_MEM_NUM> tagPtrs;
164 0 : CHK_PRT_RET(
165 : *memNum > MAX_REMOTE_MEM_NUM,
166 : HCCL_ERROR("[HcclChannelGetRemoteMems] memNum[%u] exceeds max[%u]", *memNum, MAX_REMOTE_MEM_NUM),
167 : HCCL_E_PARA);
168 0 : for (uint32_t i = 0; i < *memNum; ++i) {
169 0 : tagPtrs[i] = const_cast<char*>(HCCL_BUFFER_TAG);
170 : }
171 0 : *memTags = tagPtrs.data();
172 : } else {
173 0 : *memTags = nullptr;
174 : }
175 0 : HCCL_INFO("[HcclChannelGetRemoteMems]legacy success: memNum[%u]", *memNum);
176 0 : return HCCL_SUCCESS;
177 0 : }
|