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