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_ccu_res.h"
12 :
13 : #include <mutex>
14 :
15 : #include "hccl_comm_pub.h"
16 :
17 : #include "exception_handler.h"
18 : #include "ccu_instance_mgr.h"
19 :
20 : /**
21 : * @note 职责:集合通信的通信域CCU管理的C接口的C到C++适配
22 : */
23 0 : HcclResult HcclCommQueryCcuIns(HcclComm comm, CcuInsHandle* insHandles, uint32_t* insNum)
24 : {
25 : EXCEPTION_HANDLE_BEGIN
26 :
27 0 : HcclUs startut = TIME_NOW();
28 :
29 0 : CHK_PTR_NULL(comm);
30 0 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
31 0 : const auto& commId = hcclComm->GetIdentifier();
32 0 : HCCL_INFO("[%s] CommId[%s] query ccu instance.", __func__, commId.c_str());
33 :
34 0 : CHK_PTR_NULL(insHandles);
35 0 : CHK_PTR_NULL(insNum);
36 :
37 : // CCU不支持A5之前代际
38 0 : if (!hcclComm->IsCommunicatorV2()) {
39 0 : HCCL_WARNING("[%s] is not supported.", __func__);
40 0 : return HcclResult::HCCL_E_NOT_SUPPORT;
41 : }
42 :
43 0 : auto* collComm = hcclComm->GetCollComm();
44 0 : CHK_PTR_NULL(collComm);
45 0 : auto* myRank = collComm->GetMyRank();
46 0 : CHK_PTR_NULL(myRank);
47 :
48 : // 非CCU通信域允许查询,不认为是错误
49 0 : auto ccuInsHandle = myRank->GetCcuInstance();
50 0 : if (ccuInsHandle == 0) {
51 0 : auto opExpansionMode = myRank->GetOpExpansionMode();
52 0 : HCCL_WARNING(
53 : "[%s] failed to get ccu instance, commId[%s] op expansion mode[%u].", __func__, commId.c_str(),
54 : opExpansionMode);
55 0 : return HcclResult::HCCL_E_UNAVAIL;
56 : }
57 :
58 0 : insHandles[0] = ccuInsHandle;
59 0 : *insNum = 1;
60 0 : HCCL_INFO("[%s] success, take time [%lld]us.", __func__, DURATION_US(TIME_NOW() - startut).count());
61 :
62 0 : EXCEPTION_HANDLE_END
63 0 : return HcclResult::HCCL_SUCCESS;
64 : }
65 :
66 13 : HcclResult HcclCommAssignCcuIns(HcclComm comm, CcuInsHandle insHandle)
67 : {
68 : EXCEPTION_HANDLE_BEGIN
69 :
70 13 : HcclUs startut = TIME_NOW();
71 :
72 21 : CHK_PTR_NULL(comm);
73 12 : auto* hcclComm = static_cast<hccl::hcclComm*>(comm);
74 12 : const auto& commId = hcclComm->GetIdentifier();
75 12 : HCCL_INFO(
76 : "[%s] CommId[%s] assign ccu instance[%llu].", __func__, commId.c_str(),
77 : static_cast<unsigned long long>(insHandle));
78 :
79 12 : if (insHandle == 0) {
80 1 : HCCL_ERROR(
81 : "[%s] failed, commId[%s] insHandle[%llu] is invalid.", __func__, commId.c_str(),
82 : static_cast<unsigned long long>(insHandle));
83 1 : return HcclResult::HCCL_E_PARA;
84 : }
85 :
86 11 : if (!hcclComm->IsCommunicatorV2()) {
87 1 : HCCL_WARNING("[%s] is not supported.", __func__);
88 1 : return HcclResult::HCCL_E_NOT_SUPPORT;
89 : }
90 :
91 10 : auto* collComm = hcclComm->GetCollComm();
92 10 : CHK_PTR_NULL(collComm);
93 9 : auto* myRank = collComm->GetMyRank();
94 9 : CHK_PTR_NULL(myRank);
95 :
96 : {
97 : // 仅保证多个 Assign 调用之间并发安全,Query 和通信域销毁由调用方保证不与 Assign 并发。
98 : static std::mutex assignCcuInsMutex;
99 8 : std::lock_guard<std::mutex> lock(assignCcuInsMutex);
100 :
101 8 : auto oldInsHandle = myRank->GetCcuInstance();
102 8 : if (oldInsHandle != 0) {
103 3 : HCCL_ERROR(
104 : "[%s] failed, commId[%s] already has ccu instance[%llu], "
105 : "new instance[%llu] will not be assigned.",
106 : __func__, commId.c_str(), static_cast<unsigned long long>(oldInsHandle),
107 : static_cast<unsigned long long>(insHandle));
108 3 : return HcclResult::HCCL_E_PARA;
109 : }
110 :
111 5 : const auto devLogicId = collComm->GetDeviceLogicId();
112 5 : auto* ccuIns = hcomm::CcuInstanceMgr::GetInstance(devLogicId).Get(insHandle);
113 4 : if (ccuIns == nullptr) {
114 1 : HCCL_ERROR(
115 : "[%s] failed, commId[%s] ccu instance[%llu] is not found.", __func__, commId.c_str(),
116 : static_cast<unsigned long long>(insHandle));
117 1 : return HcclResult::HCCL_E_NOT_FOUND;
118 : }
119 :
120 3 : myRank->SetCcuInstance(insHandle);
121 8 : }
122 :
123 3 : HCCL_INFO(
124 : "[%s] success, commId[%s] ccu instance[%llu], take time [%lld]us.", __func__, commId.c_str(),
125 : static_cast<unsigned long long>(insHandle), DURATION_US(TIME_NOW() - startut));
126 :
127 13 : EXCEPTION_HANDLE_END
128 3 : return HcclResult::HCCL_SUCCESS;
129 : }
|