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 "ins_coll_alg_registry.h"
12 :
13 : #include <string>
14 : #include <iostream>
15 : #include <map>
16 : #include <mutex>
17 : #include <new>
18 :
19 : namespace Hccl {
20 :
21 121 : InsCollAlgRegistry* InsCollAlgRegistry::Global()
22 : {
23 121 : static InsCollAlgRegistry* globalAlgImplRegistry = new (std::nothrow) InsCollAlgRegistry;
24 121 : if (globalAlgImplRegistry == nullptr) {
25 0 : HCCL_ERROR("[Alg][Registry] allocate global InsCollAlgRegistry failed, out of memory.");
26 0 : return nullptr;
27 : }
28 121 : return globalAlgImplRegistry;
29 : }
30 :
31 : HcclResult
32 121 : InsCollAlgRegistry::Register(const OpType type, const std::string& funcName, const InsCollAlgCreator& collAlgCreator)
33 : {
34 121 : const std::lock_guard<std::mutex> lock(mu_);
35 121 : if (impls_[type].count(funcName) != 0) {
36 0 : HCCL_ERROR("[%d]: [%s] already registered.", type, funcName.c_str());
37 0 : return HcclResult::HCCL_E_INTERNAL;
38 : }
39 121 : impls_[type].emplace(funcName, collAlgCreator);
40 121 : return HcclResult::HCCL_SUCCESS;
41 121 : }
42 :
43 0 : void InsCollAlgRegistry::PrintAllImpls()
44 : {
45 0 : for (auto& iter : impls_) {
46 0 : HCCL_DEBUG("-------------------------------------");
47 0 : HCCL_DEBUG("Optype [%s]", iter.first.Describe().c_str());
48 0 : for (auto& alg : iter.second) {
49 0 : HCCL_DEBUG(" with alg [%s]", alg.first.c_str());
50 0 : if (alg.second == nullptr) {
51 0 : HCCL_DEBUG(" alg func is nullptr");
52 : }
53 : }
54 : }
55 0 : }
56 :
57 0 : std::map<OpType, std::vector<std::string>> InsCollAlgRegistry::GetAvailAlgs()
58 : {
59 0 : std::map<OpType, std::vector<std::string>> algs;
60 0 : for (auto& iter : impls_) {
61 0 : HCCL_DEBUG("OpType [%s]", iter.first.Describe().c_str());
62 0 : std::vector<std::string> tmpAvailAlgs;
63 0 : for (auto& alg : iter.second) {
64 0 : HCCL_DEBUG("AlgName [%s]", alg.first.c_str());
65 0 : tmpAvailAlgs.push_back(alg.first);
66 0 : if (alg.second == nullptr) {
67 0 : HCCL_DEBUG("Alg function is nullptr.");
68 : }
69 : }
70 0 : algs.insert(std::make_pair(iter.first, tmpAvailAlgs));
71 0 : }
72 0 : return algs;
73 0 : }
74 :
75 0 : std::shared_ptr<InsCollAlgBase> InsCollAlgRegistry::GetAlgImpl(const OpType type, const std::string& funcName)
76 : {
77 0 : if (impls_.count(type) == 0 || impls_[type].count(funcName) == 0) {
78 0 : HCCL_WARNING("%s:%s is not registered.", type.Describe().c_str(), funcName.c_str());
79 0 : return nullptr;
80 : }
81 0 : return std::shared_ptr<InsCollAlgBase>(impls_[type][funcName]());
82 : }
83 : } // namespace Hccl
|