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 <atomic>
12 : #include <unordered_map>
13 : #include <mutex>
14 : #include <memory>
15 : #include <vector>
16 : #include <string>
17 : #include "hccl/hccl_res.h"
18 : #include "hccl_mem.h"
19 : #include "stream_pub.h"
20 : #include "hccl_communicator.h"
21 : #include "hccl_comm_pub.h"
22 : #include "param_check_pub.h"
23 : #include "op_base.h"
24 : #include "hccl_res.h"
25 : #include "symmetric_memory/symmetric_memory.h"
26 : #include "hccl_team_c_adpt.h"
27 :
28 : using namespace hccl;
29 :
30 11 : HcclResult HcclCommMemReg(HcclComm comm, const char* memTag, const CommMem* mem, HcclMemHandle* memHandle)
31 :
32 : {
33 11 : CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[HcclCommMemReg]comm is null"), HCCL_E_PTR);
34 10 : CHK_PRT_RET(memTag == nullptr, HCCL_ERROR("[HcclCommMemReg]memTag is null"), HCCL_E_PTR);
35 9 : CHK_PRT_RET(
36 : strlen(memTag) == 0 || strlen(memTag) > HCCL_RES_TAG_MAX_LEN,
37 : HCCL_ERROR("[HcclCommMemReg]memTag length is %zu", strlen(memTag)), HCCL_E_PARA);
38 7 : std::string memTagStr(memTag);
39 7 : CHK_PRT_RET(
40 : memTagStr.compare(0, strlen(HCCL_SYMMETRIC_MEMORY_TAG_PREFIX), HCCL_SYMMETRIC_MEMORY_TAG_PREFIX) == 0,
41 : HCCL_ERROR(
42 : "[HcclCommMemReg]memTag[%s] uses reserved symmetric memory prefix[%s]", memTag,
43 : HCCL_SYMMETRIC_MEMORY_TAG_PREFIX),
44 : HCCL_E_PARA);
45 7 : CHK_PRT_RET(
46 : memTagStr.compare(0, strlen(HCCL_TEAM_SYNCMEM_TAG_PREFIX), HCCL_TEAM_SYNCMEM_TAG_PREFIX) == 0,
47 : HCCL_ERROR(
48 : "[HcclCommMemReg]memTag[%s] uses reserved team syncmem prefix[%s]", memTag, HCCL_TEAM_SYNCMEM_TAG_PREFIX),
49 : HCCL_E_PARA);
50 7 : CHK_PRT_RET(
51 : memTagStr.compare(0, strlen(HCCL_TEAM_USERMEM_TAG_PREFIX), HCCL_TEAM_USERMEM_TAG_PREFIX) == 0,
52 : HCCL_ERROR(
53 : "[HcclCommMemReg]memTag[%s] uses reserved team usermem prefix[%s]", memTag, HCCL_TEAM_USERMEM_TAG_PREFIX),
54 : HCCL_E_PARA);
55 7 : CHK_PRT_RET(mem == nullptr, HCCL_ERROR("[HcclCommMemReg]mem is null"), HCCL_E_PTR);
56 6 : CHK_PRT_RET(memHandle == nullptr, HCCL_ERROR("[HcclCommMemReg]memHandle is null"), HCCL_E_PTR);
57 5 : CHK_PRT_RET(
58 : (mem->type != COMM_MEM_TYPE_DEVICE) && (mem->type != COMM_MEM_TYPE_HOST),
59 : HCCL_ERROR("[HcclCommMemReg]memoryType[%d] must be device or host", mem->type), HCCL_E_PARA);
60 4 : CHK_PRT_RET(mem->addr == nullptr, HCCL_ERROR("[HcclCommMemReg]addr is null"), HCCL_E_PTR);
61 3 : CHK_PRT_RET(
62 : mem->size == 0, HCCL_ERROR("[HcclCommMemReg]size[%llu] invalid", static_cast<unsigned long long>(mem->size)),
63 : HCCL_E_PARA);
64 :
65 : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
66 4 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
67 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
68 : std::string commId = hcclComm->GetIdentifier();
69 : HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
70 : hccl::CollComm* collComm = hcclComm->GetCollComm();
71 : CHK_PTR_NULL(collComm);
72 : auto myRank = collComm->GetMyRank();
73 : CHK_PTR_NULL(myRank);
74 : CommMems* commMem = myRank->GetCommMems();
75 : HcclResult ret = HCCL_SUCCESS;
76 : ret = commMem->CommRegMem(memTagStr, *mem, memHandle);
77 : CHK_PRT_RET(
78 : ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommMemReg]Bind failed. memTag[%s], ret[%d]", memTag, ret), ret);
79 : HCCL_INFO("[HcclCommMemReg] success: raw handle[%p]", *memHandle);
80 : return HCCL_SUCCESS;
81 : }());
82 : #endif
83 0 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
84 0 : CHK_PTR_NULL(hcclComm);
85 : HcclMem hcclMem;
86 0 : hcclMem.addr = mem->addr;
87 0 : hcclMem.size = mem->size;
88 0 : hcclMem.type = (mem->type == COMM_MEM_TYPE_DEVICE) ? HCCL_MEM_TYPE_DEVICE : HCCL_MEM_TYPE_HOST;
89 : HcclRegMemAttr attr;
90 0 : attr.value = 0;
91 0 : HcclResult ret = hcclComm->GetIndependentOp().GetCommMemMgr().CommRegMem(memTagStr, hcclMem, attr, memHandle);
92 0 : CHK_PRT_RET(
93 : ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommMemReg]legcy Bind failed. memTag[%s], ret[%d]", memTag, ret), ret);
94 0 : HCCL_INFO("[HcclCommMemReg]legcy success: raw handle[%p]", *memHandle);
95 :
96 0 : return HCCL_SUCCESS;
97 7 : }
98 :
99 0 : HcclResult HcclCommDeregMem(HcclComm comm, const char* memTag, const void* memHandle)
100 : {
101 0 : CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[HcclCommDeregMem]comm is null"), HCCL_E_PTR);
102 0 : CHK_PRT_RET(memHandle == nullptr, HCCL_ERROR("[HcclCommDeregMem]memHandle is null"), HCCL_E_PTR);
103 0 : CHK_PRT_RET(memTag == nullptr, HCCL_ERROR("[HcclCommDeregMem]memTag is null"), HCCL_E_PARA);
104 0 : CHK_PRT_RET(strlen(memTag) == 0, HCCL_ERROR("[HcclCommDeregMem]memTag length is 0"), HCCL_E_PARA);
105 :
106 0 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
107 0 : std::string commId = hcclComm->GetIdentifier();
108 0 : HCCL_RUN_INFO("Entry-%s: comm[%s], handle[%p]", __func__, commId.c_str(), memHandle);
109 :
110 : // 解绑某算子下的该句柄
111 0 : HcclResult ret = HCCL_SUCCESS;
112 0 : if (hcclComm->IsCommunicatorV2()) {
113 0 : hccl::CollComm* collComm = hcclComm->GetCollComm();
114 0 : CHK_PTR_NULL(collComm);
115 0 : CommMemMgr* commMemMgr = collComm->GetCommMemMgr();
116 0 : CHK_PTR_NULL(commMemMgr);
117 0 : ret = commMemMgr->CommUnregMem(std::string(memTag), memHandle);
118 : } else {
119 0 : auto& commMemMgr = hcclComm->GetIndependentOp().GetCommMemMgr();
120 0 : ret = commMemMgr.CommUnregMem(std::string(memTag), memHandle);
121 : }
122 :
123 0 : CHK_PRT_RET(
124 : ret == HCCL_E_NOT_FOUND, HCCL_WARNING("[HcclCommDeregMem]handle not bound in this domain. raw[%p]", memHandle),
125 : HCCL_SUCCESS);
126 0 : CHK_PRT_RET(
127 : ret != HCCL_SUCCESS, HCCL_ERROR("[HcclCommDeregMem] unBind failed. handle[%p], ret[%d]", memHandle, ret), ret);
128 0 : HCCL_INFO("[HcclCommDeregMem]success: raw handle[%p]", memHandle);
129 0 : return HCCL_SUCCESS;
130 0 : }
131 :
132 5 : HcclResult GetHcclBufferWithClearFlag(HcclComm comm, void** buffer, uint64_t* size, bool clearFlag)
133 : {
134 5 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
135 5 : const std::string& commId = hcclComm->GetIdentifier();
136 5 : hccl::CollComm* collComm = hcclComm->GetCollComm();
137 5 : CHK_PTR_NULL(collComm);
138 4 : auto myRank = collComm->GetMyRank();
139 4 : CHK_PTR_NULL(myRank);
140 3 : CommMems* commMem = myRank->GetCommMems();
141 3 : CHK_PTR_NULL(commMem);
142 2 : CHK_RET(commMem->GetHcclBuffer(*buffer, *size));
143 2 : CHK_RET(commMem->HcclBufferMemset(*buffer, *size, clearFlag));
144 :
145 2 : return HCCL_SUCCESS;
146 5 : }
147 :
148 17 : HcclResult HcclGetHcclBuffer(HcclComm comm, void** buffer, uint64_t* size)
149 : {
150 17 : CHK_PRT_RET(buffer == nullptr, HCCL_ERROR("[%s] buffer is null", __func__), HCCL_E_PTR);
151 14 : CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[%s] comm is null", __func__), HCCL_E_PTR);
152 13 : CHK_PRT_RET(size == nullptr, HCCL_ERROR("[%s] size is null", __func__), HCCL_E_PTR);
153 :
154 : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
155 15 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
156 : return GetHcclBufferWithClearFlag(comm, buffer, size, false);
157 : }());
158 : #endif
159 :
160 7 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
161 7 : CollComm* collComm = hcclComm->GetCollComm();
162 7 : hccl::MyRank* myRank = nullptr;
163 7 : if (collComm != nullptr) {
164 0 : myRank = collComm->GetMyRank();
165 : }
166 7 : if (collComm != nullptr && hcclComm->GetConnectMode() != 0 && myRank != nullptr) {
167 0 : CommMems* commMem = myRank->GetCommMems();
168 0 : CHK_PTR_NULL(commMem);
169 0 : CHK_RET(commMem->GetHcclBuffer(*buffer, *size));
170 0 : return HCCL_SUCCESS;
171 : }
172 :
173 7 : std::string commId = hcclComm->GetIdentifier();
174 7 : HCCL_RUN_INFO("Entry-%s:comm[%s]", __func__, commId.c_str());
175 7 : HcclResult ret = HCCL_SUCCESS;
176 : CommBuffer commBuffer;
177 :
178 7 : auto& commMemMgr = hcclComm->GetIndependentOp().GetCommMemMgr();
179 7 : ret = commMemMgr.GetHcclBuffer(&commBuffer);
180 7 : if (ret != HCCL_SUCCESS) {
181 0 : HCCL_ERROR("[%s] Failed to get local cclBuffer ret[%d]", __func__, ret);
182 0 : return ret;
183 : }
184 7 : *buffer = commBuffer.addr;
185 7 : *size = commBuffer.size;
186 7 : HCCL_RUN_INFO(
187 : "Entry-%s: success: comm[%s], buffer[%p] size[%llu]", __func__, commId.c_str(), *buffer,
188 : static_cast<unsigned long long>(*size));
189 7 : return HCCL_SUCCESS;
190 7 : }
191 :
192 1 : HcclResult HcclGetHcclBufferCleared(HcclComm comm, void** buffer, uint64_t* size)
193 : {
194 1 : CHK_PRT_RET(buffer == nullptr, HCCL_ERROR("[%s] buffer is null", __func__), HCCL_E_PTR);
195 1 : CHK_PRT_RET(comm == nullptr, HCCL_ERROR("[%s] comm is null", __func__), HCCL_E_PTR);
196 1 : CHK_PRT_RET(size == nullptr, HCCL_ERROR("[%s] size is null", __func__), HCCL_E_PTR);
197 :
198 : #if (!defined(HCCD)) && (!defined(CCL_KERNEL_AICPU))
199 2 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
200 : return GetHcclBufferWithClearFlag(comm, buffer, size, true);
201 : }());
202 : #endif
203 :
204 0 : HCCL_ERROR("HcclGetHcclBufferCleared is not supported");
205 0 : return HCCL_E_NOT_SUPPORT;
206 : }
|