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 : template <typename T>
24 1513 : std::size_t operator()(T t) const
25 : {
26 1513 : return static_cast<std::size_t>(t);
27 : }
28 : };
29 :
30 : using CollAlgOpCreator = std::function<CollAlgOperator*(
31 : AlgConfigurator* algConfigurator, CCLBufferManager&, HcclDispatcher, std::unique_ptr<TopoMatcher>&)>;
32 :
33 : template <typename P>
34 66 : static CollAlgOperator* DefaultOpCreator(
35 : AlgConfigurator* algConfigurator, CCLBufferManager& cclBufferManager, HcclDispatcher dispatcher,
36 : std::unique_ptr<TopoMatcher>& topoMatcher)
37 : {
38 : static_assert(std::is_base_of<CollAlgOperator, P>::value, "CollAlgOp type must derived from Hccl::CollAlgOperator");
39 66 : return new (std::nothrow) P(algConfigurator, cclBufferManager, dispatcher, topoMatcher);
40 : }
41 :
42 : class CollAlgOpRegistry {
43 : public:
44 : static CollAlgOpRegistry& Instance();
45 : HcclResult Register(const HcclCMDType& opType, const CollAlgOpCreator& collAlgOpCreator);
46 : std::unique_ptr<CollAlgOperator> GetAlgOp(
47 : const HcclCMDType& opType, AlgConfigurator* algConfigurator, CCLBufferManager& cclBufferManager,
48 : 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 = CollAlgOpRegistry::Instance().Register(type, DefaultOpCreator<collOpBase>)
57 :
58 : #define REGISTER_OP_HELPER_1(ctr, type, name, collOpBase) REGISTER_OP_HELPER(ctr, type, name, collOpBase)
59 :
60 : #define REGISTER_OP(type, name, collOpBase) REGISTER_OP_HELPER_1(__COUNTER__, type, name, collOpBase)
61 :
62 : } // namespace hccl
63 :
64 : #endif
|