Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "rank_graph_builder_bridge.h"
12 :
13 : #include "log.h"
14 :
15 : namespace Hccl {
16 : namespace {
17 : // 槽位属于 hccl_v2;使用函数内静态保证 hcomm 装载期注册时不存在跨 SO 初始化顺序依赖。
18 56 : RankGraphBuilderBridge& BridgeSlot()
19 : {
20 : static RankGraphBuilderBridge bridge{};
21 56 : return bridge;
22 : }
23 : } // namespace
24 :
25 48 : HcclResult RegisterRankGraphBuilderBridge(const RankGraphBuilderBridge& bridge)
26 : {
27 : // 构建、恢复和所有权接管回调必须同时发布,确保返回对象始终能由 hcomm 侧正确释放。
28 48 : if (bridge.buildFromString == nullptr || bridge.buildFromRankTable == nullptr || bridge.recoverBuild == nullptr
29 48 : || bridge.adoptRankGraph == nullptr) {
30 0 : return HCCL_E_PTR;
31 : }
32 48 : auto& registeredBridge = BridgeSlot();
33 : // bridge 在动态库装载期完成一次注册,运行阶段不允许替换回调集合。
34 48 : if (registeredBridge.buildFromString != nullptr || registeredBridge.buildFromRankTable != nullptr
35 48 : || registeredBridge.recoverBuild != nullptr || registeredBridge.adoptRankGraph != nullptr) {
36 0 : HCCL_ERROR("[%s] RankGraphBuilder bridge has already been registered.", __func__);
37 0 : return HCCL_E_INTERNAL;
38 : }
39 48 : registeredBridge = bridge;
40 48 : return HCCL_SUCCESS;
41 : }
42 :
43 8 : const RankGraphBuilderBridge* GetRankGraphBuilderBridge()
44 : {
45 8 : const auto& bridge = BridgeSlot();
46 : // 不向调用方暴露不完整回调表,避免构建成功后缺少匹配的销毁路径。
47 8 : if (bridge.buildFromString == nullptr || bridge.buildFromRankTable == nullptr || bridge.recoverBuild == nullptr
48 8 : || bridge.adoptRankGraph == nullptr) {
49 0 : return nullptr;
50 : }
51 8 : return &bridge;
52 : }
53 : } // namespace Hccl
|