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 : #include "alg_profiling.h"
12 : #include "adapter_rts_common.h"
13 :
14 : namespace hccl {
15 :
16 1336 : AlgWrap& AlgWrap::GetInstance()
17 : {
18 1336 : static AlgWrap algWrap;
19 1336 : return algWrap;
20 : }
21 :
22 : HcclResult
23 524 : AlgWrap::RegisterAlgCallBack(const std::string& comm, void* userPtr, TaskCallBack callback, s32 deviceLogicID)
24 : {
25 524 : CHK_PRT_RET(
26 : initialized_ == false, HCCL_WARNING("[alg_profiling][RegisterAlgCallBack] AlgWrap has not initialized"),
27 : HCCL_SUCCESS);
28 :
29 524 : if (deviceLogicID < 0 || static_cast<u32>(deviceLogicID) >= MAX_MODULE_DEVICE_NUM) {
30 0 : HCCL_ERROR("[alg_profiling][RegisteralgCallBack] deviceLogicID %d is invalid", deviceLogicID);
31 0 : return HCCL_E_PARA;
32 : }
33 524 : std::lock_guard<std::mutex> lock(aivCallBackMutex_);
34 524 : aivCallBackMap_[comm][deviceLogicID] = callback;
35 524 : aivCallBackUserPtrMap_[comm][deviceLogicID] = userPtr;
36 524 : return HCCL_SUCCESS;
37 524 : }
38 :
39 807 : void AlgWrap::UnregisterAlgCallBack(const std::string& comm)
40 : {
41 807 : if (!initialized_) {
42 0 : HCCL_WARNING("[alg_profiling][UnRegisterAlgCallBack] AlgWrap has not initialized yet");
43 0 : return;
44 : }
45 :
46 807 : std::lock_guard<std::mutex> lock(aivCallBackMutex_);
47 807 : aivCallBackMap_.erase(comm);
48 807 : aivCallBackUserPtrMap_.erase(comm);
49 807 : }
50 :
51 5 : HcclResult AlgWrap::TaskAivProfiler(const std::string& comm, struct TaskParaGeneral& taskParaGeneral)
52 : {
53 5 : CHK_PRT_RET(
54 : initialized_ == false, HCCL_WARNING("[alg_profiling][RegisterAlgCallBack] AlgWrap has not initialized"),
55 : HCCL_SUCCESS);
56 :
57 5 : s32 deviceLogicID = INVALID_INT;
58 5 : CHK_RET(hrtGetDevice(&deviceLogicID));
59 5 : if (deviceLogicID < 0 || static_cast<u32>(deviceLogicID) >= MAX_MODULE_DEVICE_NUM) {
60 0 : HCCL_ERROR("[alg_profiling][TaskAivProfiler] deviceLogicID %d is invalid", deviceLogicID);
61 0 : return HCCL_E_PARA;
62 : }
63 :
64 5 : std::lock_guard<std::mutex> lock(aivCallBackMutex_);
65 5 : if (aivCallBackMap_.find(comm) == aivCallBackMap_.end()
66 5 : || aivCallBackUserPtrMap_.find(comm) == aivCallBackUserPtrMap_.end()) {
67 0 : HCCL_ERROR("[alg_profiling][TaskAivProfiler] comm %s is invalid", comm.c_str());
68 0 : return HCCL_E_PARA;
69 : }
70 :
71 5 : auto* aivCallBack = aivCallBackMap_[comm][deviceLogicID];
72 5 : auto* aivCallBackUserPtr = aivCallBackUserPtrMap_[comm][deviceLogicID];
73 5 : if (aivCallBack == nullptr || aivCallBackUserPtr == nullptr) {
74 0 : HCCL_ERROR("[alg_profiling][TaskAivProfiler] aivCallBack or aivCallBackUserPtr is invalid");
75 0 : return HCCL_E_PTR;
76 : }
77 :
78 : // 回调
79 5 : (aivCallBack)(aivCallBackUserPtr, static_cast<void*>(&taskParaGeneral), sizeof(struct TaskParaGeneral));
80 5 : return HCCL_SUCCESS;
81 5 : }
82 :
83 : } // namespace hccl
|