Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "hcomm_c_adpt.h"
12 : #include "hcomm_c_adpt_common.h"
13 : #include "hcomm_result_defs.h"
14 : #include "log.h"
15 : #include "hcomm_res_defs.h"
16 : #include "hcom_common.h"
17 : #include "../hcomm_res_mgr.h"
18 : #include "param_check_pub.h"
19 : #include "comm_engine_utils.h"
20 : #include "exception_handler.h"
21 : #include "hcclCommDfx.h"
22 : #include "launch_device.h"
23 : #include "launch_aicpu.h"
24 : #include "comm_configer.h"
25 : #include "hcomm_adapter_runtime.h"
26 :
27 : using namespace hcomm;
28 :
29 : static HcommEndpointMap g_EndpointMap;
30 :
31 279 : HcommEndpointMap &GetEndpointMap()
32 : {
33 279 : return g_EndpointMap;
34 : }
35 :
36 : namespace {
37 180 : HcclResult RefreshCurrentDeviceContext()
38 : {
39 180 : s32 deviceLogicId = 0;
40 180 : CHK_RET(hrtGetDeviceRefresh(&deviceLogicId));
41 180 : u32 devicePhyId = 0;
42 180 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceLogicId), devicePhyId, true));
43 180 : HCCL_INFO("[RefreshCurrentDeviceContext] deviceLogicId[%d], devicePhyId[%u].", deviceLogicId, devicePhyId);
44 180 : return HCCL_SUCCESS;
45 : }
46 : } // namespace
47 :
48 199 : HcclResult RefreshEndpointContext(const EndpointDesc &endpointDesc)
49 : {
50 199 : if (endpointDesc.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
51 21 : return HCCL_SUCCESS;
52 : }
53 178 : return RefreshCurrentDeviceContext();
54 : }
55 :
56 14 : HcclResult RefreshCommEngineContext(CommEngine engine)
57 : {
58 14 : if (engine != COMM_ENGINE_AICPU && engine != COMM_ENGINE_AICPU_TS) {
59 12 : return HCCL_SUCCESS;
60 : }
61 2 : return RefreshCurrentDeviceContext();
62 : }
63 :
64 1291 : HcommResult HcommResMgrInit(uint32_t devPhyId)
65 : {
66 1291 : bool noDevice = false;
67 1291 : if (devPhyId == UINT32_MAX) {
68 301 : CHK_RET(ResolveRuntimeDevicePhyId(devPhyId, noDevice));
69 : }
70 :
71 : // 临时方案:触发统一平台层单例触发静态对象声明
72 : // 内部流程触发各种单例声明,保证时序
73 : EXCEPTION_HANDLE_BEGIN
74 1291 : if (noDevice) {
75 0 : (void)HcommResMgr::GetInstance(devPhyId);
76 0 : return HCCL_SUCCESS;
77 : }
78 :
79 1453 : HCCLV2_FUNC_RUN([&]() -> HcclResult {
80 : (void)HcommResMgr::GetInstance(devPhyId);
81 : return HcclResult::HCCL_SUCCESS;
82 : }());
83 0 : EXCEPTION_HANDLE_END
84 1129 : return HCCL_SUCCESS;
85 : }
86 :
87 0 : HcommResult HcommDfxKernelLaunch(const std::string &commTag, aclrtBinHandle binHandle, HcclDfxOpInfo dfxOpInfo)
88 : {
89 : // 申请device侧内存
90 0 : hccl::DeviceMem devicePackBuf = hccl::DeviceMem::alloc(sizeof(dfxOpInfo));
91 0 : CHK_PTR_NULL(devicePackBuf.ptr());
92 :
93 : // 将dfxOpInfo信息传递给device侧
94 0 : CHK_RET(hrtMemSyncCopy(devicePackBuf.ptr(), sizeof(dfxOpInfo), &dfxOpInfo, sizeof(dfxOpInfo),
95 : HcclRtMemcpyKind::HCCL_RT_MEMCPY_KIND_HOST_TO_DEVICE));
96 :
97 : // 创建局部流
98 0 : hccl::Stream localStream(hccl::StreamType::STREAM_TYPE_ONLINE);
99 0 : constexpr u32 aicpuStreamMode = 1;
100 0 : CHK_RET(hrtStreamSetMode(localStream.ptr(), aicpuStreamMode));
101 :
102 : // 下kernel
103 0 : std::string kernelName = "RunAicpuDfxOpInfoInitV2";
104 :
105 : struct InitTask {
106 : u64 context;
107 : char commTag[256];
108 : };
109 :
110 0 : InitTask customInitTask = {0, ""};
111 0 : customInitTask.context = reinterpret_cast<u64>(devicePackBuf.ptr());
112 0 : s32 sRet = strncpy_s(customInitTask.commTag, TAG_MAX_LENGTH, commTag.c_str(), TAG_MAX_LENGTH - 1);
113 0 : CHK_PRT_RET(sRet != EOK, HCCL_ERROR("[%s] str copy fail. return[%d]", __func__, sRet), HCCL_E_INTERNAL);
114 :
115 0 : CHK_RET(hccl::AicpuAclKernelLaunch(localStream.ptr(), reinterpret_cast<void *>(&customInitTask),
116 : sizeof(customInitTask), binHandle, kernelName, true, NOTIFY_DEFAULT_WAIT_TIME));
117 :
118 0 : CHK_RET(
119 : hcclStreamSynchronize(localStream.ptr(), hccl::CommConfiger::GetInstance().GetCommConfigExecTimeOut(commTag)));
120 :
121 0 : HCCL_INFO("[%s] channel kernel launch success.", __func__);
122 :
123 0 : return HCCL_SUCCESS;
124 0 : }
|