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 <map>
12 : #include <mutex>
13 : #include <algorithm>
14 : #include <functional>
15 : #include <sstream>
16 : #include "acl_rt_impl.h"
17 : #include "runtime/mem.h"
18 : #include "runtime/rts/rts_mem.h"
19 : #include "runtime/dev.h"
20 : #include "runtime/rts/rts_device.h"
21 : #include "runtime/rt_stars.h"
22 : #include "runtime/rt_mem_queue.h"
23 : #include "runtime/rt_inner_mem.h"
24 : #include "runtime/inner_kernel.h"
25 : #include "utils/math_utils.h"
26 : #include "common/log_inner.h"
27 : #include "common/error_codes_inner.h"
28 : #include "common/prof_reporter.h"
29 : #include "common/resource_statistics.h"
30 : #include "utils/data_type_utils.h"
31 :
32 : namespace {
33 : constexpr uint32_t MEM_SIZE_MAX = 96U;
34 : constexpr uint32_t MAX_PADDING_SIZE_STR_LEN = 32U;
35 : constexpr int32_t STRTOUL_DECIMAL_BASE = 10;
36 : constexpr size_t DATA_MEMORY_ALIGN_SIZE = 32UL;
37 : constexpr size_t DATA_MEMORY_PADDING_SIZE = 32UL;
38 : constexpr unsigned int FLAG_START_DYNAMIC_ALLOC_MEM = 0x200U;
39 : constexpr uint32_t DRV_MEM_HOST_NUMA_SIDE = 2U;
40 : constexpr size_t ALIGNMENT_4BYTE = 4;
41 : constexpr size_t ALIGNMENT_4BYTE_MASK = ALIGNMENT_4BYTE - 1; // 0x3
42 :
43 : static const std::map<aclDataType, rtDataType> kMapDataType = {
44 : { ACL_FLOAT, RT_DATA_TYPE_FP32 },
45 : { ACL_FLOAT16, RT_DATA_TYPE_FP16 },
46 : { ACL_INT16, RT_DATA_TYPE_INT16 },
47 : { ACL_INT4, RT_DATA_TYPE_INT4 },
48 : { ACL_INT8, RT_DATA_TYPE_INT8 },
49 : { ACL_INT32, RT_DATA_TYPE_INT32 },
50 : { ACL_BF16, RT_DATA_TYPE_BFP16 },
51 : { ACL_UINT8, RT_DATA_TYPE_UINT8 },
52 : { ACL_UINT16, RT_DATA_TYPE_UINT16 },
53 : { ACL_UINT32, RT_DATA_TYPE_UINT32 },
54 : };
55 :
56 : using Handler = std::function<void(rtDrvMemProp_t&, bool, bool)>;
57 : static const std::map<int32_t, Handler> memAttrHandlers = {
58 : // HBM (Direct Assign)
59 7 : {ACL_HBM_MEM_HUGE, [](rtDrvMemProp_t& p, bool, bool) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = HBM_TYPE; }},
60 4 : {ACL_HBM_MEM_NORMAL, [](rtDrvMemProp_t& p, bool, bool) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = HBM_TYPE; }},
61 3 : {ACL_HBM_MEM_HUGE1G, [](rtDrvMemProp_t& p, bool, bool) { p.pg_type = HUGE1G_PAGE_TYPE; p.mem_type = HBM_TYPE; }},
62 :
63 : // DDR (Host Only)
64 1 : {ACL_DDR_MEM_HUGE, [](rtDrvMemProp_t& p, bool isHost, bool) { if(isHost) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = DDR_TYPE; } }},
65 1 : {ACL_DDR_MEM_NORMAL, [](rtDrvMemProp_t& p, bool isHost, bool) { if(isHost) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = DDR_TYPE; } }},
66 1 : {ACL_DDR_MEM_P2P_HUGE, [](rtDrvMemProp_t& p, bool isHost, bool) { if(isHost) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = P2P_DDR_TYPE; } }},
67 1 : {ACL_DDR_MEM_P2P_NORMAL, [](rtDrvMemProp_t& p, bool isHost, bool) { if(isHost) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = P2P_DDR_TYPE; } }},
68 :
69 : // Generic (Host / Device)
70 2 : {ACL_MEM_NORMAL, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = DDR_TYPE; } else if(isDev) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = HBM_TYPE; } }},
71 2 : {ACL_MEM_HUGE, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = DDR_TYPE; } else if(isDev) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = HBM_TYPE; } }},
72 2 : {ACL_MEM_HUGE1G, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = HUGE1G_PAGE_TYPE; p.mem_type = DDR_TYPE; } else if(isDev) { p.pg_type = HUGE1G_PAGE_TYPE; p.mem_type = HBM_TYPE; } }},
73 :
74 : // P2P (Host / Device)
75 2 : {ACL_MEM_P2P_NORMAL, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = P2P_DDR_TYPE; } else if(isDev) { p.pg_type = NORMAL_PAGE_TYPE; p.mem_type = P2P_HBM_TYPE; } }},
76 2 : {ACL_MEM_P2P_HUGE, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = P2P_DDR_TYPE; } else if(isDev) { p.pg_type = HUGE_PAGE_TYPE; p.mem_type = P2P_HBM_TYPE; } }},
77 2 : {ACL_MEM_P2P_HUGE1G, [](rtDrvMemProp_t& p, bool isHost, bool isDev) { if(isHost) { p.pg_type = HUGE1G_PAGE_TYPE; p.mem_type = P2P_DDR_TYPE; } else if(isDev) { p.pg_type = HUGE1G_PAGE_TYPE; p.mem_type = P2P_HBM_TYPE; } }}
78 : };
79 :
80 36 : inline aclError MemcpyKindTranslate(const aclrtMemcpyKind kind, rtMemcpyKind_t &rtKind)
81 : {
82 36 : switch (kind) {
83 8 : case ACL_MEMCPY_HOST_TO_DEVICE: {
84 8 : rtKind = RT_MEMCPY_HOST_TO_DEVICE;
85 8 : break;
86 : }
87 7 : case ACL_MEMCPY_DEVICE_TO_DEVICE: {
88 7 : rtKind = RT_MEMCPY_DEVICE_TO_DEVICE;
89 7 : break;
90 : }
91 4 : case ACL_MEMCPY_DEVICE_TO_HOST: {
92 4 : rtKind = RT_MEMCPY_DEVICE_TO_HOST;
93 4 : break;
94 : }
95 5 : case ACL_MEMCPY_HOST_TO_HOST: {
96 5 : rtKind = RT_MEMCPY_HOST_TO_HOST;
97 5 : break;
98 : }
99 4 : case ACL_MEMCPY_DEFAULT: {
100 4 : rtKind = RT_MEMCPY_DEFAULT;
101 4 : break;
102 : }
103 4 : case ACL_MEMCPY_HOST_TO_BUF_TO_DEVICE: {
104 4 : rtKind = RT_MEMCPY_HOST_TO_DEVICE_EX;
105 4 : break;
106 : }
107 4 : default: {
108 4 : ACL_LOG_ERROR("[Check][MemcpyKindTranslate]param kind invalid, which is %s.", acl::GetMemcpyKindDesc(kind));
109 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
110 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
111 4 : std::vector<const char *>({"Memory copy type conversion", acl::GetMemcpyKindDesc(kind), "kind",
112 : "ACL_MEMCPY_HOST_TO_DEVICE or "
113 : "ACL_MEMCPY_DEVICE_TO_DEVICE or ACL_MEMCPY_DEVICE_TO_HOST or "
114 8 : "ACL_MEMCPY_HOST_TO_HOST or ACL_MEMCPY_DEFAULT or ACL_MEMCPY_HOST_TO_BUF_TO_DEVICE."}));
115 4 : return ACL_ERROR_INVALID_PARAM;
116 : }
117 : }
118 32 : return ACL_SUCCESS;
119 : }
120 :
121 29 : inline bool IsZeroSizeMemcpy2d(const size_t width, const size_t height)
122 : {
123 29 : return (width == 0UL) || (height == 0UL);
124 : }
125 :
126 26 : bool IsAllZeroSizeBatch(const size_t * const sizes, const size_t numBatches)
127 : {
128 26 : return std::all_of(sizes, sizes + numBatches, [](const size_t size) {
129 45 : return size == 0UL;
130 26 : });
131 : }
132 :
133 29 : aclError CheckMemcpy2dParam(const void *const dst, const size_t dpitch, const void *const src, const size_t spitch,
134 : const size_t width, const size_t height, const aclrtMemcpyKind kind, rtMemcpyKind_t &rtKind)
135 : {
136 29 : ACL_LOG_DEBUG("start to execute CheckMemcpy2dParam");
137 29 : if (IsZeroSizeMemcpy2d(width, height)) {
138 10 : return ACL_SUCCESS;
139 : }
140 :
141 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(dst, "Checking the synchronous memory copy parameter validity");
142 17 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(src, "Checking the synchronous memory copy parameter validity");
143 :
144 15 : if ((width > spitch) || (width > dpitch)) {
145 2 : ACL_LOG_ERROR("[Check][Width]input param width[%zu] must be smaller than spitch[%zu] and dpitch[%zu]",
146 : width, spitch, dpitch);
147 2 : const std::string widthVal = std::to_string(width);
148 : std::string errMsg = acl::AclErrorLogManager::FormatStr("must be less than spitch and dpitch, spitch=%zu, dpitch=%zu",
149 2 : spitch, dpitch);
150 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
151 4 : std::vector<const char *>({"func", "value", "param", "reason"}),
152 4 : std::vector<const char *>({"Checking the synchronous memory copy parameter validity", widthVal.c_str(), "width", errMsg.c_str()}));
153 2 : return ACL_ERROR_INVALID_PARAM;
154 2 : }
155 13 : switch (kind) {
156 6 : case ACL_MEMCPY_HOST_TO_DEVICE: {
157 6 : rtKind = RT_MEMCPY_HOST_TO_DEVICE;
158 6 : break;
159 : }
160 0 : case ACL_MEMCPY_DEVICE_TO_HOST: {
161 0 : rtKind = RT_MEMCPY_DEVICE_TO_HOST;
162 0 : break;
163 : }
164 1 : case ACL_MEMCPY_DEVICE_TO_DEVICE: {
165 1 : rtKind = RT_MEMCPY_DEVICE_TO_DEVICE;
166 1 : break;
167 : }
168 2 : case ACL_MEMCPY_DEFAULT: {
169 2 : rtKind = RT_MEMCPY_DEFAULT;
170 2 : break;
171 : }
172 4 : default: {
173 4 : ACL_LOG_ERROR("[Check][Kind]invalid kind of memcpy, kind = %s", acl::GetMemcpyKindDesc(kind));
174 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
175 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
176 4 : std::vector<const char *>({"Checking the synchronous memory copy parameter validity", acl::GetMemcpyKindDesc(kind),
177 8 : "kind", "ACL_MEMCPY_HOST_TO_DEVICE or ACL_MEMCPY_DEVICE_TO_HOST or ACL_MEMCPY_DEVICE_TO_DEVICE or ACL_MEMCPY_DEFAULT"}));
178 4 : return ACL_ERROR_INVALID_PARAM;
179 : }
180 : }
181 9 : return ACL_SUCCESS;
182 : }
183 : }
184 :
185 : namespace acl {
186 6 : void GetPaddingSize(size_t *paddingSize)
187 : {
188 6 : const char* AI_CORE_SPEC_STR = "AICoreSpec";
189 6 : const char* PADDING_SIZE_STR = "padding_size";
190 6 : char paddingSizeStr[MAX_PADDING_SIZE_STR_LEN] = {0};
191 6 : const rtError_t error = rtGetSocSpec(AI_CORE_SPEC_STR, PADDING_SIZE_STR, paddingSizeStr, sizeof(paddingSizeStr));
192 6 : if (error != RT_ERROR_NONE) {
193 1 : ACL_LOG_EVENT("rtGetSocSpec did not complete successfully, ret=%d.", error);
194 1 : return;
195 : }
196 5 : char *endPtr = NULL;
197 5 : errno = 0;
198 5 : *paddingSize = static_cast<size_t>(strtoul(paddingSizeStr, &endPtr, STRTOUL_DECIMAL_BASE));
199 5 : if (errno == ERANGE || endPtr == paddingSizeStr || *endPtr != '\0') {
200 3 : *paddingSize = DATA_MEMORY_PADDING_SIZE;
201 3 : ACL_LOG_EVENT("paddingSizeStr could not be converted, paddingSizeStr[%s] is invalid.", paddingSizeStr);
202 : }
203 : }
204 :
205 54 : aclError GetAlignedAndPaddingSize(const size_t size, const bool isPadding, size_t &alignedSize)
206 : {
207 : static std::once_flag hasReadPaddingSize;
208 : static size_t paddingSize = DATA_MEMORY_PADDING_SIZE;
209 54 : std::call_once(hasReadPaddingSize, [&]() {
210 1 : GetPaddingSize(&paddingSize);
211 1 : });
212 : // align size to multiple of 32 and paddingSize if needed
213 54 : const size_t appendSize = isPadding ? DATA_MEMORY_ALIGN_SIZE + paddingSize : DATA_MEMORY_ALIGN_SIZE;
214 :
215 : // check overflow before alignment calculation
216 54 : if ((size + appendSize) < size) {
217 6 : ACL_LOG_INNER_ERROR("[Check][Size]size too large: %zu", size);
218 6 : return ACL_ERROR_INVALID_PARAM;
219 : }
220 :
221 48 : alignedSize = (size + appendSize - 1UL) / DATA_MEMORY_ALIGN_SIZE * DATA_MEMORY_ALIGN_SIZE;
222 48 : return ACL_SUCCESS;
223 : }
224 :
225 43 : aclError aclMallocMemInner(void **devPtr, const size_t size, bool isPadding,
226 : const aclrtMemMallocPolicy policy, const uint16_t moduleId)
227 : {
228 43 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
229 43 : ACL_LOG_DEBUG("start to execute aclMallocMemInner, size = %zu", size);
230 43 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
231 49 : ACL_REQUIRES_POSITIVE_REPORT(size);
232 37 : size_t alignedSize = size;
233 37 : const bool huge1g = (policy == ACL_MEM_MALLOC_HUGE1G_ONLY) || (policy == ACL_MEM_MALLOC_HUGE1G_ONLY_P2P);
234 37 : isPadding = !huge1g && isPadding;
235 37 : ACL_REQUIRES_OK(acl::GetAlignedAndPaddingSize(size, isPadding, alignedSize));
236 34 : uint32_t flags = RT_MEMORY_DEFAULT;
237 34 : if (policy == ACL_MEM_MALLOC_HUGE_FIRST) {
238 9 : flags |= RT_MEMORY_POLICY_HUGE_PAGE_FIRST;
239 25 : } else if (policy == ACL_MEM_MALLOC_HUGE_ONLY) {
240 4 : flags |= RT_MEMORY_POLICY_HUGE_PAGE_ONLY;
241 21 : } else if (policy == ACL_MEM_MALLOC_NORMAL_ONLY) {
242 4 : flags |= RT_MEMORY_POLICY_DEFAULT_PAGE_ONLY;
243 17 : } else if (policy == ACL_MEM_MALLOC_HUGE_FIRST_P2P) {
244 3 : flags |= RT_MEMORY_POLICY_HUGE_PAGE_FIRST_P2P;
245 14 : } else if (policy == ACL_MEM_MALLOC_HUGE_ONLY_P2P) {
246 7 : flags |= RT_MEMORY_POLICY_HUGE_PAGE_ONLY_P2P;
247 7 : } else if (policy == ACL_MEM_MALLOC_NORMAL_ONLY_P2P) {
248 3 : flags |= RT_MEMORY_POLICY_DEFAULT_PAGE_ONLY_P2P;
249 4 : } else if (policy == ACL_MEM_MALLOC_HUGE1G_ONLY) {
250 2 : flags |= RT_MEMORY_POLICY_HUGE1G_PAGE_ONLY;
251 2 : } else if (policy == ACL_MEM_MALLOC_HUGE1G_ONLY_P2P) {
252 2 : flags |= RT_MEMORY_POLICY_HUGE1G_PAGE_ONLY_P2P;
253 : } else {
254 0 : flags = RT_MEMORY_DEFAULT;
255 : }
256 34 : ACL_REQUIRES_RTS_OK(rtMalloc(devPtr, alignedSize, flags, moduleId));
257 31 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
258 31 : return ACL_SUCCESS;
259 : }
260 :
261 13 : aclError aclrtMallocInnerWithCfg(void **devPtr, const size_t size, aclrtMemMallocPolicy policy, rtMallocAdvise advise,
262 : aclrtMallocConfig *cfg)
263 : {
264 13 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
265 13 : ACL_LOG_DEBUG("start to execute aclrtMallocInnerWithCfg, size = %zu", size);
266 13 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
267 :
268 : // check attrs pointer
269 12 : if ((cfg != nullptr) && (cfg->numAttrs != 0) && (cfg->attrs == nullptr)) {
270 1 : const std::string numAttrsVal = std::to_string(cfg->numAttrs);
271 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
272 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
273 1 : std::vector<const char *>({__func__, numAttrsVal.c_str(), "cfg->numAttrs",
274 2 : "cfg->attrs must not be null when cfg->numAttrs is not 0"}));
275 1 : return ACL_ERROR_INVALID_PARAM;
276 1 : }
277 : // size must be greater than zero
278 14 : ACL_REQUIRES_POSITIVE_REPORT(size);
279 :
280 10 : ACL_REQUIRES_RTS_OK(rtsMalloc(devPtr, size, static_cast<rtMallocPolicy>(policy), advise,
281 : reinterpret_cast<rtMallocConfig_t*>(cfg)));
282 8 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
283 8 : return ACL_SUCCESS;
284 : }
285 : } // namespace acl
286 :
287 : #ifdef __cplusplus
288 : extern "C" {
289 : #endif
290 :
291 31 : aclError aclrtMallocImpl(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
292 : {
293 31 : ACL_PROFILING_REG(acl::AclProfType::AclrtMalloc);
294 31 : ACL_LOG_DEBUG("start to execute aclrtMalloc, size = %zu", size);
295 62 : return acl::aclMallocMemInner(devPtr, size, true, policy, acl::APP_MODE_ID_U16);
296 31 : }
297 :
298 12 : aclError aclrtMallocAlign32Impl(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
299 : {
300 12 : ACL_LOG_DEBUG("start to execute aclrtMallocAlign32, size = %zu", size);
301 12 : return acl::aclMallocMemInner(devPtr, size, false, policy, acl::APP_MODE_ID_U16);
302 : }
303 :
304 11 : aclError aclrtMallocCachedImpl(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
305 : {
306 11 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocCached);
307 11 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
308 11 : ACL_LOG_DEBUG("start to execute aclrtMallocCached, size = %zu", size);
309 11 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
310 :
311 17 : ACL_REQUIRES_POSITIVE_REPORT(size);
312 9 : size_t alignedSize = size;
313 9 : const bool huge1g = (policy == ACL_MEM_MALLOC_HUGE1G_ONLY) || (policy == ACL_MEM_MALLOC_HUGE1G_ONLY_P2P);
314 9 : const bool isPadding = !huge1g;
315 9 : ACL_REQUIRES_OK(acl::GetAlignedAndPaddingSize(size, isPadding, alignedSize));
316 7 : uint32_t cacheFlags = RT_MEMORY_DEFAULT;
317 7 : if (policy == ACL_MEM_MALLOC_HUGE_FIRST) {
318 3 : cacheFlags |= RT_MEMORY_POLICY_HUGE_PAGE_FIRST;
319 4 : } else if (policy == ACL_MEM_MALLOC_HUGE_ONLY) {
320 2 : cacheFlags |= RT_MEMORY_POLICY_HUGE_PAGE_ONLY;
321 2 : } else if (policy == ACL_MEM_MALLOC_NORMAL_ONLY) {
322 1 : cacheFlags |= RT_MEMORY_POLICY_DEFAULT_PAGE_ONLY;
323 1 : } else if (policy == ACL_MEM_MALLOC_HUGE1G_ONLY) {
324 1 : cacheFlags |= RT_MEMORY_POLICY_HUGE1G_PAGE_ONLY;
325 : } else {
326 0 : cacheFlags = RT_MEMORY_DEFAULT;
327 : }
328 7 : ACL_REQUIRES_RTS_OK(rtMallocCached(devPtr, alignedSize, cacheFlags, acl::APP_MODE_ID_U16));
329 5 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
330 5 : return ACL_SUCCESS;
331 11 : }
332 :
333 10 : aclError aclrtMallocWithCfgImpl(void **devPtr, size_t size, aclrtMemMallocPolicy policy, aclrtMallocConfig *cfg)
334 : {
335 10 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocWithCfg);
336 10 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
337 10 : ACL_LOG_DEBUG("start to execute aclrtMallocWithCfg, size = %zu", size);
338 20 : return acl::aclrtMallocInnerWithCfg(devPtr, size, policy, RT_MEM_ADVISE_NONE, cfg);
339 10 : }
340 :
341 3 : aclError aclrtMallocForTaskSchedulerImpl(void **devPtr, size_t size, aclrtMemMallocPolicy policy,
342 : aclrtMallocConfig *cfg)
343 : {
344 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocForTaskScheduler);
345 3 : ACL_LOG_DEBUG("start to execute aclrtMallocForTaskScheduler, size = %zu", size);
346 6 : return acl::aclrtMallocInnerWithCfg(devPtr, size, policy, RT_MEM_ADVISE_TS, cfg);
347 3 : }
348 :
349 6 : aclError aclrtMallocHostWithCfgImpl(void **ptr, uint64_t size, aclrtMallocConfig *cfg)
350 : {
351 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocHostWithCfg);
352 6 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
353 6 : ACL_LOG_DEBUG("start to execute aclrtMallocHostWithCfg, size = %zu", size);
354 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
355 8 : ACL_REQUIRES_POSITIVE_REPORT(size);
356 4 : ACL_REQUIRES_RTS_OK(rtsMallocHost(ptr, size, reinterpret_cast<rtMallocConfig_t*>(cfg)));
357 3 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
358 3 : return ACL_SUCCESS;
359 6 : }
360 :
361 13 : aclError aclrtPointerGetAttributesImpl(const void *ptr, aclrtPtrAttributes *attributes)
362 : {
363 13 : ACL_PROFILING_REG(acl::AclProfType::AclrtPointerGetAttributes);
364 13 : ACL_LOG_DEBUG("start to execute aclrtPointerGetAttributes");
365 13 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
366 12 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(attributes);
367 12 : ACL_REQUIRES_RTS_OK(rtsPointerGetAttributes(ptr, reinterpret_cast<rtPtrAttributes_t*>(attributes)));
368 11 : return ACL_SUCCESS;
369 13 : }
370 :
371 1 : aclError aclrtMemManagedGetAttrImpl(aclrtMemManagedRangeAttribute attribute, const void *ptr, size_t size, void *data,
372 : size_t dataSize)
373 : {
374 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedGetAttr);
375 1 : ACL_LOG_DEBUG("start to execute aclrtMemManagedGetAttr");
376 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
377 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(data);
378 1 : ACL_REQUIRES_POSITIVE_REPORT(size);
379 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemManagedGetAttr(static_cast<rtMemManagedRangeAttribute>(attribute), ptr, size, data, dataSize), rtMemManagedGetAttr);
380 1 : return ACL_SUCCESS;
381 1 : }
382 :
383 1 : aclError aclrtMemManagedGetAttrsImpl(aclrtMemManagedRangeAttribute *attributes, size_t numAttributes, const void *ptr,
384 : size_t size, void **data, size_t *dataSizes)
385 : {
386 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedGetAttrs);
387 1 : ACL_LOG_DEBUG("start to execute aclrtMemManagedGetAttrs");
388 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
389 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(attributes);
390 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(data);
391 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataSizes);
392 1 : ACL_REQUIRES_POSITIVE_REPORT(size);
393 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemManagedGetAttrs(reinterpret_cast<rtMemManagedRangeAttribute *>(attributes),
394 : numAttributes, ptr, size, data, dataSizes), rtMemManagedGetAttrs);
395 1 : return ACL_SUCCESS;
396 1 : }
397 :
398 5 : aclError aclrtHostRegisterImpl(void *ptr, uint64_t size, aclrtHostRegisterType type, void **devPtr)
399 : {
400 5 : ACL_PROFILING_REG(acl::AclProfType::AclrtHostRegister);
401 5 : ACL_LOG_DEBUG("start to execute aclrtHostRegister");
402 5 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
403 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
404 : // size must be greater than zero
405 7 : ACL_REQUIRES_POSITIVE_REPORT(size);
406 3 : ACL_REQUIRES_RTS_OK(rtsHostRegister(ptr, size, static_cast<rtHostRegisterType>(type), devPtr));
407 2 : return ACL_SUCCESS;
408 5 : }
409 :
410 9 : aclError aclrtHostRegisterV2Impl(void *ptr, uint64_t size, uint32_t flag)
411 : {
412 9 : ACL_PROFILING_REG(acl::AclProfType::AclrtHostRegisterV2);
413 9 : ACL_LOG_DEBUG("start to execute aclrtHostRegisterV2");
414 9 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
415 11 : ACL_REQUIRES_POSITIVE_REPORT(size);
416 7 : ACL_REQUIRES_RTS_OK(rtHostRegisterV2(ptr, size, flag));
417 6 : return ACL_SUCCESS;
418 9 : }
419 :
420 5 : aclError aclrtHostGetDevicePointerImpl(void *pHost, void **pDevice, uint32_t flag)
421 : {
422 5 : ACL_PROFILING_REG(acl::AclProfType::AclrtHostGetDevicePointer);
423 5 : ACL_LOG_DEBUG("start to execute aclrtHostGetDevicePointer");
424 5 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pHost);
425 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pDevice);
426 7 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flag, 0, ACL_ERROR_INVALID_PARAM);
427 3 : ACL_REQUIRES_RTS_OK(rtHostGetDevicePointer(pHost, pDevice, flag));
428 2 : return ACL_SUCCESS;
429 5 : }
430 :
431 4 : aclError aclrtHostUnregisterImpl(void *ptr)
432 : {
433 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtHostUnregister);
434 4 : ACL_LOG_DEBUG("start to execute aclrtHostUnregister");
435 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
436 3 : ACL_REQUIRES_RTS_OK(rtsHostUnregister(ptr));
437 2 : return ACL_SUCCESS;
438 4 : }
439 :
440 2 : aclError aclrtHostMemMapCapabilitiesImpl(uint32_t deviceId, aclrtHacType hacType,
441 : aclrtHostMemMapCapability *capabilities)
442 : {
443 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtHostMemMapCapabilities);
444 2 : ACL_LOG_DEBUG("start to execute aclrtHostMemMapCapabilities, deviceId = %u", deviceId);
445 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(capabilities);
446 2 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtHostMemMapCapabilities(deviceId, static_cast<rtHacType>(hacType),
447 : reinterpret_cast<rtHostMemMapCapability*>(capabilities)), rtHostMemMapCapabilities);
448 1 : return ACL_SUCCESS;
449 2 : }
450 :
451 6 : aclError aclrtMemFlushImpl(void *devPtr, size_t size)
452 : {
453 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemFlush);
454 6 : ACL_LOG_DEBUG("start to execute aclrtMemFlush, size = %zu", size);
455 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
456 :
457 11 : ACL_REQUIRES_POSITIVE_REPORT(size);
458 3 : ACL_REQUIRES_RTS_OK(rtFlushCache(devPtr, size));
459 1 : return ACL_SUCCESS;
460 6 : }
461 :
462 6 : aclError aclrtMemInvalidateImpl(void *devPtr, size_t size)
463 : {
464 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemInvalidate);
465 6 : ACL_LOG_INFO("start to execute aclrtMemInvalidate, size = %zu", size);
466 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
467 :
468 11 : ACL_REQUIRES_POSITIVE_REPORT(size);
469 3 : ACL_REQUIRES_RTS_OK(rtInvalidCache(devPtr, size));
470 1 : return ACL_SUCCESS;
471 6 : }
472 :
473 25 : aclError aclrtFreeImpl(void *devPtr)
474 : {
475 25 : ACL_PROFILING_REG(acl::AclProfType::AclrtFree);
476 25 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
477 25 : ACL_LOG_DEBUG("start to execute aclrtFree");
478 25 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
479 :
480 24 : ACL_REQUIRES_RTS_OK(rtFree(devPtr));
481 23 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
482 23 : return ACL_SUCCESS;
483 25 : }
484 :
485 24 : aclError aclrtMallocHostImpl(void **hostPtr, size_t size)
486 : {
487 24 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocHost);
488 24 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
489 24 : ACL_LOG_DEBUG("start to execute aclrtMallocHost, size = %zu", size);
490 24 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(hostPtr);
491 : // size must be greater than zero
492 28 : ACL_REQUIRES_POSITIVE_REPORT(size);
493 20 : ACL_REQUIRES_RTS_OK(rtMallocHost(hostPtr, size, acl::APP_MODE_ID_U16));
494 18 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
495 18 : return ACL_SUCCESS;
496 24 : }
497 :
498 7 : aclError aclrtMemAllocManagedImpl(void **ptr, uint64_t size, uint32_t flag)
499 : {
500 7 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemAllocManaged);
501 7 : ACL_LOG_DEBUG("start to execute aclrtMemAllocManaged");
502 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
503 10 : ACL_REQUIRES_PARAM_EQUAL_REPORT(flag, ACL_RT_MEM_ATTACH_GLOBAL);
504 6 : ACL_REQUIRES_RTS_OK(rtMemAllocManaged(ptr, size, RT_MEMORY_ATTACH_GLOBAL, acl::APP_MODE_ID_U16));
505 6 : return ACL_SUCCESS;
506 7 : }
507 :
508 1 : aclError aclrtMemManagedAdviseImpl(const void *const ptr, uint64_t size, aclrtMemManagedAdviseType advise,
509 : aclrtMemManagedLocation location)
510 : {
511 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedAdvise);
512 1 : ACL_LOG_DEBUG("start to execute aclrtMemManagedAdvise");
513 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
514 1 : ACL_REQUIRES_POSITIVE_REPORT(size);
515 :
516 : rtMemManagedLocation memLocation;
517 1 : memLocation.id = location.id;
518 1 : memLocation.type = static_cast<rtMemManagedLocationType>(location.type);
519 :
520 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemManagedAdvise(ptr, size, advise, memLocation), rtMemManagedAdvise);
521 1 : return ACL_SUCCESS;
522 1 : }
523 :
524 18 : aclError aclrtFreeHostImpl(void *hostPtr)
525 : {
526 18 : ACL_PROFILING_REG(acl::AclProfType::AclrtFreeHost);
527 18 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
528 18 : ACL_LOG_DEBUG("start to execute aclrtFreeHost");
529 18 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(hostPtr);
530 17 : ACL_REQUIRES_RTS_OK(rtFreeHost(hostPtr));
531 16 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
532 16 : return ACL_SUCCESS;
533 18 : }
534 :
535 4 : aclError aclrtFreeWithDevSyncImpl(void *devPtr)
536 : {
537 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtFreeWithDevSync);
538 4 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
539 4 : ACL_LOG_DEBUG("start to execute aclrtFreeWithDevSync");
540 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
541 :
542 3 : ACL_REQUIRES_RTS_OK(rtFreeWithDevSync(devPtr));
543 2 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE);
544 2 : return ACL_SUCCESS;
545 4 : }
546 :
547 4 : aclError aclrtFreeHostWithDevSyncImpl(void *hostPtr)
548 : {
549 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtFreeHostWithDevSync);
550 4 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
551 4 : ACL_LOG_DEBUG("start to execute aclrtFreeHostWithDevSync");
552 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(hostPtr);
553 3 : ACL_REQUIRES_RTS_OK(rtFreeHostWithDevSync(hostPtr));
554 2 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_HOST);
555 2 : return ACL_SUCCESS;
556 4 : }
557 :
558 14 : aclError aclrtMemcpyImpl(void *dst,
559 : size_t destMax,
560 : const void *src,
561 : size_t count,
562 : aclrtMemcpyKind kind)
563 : {
564 14 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy);
565 14 : if (count == 0UL) {
566 3 : ACL_LOG_INFO("count is zero, no memory copy will be performed");
567 3 : return ACL_SUCCESS;
568 : }
569 11 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dst);
570 10 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
571 9 : rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED;
572 9 : const aclError ret = MemcpyKindTranslate(kind, rtKind);
573 9 : if (ret != ACL_SUCCESS) {
574 1 : ACL_LOG_ERROR("invalid kind of memcpy, kind = %s", acl::GetMemcpyKindDesc(kind));
575 1 : return ret;
576 : }
577 :
578 8 : ACL_REQUIRES_RTS_OK(rtMemcpy(dst, destMax, src, count, rtKind));
579 7 : return ACL_SUCCESS;
580 14 : }
581 :
582 5 : aclError aclrtMemsetImpl(void *devPtr, size_t maxCount, int32_t value, size_t count)
583 : {
584 5 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemset);
585 5 : ACL_LOG_DEBUG("start to execute aclrtMemset, maxSize = %zu, size = %zu, value = %d",
586 : maxCount, count, value);
587 5 : if (count == 0UL) {
588 2 : ACL_LOG_INFO("zero-size memset, no memory set will be performed");
589 2 : return ACL_SUCCESS;
590 : }
591 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
592 :
593 2 : ACL_REQUIRES_RTS_OK(rtMemset(devPtr, maxCount, static_cast<uint32_t>(value), count));
594 1 : return ACL_SUCCESS;
595 5 : }
596 :
597 25 : aclError aclrtMemcpyAsyncImpl(void *dst,
598 : size_t destMax,
599 : const void *src,
600 : size_t count,
601 : aclrtMemcpyKind kind,
602 : aclrtStream stream)
603 : {
604 25 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsync);
605 25 : if (count == 0UL) {
606 3 : ACL_LOG_INFO("count is zero, no memory copy async will be performed");
607 3 : return ACL_SUCCESS;
608 : }
609 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dst);
610 20 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
611 18 : rtMemcpyKind_t rtKindVal = RT_MEMCPY_RESERVED;
612 18 : const aclError ret = MemcpyKindTranslate(kind, rtKindVal);
613 18 : if (ret != ACL_SUCCESS) {
614 2 : ACL_LOG_ERROR("invalid kind of memcpy, kind = %s", acl::GetMemcpyKindDesc(kind));
615 2 : return ret;
616 : }
617 :
618 16 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemcpyAsync(dst, destMax, src, count, rtKindVal, static_cast<rtStream_t>(stream)),
619 : rtMemcpyAsync);
620 14 : return ACL_SUCCESS;
621 25 : }
622 :
623 15 : aclError aclrtMemcpyAsyncWithConditionImpl(void *dst,
624 : size_t destMax,
625 : const void *src,
626 : size_t count,
627 : aclrtMemcpyKind kind,
628 : aclrtStream stream)
629 : {
630 15 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithCondition);
631 15 : ACL_LOG_DEBUG("start to execute aclrtMemcpyAsyncWithCondition, destMaxSize = %zu, srcSize = %zu, kind = %d",
632 : destMax, count, static_cast<int32_t>(kind));
633 15 : if (count == 0UL) {
634 4 : ACL_LOG_INFO("zero-size memcpy, no memory copy async will be performed");
635 4 : return ACL_SUCCESS;
636 : }
637 11 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dst);
638 10 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
639 9 : rtMemcpyKind_t rtKindValue = RT_MEMCPY_RESERVED;
640 9 : const aclError ret = MemcpyKindTranslate(kind, rtKindValue);
641 9 : if (ret != ACL_SUCCESS) {
642 1 : ACL_LOG_ERROR("invalid kind of memcpy, kind = %s", acl::GetMemcpyKindDesc(kind));
643 1 : return ret;
644 : }
645 :
646 : rtMemcpyAttributeValue_t memcpyAttrValue;
647 : // bit0 standing for not checking matching between address and kind, bit1 standing for checking page-locked addr
648 8 : memcpyAttrValue.checkBitmap = 0x00000002U;
649 8 : rtMemcpyAttribute_t memcpyAttr = {
650 : .id = RT_MEMCPY_ATTRIBUTE_CHECK,
651 : .value = memcpyAttrValue
652 8 : };
653 8 : rtMemcpyConfig_t memcpyConfig = {
654 : .attrs = &memcpyAttr,
655 : .numAttrs = 1U
656 8 : };
657 :
658 8 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemcpyAsyncEx(dst, destMax, src, count, rtKindValue, static_cast<rtStream_t>(stream),
659 : &memcpyConfig), rtMemcpyAsyncEx);
660 7 : return ACL_SUCCESS;
661 15 : }
662 :
663 8 : aclError aclrtMemsetAsyncImpl(void *devPtr, size_t maxCount, int32_t value, size_t count, aclrtStream stream)
664 : {
665 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemsetAsync);
666 8 : ACL_LOG_DEBUG("start to execute aclrtMemsetAsync, maxCount = %zu, value = %d, count = %zu",
667 : maxCount, value, count);
668 8 : if (count == 0UL) {
669 2 : ACL_LOG_INFO("zero-size memset, no memory set async will be performed");
670 2 : return ACL_SUCCESS;
671 : }
672 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
673 :
674 4 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemsetAsync(devPtr, maxCount, static_cast<uint32_t>(value), count, stream),
675 : rtMemsetAsync);
676 2 : return ACL_SUCCESS;
677 8 : }
678 :
679 : // Determine whether it is ACL-allocated pinned memory (Host pinned memory or Device memory)
680 9 : static aclError IsAclPinnedMemory(const void* ptr, bool& isAclMem)
681 : {
682 9 : if (ptr == nullptr) {
683 0 : isAclMem = false;
684 0 : return ACL_SUCCESS;
685 : }
686 : aclrtPtrAttributes attr;
687 9 : aclError ret = aclrtPointerGetAttributesImpl(ptr, &attr);
688 9 : if (ret != ACL_SUCCESS) {
689 0 : isAclMem = false;
690 0 : return ret;
691 : }
692 21 : isAclMem = (attr.location.type == ACL_MEM_LOCATION_TYPE_HOST ||
693 11 : attr.location.type == ACL_MEM_LOCATION_TYPE_DEVICE ||
694 2 : attr.location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
695 9 : return ACL_SUCCESS;
696 : }
697 :
698 11 : aclError aclrtMemsetD32Impl(void* ptr, size_t memSize, uint32_t value, size_t N)
699 : {
700 11 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemsetD32);
701 :
702 11 : ACL_LOG_DEBUG("start to execute aclrtMemsetD32, memSize = %zu, N = %zu, value = 0x%x",
703 : memSize, N, value);
704 :
705 11 : if (N == 0UL) {
706 1 : ACL_LOG_INFO("zero-size memsetD32, no memory set will be performed");
707 1 : return ACL_SUCCESS;
708 : }
709 10 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
710 8 : ACL_REQUIRES_POSITIVE(N);
711 :
712 : // Check byte alignment
713 8 : if ((reinterpret_cast<uintptr_t>(ptr) & ALIGNMENT_4BYTE_MASK) != 0) {
714 2 : ACL_LOG_ERROR("Pointer ptr=%p is not 4-byte aligned", ptr);
715 2 : return ACL_ERROR_INVALID_PARAM;
716 : }
717 :
718 6 : const size_t requiredBytes = N * sizeof(uint32_t);
719 6 : if (memSize < requiredBytes) {
720 2 : ACL_LOG_ERROR("[Check][PARAM]N * 4 must be less than or equal to memSize, but N=%zu, memSize=%zu, requiredBytes=%zu",
721 : N, memSize, requiredBytes);
722 2 : const std::string nVal = std::to_string(N);
723 : std::string errMsg = acl::AclErrorLogManager::FormatStr(
724 : "N × 4 (%zu) is greater than memSize (%zu), which does not meet the requirement",
725 2 : requiredBytes, memSize);
726 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
727 4 : std::vector<const char *>({"func", "value", "param", "reason"}),
728 4 : std::vector<const char *>({"aclrtMemsetD32", nVal.c_str(), "N", errMsg.c_str()}));
729 2 : return ACL_ERROR_INVALID_PARAM;
730 2 : }
731 :
732 4 : bool isAclMem = false;
733 4 : aclError ret = IsAclPinnedMemory(ptr, isAclMem);
734 4 : if (ret != ACL_SUCCESS) {
735 0 : ACL_LOG_INNER_ERROR("Failed to check memory type, ret=%d", ret);
736 0 : return ret;
737 : }
738 4 : if (!isAclMem) {
739 1 : ACL_LOG_INNER_ERROR("Only memory allocated by aclrtMalloc or aclrtMallocHost is supported.");
740 1 : return ACL_ERROR_INVALID_PARAM;
741 : }
742 :
743 3 : const rtError_t rtErr = rtMemsetD32(ptr, static_cast<uint64_t>(memSize), value, N);
744 3 : if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
745 0 : ACL_LOG_WARN("rtMemsetD32 does not support this feature, runtime result = %d", rtErr);
746 3 : } else if (rtErr != RT_ERROR_NONE) {
747 1 : return ACL_GET_ERRCODE_RTS(rtErr);
748 : }
749 :
750 2 : return ACL_SUCCESS;
751 11 : }
752 :
753 9 : aclError aclrtMemsetD32AsyncImpl(void* ptr, size_t memSize, uint32_t value,
754 : size_t N, aclrtStream stream)
755 : {
756 9 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemsetD32Async);
757 :
758 9 : ACL_LOG_DEBUG("start to execute aclrtMemsetD32Async, memSize = %zu, N = %zu, value = 0x%x",
759 : memSize, N, value);
760 :
761 9 : if (N == 0UL) {
762 1 : ACL_LOG_INFO("zero-size memsetD32 async, no memory set will be performed");
763 1 : return ACL_SUCCESS;
764 : }
765 8 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
766 7 : ACL_REQUIRES_POSITIVE(N);
767 :
768 : // Check byte alignment
769 7 : if ((reinterpret_cast<uintptr_t>(ptr) & ALIGNMENT_4BYTE_MASK) != 0) {
770 1 : ACL_LOG_ERROR("Pointer ptr=%p is not 4-byte aligned", ptr);
771 1 : return ACL_ERROR_INVALID_PARAM;
772 : }
773 :
774 6 : const size_t requiredBytes = N * sizeof(uint32_t);
775 6 : if (memSize < requiredBytes) {
776 1 : ACL_LOG_ERROR("[Check][PARAM]N * 4 must be less than or equal to memSize, but N=%zu, memSize=%zu, requiredBytes=%zu",
777 : N, memSize, requiredBytes);
778 1 : const std::string nVal = std::to_string(N);
779 : std::string errMsg = acl::AclErrorLogManager::FormatStr(
780 : "N × 4 (%zu) is greater than memSize (%zu), which does not meet the requirement",
781 1 : requiredBytes, memSize);
782 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
783 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
784 2 : std::vector<const char *>({"aclrtMemsetD32Async", nVal.c_str(), "N", errMsg.c_str()}));
785 1 : return ACL_ERROR_INVALID_PARAM;
786 1 : }
787 :
788 5 : bool isAclMem = false;
789 5 : aclError ret = IsAclPinnedMemory(ptr, isAclMem);
790 5 : if (ret != ACL_SUCCESS) {
791 0 : ACL_LOG_INNER_ERROR("Failed to check memory type, ret=%d", ret);
792 0 : return ret;
793 : }
794 5 : if (!isAclMem) {
795 1 : ACL_LOG_INNER_ERROR("Only memory allocated by aclrtMalloc or aclrtMallocHost is supported.");
796 1 : return ACL_ERROR_INVALID_PARAM;
797 : }
798 :
799 4 : const rtError_t rtErr = rtMemsetD32Async(ptr, static_cast<uint64_t>(memSize),
800 : value, N, static_cast<rtStream_t>(stream));
801 4 : if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
802 0 : ACL_LOG_WARN("rtMemsetD32Async does not support this feature, runtime result = %d", rtErr);
803 4 : } else if (rtErr != RT_ERROR_NONE) {
804 1 : return ACL_GET_ERRCODE_RTS(rtErr);
805 : }
806 :
807 3 : return ACL_SUCCESS;
808 9 : }
809 :
810 8 : aclError aclrtDeviceCanAccessPeerImpl(int32_t *canAccessPeer, int32_t deviceId, int32_t peerDeviceId)
811 : {
812 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceCanAccessPeer);
813 8 : ACL_LOG_INFO("start to execute aclrtDeviceCanAccessPeer");
814 :
815 8 : if (deviceId == peerDeviceId) {
816 2 : ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
817 2 : const std::string deviceIdVal = std::to_string(deviceId);
818 : std::string errMsg = acl::AclErrorLogManager::FormatStr("deviceId %d cannot be equal to peerDeviceId %d",
819 2 : deviceId, peerDeviceId);
820 2 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
821 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
822 4 : std::vector<const char *>({"func", "value", "param", "reason"}),
823 4 : std::vector<const char *>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
824 2 : return ACL_ERROR_INVALID_PARAM;
825 2 : }
826 :
827 6 : uint32_t peerPhyId = 0U;
828 6 : ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
829 :
830 4 : ACL_REQUIRES_RTS_OK(rtDeviceCanAccessPeer(canAccessPeer, static_cast<uint32_t>(deviceId), peerPhyId));
831 :
832 2 : return ACL_SUCCESS;
833 8 : }
834 :
835 10 : aclError aclrtDeviceEnablePeerAccessImpl(int32_t peerDeviceId, uint32_t flags)
836 : {
837 10 : ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceEnablePeerAccess);
838 10 : ACL_LOG_INFO("start to execute aclrtDeviceEnablePeerAccess");
839 16 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0U, ACL_ERROR_FEATURE_UNSUPPORTED);
840 :
841 8 : int32_t deviceId = 0;
842 8 : ACL_REQUIRES_RTS_OK(rtGetDevice(&deviceId));
843 :
844 6 : if (deviceId == peerDeviceId) {
845 1 : ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
846 1 : const std::string deviceIdVal = std::to_string(deviceId);
847 : std::string errMsg = acl::AclErrorLogManager::FormatStr("deviceId %d cannot be equal to peerDeviceId %d",
848 1 : deviceId, peerDeviceId);
849 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
850 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
851 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
852 2 : std::vector<const char *>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
853 1 : return ACL_ERROR_INVALID_PARAM;
854 1 : }
855 :
856 5 : uint32_t peerPhyId = 0U;
857 5 : ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
858 :
859 3 : ACL_REQUIRES_RTS_OK(rtEnableP2P(static_cast<uint32_t>(deviceId), peerPhyId, flags));
860 :
861 1 : return ACL_SUCCESS;
862 10 : }
863 :
864 9 : aclError aclrtDeviceDisablePeerAccessImpl(int32_t peerDeviceId)
865 : {
866 9 : ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceDisablePeerAccess);
867 9 : ACL_LOG_INFO("start to execute aclrtDeviceDisablePeerAccess");
868 :
869 9 : int32_t deviceId = 0;
870 9 : ACL_REQUIRES_RTS_OK(rtGetDevice(&deviceId));
871 :
872 7 : if (deviceId == peerDeviceId) {
873 2 : ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
874 2 : const std::string deviceIdVal = std::to_string(deviceId);
875 : std::string errMsg = acl::AclErrorLogManager::FormatStr("deviceId %d cannot be equal to peerDeviceId %d",
876 2 : deviceId, peerDeviceId);
877 2 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
878 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
879 4 : std::vector<const char *>({"func", "value", "param", "reason"}),
880 4 : std::vector<const char *>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
881 2 : return ACL_ERROR_INVALID_PARAM;
882 2 : }
883 :
884 5 : uint32_t peerPhyId = 0U;
885 5 : ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
886 :
887 3 : ACL_REQUIRES_RTS_OK(rtDisableP2P(static_cast<uint32_t>(deviceId), peerPhyId));
888 :
889 1 : return ACL_SUCCESS;
890 9 : }
891 :
892 7 : aclError aclrtGetMemInfoImpl(aclrtMemAttr attr, size_t *free, size_t *total)
893 : {
894 7 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemInfo);
895 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(free);
896 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(total);
897 7 : ACL_LOG_DEBUG("start to execute aclrtGetMemInfo, memory attribute = %d", static_cast<int32_t>(attr));
898 :
899 7 : ACL_REQUIRES_RTS_OK(rtMemGetInfoEx(static_cast<rtMemInfoType_t>(attr), free, total));
900 :
901 5 : ACL_LOG_DEBUG("successfully execute aclrtGetMemInfo, memory attribute = %d, free memory = %zu bytes, "
902 : "total memory = %zu bytes", static_cast<int32_t>(attr), *free, *total);
903 5 : return ACL_SUCCESS;
904 7 : }
905 :
906 2 : aclError aclrtGetMemUsageInfoImpl(int32_t deviceId, aclrtMemUsageInfo *memUsageInfo, size_t inputNum, size_t *outputNum)
907 : {
908 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemUsageInfo);
909 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memUsageInfo);
910 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(outputNum);
911 2 : ACL_LOG_DEBUG("start to execute aclrtGetMemUsageInfo, deviceId = %d, inputNum = %zu", static_cast<int32_t>(deviceId), inputNum);
912 :
913 2 : ACL_REQUIRES_RTS_OK(rtGetMemUsageInfo(static_cast<uint32_t>(deviceId), reinterpret_cast<rtMemUsageInfo_t*>(memUsageInfo), inputNum, outputNum));
914 :
915 1 : ACL_LOG_DEBUG("successfully execute aclrtGetMemUsageInfo, deviceId = %d, inputNum = %zu", deviceId, inputNum);
916 1 : return ACL_SUCCESS;
917 2 : }
918 :
919 14 : aclError aclrtMemcpy2dImpl(void *dst,
920 : size_t dpitch,
921 : const void *src,
922 : size_t spitch,
923 : size_t width,
924 : size_t height,
925 : aclrtMemcpyKind kind)
926 : {
927 14 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2d);
928 14 : ACL_LOG_DEBUG("start to execute aclrtMemcpy2d, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, kind = %d",
929 : dpitch, spitch, width, height, static_cast<int32_t>(kind));
930 :
931 14 : rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED;
932 14 : const aclError ret = CheckMemcpy2dParam(dst, dpitch, src, spitch, width, height, kind, rtKind);
933 14 : if (ret != ACL_SUCCESS) {
934 5 : return ret;
935 : }
936 :
937 9 : ACL_REQUIRES_RTS_OK(rtMemcpy2d(dst, dpitch, src, spitch, width, height, rtKind));
938 :
939 8 : ACL_LOG_DEBUG("Successfuly execute aclrtMemcpy2d, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, "
940 : "kind = %d", dpitch, spitch, width, height, static_cast<int32_t>(kind));
941 8 : return ACL_SUCCESS;
942 14 : }
943 :
944 15 : aclError aclrtMemcpy2dAsyncImpl(void *dst,
945 : size_t dpitch,
946 : const void *src,
947 : size_t spitch,
948 : size_t width,
949 : size_t height,
950 : aclrtMemcpyKind kind,
951 : aclrtStream stream)
952 : {
953 15 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2dAsync);
954 15 : ACL_LOG_DEBUG("start to execute aclrtMemcpy2dAsync, dpitch = %zu, spitch = %zu, width = %zu, height = %zu,"
955 : " kind = %d", dpitch, spitch, width, height, static_cast<int32_t>(kind));
956 :
957 15 : rtMemcpyKind_t rtKindVal = RT_MEMCPY_RESERVED;
958 15 : const aclError ret = CheckMemcpy2dParam(dst, dpitch, src, spitch, width, height, kind, rtKindVal);
959 15 : if (ret != ACL_SUCCESS) {
960 5 : return ret;
961 : }
962 :
963 10 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemcpy2dAsync(dst, dpitch, src, spitch, width, height, rtKindVal, stream),
964 : rtMemcpy2dAsync);
965 :
966 9 : ACL_LOG_DEBUG("Successfuly execute aclrtMemcpy2dAsync, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, "
967 : "kind = %d", dpitch, spitch, width, height, static_cast<int32_t>(kind));
968 9 : return ACL_SUCCESS;
969 15 : }
970 :
971 10 : aclError aclrtReserveMemAddressImpl(void **virPtr,
972 : size_t size,
973 : size_t alignment,
974 : void *expectPtr,
975 : uint64_t flags)
976 : {
977 10 : ACL_PROFILING_REG(acl::AclProfType::AclrtReserveMemAddress);
978 10 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
979 10 : ACL_LOG_DEBUG("start to execute aclrtReserveMemAddress, size = %zu", size);
980 10 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
981 :
982 13 : ACL_REQUIRES_POSITIVE_REPORT(size);
983 : // flags参数取1,为了早期接口兼容性保留
984 12 : ACL_CHECK_INVALID_VALUE_WITH_EXPECT((flags == 0ULL) || (flags == 1ULL), flags, "0");
985 :
986 8 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtReserveMemAddress(virPtr, size, alignment, expectPtr, flags), rtReserveMemAddress);
987 6 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
988 6 : return ACL_SUCCESS;
989 10 : }
990 :
991 8 : aclError aclrtReleaseMemAddressImpl(void *virPtr)
992 : {
993 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtReleaseMemAddress);
994 8 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
995 8 : ACL_LOG_DEBUG("start to execute aclrtReleaseMemAddress");
996 8 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
997 :
998 8 : ACL_REQUIRES_RTS_OK(rtReleaseMemAddress(virPtr));
999 6 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
1000 6 : return ACL_SUCCESS;
1001 8 : }
1002 :
1003 35 : aclError aclrtMallocPhysicalImpl(aclrtDrvMemHandle *handle,
1004 : size_t size,
1005 : const aclrtPhysicalMemProp *prop,
1006 : uint64_t flags)
1007 : {
1008 35 : ACL_PROFILING_REG(acl::AclProfType::AclrtMallocPhysical);
1009 35 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
1010 35 : ACL_LOG_DEBUG("start to execute aclrtMallocPhysical, size = %zu", size);
1011 35 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1012 35 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
1013 :
1014 38 : ACL_REQUIRES_POSITIVE_REPORT(size);
1015 37 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
1016 36 : ACL_REQUIRES_PARAM_EQUAL_REPORT(prop->handleType, ACL_MEM_HANDLE_TYPE_NONE);
1017 38 : ACL_REQUIRES_PARAM_EQUAL_REPORT(prop->allocationType, ACL_MEM_ALLOCATION_TYPE_PINNED);
1018 30 : if (prop->location.type == ACL_MEM_LOCATION_TYPE_UNREGISTERED ||
1019 30 : prop->location.type == ACL_MEM_LOCATION_TYPE_MANAGED) {
1020 1 : ACL_LOG_ERROR(
1021 : "[Check][PARAM]prop->location.type is invalid, location type does not support %s. value=%s",
1022 : acl::GetMemLocationTypeDesc(prop->location.type), acl::GetMemLocationTypeDesc(prop->location.type));
1023 : std::string errMsg = acl::AclErrorLogManager::FormatStr(
1024 1 : "location type does not support %s", acl::GetMemLocationTypeDesc(prop->location.type));
1025 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1026 1 : acl::AclErrorLogManager::ReportInputError(
1027 2 : acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
1028 1 : std::vector<const char*>(
1029 1 : {funcName.c_str(), acl::GetMemLocationTypeDesc(prop->location.type), "prop->location.type",
1030 2 : errMsg.c_str()}));
1031 1 : return ACL_ERROR_INVALID_PARAM;
1032 1 : }
1033 :
1034 29 : rtDrvMemProp_t rtProp = {};
1035 29 : rtProp.side = prop->location.type;
1036 29 : rtProp.devid = prop->location.id;
1037 29 : rtProp.module_id = acl::APP_MODE_ID_U16;
1038 29 : rtProp.reserve = prop->reserve;
1039 :
1040 : // device alloc
1041 29 : bool isDeviceAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_DEVICE);
1042 29 : if (isDeviceAlloc && ((prop->memAttr == ACL_DDR_MEM_HUGE) || (prop->memAttr == ACL_DDR_MEM_NORMAL) || (prop->memAttr == ACL_DDR_MEM_P2P_HUGE)
1043 18 : || (prop->memAttr == ACL_DDR_MEM_P2P_NORMAL))) {
1044 1 : ACL_LOG_ERROR("memAttr [%s] only support ACL_MEM_LOCATION_TYPE_HOST or ACL_MEM_LOCATION_TYPE_HOST_NUMA.", acl::GetMemAttrDesc(prop->memAttr));
1045 1 : const std::string memAttrVal = acl::GetMemAttrDesc(prop->memAttr);
1046 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1047 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
1048 2 : std::vector<const char *>({"func", "value", "param", "expect"}),
1049 2 : std::vector<const char *>({funcName.c_str(), memAttrVal.c_str(), "memAttr",
1050 2 : "ACL_MEM_LOCATION_TYPE_HOST or ACL_MEM_LOCATION_TYPE_HOST_NUMA"}));
1051 1 : return ACL_ERROR_INVALID_PARAM;
1052 1 : }
1053 28 : auto it = memAttrHandlers.find(static_cast<int32_t>(prop->memAttr));
1054 28 : if (it != memAttrHandlers.end()) {
1055 : // host alloc
1056 26 : const bool isHostAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) || (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
1057 26 : it->second(rtProp, isHostAlloc, isDeviceAlloc);
1058 : } else {
1059 2 : ACL_LOG_ERROR("memAttr [%s] is not supported. "
1060 : "For details, please refer to the manual.",
1061 : acl::GetMemAttrDesc(prop->memAttr));
1062 2 : const std::string memAttrVal = acl::GetMemAttrDesc(prop->memAttr);
1063 2 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1064 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1065 4 : std::vector<const char *>({"func", "value", "param", "reason"}),
1066 4 : std::vector<const char *>({funcName.c_str(), memAttrVal.c_str(), "memAttr",
1067 4 : "The current physical memory attribute is not supported"}));
1068 2 : return ACL_ERROR_INVALID_PARAM;
1069 2 : }
1070 :
1071 26 : ACL_REQUIRES_RTS_OK(rtMallocPhysical(reinterpret_cast<rtDrvMemHandle*>(handle), size, &rtProp, flags));
1072 25 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
1073 25 : return ACL_SUCCESS;
1074 35 : }
1075 :
1076 6 : aclError aclrtFreePhysicalImpl(aclrtDrvMemHandle handle)
1077 : {
1078 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtFreePhysical);
1079 6 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
1080 6 : ACL_LOG_DEBUG("start to execute aclrtFreePhysical");
1081 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1082 :
1083 6 : ACL_REQUIRES_RTS_OK(rtFreePhysical(reinterpret_cast<rtDrvMemHandle>(handle)));
1084 5 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
1085 5 : return ACL_SUCCESS;
1086 6 : }
1087 :
1088 6 : aclError aclrtMapMemImpl(void *virPtr,
1089 : size_t size,
1090 : size_t offset,
1091 : aclrtDrvMemHandle handle,
1092 : uint64_t flags)
1093 : {
1094 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtMapMem);
1095 6 : ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
1096 6 : ACL_LOG_DEBUG("start to execute aclrtMapMem, size = %zu, offset = %zu", size, offset);
1097 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
1098 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1099 :
1100 9 : ACL_REQUIRES_POSITIVE_REPORT(size);
1101 8 : ACL_REQUIRES_PARAM_EQUAL_REPORT(flags, 0);
1102 4 : ACL_REQUIRES_RTS_OK(rtMapMem(virPtr, size, offset, reinterpret_cast<rtDrvMemHandle>(handle), flags));
1103 3 : ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
1104 3 : return ACL_SUCCESS;
1105 6 : }
1106 :
1107 4 : aclError aclrtUnmapMemImpl(void *virPtr)
1108 : {
1109 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtUnmapMem);
1110 4 : ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
1111 4 : ACL_LOG_DEBUG("start to execute aclrtUnmapMem");
1112 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
1113 :
1114 4 : ACL_REQUIRES_RTS_OK(rtUnmapMem(virPtr));
1115 3 : ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
1116 3 : return ACL_SUCCESS;
1117 4 : }
1118 :
1119 2 : aclError aclrtMemGetAccessImpl(void *virPtr, aclrtMemLocation *location, uint64_t *flag)
1120 : {
1121 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAccess);
1122 2 : ACL_LOG_DEBUG("start to execute aclrtMemGetAccess");
1123 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
1124 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(location);
1125 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(flag);
1126 :
1127 2 : ACL_REQUIRES_RTS_OK(rtMemGetAccess(virPtr, reinterpret_cast<rtMemLocation*>(location), flag));
1128 1 : return ACL_SUCCESS;
1129 2 : }
1130 :
1131 4 : aclError aclrtMemExportToShareableHandleImpl(aclrtDrvMemHandle handle, aclrtMemHandleType handleType,
1132 : uint64_t flags, uint64_t *shareableHandle)
1133 : {
1134 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemExportToShareableHandle);
1135 4 : ACL_LOG_DEBUG("start to execute aclrtMemExportToShareableHandle");
1136 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1137 6 : ACL_REQUIRES_PARAM_EQUAL_REPORT(handleType, ACL_MEM_HANDLE_TYPE_NONE);
1138 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
1139 :
1140 2 : ACL_REQUIRES_RTS_OK(rtsMemExportToShareableHandle(reinterpret_cast<rtDrvMemHandle>(handle),
1141 : RT_MEM_HANDLE_TYPE_NONE, flags, shareableHandle));
1142 1 : return ACL_SUCCESS;
1143 4 : }
1144 :
1145 3 : aclError aclrtMemExportToShareableHandleV2Impl(aclrtDrvMemHandle handle, uint64_t flags,
1146 : aclrtMemSharedHandleType shareType, void *shareableHandle)
1147 : {
1148 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemExportToShareableHandleV2);
1149 3 : ACL_LOG_DEBUG("start to execute aclrtMemExportToShareableHandleV2");
1150 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1151 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
1152 :
1153 2 : ACL_REQUIRES_RTS_OK(rtMemExportToShareableHandleV2(reinterpret_cast<rtDrvMemHandle>(handle),
1154 : static_cast<rtMemSharedHandleType>(shareType), flags, shareableHandle));
1155 1 : return ACL_SUCCESS;
1156 3 : }
1157 :
1158 3 : aclError aclrtMemImportFromShareableHandleImpl(uint64_t shareableHandle,
1159 : int32_t deviceId, aclrtDrvMemHandle *handle)
1160 : {
1161 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemImportFromShareableHandle);
1162 3 : ACL_LOG_DEBUG("start to execute aclrtMemImportFromShareableHandle");
1163 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1164 :
1165 2 : ACL_REQUIRES_RTS_OK(rtMemImportFromShareableHandle(shareableHandle, deviceId,
1166 : reinterpret_cast<rtDrvMemHandle*>(handle)));
1167 1 : return ACL_SUCCESS;
1168 3 : }
1169 :
1170 4 : aclError aclrtMemImportFromShareableHandleV2Impl(void *shareableHandle, aclrtMemSharedHandleType shareType,
1171 : uint64_t flags, aclrtDrvMemHandle *handle)
1172 : {
1173 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemImportFromShareableHandleV2);
1174 4 : ACL_LOG_DEBUG("start to execute aclrtMemImportFromShareableHandleV2");
1175 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
1176 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1177 6 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
1178 :
1179 2 : int32_t deviceId = 0;
1180 2 : const rtError_t rtRet = rtsGetDevice(&deviceId);
1181 2 : if (rtRet != ACL_RT_SUCCESS) {
1182 0 : return rtRet;
1183 : }
1184 :
1185 2 : ACL_REQUIRES_RTS_OK(rtMemImportFromShareableHandleV2(shareableHandle,
1186 : static_cast<rtMemSharedHandleType>(shareType), flags, deviceId, reinterpret_cast<rtDrvMemHandle*>(handle)));
1187 1 : return ACL_SUCCESS;
1188 4 : }
1189 :
1190 4 : aclError aclrtMemSetPidToShareableHandleImpl(uint64_t shareableHandle, int32_t *pid, size_t pidNum)
1191 : {
1192 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetPidToShareableHandle);
1193 4 : ACL_LOG_DEBUG("start to execute aclrtMemSetPidToShareableHandle");
1194 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
1195 6 : ACL_REQUIRES_POSITIVE_REPORT(pidNum);
1196 2 : ACL_REQUIRES_RTS_OK(rtMemSetPidToShareableHandle(shareableHandle, pid, static_cast<uint32_t>(pidNum)));
1197 1 : return ACL_SUCCESS;
1198 4 : }
1199 :
1200 4 : aclError aclrtMemSetPidToShareableHandleV2Impl(void *shareableHandle, aclrtMemSharedHandleType shareType,
1201 : int32_t *pid, size_t pidNum)
1202 : {
1203 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetPidToShareableHandleV2);
1204 4 : ACL_LOG_DEBUG("start to execute AclrtMemSetPidToShareableHandleV2");
1205 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
1206 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
1207 6 : ACL_REQUIRES_POSITIVE_REPORT(pidNum);
1208 :
1209 2 : ACL_REQUIRES_RTS_OK(rtMemSetPidToShareableHandleV2(shareableHandle,
1210 : static_cast<rtMemSharedHandleType>(shareType), pid, static_cast<uint32_t>(pidNum)));
1211 1 : return ACL_SUCCESS;
1212 4 : }
1213 :
1214 11 : aclError aclrtMemGetAllocationGranularityImpl(aclrtPhysicalMemProp *prop, aclrtMemGranularityOptions option,
1215 : size_t *granularity)
1216 : {
1217 11 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAllocationGranularity);
1218 11 : ACL_LOG_DEBUG("start to execute aclrtMemGetAllocationGranularity");
1219 11 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
1220 10 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(granularity);
1221 :
1222 9 : rtDrvMemProp_t rtProp1 = {};
1223 9 : rtProp1.side = prop->location.type;
1224 9 : rtProp1.devid = prop->location.id;
1225 9 : rtProp1.module_id = acl::APP_MODE_ID_U16;
1226 9 : rtProp1.reserve = prop->reserve;
1227 :
1228 : // device alloc
1229 9 : bool isDeviceAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_DEVICE);
1230 9 : if (isDeviceAlloc && ((prop->memAttr == ACL_DDR_MEM_HUGE) || (prop->memAttr == ACL_DDR_MEM_NORMAL) || (prop->memAttr == ACL_DDR_MEM_P2P_HUGE)
1231 1 : || (prop->memAttr == ACL_DDR_MEM_P2P_NORMAL))) {
1232 4 : ACL_LOG_ERROR("memAttr [%s] only support ACL_MEM_LOCATION_TYPE_HOST or ACL_MEM_LOCATION_TYPE_HOST_NUMA.", acl::GetMemAttrDesc(prop->memAttr));
1233 4 : const std::string memAttrVal = acl::GetMemAttrDesc(prop->memAttr);
1234 4 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1235 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
1236 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
1237 8 : std::vector<const char *>({funcName.c_str(), memAttrVal.c_str(), "memAttr",
1238 8 : "ACL_MEM_LOCATION_TYPE_HOST or ACL_MEM_LOCATION_TYPE_HOST_NUMA"}));
1239 4 : return ACL_ERROR_INVALID_PARAM;
1240 4 : }
1241 5 : auto it = memAttrHandlers.find(static_cast<int32_t>(prop->memAttr));
1242 5 : if (it != memAttrHandlers.end()) {
1243 : // host alloc
1244 4 : const bool isHostAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) || (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
1245 4 : it->second(rtProp1, isHostAlloc, isDeviceAlloc);
1246 : } else {
1247 1 : ACL_LOG_ERROR("memAttr [%s] is not supported. "
1248 : "For details, please refer to the manual.",
1249 : acl::GetMemAttrDesc(prop->memAttr));
1250 1 : const std::string memAttrVal2 = acl::GetMemAttrDesc(prop->memAttr);
1251 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1252 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1253 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
1254 2 : std::vector<const char *>({funcName.c_str(), memAttrVal2.c_str(), "memAttr",
1255 2 : "The current physical memory attribute is not supported"}));
1256 1 : return ACL_ERROR_INVALID_PARAM;
1257 1 : }
1258 :
1259 4 : ACL_REQUIRES_RTS_OK(rtMemGetAllocationGranularity(&rtProp1,
1260 : static_cast<rtDrvMemGranularityOptions>(option), granularity));
1261 3 : return ACL_SUCCESS;
1262 11 : }
1263 :
1264 3 : aclError aclrtDeviceGetBareTgidImpl(int32_t *pid)
1265 : {
1266 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceGetBareTgid);
1267 3 : ACL_LOG_DEBUG("start to execute aclrtDeviceGetBareTgid");
1268 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
1269 :
1270 2 : ACL_REQUIRES_RTS_OK(rtDeviceGetBareTgid(reinterpret_cast<uint32_t *>(pid)));
1271 1 : return ACL_SUCCESS;
1272 3 : }
1273 :
1274 4 : aclError aclrtCmoAsyncImpl(void *src, size_t size, aclrtCmoType cmoType, aclrtStream stream)
1275 : {
1276 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsync);
1277 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
1278 6 : ACL_REQUIRES_POSITIVE_REPORT(size);
1279 2 : const rtCmoOpCode_t type = static_cast<rtCmoOpCode_t>(static_cast<uint32_t>(cmoType) +
1280 : (static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH)));
1281 2 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtCmoAsync(src, size, type, stream), rtCmoAsync);
1282 1 : return ACL_SUCCESS;
1283 4 : }
1284 :
1285 2 : aclError aclrtGetMemcpyDescSizeImpl(aclrtMemcpyKind kind, size_t *descSize)
1286 : {
1287 2 : ACL_LOG_INFO("start to execute aclrtGetMemcpyDescSize");
1288 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(descSize);
1289 2 : ACL_CHECK_INVALID_VALUE_WITH_DESC(
1290 : static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX),
1291 : acl::GetMemcpyKindDesc(kind), "kind",
1292 : "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)",
1293 : ACL_ERROR_INVALID_PARAM);
1294 2 : const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<uint32_t>(kind));
1295 2 : ACL_REQUIRES_RTS_OK(rtsGetMemcpyDescSize(rt_mem_kind, descSize));
1296 1 : return ACL_SUCCESS;
1297 : }
1298 :
1299 6 : aclError aclrtSetMemcpyDescImpl(void *desc, aclrtMemcpyKind kind, void *srcAddr, void *dstAddr, size_t count,
1300 : void *config)
1301 : {
1302 6 : ACL_LOG_INFO("start to execute aclrtSetMemcpyDesc");
1303 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(desc);
1304 5 : ACL_CHECK_INVALID_VALUE_WITH_DESC(
1305 : static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX),
1306 : acl::GetMemcpyKindDesc(kind), "kind",
1307 : "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)",
1308 : ACL_ERROR_INVALID_PARAM);
1309 5 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(srcAddr);
1310 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dstAddr);
1311 6 : ACL_REQUIRES_POSITIVE_REPORT(count);
1312 2 : ACL_CHECK_INVALID_PARAM_NO_VALUE(config == nullptr, "reserve", "config is a reserved parameter and must be nullptr");
1313 :
1314 2 : const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<uint32_t>(kind));
1315 2 : ACL_REQUIRES_RTS_OK(rtsSetMemcpyDesc(static_cast<rtMemcpyDesc_t>(desc), rt_mem_kind, srcAddr, dstAddr,
1316 : count, nullptr));
1317 1 : return ACL_SUCCESS;
1318 : }
1319 :
1320 4 : aclError aclrtMemcpyAsyncWithDescImpl(void *desc, aclrtMemcpyKind kind, aclrtStream stream)
1321 : {
1322 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithDesc);
1323 4 : ACL_LOG_INFO("start to execute aclrtMemcpyAsyncWithDesc");
1324 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(desc);
1325 3 : ACL_CHECK_INVALID_VALUE_WITH_DESC(
1326 : static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX),
1327 : acl::GetMemcpyKindDesc(kind), "kind",
1328 : "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)",
1329 : ACL_ERROR_INVALID_PARAM);
1330 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);
1331 :
1332 2 : const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<int32_t>(kind));
1333 2 : ACL_REQUIRES_RTS_OK(rtsMemcpyAsyncWithDesc(static_cast<rtMemcpyDesc_t>(desc), rt_mem_kind, nullptr, stream));
1334 1 : return ACL_SUCCESS;
1335 4 : }
1336 :
1337 6 : aclError aclrtMemcpyAsyncWithOffsetImpl(void **dst, size_t destMax, size_t dstDataOffset, const void **src,
1338 : size_t count, size_t srcDataOffset, aclrtMemcpyKind kind, aclrtStream stream)
1339 : {
1340 6 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithOffset);
1341 6 : ACL_LOG_INFO("start to execute aclrtMemcpyAsyncWithOffset");
1342 6 : if (count == 0UL) {
1343 2 : ACL_LOG_INFO("zero-size memcpy, no memory copy async with offsetwill be performed");
1344 2 : return ACL_SUCCESS;
1345 : }
1346 :
1347 4 : const auto memKind = static_cast<rtMemcpyKind>(static_cast<int32_t>(kind));
1348 4 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemcpyAsyncWithOffset(dst, destMax, dstDataOffset, src, count, srcDataOffset, memKind, stream), rtMemcpyAsyncWithOffset);
1349 1 : ACL_LOG_INFO("successfully execute aclrtMemcpyAsyncWithOffset");
1350 1 : return ACL_SUCCESS;
1351 6 : }
1352 :
1353 3 : aclError aclrtValueWriteImpl(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
1354 : {
1355 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtValueWrite);
1356 3 : ACL_LOG_INFO("start to execute aclrtValueWrite");
1357 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devAddr);
1358 :
1359 2 : ACL_REQUIRES_RTS_OK(rtsValueWrite(devAddr, value, flag, static_cast<rtStream_t>(stream)));
1360 1 : return ACL_SUCCESS;
1361 3 : }
1362 :
1363 3 : aclError aclrtValueWaitImpl(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
1364 : {
1365 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtValueWait);
1366 3 : ACL_LOG_INFO("start to execute aclrtValueWait");
1367 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devAddr);
1368 :
1369 2 : ACL_REQUIRES_RTS_OK(rtsValueWait(devAddr, value, flag, static_cast<rtStream_t>(stream)));
1370 1 : return ACL_SUCCESS;
1371 3 : }
1372 :
1373 15 : aclError aclrtReduceAsyncImpl(void *dst, const void *src, uint64_t count, aclrtReduceKind kind,
1374 : aclDataType type, aclrtStream stream, void *reserve)
1375 : {
1376 15 : ACL_PROFILING_REG(acl::AclProfType::AclrtReduceAsync);
1377 15 : ACL_LOG_DEBUG("start to execute aclrtReduceAsync, count = [%lu], kind = [%u], type = [%u]", count,
1378 : static_cast<uint32_t>(kind), static_cast<uint32_t>(type));
1379 15 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dst);
1380 14 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
1381 13 : ACL_CHECK_INVALID_PARAM_NO_VALUE(reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr");
1382 :
1383 : rtDataType dataType;
1384 12 : if (kMapDataType.count(type) > 0) {
1385 11 : dataType = kMapDataType.at(type);
1386 : } else {
1387 1 : ACL_LOG_ERROR("[Check][param]param type [%d] is invalid.", static_cast<int32_t>(type));
1388 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1389 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1390 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
1391 2 : std::vector<const char *>({funcName.c_str(), acl::GetDataTypeDesc(type), "type", "The data type is currently not supported"}));
1392 1 : return ACL_ERROR_INVALID_PARAM;
1393 1 : }
1394 :
1395 : rtReduceInfo_t reduceInfo;
1396 11 : reduceInfo.dst = dst;
1397 11 : reduceInfo.src = const_cast<void*>(src);
1398 11 : reduceInfo.count = static_cast<size_t>(count);
1399 11 : reduceInfo.kind = static_cast<rtReduceKind>(kind);
1400 11 : reduceInfo.type = dataType;
1401 11 : ACL_REQUIRES_RTS_OK(rtsLaunchReduceAsyncTask(&reduceInfo, static_cast<rtStream_t>(stream), reserve));
1402 10 : return ACL_SUCCESS;
1403 15 : }
1404 :
1405 1 : aclError aclrtGetBufFromChainImpl(aclrtMbuf headBuf, uint32_t index, aclrtMbuf *buf)
1406 : {
1407 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufFromChain);
1408 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
1409 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1410 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainGetMbuf(headBuf, index, buf), rtMbufChainGetMbuf);
1411 1 : return ACL_SUCCESS;
1412 1 : }
1413 :
1414 1 : aclError aclrtGetBufChainNumImpl(aclrtMbuf headBuf, uint32_t *num)
1415 : {
1416 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufChainNum);
1417 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
1418 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(num);
1419 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainGetMbufNum(headBuf, num), rtMbufChainGetMbufNum);
1420 1 : return ACL_SUCCESS;
1421 1 : }
1422 :
1423 1 : aclError aclrtAppendBufChainImpl(aclrtMbuf headBuf, aclrtMbuf buf)
1424 : {
1425 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtAppendBufChain);
1426 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
1427 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1428 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainAppend(headBuf, buf), rtMbufChainAppend);
1429 1 : return ACL_SUCCESS;
1430 1 : }
1431 :
1432 1 : aclError aclrtCopyBufRefImpl(const aclrtMbuf buf, aclrtMbuf *newBuf)
1433 : {
1434 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtCopyBufRef);
1435 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1436 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(newBuf);
1437 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufCopyBufRef(buf, newBuf), rtMbufCopyBufRef);
1438 1 : return ACL_SUCCESS;
1439 1 : }
1440 :
1441 3 : aclError aclrtGetBufUserDataImpl(const aclrtMbuf buf, void *dataPtr, size_t size, size_t offset)
1442 : {
1443 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufUserData);
1444 : // The current default private data area size is 96B, if offset+size exceeds 96, an error is reported
1445 3 : if (size + offset > MEM_SIZE_MAX) {
1446 1 : ACL_LOG_ERROR("%s failed because the sum of size and offset is greater than %u, size=%zu, offset=%zu.", __func__,
1447 : MEM_SIZE_MAX, size, offset);
1448 1 : const std::string sizeVal = std::to_string(size);
1449 : std::string errMsg = acl::AclErrorLogManager::FormatStr("the sum of size and offset is greater than %u, size=%zu, offset=%zu.",
1450 1 : MEM_SIZE_MAX, size, offset);
1451 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1452 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1453 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
1454 2 : std::vector<const char *>({funcName.c_str(), sizeVal.c_str(), "size", errMsg.c_str()}));
1455 1 : return ACL_ERROR_INVALID_PARAM;
1456 1 : }
1457 :
1458 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1459 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
1460 2 : uint64_t bufSize = 0U;
1461 2 : void *tmpDataPtr = nullptr;
1462 2 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetPrivInfo(buf, &tmpDataPtr, &bufSize), rtMbufGetPrivInfo);
1463 2 : ACL_CHECK_LESS_UINT(size + offset, static_cast<size_t>(bufSize));
1464 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(tmpDataPtr);
1465 2 : const void *const srcAddr = static_cast<uint8_t *>(tmpDataPtr) + offset;
1466 2 : const auto ret = memcpy_s(dataPtr, size, srcAddr, size);
1467 2 : if (ret != EOK) {
1468 1 : const std::string retVal = std::to_string(ret);
1469 1 : std::stringstream ss;
1470 1 : ss << std::hex << "src=0x" << reinterpret_cast<uintptr_t>(srcAddr)
1471 1 : << ", dataPtr=0x" << reinterpret_cast<uintptr_t>(dataPtr)
1472 1 : << std::dec << ", size=" << size << ", count=" << size << ".";
1473 1 : const std::string extendInfo = ss.str();
1474 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1475 1 : acl::AclErrorLogManager::ReportInputError(acl::STANDARD_FUNC_FAILED_MSG,
1476 2 : std::vector<const char *>({"func1", "func2", "ret_code", "reason", "extend_info"}),
1477 2 : std::vector<const char *>({funcName.c_str(), "memcpy_s", retVal.c_str(),
1478 2 : strerror(ret), extendInfo.c_str()}));
1479 1 : ACL_LOG_ERROR("call memcpy_s failed, result = %d, size = %zu, bufSize = %lu, offset = %zu",
1480 : ret, size, bufSize, offset);
1481 1 : return ACL_ERROR_FAILURE;
1482 1 : }
1483 1 : return ACL_SUCCESS;
1484 3 : }
1485 :
1486 3 : aclError aclrtSetBufUserDataImpl(aclrtMbuf buf, const void *dataPtr, size_t size, size_t offset)
1487 : {
1488 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtSetBufUserData);
1489 : // The current default private data area size is 96B, if offset+size exceeds 96, an error is reported
1490 3 : if (size + offset > MEM_SIZE_MAX) {
1491 1 : ACL_LOG_ERROR("%s failed because the sum of size and offset is greater than %u, size=%zu, offset=%zu.", __func__,
1492 : MEM_SIZE_MAX, size, offset);
1493 1 : const std::string sizeVal = std::to_string(size);
1494 : std::string errMsg = acl::AclErrorLogManager::FormatStr("the sum of size and offset is greater than %u, size=%zu, offset=%zu",
1495 1 : MEM_SIZE_MAX, size, offset);
1496 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1497 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1498 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
1499 2 : std::vector<const char *>({funcName.c_str(), sizeVal.c_str(), "size", errMsg.c_str()}));
1500 1 : return ACL_ERROR_INVALID_PARAM;
1501 1 : }
1502 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1503 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
1504 2 : uint64_t bufSize = 0U;
1505 2 : void *tmpDataPtr = nullptr;
1506 2 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetPrivInfo(buf, &tmpDataPtr, &bufSize), rtMbufGetPrivInfo);
1507 2 : ACL_CHECK_LESS_UINT(size + offset, static_cast<size_t>(bufSize));
1508 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(tmpDataPtr);
1509 2 : void *const destAddr = static_cast<uint8_t *>(tmpDataPtr) + offset;
1510 2 : const size_t destMax = static_cast<size_t>(bufSize) - offset;
1511 2 : const auto ret = memcpy_s(destAddr, destMax, dataPtr, size);
1512 2 : if (ret != EOK) {
1513 1 : const std::string retVal = std::to_string(ret);
1514 1 : std::stringstream ss;
1515 1 : ss << std::hex << "dataPtr=0x" << reinterpret_cast<uintptr_t>(dataPtr)
1516 1 : << ", dest=0x" << reinterpret_cast<uintptr_t>(destAddr)
1517 1 : << std::dec << ", dest_max=" << destMax << ", size=" << size << ".";
1518 1 : const std::string extendInfo = ss.str();
1519 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
1520 1 : acl::AclErrorLogManager::ReportInputError(acl::STANDARD_FUNC_FAILED_MSG,
1521 2 : std::vector<const char *>({"func1", "func2", "ret_code", "reason", "extend_info"}),
1522 2 : std::vector<const char *>({funcName.c_str(), "memcpy_s", retVal.c_str(),
1523 2 : strerror(ret), extendInfo.c_str()}));
1524 1 : ACL_LOG_ERROR("call memcpy_s failed, result = %d, size = %zu, bufSize = %lu, offset = %zu",
1525 : ret, size, bufSize, offset);
1526 1 : return ACL_ERROR_FAILURE;
1527 1 : }
1528 1 : return ACL_SUCCESS;
1529 3 : }
1530 :
1531 4 : aclError aclrtGetBufDataImpl(const aclrtMbuf buf, void **dataPtr, size_t *size)
1532 : {
1533 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufData);
1534 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1535 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
1536 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(size);
1537 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetBuffAddr(buf, dataPtr), rtMbufGetBuffAddr);
1538 1 : uint64_t bufSize = 0U;
1539 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetBuffSize(buf, &bufSize), rtMbufGetBuffSize);
1540 1 : *size = static_cast<size_t>(bufSize);
1541 1 : return ACL_SUCCESS;
1542 4 : }
1543 :
1544 1 : aclError aclrtGetBufDataLenImpl(aclrtMbuf buf, size_t *len)
1545 : {
1546 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufDataLen);
1547 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1548 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(len);
1549 1 : uint64_t dataLen = 0U;
1550 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetDataLen(buf, &dataLen), rtMbufGetDataLen);
1551 1 : *len = static_cast<size_t>(dataLen);
1552 1 : return ACL_SUCCESS;
1553 1 : }
1554 :
1555 1 : aclError aclrtSetBufDataLenImpl(aclrtMbuf buf, size_t len)
1556 : {
1557 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtSetBufDataLen);
1558 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1559 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufSetDataLen(buf, len), rtMbufSetDataLen);
1560 1 : return ACL_SUCCESS;
1561 1 : }
1562 :
1563 8 : aclError aclrtFreeBufImpl(aclrtMbuf buf)
1564 : {
1565 8 : ACL_PROFILING_REG(acl::AclProfType::AclrtFreeBuf);
1566 8 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1567 8 : ACL_REQUIRES_RTS_OK(rtMbufFree(buf));
1568 8 : buf = nullptr;
1569 8 : return ACL_SUCCESS;
1570 8 : }
1571 :
1572 4 : aclError aclrtAllocBufImpl(aclrtMbuf *buf, size_t size)
1573 : {
1574 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtAllocBuf);
1575 4 : ACL_LOG_INFO("start to execute aclrtAllocBuf, size is [%zu]", size);
1576 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
1577 : // size must be greater than zero
1578 6 : ACL_REQUIRES_POSITIVE_REPORT(size);
1579 2 : ACL_REQUIRES_RTS_OK(rtMbufAlloc(buf, size));
1580 1 : return ACL_SUCCESS;
1581 4 : }
1582 :
1583 3 : aclError aclrtCmoAsyncWithBarrierImpl(void *src, size_t size, aclrtCmoType cmoType, uint32_t barrierId,
1584 : aclrtStream stream)
1585 : {
1586 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsyncWithBarrier);
1587 3 : ACL_LOG_INFO("start to execute aclrtCmoAsyncWithBarrier, size is [%zu], cmoType is [%u], barrierId is [%u]",
1588 : size, static_cast<uint32_t>(cmoType), barrierId);
1589 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
1590 2 : const rtCmoOpCode rtCmoType = static_cast<rtCmoOpCode>(static_cast<uint32_t>(cmoType) +
1591 : (static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH)));
1592 2 : ACL_REQUIRES_RTS_OK(rtsCmoAsyncWithBarrier(src, size, rtCmoType, barrierId, static_cast<rtStream_t>(stream)));
1593 1 : return ACL_SUCCESS;
1594 3 : }
1595 :
1596 22 : static aclError ValidateMemcpyBatchParams(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes,
1597 : size_t numBatches, aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs)
1598 : {
1599 22 : constexpr const char_t *funcDesc = "Checking the batch memory copy parameter validity";
1600 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(dsts, funcDesc);
1601 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(destMaxs, funcDesc);
1602 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(srcs, funcDesc);
1603 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(sizes, funcDesc);
1604 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(attrs, funcDesc);
1605 22 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(attrsIndexes, funcDesc);
1606 :
1607 126 : for (size_t i = 0UL; i < numBatches; i++) {
1608 108 : if (destMaxs[i] < sizes[i]) {
1609 4 : ACL_LOG_ERROR("element of destMaxs must be equal to or greater than corresponding element of sizes");
1610 4 : const std::string destMaxsVal = std::to_string(destMaxs[i]);
1611 : std::string errMsg = acl::AclErrorLogManager::FormatStr("The memory copy size %zu at index %zu exceeds the size %zu of the destination buffer",
1612 4 : sizes[i], i, destMaxs[i]);
1613 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
1614 8 : std::vector<const char *>({"func", "value", "param", "reason"}),
1615 4 : std::vector<const char *>({funcDesc, destMaxsVal.c_str(), "destMaxs",
1616 8 : errMsg.c_str()}));
1617 4 : return ACL_ERROR_INVALID_PARAM;
1618 4 : }
1619 : }
1620 :
1621 18 : constexpr uint32_t rsvMaxSize = sizeof(aclrtMemcpyBatchAttr::rsv) / sizeof(uint8_t);
1622 32 : for (size_t idx = 0UL; idx < numAttrs; idx++) {
1623 242 : for (uint32_t i = 0U; i < rsvMaxSize; i++) {
1624 228 : if (attrs[idx].rsv[i] != 0U) {
1625 4 : ACL_LOG_ERROR("rsv field of attrs[%zu] must be zero", idx);
1626 4 : const std::string rsvVal = std::to_string(attrs[idx].rsv[i]);
1627 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
1628 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
1629 8 : std::vector<const char *>({funcDesc, rsvVal.c_str(), "attrs.rsv", "0"}));
1630 4 : return ACL_ERROR_INVALID_PARAM;
1631 4 : }
1632 : }
1633 : }
1634 :
1635 14 : return ACL_SUCCESS;
1636 : }
1637 :
1638 42 : static aclError MemcpyBatchImpl(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes, size_t numBatches,
1639 : aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs, size_t *failIndex,
1640 : aclrtStream stream, bool async, const char *apiName)
1641 : {
1642 : // 判断 sizes == nullptr, count == 0 报参数异常
1643 42 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(sizes, "Batch memory copy");
1644 58 : ACL_REQUIRES_POSITIVE_REPORT_WITH_FUNC_DESC(numBatches, "Batch memory copy");
1645 : // 如果所有批次的 size 都为 0,则不执行 memcpy,直接返回成功,不需要校验其他参数
1646 26 : if (IsAllZeroSizeBatch(sizes, numBatches)) {
1647 4 : if (failIndex != nullptr) {
1648 2 : *failIndex = SIZE_MAX;
1649 : }
1650 4 : ACL_LOG_INFO("successfully execute %s", apiName);
1651 4 : return ACL_SUCCESS;
1652 : }
1653 22 : const aclError ret = ValidateMemcpyBatchParams(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes,
1654 : numAttrs);
1655 22 : if (ret != ACL_SUCCESS) {
1656 8 : return ret;
1657 : }
1658 :
1659 14 : if (async) {
1660 7 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtsMemcpyBatchAsync(dsts, destMaxs, srcs, sizes, numBatches, reinterpret_cast<rtMemcpyBatchAttr*>(attrs),
1661 : attrsIndexes, numAttrs, failIndex, stream), rtsMemcpyBatchAsync);
1662 3 : ACL_LOG_INFO("successfully execute %s", apiName);
1663 : } else {
1664 7 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtsMemcpyBatch(dsts, srcs, sizes, numBatches, reinterpret_cast<rtMemcpyBatchAttr*>(attrs),
1665 : attrsIndexes, numAttrs, failIndex), rtsMemcpyBatch);
1666 3 : ACL_LOG_INFO("successfully execute %s", apiName);
1667 : }
1668 :
1669 6 : return ACL_SUCCESS;
1670 : }
1671 :
1672 4 : aclError aclrtIpcMemGetExportKeyImpl(void *devPtr, size_t size, char *key, size_t len, uint64_t flags)
1673 : {
1674 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemGetExportKey);
1675 4 : ACL_LOG_INFO("start to execute aclrtIpcMemGetExportKey, size is [%zu], len is [%zu], flags is [%lu]",
1676 : size, len, flags);
1677 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
1678 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
1679 2 : ACL_REQUIRES_RTS_OK(rtsIpcMemGetExportKey(devPtr, size, key, static_cast<uint32_t>(len), flags));
1680 1 : return ACL_SUCCESS;
1681 4 : }
1682 :
1683 3 : aclError aclrtIpcMemCloseImpl(const char *key)
1684 : {
1685 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemClose);
1686 3 : ACL_LOG_INFO("start to execute aclrtIpcMemClose");
1687 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
1688 2 : ACL_REQUIRES_RTS_OK(rtsIpcMemClose(key));
1689 1 : return ACL_SUCCESS;
1690 3 : }
1691 :
1692 4 : aclError aclrtIpcMemImportByKeyImpl(void **devPtr, const char *key, uint64_t flags)
1693 : {
1694 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemImportByKey);
1695 4 : ACL_LOG_INFO("start to execute aclrtIpcMemImportByKey, flags is [%lu]", flags);
1696 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
1697 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
1698 2 : ACL_REQUIRES_RTS_OK(rtsIpcMemImportByKey(devPtr, key, flags));
1699 1 : return ACL_SUCCESS;
1700 4 : }
1701 :
1702 16 : aclError aclrtMemcpyBatchImpl(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes, size_t numBatches,
1703 : aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs, size_t *failIndex)
1704 : {
1705 16 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatch);
1706 16 : ACL_LOG_INFO("start to execute aclrtMemcpyBatch");
1707 16 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(failIndex);
1708 9 : return MemcpyBatchImpl(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, failIndex,
1709 9 : nullptr, false, "aclrtMemcpyBatch");
1710 16 : }
1711 :
1712 12 : aclError aclrtMemcpyBatchV2Impl(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes, size_t numBatches,
1713 : aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs)
1714 : {
1715 12 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchV2);
1716 12 : ACL_LOG_INFO("start to execute aclrtMemcpyBatchV2");
1717 12 : return MemcpyBatchImpl(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, nullptr,
1718 24 : nullptr, false, "aclrtMemcpyBatchV2");
1719 12 : }
1720 :
1721 16 : aclError aclrtMemcpyBatchAsyncImpl(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes,
1722 : size_t numBatches, aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs, size_t *failIndex,
1723 : aclrtStream stream)
1724 : {
1725 16 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchAsync);
1726 16 : ACL_LOG_INFO("start to execute aclrtMemcpyBatchAsync");
1727 16 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(failIndex);
1728 9 : return MemcpyBatchImpl(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, failIndex,
1729 9 : stream, true, "aclrtMemcpyBatchAsync");
1730 16 : }
1731 :
1732 12 : aclError aclrtMemcpyBatchAsyncV2Impl(void **dsts, size_t *destMaxs, void **srcs, size_t *sizes,
1733 : size_t numBatches, aclrtMemcpyBatchAttr *attrs, size_t *attrsIndexes, size_t numAttrs, aclrtStream stream)
1734 : {
1735 12 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchAsyncV2);
1736 12 : ACL_LOG_INFO("start to execute aclrtMemcpyBatchAsyncV2");
1737 12 : return MemcpyBatchImpl(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, nullptr,
1738 24 : stream, true, "aclrtMemcpyBatchAsyncV2");
1739 12 : }
1740 :
1741 4 : aclError aclrtIpcMemSetImportPidImpl(const char *key, int32_t *pid, size_t num)
1742 : {
1743 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemSetImportPid);
1744 4 : ACL_LOG_INFO("start to execute aclrtIpcMemSetImportPid, num is [%zu]", num);
1745 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
1746 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
1747 2 : ACL_REQUIRES_RTS_OK(rtsIpcMemSetImportPid(key, pid, static_cast<int32_t>(num)));
1748 1 : return ACL_SUCCESS;
1749 4 : }
1750 :
1751 2 : aclError aclrtIpcMemSetAttrImpl(const char *key, aclrtIpcMemAttrType type, uint64_t attr)
1752 : {
1753 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemSetAttr);
1754 2 : ACL_LOG_INFO("start to execute aclrtIpcMemSetAttr, type is [%d], attr is [%lu]", type, attr);
1755 2 : ACL_REQUIRES_RTS_OK(rtIpcSetMemoryAttr(key, type, attr));
1756 1 : ACL_LOG_INFO("successfully execute aclrtIpcMemSetAttr");
1757 1 : return ACL_SUCCESS;
1758 2 : }
1759 :
1760 2 : aclError aclrtIpcMemImportPidInterServerImpl(const char *key, aclrtServerPid *serverPids, size_t num)
1761 : {
1762 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemImportPidInterServer);
1763 2 : ACL_LOG_INFO("start to execute aclrtIpcMemImportPidInterServer, num is [%zu]", num);
1764 2 : ACL_REQUIRES_RTS_OK(rtIpcMemImportPidInterServer(key, reinterpret_cast<const rtServerPid *>(serverPids), num));
1765 1 : ACL_LOG_INFO("successfully execute aclrtIpcMemImportPidInterServer");
1766 1 : return ACL_SUCCESS;
1767 2 : }
1768 :
1769 1 : aclError aclrtCheckMemTypeImpl(void** addrList, uint32_t size, uint32_t memType, uint32_t *checkResult, uint32_t reserve)
1770 : {
1771 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtCheckMemType);
1772 1 : ACL_LOG_INFO("start to execute AclrtCheckMemType, size is [%u], memType is [%u], reserve is [%u]", size, memType, reserve);
1773 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(addrList);
1774 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(checkResult);
1775 1 : ACL_REQUIRES_RTS_OK(rtsCheckMemType(addrList, size, memType, checkResult, reserve));
1776 1 : return ACL_SUCCESS;
1777 1 : }
1778 :
1779 3 : aclError aclrtDevicePeerAccessStatusImpl(int32_t deviceId, int32_t peerDeviceId, int32_t *status)
1780 : {
1781 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtDevicePeerAccessStatus);
1782 3 : ACL_LOG_INFO("start to execute aclrtDevicePeerAccessStatus");
1783 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(status);
1784 2 : ACL_REQUIRES_RTS_OK(rtsGetP2PStatus(
1785 : static_cast<uint32_t>(deviceId), static_cast<uint32_t>(peerDeviceId), reinterpret_cast<uint32_t *>(status)));
1786 1 : ACL_LOG_INFO("successfully execute aclrtDevicePeerAccessStatus");
1787 1 : return ACL_SUCCESS;
1788 3 : }
1789 :
1790 2 : aclError aclrtCmoGetDescSizeImpl(size_t *size)
1791 : {
1792 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtCmoGetDescSize);
1793 2 : ACL_LOG_DEBUG("start to execute aclrtCmoGetDescSize");
1794 2 : ACL_REQUIRES_RTS_OK(rtsGetCmoDescSize(size));
1795 1 : ACL_LOG_INFO("successfully execute aclrtCmoGetDescSize");
1796 1 : return ACL_SUCCESS;
1797 2 : }
1798 :
1799 2 : aclError aclrtCmoSetDescImpl(void *cmoDesc, void *src, size_t size)
1800 : {
1801 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtCmoSetDesc);
1802 2 : ACL_LOG_DEBUG("start to execute aclrtCmoSetDesc, memLen =%zu", size);
1803 2 : ACL_REQUIRES_RTS_OK(rtsSetCmoDesc(cmoDesc, src, size));
1804 1 : ACL_LOG_INFO("successfully execute aclrtCmoSetDesc");
1805 1 : return ACL_SUCCESS;
1806 2 : }
1807 :
1808 2 : static rtCmoOpCode ConvertCmoType(aclrtCmoType cmoType)
1809 : {
1810 2 : constexpr uint32_t offset =
1811 : static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH);
1812 2 : return static_cast<rtCmoOpCode>(static_cast<uint32_t>(cmoType) + offset);
1813 : }
1814 :
1815 2 : aclError aclrtCmoAsyncWithDescImpl(void *cmoDesc, aclrtCmoType cmoType, aclrtStream stream, const void *reserve)
1816 : {
1817 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsyncWithDesc);
1818 2 : ACL_LOG_DEBUG("start to execute aclrtCmoAsyncWithDesc");
1819 2 : const rtCmoOpCode rtCmoType = ConvertCmoType(cmoType);
1820 2 : ACL_REQUIRES_RTS_OK(rtsLaunchCmoAddrTask(cmoDesc, stream, rtCmoType, reserve));
1821 1 : ACL_LOG_INFO("successfully execute aclrtCmoAsyncWithDesc");
1822 1 : return ACL_SUCCESS;
1823 2 : }
1824 :
1825 4 : aclError aclrtMemSetAccessImpl(void *virPtr, size_t size, aclrtMemAccessDesc *desc, size_t count)
1826 : {
1827 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetAccess);
1828 4 : ACL_LOG_INFO("start to execute aclrtMemSetAccess");
1829 :
1830 4 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemSetAccess(virPtr, size, reinterpret_cast<rtMemAccessDesc*>(desc), count), rtMemSetAccess);
1831 2 : ACL_LOG_INFO("successfully execute aclrtMemSetAccess");
1832 2 : return ACL_SUCCESS;
1833 4 : }
1834 :
1835 3 : aclError aclrtMemRetainAllocationHandleImpl(void* virPtr, aclrtDrvMemHandle *handle)
1836 : {
1837 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemRetainAllocationHandle);
1838 3 : ACL_LOG_DEBUG("start to execute aclrtMemRetainAllocationHandle");
1839 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
1840 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1841 :
1842 2 : ACL_REQUIRES_RTS_OK(rtMemRetainAllocationHandle(virPtr, reinterpret_cast<rtDrvMemHandle*>(handle)));
1843 1 : return ACL_SUCCESS;
1844 3 : }
1845 :
1846 : //initialize the mapping table
1847 : static const MemAttrMapping mapping[] {
1848 : {HUGE_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_HUGE},
1849 : {NORMAL_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_NORMAL},
1850 : {HUGE1G_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_HUGE1G},
1851 : {NORMAL_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_DDR_MEM_P2P_NORMAL},
1852 : {NORMAL_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_NORMAL},
1853 : {HUGE_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_HUGE},
1854 : {HUGE1G_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_HUGE1G},
1855 : {HUGE_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_MEM_P2P_HUGE},
1856 : {HUGE1G_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_MEM_P2P_HUGE1G},
1857 : {NORMAL_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_NORMAL},
1858 : {HUGE_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_HUGE},
1859 : {HUGE1G_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_HUGE1G},
1860 : {NORMAL_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_NORMAL},
1861 : {HUGE_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_HUGE},
1862 : {HUGE1G_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_HUGE1G},
1863 : {NORMAL_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_NORMAL},
1864 : {HUGE_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_HUGE},
1865 : {HUGE1G_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_HUGE1G}
1866 : };
1867 :
1868 5 : aclError aclrtMemGetAllocationPropertiesFromHandleImpl(aclrtDrvMemHandle handle, aclrtPhysicalMemProp* prop)
1869 : {
1870 5 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAllocationPropertiesFromHandle);
1871 5 : ACL_LOG_DEBUG("start to execute AclrtMemGetAllocationPropertiesFromHandle");
1872 5 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
1873 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
1874 :
1875 4 : rtDrvMemProp_t rtProp = {};
1876 4 : ACL_REQUIRES_RTS_OK(rtMemGetAllocationPropertiesFromHandle(reinterpret_cast<rtDrvMemHandle>(handle), &rtProp));
1877 :
1878 3 : prop->handleType = ACL_MEM_HANDLE_TYPE_NONE;
1879 3 : prop->allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED;
1880 3 : if (rtProp.side == DRV_MEM_HOST_NUMA_SIDE) {
1881 : // convert drv side to acl locationtype
1882 0 : prop->location.type = ACL_MEM_LOCATION_TYPE_HOST_NUMA;
1883 : } else {
1884 3 : prop->location.type = static_cast<aclrtMemLocationType>(rtProp.side);
1885 : }
1886 3 : prop->location.id = rtProp.devid;
1887 3 : prop->reserve = rtProp.reserve;
1888 :
1889 : //host alloc
1890 3 : bool isHostAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) || (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
1891 :
1892 9 : const auto& it = std::find_if(std::begin(mapping), std::end(mapping),
1893 6 : [rtProp, isHostAlloc](const MemAttrMapping& entry) {
1894 9 : return (entry.pgType == rtProp.pg_type) &&
1895 9 : (entry.memType == rtProp.mem_type) &&
1896 9 : (entry.isHostAlloc == isHostAlloc);
1897 3 : });
1898 6 : if (it != std::end(mapping)) {
1899 3 : prop->memAttr = it->memAttr;
1900 : } else {
1901 0 : ACL_LOG_ERROR("memAttr not found for pg_type=%u, mem_type=%u, isHostAlloc=%u",
1902 : rtProp.pg_type, rtProp.mem_type, isHostAlloc);
1903 0 : return ACL_ERROR_INVALID_PARAM;
1904 : }
1905 3 : return ACL_SUCCESS;
1906 5 : }
1907 :
1908 5 : aclError aclrtReserveMemAddressNoUCMemoryImpl(void **virPtr, size_t size, size_t alignment, void *expectPtr, uint64_t flags)
1909 : {
1910 5 : ACL_PROFILING_REG(acl::AclProfType::AclrtReserveMemAddressNoUCMemory);
1911 5 : ACL_LOG_DEBUG("start to execute aclrtReserveMemAddressNoUCMemory, size = %zu", size);
1912 5 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
1913 :
1914 8 : ACL_REQUIRES_POSITIVE_REPORT(size);
1915 : // flags参数取1,为了早期接口兼容性保留
1916 7 : ACL_CHECK_INVALID_VALUE_WITH_EXPECT((flags == 0ULL) || (flags == 1ULL), flags, "0");
1917 :
1918 3 : flags = flags | FLAG_START_DYNAMIC_ALLOC_MEM; // bit 9置1
1919 3 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtReserveMemAddress(virPtr, size, alignment, expectPtr, flags), rtReserveMemAddress);
1920 1 : return ACL_SUCCESS;
1921 5 : }
1922 :
1923 3 : aclError aclrtMemGetAddressRangeImpl(void *ptr, void **pbase, size_t *psize)
1924 : {
1925 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAddressRange);
1926 3 : ACL_LOG_DEBUG("start to execute aclrtMemGetAddressRange");
1927 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
1928 2 : ACL_REQUIRES_RTS_OK(rtMemGetAddressRange(ptr, pbase, psize));
1929 1 : ACL_LOG_INFO("successfully execute aclrtMemGetAddressRange");
1930 1 : return ACL_SUCCESS;
1931 3 : }
1932 :
1933 6 : aclError aclrtMemP2PMapImpl(void *devPtr, size_t size, int32_t dstDevId, uint64_t flags)
1934 : {
1935 6 : ACL_PROFILING_REG(acl::AclProfType::aclrtMemP2PMap);
1936 6 : ACL_LOG_INFO("start to execute aclrtMemP2PMap");
1937 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
1938 8 : ACL_REQUIRES_POSITIVE_REPORT(size);
1939 7 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
1940 3 : uint32_t phyId = 0U;
1941 3 : ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(dstDevId), &phyId));
1942 2 : ACL_REQUIRES_RTS_OK(rtMemPrefetchToDevice(devPtr, size, phyId));
1943 1 : ACL_LOG_INFO("successfully execute aclrtMemP2PMap");
1944 1 : return ACL_SUCCESS;
1945 6 : }
1946 :
1947 3 : aclError aclrtMemPoolCreateImpl(aclrtMemPool *memPool, const aclrtMemPoolProps *poolProps)
1948 : {
1949 3 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolCreate);
1950 3 : ACL_LOG_INFO("start to execute aclrtMemPoolCreate.");
1951 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
1952 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(poolProps);
1953 :
1954 3 : ACL_CHECK_INVALID_VALUE_WITH_DESC(poolProps->allocType == aclrtMemAllocationType::ACL_MEM_ALLOCATION_TYPE_PINNED,
1955 : acl::GetMemAllocationTypeDesc(poolProps->allocType), "poolProps->allocType",
1956 : "ACL_MEM_ALLOCATION_TYPE_PINNED", ACL_ERROR_INVALID_PARAM);
1957 :
1958 6 : ACL_CHECK_INVALID_VALUE_WITH_DESC(poolProps->location.type == aclrtMemLocationType::ACL_MEM_LOCATION_TYPE_DEVICE,
1959 : acl::GetMemLocationTypeDesc(poolProps->location.type), "poolProps->location.type",
1960 : "ACL_MEM_LOCATION_TYPE_DEVICE", ACL_ERROR_INVALID_PARAM);
1961 :
1962 : rtMemPoolProps rtPoolProps;
1963 2 : rtPoolProps.side = ACL_MEM_LOCATION_TYPE_DEVICE;
1964 2 : rtPoolProps.devId = poolProps->location.id;
1965 2 : rtPoolProps.handleType = static_cast<rtDrvMemHandleType>(poolProps->handleType);
1966 2 : rtPoolProps.maxSize = poolProps->maxSize;
1967 2 : rtPoolProps.reserve = 0;
1968 :
1969 2 : uint8_t zeros[sizeof(poolProps->reserved)] = {0};
1970 2 : ACL_CHECK_INVALID_PARAM_NO_VALUE(memcmp(poolProps->reserved, zeros, sizeof(poolProps->reserved)) == 0,
1971 : "poolProps->reserved", "poolProps->reserved is a reserved parameter and must be nullptr");
1972 :
1973 1 : ACL_REQUIRES_RTS_OK(rtMemPoolCreate(reinterpret_cast<rtMemPool_t*>(memPool), &rtPoolProps));
1974 1 : return ACL_SUCCESS;
1975 3 : }
1976 :
1977 1 : aclError aclrtMemPoolDestroyImpl(const aclrtMemPool memPool)
1978 : {
1979 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolDestroy);
1980 1 : ACL_LOG_INFO("start to execute aclrtMemPoolDestroy.");
1981 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
1982 :
1983 1 : ACL_REQUIRES_RTS_OK(rtMemPoolDestroy(static_cast<rtMemPool_t>(memPool)));
1984 1 : return ACL_SUCCESS;
1985 1 : }
1986 :
1987 1 : aclError aclrtMemPoolSetAttrImpl(aclrtMemPool memPool, aclrtMemPoolAttr attr, void *value)
1988 : {
1989 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolSetAttr);
1990 1 : ACL_LOG_INFO("start to execute aclrtMemPoolSetAttr.");
1991 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
1992 :
1993 1 : ACL_REQUIRES_RTS_OK(rtMemPoolSetAttr(static_cast<rtMemPool_t>(memPool), static_cast<rtMemPoolAttr>(attr), value));
1994 1 : return ACL_SUCCESS;
1995 1 : }
1996 :
1997 1 : aclError aclrtMemPoolGetAttrImpl(aclrtMemPool memPool, aclrtMemPoolAttr attr, void *value)
1998 : {
1999 1 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolGetAttr);
2000 1 : ACL_LOG_INFO("start to execute aclrtMemPoolGetAttr.");
2001 1 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
2002 :
2003 1 : ACL_REQUIRES_RTS_OK(rtMemPoolGetAttr(static_cast<rtMemPool_t>(memPool), static_cast<rtMemPoolAttr>(attr), value));
2004 1 : return ACL_SUCCESS;
2005 1 : }
2006 :
2007 :
2008 2 : aclError aclrtMemPoolMallocAsyncImpl(void ** ptr, size_t size, aclrtMemPool memPool, aclrtStream stream)
2009 : {
2010 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolMallocAsync);
2011 2 : ACL_LOG_INFO("Start to execute aclrtMemPoolMallocAsync.");
2012 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
2013 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
2014 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);
2015 2 : if (size == 0) {
2016 1 : return ACL_SUCCESS;
2017 : }
2018 :
2019 1 : ACL_REQUIRES_RTS_OK(rtMemPoolMallocAsync(ptr, size, memPool, stream));
2020 :
2021 1 : return ACL_SUCCESS;
2022 2 : }
2023 :
2024 2 : aclError aclrtMemPoolFreeAsyncImpl(void * ptr, aclrtStream stream)
2025 : {
2026 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolFreeAsync);
2027 2 : ACL_LOG_INFO("Start to execute aclrtMemPoolFreeAsync.");
2028 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
2029 :
2030 1 : ACL_REQUIRES_RTS_OK(rtMemPoolFreeAsync(ptr, stream));
2031 1 : return ACL_SUCCESS;
2032 2 : }
2033 :
2034 2 : aclError aclrtMemPoolTrimToImpl(aclrtMemPool memPool, size_t minBytesToKeep)
2035 : {
2036 2 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolTrimTo);
2037 2 : ACL_LOG_INFO("Start to execute aclrtMemPoolTrimTo.");
2038 2 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
2039 :
2040 2 : ACL_REQUIRES_RTS_OK(rtMemPoolTrimTo(static_cast<rtMemPool_t>(memPool), minBytesToKeep));
2041 1 : return ACL_SUCCESS;
2042 2 : }
2043 :
2044 9 : static rtMemManagedLocationType ConvertMemManagedLocationType(aclrtMemManagedLocationType const locationType)
2045 : {
2046 9 : switch (locationType) {
2047 2 : case ACL_MEM_LOCATIONTYPE_DEVICE:
2048 2 : return rtMemLocationTypeDevice;
2049 3 : case ACL_MEM_LOCATIONTYPE_HOST:
2050 3 : return rtMemLocationTypeHost;
2051 2 : case ACL_MEM_LOCATIONTYPE_HOST_NUMA:
2052 2 : return rtMemLocationTypeHostNuma;
2053 2 : case ACL_MEM_LOCATIONTYPE_HOST_NUMA_CURRENT:
2054 2 : return rtMemLocationTypeHostNumaCurrent;
2055 0 : default:
2056 0 : return rtMemLocationTypeInvalid;
2057 : }
2058 : }
2059 :
2060 4 : aclError aclrtMemManagedPrefetchAsyncImpl(const void* ptr, size_t size, aclrtMemManagedLocation location, uint32_t flags,
2061 : aclrtStream stream)
2062 : {
2063 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedPrefetchAsync);
2064 4 : ACL_LOG_DEBUG("start to execute aclrtMemManagedPrefetchAsync");
2065 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
2066 6 : ACL_REQUIRES_POSITIVE_REPORT(size);
2067 5 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
2068 1 : rtMemManagedLocation uvmLocation = { ConvertMemManagedLocationType(location.type), location.id };
2069 1 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemManagedPrefetchAsync(ptr, size, uvmLocation, flags, static_cast<rtStream_t>(stream)),
2070 : rtMemManagedPrefetchAsync);
2071 1 : return ACL_SUCCESS;
2072 4 : }
2073 :
2074 9 : aclError aclrtMemManagedPrefetchBatchAsyncImpl(const void** ptrs, size_t* sizes, size_t count,
2075 : aclrtMemManagedLocation* prefetchLocs, size_t* prefetchLocIdxs, size_t numPrefetchLocs, uint64_t flags,
2076 : aclrtStream stream)
2077 : {
2078 9 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedPrefetchBatchAsync);
2079 9 : ACL_LOG_DEBUG("start to execute aclrtMemManagedPrefetchBatchAsync");
2080 9 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptrs);
2081 8 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(sizes);
2082 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prefetchLocs);
2083 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prefetchLocIdxs);
2084 :
2085 9 : ACL_REQUIRES_POSITIVE_REPORT(count);
2086 8 : ACL_REQUIRES_POSITIVE_REPORT(numPrefetchLocs);
2087 7 : ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
2088 3 : if (count < numPrefetchLocs) {
2089 1 : ACL_LOG_ERROR("[Check][PARAM]count must be greater than or equal to numPrefetchLocs");
2090 1 : const std::string countVal = std::to_string(count);
2091 1 : std::string errMsg = acl::AclErrorLogManager::FormatStr("must be greater than or equal to numPrefetchLocs %zu", numPrefetchLocs);
2092 1 : std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
2093 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
2094 2 : std::vector<const char *>({"func", "value", "param", "reason"}),
2095 2 : std::vector<const char *>({funcName.c_str(), countVal.c_str(), "count", errMsg.c_str()}));
2096 1 : return ACL_ERROR_INVALID_PARAM;
2097 1 : }
2098 :
2099 2 : rtMemManagedLocation* uvmPrefetchLocs = new(std::nothrow) rtMemManagedLocation[numPrefetchLocs];
2100 2 : ACL_CHECK_MALLOC_RESULT_REPORT_RET(uvmPrefetchLocs, sizeof(rtMemManagedLocation) * numPrefetchLocs, "new", ACL_ERROR_BAD_ALLOC);
2101 :
2102 10 : for (size_t numPrefetchIdx = 0; numPrefetchIdx < numPrefetchLocs; numPrefetchIdx++) {
2103 8 : uvmPrefetchLocs[numPrefetchIdx].id = prefetchLocs[numPrefetchIdx].id;
2104 8 : uvmPrefetchLocs[numPrefetchIdx].type = ConvertMemManagedLocationType(prefetchLocs[numPrefetchIdx].type);
2105 : }
2106 :
2107 2 : const rtError_t rtErr = rtMemManagedPrefetchBatchAsync(ptrs, sizes, count, uvmPrefetchLocs, prefetchLocIdxs, numPrefetchLocs, flags, static_cast<rtStream_t>(stream));
2108 2 : ACL_DELETE_ARRAY_AND_SET_NULL(uvmPrefetchLocs);
2109 2 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtErr, rtMemManagedPrefetchBatchAsync);
2110 2 : return ACL_SUCCESS;
2111 9 : }
2112 :
2113 4 : aclError aclrtGetSymbolAddressImpl(const void *symbol, void **devPtr)
2114 : {
2115 4 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetSymbolAddress);
2116 4 : ACL_LOG_DEBUG("start to execute aclrtGetSymbolAddress.");
2117 :
2118 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(symbol);
2119 3 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
2120 :
2121 2 : size_t size = 0UL;
2122 2 : ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, devPtr, &size));
2123 1 : return ACL_SUCCESS;
2124 4 : }
2125 :
2126 0 : aclError aclrtGetSymbolSizeImpl(const void *symbol, size_t *size)
2127 : {
2128 0 : ACL_PROFILING_REG(acl::AclProfType::AclrtGetSymbolSize);
2129 0 : ACL_LOG_DEBUG("start to execute aclrtGetSymbolSize.");
2130 :
2131 0 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(symbol);
2132 0 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(size);
2133 :
2134 0 : void *devPtr = nullptr;
2135 0 : ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, &devPtr, size));
2136 0 : return ACL_SUCCESS;
2137 0 : }
2138 :
2139 22 : static aclError GetSymbolInfo(const void *symbol, size_t count, size_t offset,
2140 : void **symbolAddr, size_t *symbolSize)
2141 : {
2142 22 : *symbolAddr = nullptr;
2143 22 : *symbolSize = 0UL;
2144 22 : ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, symbolAddr, symbolSize));
2145 :
2146 18 : size_t totalSize = 0UL;
2147 18 : ACL_CHECK_ASSIGN_SIZET_ADD(offset, count, totalSize);
2148 14 : if (totalSize > *symbolSize) {
2149 6 : ACL_LOG_ERROR("[Check][Offset]offset[%zu] + count[%zu] must be <= symbolSize[%zu].",
2150 : offset, count, *symbolSize);
2151 6 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_MSG,
2152 12 : std::vector<const char *>({"param", "value", "reason"}),
2153 12 : std::vector<const char *>({"offset+count", std::to_string(totalSize).c_str(),
2154 18 : "must be <= symbolSize"}));
2155 6 : return ACL_ERROR_INVALID_PARAM;
2156 : }
2157 :
2158 8 : return ACL_SUCCESS;
2159 : }
2160 :
2161 19 : static aclError CheckMemcpyFromSymbol(void *dst, const void *symbol, size_t count, size_t dstMax,
2162 : aclrtMemcpyKind kind)
2163 : {
2164 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(symbol, "Checking the synchronous memory copy parameter");
2165 19 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(dst, "Checking the synchronous memory copy parameter");
2166 :
2167 17 : if (count > dstMax) {
2168 2 : ACL_LOG_ERROR("[Check][Count]count[%zu] must not be greater than dstMax[%zu].", count, dstMax);
2169 2 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_MSG,
2170 4 : std::vector<const char *>({"param", "value", "reason"}),
2171 4 : std::vector<const char *>({"count", std::to_string(count).c_str(), "must not be greater than dstMax"}));
2172 2 : return ACL_ERROR_INVALID_PARAM;
2173 : }
2174 :
2175 15 : if ((kind != ACL_MEMCPY_DEVICE_TO_HOST) && (kind != ACL_MEMCPY_DEFAULT)) {
2176 4 : ACL_LOG_ERROR("[Check][Kind]kind[%s] only support ACL_MEMCPY_DEVICE_TO_HOST or ACL_MEMCPY_DEFAULT",
2177 : acl::GetMemcpyKindDesc(kind));
2178 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
2179 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
2180 4 : std::vector<const char *>({"Checking the synchronous memory copy parameter", acl::GetMemcpyKindDesc(kind), "kind",
2181 8 : "ACL_MEMCPY_DEVICE_TO_HOST or ACL_MEMCPY_DEFAULT"}));
2182 4 : return ACL_ERROR_INVALID_PARAM;
2183 : }
2184 :
2185 11 : return ACL_SUCCESS;
2186 : }
2187 :
2188 10 : aclError aclrtMemcpyFromSymbolImpl(void *dst, size_t dstMax, const void *symbol,
2189 : size_t count, size_t offset, aclrtMemcpyKind kind)
2190 : {
2191 10 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyFromSymbol);
2192 10 : ACL_LOG_DEBUG("start to execute aclrtMemcpyFromSymbol, count = %zu, offset = %zu.", count, offset);
2193 10 : if(count == 0) {
2194 1 : ACL_LOG_INFO("count is 0, no need to execute mem copy from symbol, just return success.");
2195 1 : return ACL_SUCCESS;
2196 : }
2197 9 : aclError ret = CheckMemcpyFromSymbol(dst, symbol, count, dstMax, kind);
2198 9 : if (ret != ACL_SUCCESS) {
2199 4 : return ret;
2200 : }
2201 :
2202 5 : void *symbolAddr = nullptr;
2203 5 : size_t symbolSize = 0UL;
2204 5 : ret = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
2205 5 : if (ret != ACL_SUCCESS) {
2206 3 : return ret;
2207 : }
2208 :
2209 2 : void *srcAddr = static_cast<void *>(static_cast<uint8_t *>(symbolAddr) + offset);
2210 2 : ACL_REQUIRES_RTS_OK(rtMemcpy(dst, dstMax, srcAddr, count, RT_MEMCPY_DEVICE_TO_HOST));
2211 2 : return ACL_SUCCESS;
2212 10 : }
2213 :
2214 11 : aclError aclrtMemcpyFromSymbolAsyncImpl(void *dst, size_t dstMax, const void *symbol,
2215 : size_t count, size_t offset, aclrtMemcpyKind kind,
2216 : aclrtStream stream)
2217 : {
2218 11 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyFromSymbolAsync);
2219 11 : ACL_LOG_DEBUG("start to execute aclrtMemcpyFromSymbolAsync, count = %zu, offset = %zu.", count, offset);
2220 11 : if(count == 0) {
2221 1 : ACL_LOG_INFO("count is 0, no need to execute mem copy from symbol async, just return success.");
2222 1 : return ACL_SUCCESS;
2223 : }
2224 :
2225 10 : aclError aclErr = CheckMemcpyFromSymbol(dst, symbol, count, dstMax, kind);
2226 10 : if (aclErr != ACL_SUCCESS) {
2227 4 : return aclErr;
2228 : }
2229 :
2230 6 : void *symbolAddr = nullptr;
2231 6 : size_t symbolSize = 0UL;
2232 6 : aclErr = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
2233 6 : if (aclErr != ACL_SUCCESS) {
2234 4 : return aclErr;
2235 : }
2236 :
2237 2 : void *srcAddr = static_cast<void *>(static_cast<uint8_t *>(symbolAddr) + offset);
2238 2 : ACL_REQUIRES_RTS_OK(rtMemcpyAsync(dst, dstMax, srcAddr, count, RT_MEMCPY_DEVICE_TO_HOST, stream));
2239 2 : return ACL_SUCCESS;
2240 11 : }
2241 :
2242 17 : static aclError CheckMemcpyToSymbol(const void *symbol, const void *src,
2243 : aclrtMemcpyKind kind)
2244 : {
2245 17 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(symbol, "Checking the synchronous memory copy parameter");
2246 17 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(src, "Checking the synchronous memory copy parameter");
2247 :
2248 15 : if ((kind != ACL_MEMCPY_HOST_TO_DEVICE) && (kind != ACL_MEMCPY_DEFAULT)) {
2249 4 : ACL_LOG_ERROR("[Check][Kind]kind[%s] only support ACL_MEMCPY_HOST_TO_DEVICE or ACL_MEMCPY_DEFAULT",
2250 : acl::GetMemcpyKindDesc(kind));
2251 4 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_VALUE_MSG,
2252 8 : std::vector<const char *>({"func", "value", "param", "expect"}),
2253 4 : std::vector<const char *>({"Checking the synchronous memory copy parameter", acl::GetMemcpyKindDesc(kind), "kind",
2254 8 : "ACL_MEMCPY_HOST_TO_DEVICE or ACL_MEMCPY_DEFAULT"}));
2255 4 : return ACL_ERROR_INVALID_PARAM;
2256 : }
2257 11 : return ACL_SUCCESS;
2258 : }
2259 :
2260 9 : aclError aclrtMemcpyToSymbolImpl(const void *symbol, const void *src, size_t count,
2261 : size_t offset, aclrtMemcpyKind kind)
2262 : {
2263 9 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyToSymbol);
2264 9 : ACL_LOG_DEBUG("start to execute aclrtMemcpyToSymbol, count = %zu, offset = %zu.", count, offset);
2265 9 : if(count == 0) {
2266 1 : ACL_LOG_INFO("count is 0, no need to execute mem copy to symbol, just return success.");
2267 1 : return ACL_SUCCESS;
2268 : }
2269 :
2270 8 : aclError ret = CheckMemcpyToSymbol(symbol, src, kind);
2271 8 : if (ret != ACL_SUCCESS) {
2272 3 : return ret;
2273 : }
2274 :
2275 5 : void *symbolAddr = nullptr;
2276 5 : size_t symbolSize = 0UL;
2277 5 : ret = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
2278 5 : if (ret != ACL_SUCCESS) {
2279 3 : return ret;
2280 : }
2281 :
2282 2 : void *dstAddr = static_cast<void *>(static_cast<uint8_t *>(symbolAddr) + offset);
2283 2 : ACL_REQUIRES_RTS_OK(rtMemcpy(dstAddr, symbolSize - offset, src, count, RT_MEMCPY_HOST_TO_DEVICE));
2284 2 : return ACL_SUCCESS;
2285 9 : }
2286 :
2287 10 : aclError aclrtMemcpyToSymbolAsyncImpl(const void *symbol, const void *src, size_t count,
2288 : size_t offset, aclrtMemcpyKind kind, aclrtStream stream)
2289 : {
2290 10 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyToSymbolAsync);
2291 10 : ACL_LOG_DEBUG("start to execute aclrtMemcpyToSymbolAsync, count = %zu, offset = %zu.", count, offset);
2292 10 : if(count == 0) {
2293 1 : ACL_LOG_INFO("count is 0, no need to execute mem copy to symbol async, just return success.");
2294 1 : return ACL_SUCCESS;
2295 : }
2296 :
2297 9 : aclError aclErr = CheckMemcpyToSymbol(symbol, src, kind);
2298 9 : if (aclErr != ACL_SUCCESS) {
2299 3 : return aclErr;
2300 : }
2301 :
2302 6 : void *symbolAddr = nullptr;
2303 6 : size_t symbolSize = 0UL;
2304 6 : aclErr = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
2305 6 : if (aclErr != ACL_SUCCESS) {
2306 4 : return aclErr;
2307 : }
2308 :
2309 2 : void *dstAddr = static_cast<void *>(static_cast<uint8_t *>(symbolAddr) + offset);
2310 2 : ACL_REQUIRES_RTS_OK(rtMemcpyAsync(dstAddr, symbolSize - offset, src, count, RT_MEMCPY_HOST_TO_DEVICE, stream));
2311 2 : return ACL_SUCCESS;
2312 10 : }
2313 :
2314 7 : aclError aclrtMemMapSelectedLinkImpl(void *virPtrDst, size_t size, void *virPtrSrc, uint32_t linkIdx)
2315 : {
2316 7 : ACL_PROFILING_REG(acl::AclProfType::AclrtMemMapSelectedLink);
2317 7 : ACL_LOG_INFO("start to execute aclrtMemMapSelectedLink.");
2318 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtrDst);
2319 6 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtrSrc);
2320 5 : if (size == 0UL) {
2321 1 : ACL_LOG_ERROR("size is [%zu], size must be greater than zero", size);
2322 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_MSG,
2323 2 : std::vector<const char *>({"param", "value", "reason"}),
2324 2 : std::vector<const char *>({"size", std::to_string(size).c_str(), "size must be greater than zero"}));
2325 1 : return ACL_ERROR_INVALID_PARAM;
2326 : }
2327 4 : if (linkIdx > ACL_RT_MEM_LINK_IDX_1) {
2328 1 : ACL_LOG_ERROR("linkIdx is [%u], linkIdx in aclrtMemMapSelectedLink must be 0 or 1", linkIdx);
2329 1 : acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_MSG,
2330 2 : std::vector<const char *>({"param", "value", "reason"}),
2331 2 : std::vector<const char *>({"linkIdx", std::to_string(linkIdx).c_str(), "linkIdx in aclrtMemMapSelectedLink must be 0 or 1"}));
2332 1 : return ACL_ERROR_INVALID_PARAM;
2333 : }
2334 :
2335 3 : ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMemMapSelectedLink(virPtrDst, size, virPtrSrc, linkIdx), rtMemMapSelectedLink);
2336 1 : ACL_LOG_INFO("successfully execute aclrtMemMapSelectedLink");
2337 1 : return ACL_SUCCESS;
2338 7 : }
2339 : #ifdef __cplusplus
2340 : }
2341 : #endif // __cplusplus
|