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 : #include "dispatcher_ctx.h"
11 : #include "adapter_hal.h"
12 : #include "dispatcher_graph_pub.h"
13 : #include "dispatcher_pub.h"
14 : #include "dispatcher_aicpu_pub.h"
15 : #include "dispatcher_virtural_pub.h"
16 : #include "dlhal_function.h"
17 :
18 : namespace hccl {
19 1 : FftsCounterCallBack DispatcherCtx::GetInitTaskCallback() const
20 : {
21 1 : return g_InitTaskCallback;
22 : }
23 :
24 1 : FftsCounterCallBack DispatcherCtx::GetLaunchTaskCallback() const
25 : {
26 1 : return g_LaunchTaskCallback;
27 : }
28 :
29 :
30 419 : HcclResult DispatcherCtx::Init()
31 : {
32 419 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
33 : // 获取host侧还是device侧
34 419 : u32 info = 0;
35 419 : CHK_RET(hrtDrvGetPlatformInfo(&info));
36 419 : bool isDeviceSide = info == 0 ? true : false;
37 419 : HCCL_INFO("[DispatcherCtx][Init] isDeviceSide[%d]", isDeviceSide);
38 419 : CtxDispatcherType type = CtxDispatcherType::DISPATCHER_NORMAL;
39 : // 如果是host侧
40 419 : if (!isDeviceSide) {
41 11 : CHK_RET(hrtGetDeviceType(deviceType_));
42 11 : if ((deviceType_ == DevType::DEV_TYPE_910B) && GetExternalInputHcclEnableFfts()) {
43 2 : CHK_PRT_CONT(GetWorkflowMode() == HcclWorkflowMode::HCCL_WORKFLOW_MODE_OP_BASE && !GetExternalInputHcclAicpuUnfold(),
44 : HCCL_RUN_INFO("[DispatcherCtx][Init] Will use FFTS mode."));
45 2 : type = CtxDispatcherType::DISPATCHER_FFTS;
46 : } else {
47 9 : HCCL_RUN_INFO("[DispatcherCtx][Init] Will use NORMAL mode.");
48 9 : type = CtxDispatcherType::DISPATCHER_NORMAL;
49 : }
50 : } else { // 如果是device侧 那么默认aicpu
51 408 : HCCL_RUN_INFO("[DispatcherCtx][Init] Will use AICPU mode.");
52 408 : type = CtxDispatcherType::DISPATCHER_AICPU;
53 : }
54 419 : CHK_RET(DispatcherInit(type, devicePhyId_, &dispatcher_));
55 419 : CHK_SMART_PTR_NULL(dispatcher_);
56 :
57 419 : return HCCL_SUCCESS;
58 : }
59 838 : HcclResult DispatcherCtx::Destroy()
60 : {
61 838 : const std::lock_guard<std::mutex> lock(destroyMutex_);
62 836 : if (dispatcher_ != nullptr) {
63 419 : DispatcherPub* dispatcher = reinterpret_cast<DispatcherPub*>(dispatcher_);
64 419 : delete dispatcher;
65 419 : dispatcher_ = nullptr;
66 : }
67 836 : return HCCL_SUCCESS;
68 836 : }
69 1595 : HcclDispatcher DispatcherCtx::GetDispatcher() const
70 : {
71 1595 : return dispatcher_;
72 : }
73 :
74 0 : u32 DispatcherCtx::GetWaitTimeOut() const
75 : {
76 0 : return waitTimeOut_;
77 : }
78 :
79 0 : HcclResult DispatcherCtx::SetWaitTimeOut(u32 waitTimeOut)
80 : {
81 0 : waitTimeOut_ = waitTimeOut;
82 0 : return HCCL_SUCCESS;
83 : }
84 :
85 419 : HcclResult DispatcherCtx::DispatcherInit(CtxDispatcherType type, const s32 devicePhyId, HcclDispatcher *dispatcher)
86 : {
87 419 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
88 419 : CHK_PTR_NULL(dispatcher);
89 419 : dispatcherType_ = type;
90 419 : DispatcherPub *pDispatcher = nullptr;
91 419 : switch (type) {
92 2 : case CtxDispatcherType::DISPATCHER_FFTS:
93 : {
94 2 : u32 deviceLogicId = INVALID_UINT;
95 2 : CHK_RET(hrtGetDeviceIndexByPhyId(devicePhyId, deviceLogicId));
96 : #ifndef HCCD
97 2 : pDispatcher = new (std::nothrow) DispatcherGraph(deviceLogicId);
98 : #endif
99 2 : break;
100 : }
101 9 : case CtxDispatcherType::DISPATCHER_NORMAL:
102 : {
103 9 : u32 deviceLogicId = INVALID_UINT;
104 9 : CHK_RET(hrtGetDeviceIndexByPhyId(devicePhyId, deviceLogicId));
105 9 : pDispatcher = new (std::nothrow) DispatcherPub(deviceLogicId);
106 9 : break;
107 : }
108 0 : case CtxDispatcherType::DISPATCHER_VIRTURAL:
109 : {
110 0 : u32 deviceLogicId = INVALID_UINT;
111 0 : CHK_RET(hrtGetDeviceIndexByPhyId(devicePhyId, deviceLogicId));
112 0 : pDispatcher = new (std::nothrow) DispatcherVirtural(deviceLogicId);
113 0 : break;
114 : }
115 408 : case CtxDispatcherType::DISPATCHER_AICPU:
116 : {
117 : #ifdef CCL_KERNEL
118 408 : pDispatcher = new (std::nothrow) DispatcherAiCpu(devicePhyId);
119 : #endif
120 408 : break;
121 : }
122 0 : default: {
123 0 : HCCL_ERROR("Not support the dispatcher type[%d]", type);
124 0 : return HCCL_E_NOT_SUPPORT;
125 : }
126 : }
127 :
128 419 : CHK_PTR_NULL(pDispatcher);
129 419 : HcclResult ret = pDispatcher->Init();
130 419 : if (ret != HCCL_SUCCESS) {
131 0 : HCCL_ERROR("Dispatcher init failed, type[%d]", type);
132 0 : delete pDispatcher;
133 0 : pDispatcher = nullptr;
134 0 : return ret;
135 : }
136 419 : *dispatcher = pDispatcher;
137 419 : return HCCL_SUCCESS;
138 : }
139 :
140 0 : HcclResult DispatcherCtx::SetDispatcherHcclQos(u32 hcclQos)
141 : {
142 0 : HCCL_INFO("SetDispatcherHcclQos hcclQos = %u", hcclQos);
143 0 : CHK_PTR_NULL(dispatcher_);
144 0 : auto aiCpuDispatcher = static_cast<DispatcherAiCpu*>(dispatcher_);
145 0 : aiCpuDispatcher->SetHcclQos(hcclQos);
146 0 : return HCCL_SUCCESS;
147 : }
148 : }
|