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 "hcclCommDfxLite.h"
12 : #include "hccl_common.h"
13 :
14 : namespace hccl {
15 : // HcclCommDfxLite构造函数实现
16 40 : HcclCommDfxLite::HcclCommDfxLite() {
17 40 : }
18 :
19 : // HcclCommDfxLite初始化流程 - 修改为返回HcclResult类型
20 22 : HcclResult HcclCommDfxLite::Init(u32 deviceId, const std::string& commTag, u32 rankSize) {
21 22 : if (initializedFlag_) {
22 0 : return HCCL_SUCCESS;
23 : }
24 22 : HCCL_INFO("[HcclCommDfxLite][Init] Init begin deviceId[%u], commTag[%s]", deviceId, commTag.c_str());
25 22 : deviceId_ = deviceId;
26 22 : commTag_ = commTag;
27 22 : rankSize_ = rankSize;
28 : /*1. 如果mirrorTaskManagerLite_为空,则创建新的MirrorTaskManager
29 : 注意:实际实现中应该避免这种情况,CommunicatorImplLite应该传入已经存在的MirrorTaskManager*/
30 22 : EXCEPTION_CATCH(mirrorTaskManagerLite_ = std::make_unique<Hccl::MirrorTaskManagerLite>(), return HCCL_E_PTR);
31 0 : auto getChannelRemoteRankId = [this](u64 handle) { return this->GetChannelRemoteRankId(handle); };
32 22 : mirrorTaskManagerLite_->RegGetRemoteRankCallBack(getChannelRemoteRankId);
33 :
34 : // 2. 创建Profiling管理类
35 22 : EXCEPTION_CATCH(profilingImpl_ = std::make_unique<HcclCommProfilingLite>(deviceId_, mirrorTaskManagerLite_.get()), return HCCL_E_PTR);
36 22 : CHK_RET(profilingImpl_->Init());
37 :
38 : // 3. 注册回调到单例
39 44 : addTaskCallback_ = [this](u32 streamId, u32 taskId, const Hccl::TaskParam &taskParam, u64 handle) {
40 0 : return this->mirrorTaskManagerLite_->AddTaskInfo(streamId, taskId, taskParam, handle);
41 22 : };
42 :
43 22 : Hccl::ProfilingHandlerLite::GetInstance().SetCachedGroupName(commTag_, rankSize_);
44 22 : initializedFlag_ = true;
45 22 : return HCCL_SUCCESS; // 初始化成功返回成功码
46 : }
47 :
48 : // HcclCommDfxLite接口实现 - 修改为返回HcclResult类型
49 0 : HcclResult HcclCommDfxLite::SetCurrDfxOpInfo(std::shared_ptr<Hccl::DfxOpInfo> dfxOpInfo)
50 : {
51 0 : auto it = Hccl::CMD_OP_TYPE_INFO_MAP.find(static_cast<HcclCMDType>(dfxOpInfo->op_.oldOpType));
52 0 : if (it == Hccl::CMD_OP_TYPE_INFO_MAP.end()) {
53 0 : HCCL_WARNING("[%s] dfxOpInfo.opType[%u] not supported.", __func__, dfxOpInfo->op_.oldOpType);
54 : } else {
55 0 : dfxOpInfo->op_.opType = it->second.first;
56 0 : dfxOpInfo->tag_ = it->second.second;
57 : }
58 0 : dfxOpInfo->op_.dataType = Hccl::HcclDataTypeToDataType(
59 0 : static_cast<HcclDataType>(dfxOpInfo->op_.oldDataType));
60 :
61 : // 如果是a5老流程 还是从通信域里面取
62 0 : if ((Hccl::ProfilingHandlerLite::GetInstance().GetProfL1State()
63 0 : || Hccl::ProfilingHandlerLite::GetInstance().GetProfL0State() ) && !dfxOpInfo->isIndop_) {
64 0 : Hccl::ProfilingHandlerLite::GetInstance().SetCachedGroupName(dfxOpInfo->groupName_.c_str(), dfxOpInfo->rankSize_);
65 : }
66 0 : CHK_RET(mirrorTaskManagerLite_->SetCurrDfxOpInfo(std::move(dfxOpInfo)));
67 0 : return HCCL_SUCCESS;
68 : }
69 :
70 0 : HcclResult HcclCommDfxLite::ReportAllTasks() {
71 0 : profilingImpl_->ReportAllTasks();
72 0 : return HCCL_SUCCESS;
73 : }
74 :
75 0 : HcclResult HcclCommDfxLite::UpdateProfStat() {
76 0 : profilingImpl_->UpdateProfStat();
77 0 : return HCCL_SUCCESS;
78 : }
79 :
80 1 : void HcclCommDfxLite::AddChannelRemoteRankId(u64 handle, u32 remoteRankId) {
81 1 : HCCL_INFO("[%s] commTag[%s], handle[%lu], remoteRankId[%u]", __func__, commTag_.c_str(), handle, remoteRankId);
82 1 : channelRemoteRankIdLite_[handle] = remoteRankId;
83 1 : }
84 :
85 2 : u32 HcclCommDfxLite::GetChannelRemoteRankId(u64 handle) {
86 2 : if (handle == INVALID_U64) {
87 0 : return INVALID_UINT;
88 : }
89 2 : auto it = channelRemoteRankIdLite_.find(handle);
90 2 : if (UNLIKELY(it == channelRemoteRankIdLite_.end())) {
91 1 : HCCL_ERROR("[%s]handle[%lu] not found, commTag[%s]", __func__, handle, commTag_.c_str());
92 1 : return INVALID_UINT;
93 : }
94 1 : return it->second;
95 : }
96 :
97 9 : Hccl::MirrorTaskManagerLite* HcclCommDfxLite::GetMirrorTaskManagerLite() const {
98 9 : return mirrorTaskManagerLite_.get();
99 : }
100 : }
|