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 DLRTS_FUNCTION_V2_H
12 : #define DLRTS_FUNCTION_V2_H
13 :
14 : #include <mutex>
15 : #include <dlfcn.h>
16 : #include "hccl_dl.h"
17 : #include "log.h"
18 : namespace Hccl {
19 : template <const char* soName>
20 : class DlRtsFunctionV2 {
21 : public:
22 : explicit DlRtsFunctionV2() = default;
23 5 : virtual ~DlRtsFunctionV2()
24 : {
25 5 : if (handle_ != nullptr) {
26 3 : HcclDlclose(handle_);
27 3 : handle_ = nullptr;
28 : }
29 5 : }
30 :
31 : template <const char* funcName>
32 5 : void* Handle()
33 : {
34 5 : Init();
35 5 : if (handle_ == nullptr) {
36 2 : return nullptr;
37 : }
38 3 : return HcclDlsym(handle_, funcName);
39 : }
40 :
41 : private:
42 5 : void Init()
43 : {
44 5 : std::lock_guard<std::mutex> lock(handleMutex_);
45 5 : if (isInit_) {
46 0 : return;
47 : }
48 15 : HCCL_INFO("DlRtsFunctionInit start, open so: %s", soName);
49 5 : handle_ = HcclDlopen(soName, RTLD_NOW);
50 5 : const char* errMsg = dlerror();
51 5 : if (handle_ == nullptr) {
52 6 : HCCL_ERROR(
53 : "dlopen [%s] failed, %s", soName,
54 : (errMsg == nullptr) ? "check the file exist or permission denied." : errMsg);
55 2 : return;
56 : }
57 3 : isInit_ = true;
58 5 : }
59 : void* handle_ = nullptr;
60 : std::mutex handleMutex_;
61 : bool isInit_ = false;
62 : };
63 : } // namespace Hccl
64 : #endif // DLRTS_FUNCTION_V2_H
|