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 "alg_template_register.h"
12 :
13 : namespace hccl {
14 :
15 4608 : AlgTemplateRegistry &AlgTemplateRegistry::Instance()
16 : {
17 4608 : static AlgTemplateRegistry globalTempRegistry;
18 4608 : return globalTempRegistry;
19 : }
20 :
21 43 : AlgTemplateRegistry::AlgTemplateRegistry()
22 : {
23 43 : tempCreators_.resize(TemplateType::TEMPLATE_CUSTOM_MAX_NUM, nullptr);
24 43 : }
25 :
26 4429 : HcclResult AlgTemplateRegistry::Register(const TemplateType type, const AlgTemplateCreator &algTemplateCreator)
27 : {
28 4429 : if ((type >= TemplateType::TEMPLATE_NATIVE_MAX_NUM && type <= TemplateType::TEMPLATE_CUSTOM_BEGIN)
29 4429 : || type >= TemplateType::TEMPLATE_CUSTOM_MAX_NUM) {
30 0 : HCCL_ERROR("[AlgTemplateRegistry]template type[%d] out of range.", type);
31 0 : return HcclResult::HCCL_E_INTERNAL;
32 : }
33 :
34 4429 : const std::lock_guard<std::mutex> lock(mu_);
35 4429 : if (tempCreators_[type] != nullptr) {
36 0 : HCCL_ERROR("[AlgTemplateRegistry]template type[%d] already registered.", type);
37 0 : return HcclResult::HCCL_E_INTERNAL;
38 : }
39 4429 : tempCreators_[type] = algTemplateCreator;
40 4429 : HCCL_DEBUG("[AlgTemplateRegistry][Register]Register template by type[%d]", type);
41 4429 : return HcclResult::HCCL_SUCCESS;
42 4429 : }
43 :
44 179 : std::unique_ptr<AlgTemplateBase> AlgTemplateRegistry::GetAlgTemplate(const TemplateType type,
45 : const HcclDispatcher dispatcher)
46 : {
47 179 : if ((type >= TemplateType::TEMPLATE_NATIVE_MAX_NUM && type <= TemplateType::TEMPLATE_CUSTOM_BEGIN)
48 179 : || type >= TemplateType::TEMPLATE_CUSTOM_MAX_NUM) {
49 0 : HCCL_ERROR("[AlgTemplateRegistry]template type[%d] out of range.", type);
50 0 : return nullptr;
51 : }
52 :
53 179 : if (tempCreators_[type] == nullptr) {
54 0 : HCCL_ERROR("[AlgTemplateRegistry]Creator for template type[%d] has not registered.", type);
55 0 : return nullptr;
56 : }
57 179 : HCCL_DEBUG("[AlgTemplateRegistry][GetAlgTemplate]get template by type[%d]", type);
58 179 : return std::unique_ptr<AlgTemplateBase>(tempCreators_[type](dispatcher));
59 : }
60 :
61 : } // namespace hccl
|