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 COLL_ALG_OP_REGISTRY_H
12 : #define COLL_ALG_OP_REGISTRY_H
13 :
14 : #include <unordered_map>
15 : #include <functional>
16 : #include <memory>
17 :
18 : #include "coll_alg_operator.h"
19 :
20 : namespace hccl {
21 :
22 : struct EnumClassHash
23 : {
24 : template<typename T>
25 1328 : std::size_t operator()(T t) const
26 : {
27 1328 : return static_cast<std::size_t>(t);
28 : }
29 : };
30 :
31 : using CollAlgOpCreator = std::function<CollAlgOperator *(AlgConfigurator* algConfigurator, CCLBufferManager &,
32 : HcclDispatcher, std::unique_ptr<TopoMatcher> &)>;
33 :
34 66 : template <typename P> static CollAlgOperator *DefaultOpCreator(AlgConfigurator* algConfigurator,
35 : CCLBufferManager &cclBufferManager,
36 : HcclDispatcher dispatcher,
37 : std::unique_ptr<TopoMatcher> &topoMatcher)
38 : {
39 : static_assert(std::is_base_of<CollAlgOperator, P>::value, "CollAlgOp type must derived from Hccl::CollAlgOperator");
40 66 : return new (std::nothrow) P(algConfigurator, cclBufferManager, dispatcher, topoMatcher);
41 : }
42 :
43 : class CollAlgOpRegistry {
44 : public:
45 : static CollAlgOpRegistry &Instance();
46 : HcclResult Register(const HcclCMDType &opType, const CollAlgOpCreator &collAlgOpCreator);
47 : std::unique_ptr<CollAlgOperator> GetAlgOp(const HcclCMDType &opType, AlgConfigurator* algConfigurator,
48 : CCLBufferManager &cclBufferManager, HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher> &topoMatcher);
49 :
50 : private:
51 : std::unordered_map<HcclCMDType, const CollAlgOpCreator, EnumClassHash> opCreators_;
52 : mutable std::mutex mu_;
53 : };
54 :
55 : #define REGISTER_OP_HELPER(ctr, type, name, collOpBase) \
56 : static HcclResult g_func_##name##_##ctr \
57 : = CollAlgOpRegistry::Instance().Register(type, DefaultOpCreator<collOpBase>)
58 :
59 : #define REGISTER_OP_HELPER_1(ctr, type, name, collOpBase) REGISTER_OP_HELPER(ctr, type, name, collOpBase)
60 :
61 : #define REGISTER_OP(type, name, collOpBase) REGISTER_OP_HELPER_1(__COUNTER__, type, name, collOpBase)
62 :
63 : } // namespace hccl
64 :
65 : #endif
|