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