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 : #ifndef HCCLV2_COLL_ALG_REGISTRY
12 : #define HCCLV2_COLL_ALG_REGISTRY
13 :
14 : #include <mutex>
15 : #include <functional>
16 : #include "coll_alg_base.h"
17 : #include "log.h"
18 :
19 : namespace Hccl {
20 :
21 : using CollAlgCreator = std::function<CollAlgBase*()>;
22 : template <typename P>
23 0 : static CollAlgBase* DefaultCreator()
24 : {
25 : static_assert(std::is_base_of<CollAlgBase, P>::value, "CollAlg type must derived from Hccl::CollAlgBase");
26 0 : return new (std::nothrow) P;
27 : }
28 :
29 : class CollAlgRegistry {
30 : public:
31 : static CollAlgRegistry* Global();
32 : HcclResult Register(const OpType type, const std::string& funcName, const CollAlgCreator& collAlgCreator);
33 : void PrintAllImpls();
34 : std::shared_ptr<CollAlgBase> GetAlgImpl(const OpType type, const std::string& funcName);
35 : std::map<OpType, std::vector<std::string>> GetAvailAlgs();
36 :
37 : private:
38 : std::map<OpType, std::map<std::string, const CollAlgCreator>> impls_;
39 : mutable std::mutex mu_;
40 : };
41 :
42 : #define REGISTER_IMPL_HELPER(ctr, type, name, collAlgBase) \
43 : static HcclResult g_func_##name##_##ctr \
44 : = CollAlgRegistry::Global()->Register(type, std::string(#name), DefaultCreator<collAlgBase>)
45 :
46 : #define REGISTER_IMPL_HELPER_1(ctr, type, name, collAlgBase) REGISTER_IMPL_HELPER(ctr, type, name, collAlgBase)
47 :
48 : #define REGISTER_IMPL(type, name, collAlgBase) REGISTER_IMPL_HELPER_1(__COUNTER__, type, name, collAlgBase)
49 :
50 : #define REGISTER_IMPL_BY_TEMP_HELPER(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplate) \
51 : static HcclResult g_func_##name##_##ctr = CollAlgRegistry::Global()->Register( \
52 : type, std::string(#name), DefaultCreator<collAlgBase<AlgTopoMatch, AlgTemplate>>)
53 :
54 : #define REGISTER_IMPL_BY_TEMP_HELPER_1(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplate) \
55 : REGISTER_IMPL_BY_TEMP_HELPER(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplate)
56 :
57 : #define REGISTER_IMPL_BY_TEMP(type, name, collAlgBase, AlgTopoMatch, AlgTemplate) \
58 : REGISTER_IMPL_BY_TEMP_HELPER_1(__COUNTER__, type, name, collAlgBase, AlgTopoMatch, AlgTemplate)
59 :
60 : #define REGISTER_IMPL_BY_TWO_TEMPS_HELPER(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplateRS, AlgTemplateAG) \
61 : static HcclResult g_func_##name##_##ctr = CollAlgRegistry::Global()->Register( \
62 : type, std::string(#name), DefaultCreator<collAlgBase<AlgTopoMatch, AlgTemplateRS, AlgTemplateAG>>)
63 :
64 : #define REGISTER_IMPL_BY_TWO_TEMPS_HELPER_1(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplateRS, AlgTemplateAG) \
65 : REGISTER_IMPL_BY_TWO_TEMPS_HELPER(ctr, type, name, collAlgBase, AlgTopoMatch, AlgTemplateRS, AlgTemplateAG)
66 :
67 : #define REGISTER_IMPL_BY_TWO_TEMPS(type, name, collAlgBase, AlgTopoMatch, AlgTemplateRS, AlgTemplateAG) \
68 : REGISTER_IMPL_BY_TWO_TEMPS_HELPER_1( \
69 : __COUNTER__, type, name, collAlgBase, AlgTopoMatch, AlgTemplateRS, AlgTemplateAG)
70 :
71 : } // namespace Hccl
72 :
73 : #endif
|