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 "ccu_super_fast_load.h"
12 :
13 : #include "exception_util.h"
14 : #include "internal_exception.h"
15 : #include "communicator_impl.h"
16 : #include "ccu_jetty_mgr.h"
17 : #include "coll_service_device_mode.h"
18 :
19 : namespace Hccl {
20 :
21 16 : static void SFLReportCcuProfilingInfoInitPart(
22 : uint64_t execId, std::vector<CcuProfilingInfo>&& streamProfilingInfo, const CommunicatorImpl& comm,
23 : TaskParam& taskParam)
24 : {
25 16 : if (streamProfilingInfo.empty()) {
26 39 : HCCL_INFO("There is no ccu profiling info.");
27 13 : return;
28 : }
29 3 : taskParam.taskPara.Ccu.dieId = streamProfilingInfo[0].dieId;
30 3 : taskParam.taskPara.Ccu.missionId = streamProfilingInfo[0].missionId;
31 3 : taskParam.taskPara.Ccu.execMissionId = streamProfilingInfo[0].missionId;
32 3 : taskParam.taskPara.Ccu.instrId = streamProfilingInfo[0].instrId;
33 3 : taskParam.taskPara.Ccu.executeId = execId;
34 :
35 6 : CcuJettyMgr* ccuJettyMgr = dynamic_cast<CollServiceDeviceMode*>(comm.GetCollService())
36 : ->GetCcuInsPreprocessor()
37 : ->GetCcuComm()
38 6 : ->GetCcuJettyMgr();
39 9 : for (auto& profInfo : streamProfilingInfo) {
40 6 : for (int idx = 0; idx < CCU_MAX_CHANNEL_NUM; idx++) {
41 6 : if (profInfo.channelId[idx] == INVALID_VALUE_CHANNELID) {
42 6 : break;
43 : }
44 : profInfo.remoteRankId[idx]
45 0 : = ccuJettyMgr->GetRemoteRankIdByChannelId(profInfo.dieId, profInfo.channelId[idx]);
46 : }
47 : }
48 3 : taskParam.ccuDetailInfo = std::make_shared<std::vector<CcuProfilingInfo>>(std::move(streamProfilingInfo));
49 : }
50 :
51 13 : CachedCCUParams::CachedCCUParams(
52 : std::vector<std::vector<Hccl::CcuTaskParam>>&& ccuInstruction,
53 : std::vector<std::vector<CcuProfilingInfo>>&& profilingInfo, std::size_t execId, CcuInstType insType, bool isSlave,
54 13 : void* comm)
55 13 : : execId(execId),
56 13 : insType(insType),
57 13 : isSlave(isSlave)
58 : {
59 13 : std::vector<std::vector<rtCcuTaskInfo_t>> ccuTaskInstruction{};
60 13 : auto& commImpl = *(static_cast<CommunicatorImpl*>(comm));
61 29 : for (auto& vec : ccuInstruction) {
62 16 : std::vector<rtCcuTaskInfo_t> ccuTaskVec{};
63 36 : for (auto& ccuTask : vec) {
64 20 : rtCcuTaskInfo_t taskInfo{};
65 20 : taskInfo.dieId = ccuTask.dieId;
66 20 : taskInfo.missionId = ccuTask.missionId;
67 20 : taskInfo.instStartId = ccuTask.instStartId;
68 20 : taskInfo.instCnt = ccuTask.instCnt;
69 20 : taskInfo.key = ccuTask.key;
70 20 : taskInfo.argSize = ccuTask.argSize;
71 20 : taskInfo.timeout = commImpl.GetNotifyTimeoutCfg().GetNotifyTimeout();
72 60 : std::copy(std::begin(ccuTask.args), std::end(ccuTask.args), std::begin(taskInfo.args));
73 20 : ccuTaskVec.push_back(taskInfo);
74 : }
75 16 : ccuTaskInstruction.push_back(ccuTaskVec);
76 16 : }
77 13 : constexpr std::size_t alignment = alignof(std::max_align_t);
78 13 : ccuParams = alloc_and_memcpy_aligned(
79 13 : std::forward<std::vector<std::vector<rtCcuTaskInfo_t>>>(ccuTaskInstruction), alignment);
80 13 : taskParams.reserve(ccuInstruction.size());
81 29 : for (std::size_t i = 0; i < ccuInstruction.size(); i++) {
82 16 : TaskParam taskParam = {};
83 16 : taskParam.taskType = TaskParamType::TASK_CCU;
84 16 : SFLReportCcuProfilingInfoInitPart(
85 16 : execId, std::forward<std::vector<CcuProfilingInfo>>(profilingInfo[i]), commImpl, taskParam);
86 16 : taskParams.emplace_back(std::move(taskParam));
87 16 : }
88 39 : HCCL_RUN_INFO("Save CcuInstType: %d", insType);
89 13 : }
90 :
91 0 : CachedCCUParams::CachedCCUParams(CachedCCUParams&& other) noexcept
92 0 : : ccuParams(std::exchange(other.ccuParams, nullptr)),
93 0 : count(std::move(other.count)),
94 0 : taskParams(std::move(other.taskParams)),
95 0 : execId(other.execId),
96 0 : totalCounts(other.totalCounts),
97 0 : insType(other.insType),
98 0 : isSlave(other.isSlave)
99 0 : {}
100 :
101 0 : CachedCCUParams& CachedCCUParams::operator=(CachedCCUParams&& other) noexcept
102 : {
103 0 : if (this != &other) {
104 0 : aligned_free(ccuParams);
105 0 : ccuParams = std::exchange(other.ccuParams, nullptr);
106 0 : execId = other.execId;
107 0 : count = std::move(other.count);
108 0 : totalCounts = other.totalCounts;
109 0 : isSlave = other.isSlave;
110 0 : taskParams = std::move(other.taskParams);
111 0 : insType = other.insType;
112 : }
113 0 : return *this;
114 : }
115 :
116 13 : CachedCCUParams::~CachedCCUParams() { aligned_free(ccuParams); }
117 :
118 : rtCcuTaskInfo_t*
119 13 : CachedCCUParams::alloc_and_memcpy_aligned(const std::vector<std::vector<rtCcuTaskInfo_t>>& vecs, std::size_t alignment)
120 : {
121 13 : if (alignment < alignof(CcuTaskParam)) {
122 0 : THROW<InternalException>(StringFormat("[CachedCCUParams] alignment must be larger than type alignment."));
123 : }
124 13 : count.resize(vecs.size());
125 13 : std::size_t countIndex = 0;
126 13 : count[countIndex++] = vecs[0].size();
127 29 : for (const auto& vec : vecs) {
128 16 : totalCounts += vec.size();
129 48 : HCCL_INFO("CachedCCUParams: vec.size[%llu]", static_cast<std::uint64_t>(vec.size()));
130 : }
131 13 : if (totalCounts == 0) {
132 0 : THROW<InternalException>(StringFormat("[CachedCCUParams] total count is zero."));
133 : }
134 39 : HCCL_INFO("CachedCCUParams: totalCounts[%llu]", static_cast<std::uint64_t>(totalCounts));
135 13 : std::size_t bytes = totalCounts * sizeof(rtCcuTaskInfo_t);
136 13 : void* raw = alloc_aligned_raw(alignment, bytes);
137 13 : if (!raw) {
138 0 : THROW<InternalException>(StringFormat("[CachedCCUParams] failed to allocate memory, size %zu.", bytes));
139 : }
140 13 : rtCcuTaskInfo_t* dst = static_cast<rtCcuTaskInfo_t*>(raw);
141 13 : rtCcuTaskInfo_t* cur = dst;
142 13 : if (!vecs[0].empty()) {
143 : auto ret
144 13 : = memcpy_s(cur, count[0] * sizeof(rtCcuTaskInfo_t), vecs[0].data(), count[0] * sizeof(rtCcuTaskInfo_t));
145 13 : if (ret != EOK) {
146 0 : aligned_free(dst);
147 0 : THROW<InternalException>(StringFormat("[CachedCCUParams] failed to memcpy, ret %d.", ret));
148 : }
149 13 : cur += count[0];
150 : }
151 13 : u32 reqStreamNum = vecs.size() - 1;
152 16 : for (std::size_t i = 1; i <= reqStreamNum; i++) {
153 3 : auto& vec = vecs[i];
154 3 : if (!vec.empty()) {
155 : auto ret
156 3 : = memcpy_s(cur, vec.size() * sizeof(rtCcuTaskInfo_t), vec.data(), vec.size() * sizeof(rtCcuTaskInfo_t));
157 3 : if (ret != EOK) {
158 0 : aligned_free(dst);
159 0 : THROW<InternalException>(StringFormat("[CachedCCUParams] failed to memcpy, ret %d.", ret));
160 : }
161 3 : count[countIndex++] = vec.size();
162 3 : cur += vec.size();
163 : }
164 : }
165 29 : for (std::size_t i = 0; i < count.size(); ++i) {
166 48 : HCCL_INFO("CachedCCUParams: count value[%llu]", static_cast<std::uint64_t>(count[i]));
167 : }
168 13 : return dst;
169 : }
170 : } // namespace Hccl
|