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_EXEC_REGISTRY_H
12 : #define COLL_ALG_EXEC_REGISTRY_H
13 :
14 : #include <unordered_map>
15 : #include <functional>
16 : #include <memory>
17 :
18 : #include "coll_executor_base.h"
19 :
20 : namespace hccl {
21 :
22 : using CollExecCreator = std::function<CollExecutorBase*(const HcclDispatcher, std::unique_ptr<TopoMatcher>&)>;
23 :
24 : template <typename P>
25 61 : static CollExecutorBase* DefaultExecCreator(const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher)
26 : {
27 : static_assert(
28 : std::is_base_of<CollExecutorBase, P>::value, "Executor type must derived from Hccl::CollExecutorBase");
29 61 : return new (std::nothrow) P(dispatcher, topoMatcher);
30 : }
31 :
32 : class CollAlgExecRegistry {
33 : public:
34 : static CollAlgExecRegistry& Instance();
35 : HcclResult Register(const std::string& tag, const CollExecCreator& collExecCreator);
36 : std::unique_ptr<CollExecutorBase>
37 : GetAlgExec(const std::string& tag, const HcclDispatcher dispatcher, std::unique_ptr<TopoMatcher>& topoMatcher);
38 :
39 : private:
40 : std::unordered_map<std::string, const CollExecCreator> execCreators_;
41 : mutable std::mutex mu_;
42 : };
43 :
44 : #define REGISTER_EXEC_HELPER(ctr, tag, name, collExecBase) \
45 : static HcclResult g_func_##name##_##ctr \
46 : = CollAlgExecRegistry::Instance().Register(tag, DefaultExecCreator<collExecBase>)
47 :
48 : #define REGISTER_EXEC_HELPER_1(ctr, tag, name, collExecBase) REGISTER_EXEC_HELPER(ctr, tag, name, collExecBase)
49 :
50 : #define REGISTER_EXEC(tag, name, collExecBase) REGISTER_EXEC_HELPER_1(__COUNTER__, tag, name, collExecBase)
51 : } // namespace hccl
52 : #endif
|