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