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_INS_COLL_ALG_REGISTRY
12 : #define HCCLV2_INS_COLL_ALG_REGISTRY
13 :
14 : #include <mutex>
15 : #include <functional>
16 : #include "ins_coll_alg_base.h"
17 : #include "log.h"
18 :
19 : namespace Hccl {
20 :
21 : using InsCollAlgCreator = std::function<InsCollAlgBase*()>;
22 : template <typename P>
23 0 : static InsCollAlgBase* DefaultCreator()
24 : {
25 : static_assert(std::is_base_of<InsCollAlgBase, P>::value, "InsCollAlg type must derived from Hccl::InsCollAlgBase");
26 0 : return new (std::nothrow) P;
27 : }
28 :
29 : class InsCollAlgRegistry {
30 : public:
31 : static InsCollAlgRegistry* Global();
32 : HcclResult Register(const OpType type, const std::string& funcName, const InsCollAlgCreator& collAlgCreator);
33 : void PrintAllImpls();
34 : std::shared_ptr<InsCollAlgBase> 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 InsCollAlgCreator>> impls_;
39 : mutable std::mutex mu_;
40 : };
41 :
42 : #define INS_REGISTER_IMPL_HELPER(ctr, type, name, insCollAlgBase) \
43 : static HcclResult g_func_##name##_##ctr \
44 : = InsCollAlgRegistry::Global()->Register(type, std::string(#name), DefaultCreator<insCollAlgBase>)
45 :
46 : #define INS_REGISTER_IMPL_HELPER_1(ctr, type, name, insCollAlgBase) \
47 : INS_REGISTER_IMPL_HELPER(ctr, type, name, insCollAlgBase)
48 :
49 : #define INS_REGISTER_IMPL(type, name, insCollAlgBase) \
50 : INS_REGISTER_IMPL_HELPER_1(__COUNTER__, type, name, insCollAlgBase)
51 :
52 : #define INS_REGISTER_IMPL_BY_TEMP_HELPER(ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate) \
53 : static HcclResult g_func_##name##_##ctr = InsCollAlgRegistry::Global()->Register( \
54 : type, std::string(#name), DefaultCreator<insCollAlgBase<AlgTopoMatch, InsAlgTemplate>>)
55 :
56 : #define INS_REGISTER_IMPL_BY_TEMP_HELPER_1(ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate) \
57 : INS_REGISTER_IMPL_BY_TEMP_HELPER(ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate)
58 :
59 : #define INS_REGISTER_IMPL_BY_TEMP(type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate) \
60 : INS_REGISTER_IMPL_BY_TEMP_HELPER_1(__COUNTER__, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate)
61 :
62 : #define INS_REGISTER_IMPL_BY_TWO_TEMPS_HELPER( \
63 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1) \
64 : static HcclResult g_func_##name##_##ctr = InsCollAlgRegistry::Global()->Register( \
65 : type, std::string(#name), DefaultCreator<insCollAlgBase<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1>>)
66 :
67 : #define INS_REGISTER_IMPL_BY_TWO_TEMPS_HELPER_1( \
68 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1) \
69 : INS_REGISTER_IMPL_BY_TWO_TEMPS_HELPER( \
70 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1)
71 :
72 : #define INS_REGISTER_IMPL_BY_TWO_TEMPS(type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1) \
73 : INS_REGISTER_IMPL_BY_TWO_TEMPS_HELPER_1( \
74 : __COUNTER__, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1)
75 :
76 : #define INS_REGISTER_IMPL_BY_FOUR_TEMPS_HELPER( \
77 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3) \
78 : static HcclResult g_func_##name##_##ctr = InsCollAlgRegistry::Global()->Register( \
79 : type, std::string(#name), \
80 : DefaultCreator< \
81 : insCollAlgBase<AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3>>)
82 :
83 : #define INS_REGISTER_IMPL_BY_FOUR_TEMPS_HELPER_1( \
84 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3) \
85 : INS_REGISTER_IMPL_BY_FOUR_TEMPS_HELPER( \
86 : ctr, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, \
87 : InsAlgTemplate3)
88 :
89 : #define INS_REGISTER_IMPL_BY_FOUR_TEMPS( \
90 : type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, InsAlgTemplate3) \
91 : INS_REGISTER_IMPL_BY_FOUR_TEMPS_HELPER_1( \
92 : __COUNTER__, type, name, insCollAlgBase, AlgTopoMatch, InsAlgTemplate0, InsAlgTemplate1, InsAlgTemplate2, \
93 : InsAlgTemplate3)
94 :
95 : #define INS_REGISTER_IMPL_BY_TOPO_HELPER(ctr, type, name, insCollAlgBase, AlgTopoMatch) \
96 : static HcclResult g_func_##name##_##ctr = InsCollAlgRegistry::Global()->Register( \
97 : type, std::string(#name), DefaultCreator<insCollAlgBase<AlgTopoMatch>>)
98 :
99 : #define INS_REGISTER_IMPL_BY_TOPO_HELPER_1(ctr, type, name, insCollAlgBase, AlgTopoMatch) \
100 : INS_REGISTER_IMPL_BY_TOPO_HELPER(ctr, type, name, insCollAlgBase, AlgTopoMatch)
101 :
102 : #define INS_REGISTER_IMPL_BY_TOPO(type, name, insCollAlgBase, AlgTopoMatch) \
103 : INS_REGISTER_IMPL_BY_TOPO_HELPER_1(__COUNTER__, type, name, insCollAlgBase, AlgTopoMatch)
104 :
105 : } // namespace Hccl
106 :
107 : #endif
|