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