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 "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 7 : CollAlgRegistry* CollAlgRegistry::Global()
22 : {
23 7 : static CollAlgRegistry* globalAlgImplRegistry = new (std::nothrow) CollAlgRegistry;
24 7 : if (globalAlgImplRegistry == nullptr) {
25 0 : HCCL_ERROR("[Alg][Registry] allocate global CollAlgRegistry failed, out of memory.");
26 0 : return nullptr;
27 : }
28 7 : return globalAlgImplRegistry;
29 : }
30 :
31 : HcclResult
32 7 : CollAlgRegistry::Register(const OpType type, const std::string& funcName, const CollAlgCreator& collAlgCreator)
33 : {
34 7 : const std::lock_guard<std::mutex> lock(mu_);
35 7 : 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 7 : impls_[type].emplace(funcName, collAlgCreator);
40 7 : return HcclResult::HCCL_SUCCESS;
41 7 : }
42 :
43 0 : void CollAlgRegistry::PrintAllImpls()
44 : {
45 0 : for (auto& iter : impls_) {
46 0 : HCCL_DEBUG("-------------------------------------");
47 0 : HCCL_DEBUG("type name is %s", iter.first.Describe().c_str());
48 0 : for (auto& alg : iter.second) {
49 0 : HCCL_DEBUG(" alg name is %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>> CollAlgRegistry::GetAvailAlgs()
58 : {
59 0 : std::map<OpType, std::vector<std::string>> algs;
60 0 : for (auto& iter : impls_) {
61 0 : HCCL_DEBUG("-------------------------------------");
62 0 : HCCL_DEBUG("type name is %s", iter.first.Describe().c_str());
63 0 : std::vector<std::string> tmpAvailAlgs;
64 0 : for (auto& alg : iter.second) {
65 0 : HCCL_DEBUG(" alg name is %s", alg.first.c_str());
66 0 : tmpAvailAlgs.push_back(alg.first);
67 0 : if (alg.second == nullptr) {
68 0 : HCCL_DEBUG(" alg func is nullptr");
69 : }
70 : }
71 0 : algs.insert(std::make_pair(iter.first, tmpAvailAlgs));
72 0 : }
73 0 : return algs;
74 0 : }
75 :
76 0 : std::shared_ptr<CollAlgBase> CollAlgRegistry::GetAlgImpl(const OpType type, const std::string& funcName)
77 : {
78 0 : if (impls_.count(type) == 0 || impls_[type].count(funcName) == 0) {
79 0 : HCCL_ERROR("%s:%s is not registered.", type.Describe().c_str(), funcName.c_str());
80 0 : return nullptr;
81 : }
82 0 : return std::shared_ptr<CollAlgBase>(impls_[type][funcName]());
83 : }
84 : } // namespace Hccl
|