Line data Source code
1 : /** 2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd. 3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 4 : * CANN Open Software License Agreement Version 2.0 (the "License"). 5 : * Please refer to the License for details. You may not use this file except in compliance with the License. 6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 8 : * See LICENSE in the root of the software repository for the full text of the License. 9 : */ 10 : #include <stdlib.h> 11 : #include "acl/acl_base.h" 12 : #include "acl/acl_rt.h" 13 : #include "log_inner.h" 14 : #include "runtime/dev.h" 15 : #include "runtime/mem.h" 16 : #include "model_config_rt.h" 17 : 18 9 : static aclError GetAlignedSize(const size_t size, size_t *alignedSize) { 19 9 : const size_t DATA_MEMORY_ALIGN_SIZE = 32UL; 20 9 : if ((size + (DATA_MEMORY_ALIGN_SIZE * 2UL)) < size) { 21 1 : return ACL_ERROR_INVALID_PARAM; 22 : } 23 8 : *alignedSize = (size + (DATA_MEMORY_ALIGN_SIZE * 2UL) - 1UL) / DATA_MEMORY_ALIGN_SIZE * DATA_MEMORY_ALIGN_SIZE; 24 8 : return ACL_SUCCESS; 25 : } 26 : 27 11 : aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy) { 28 11 : size_t alignedSize = 0; 29 11 : if ((devPtr == NULL) || (size == 0UL) || (GetAlignedSize(size, &alignedSize) != ACL_SUCCESS)) { 30 3 : ACL_LOG_ERROR("%s", devPtr == NULL? "devPtr is NULL" : "size invalid"); 31 3 : return ACL_ERROR_INVALID_PARAM; 32 : } 33 8 : rtMemType_t type = RT_MEMORY_DEFAULT; 34 8 : aclError ret = GetMemTypeFromPolicy(policy, &type); 35 8 : if (ret != ACL_SUCCESS) { 36 1 : return ret; 37 : } 38 7 : return rtMalloc(devPtr, alignedSize, type, (uint16_t)(APP)); 39 : } 40 : 41 6 : aclError aclrtFree(void *devPtr) { 42 6 : if (devPtr == NULL) { 43 1 : ACL_LOG_ERROR("devPtr is NULL."); 44 1 : return ACL_ERROR_INVALID_PARAM; 45 : } 46 5 : return rtFree(devPtr); 47 : } 48 : 49 6 : static aclError MemcpyKindTranslate(const aclrtMemcpyKind kind, rtMemcpyKind_t *rtKind) { 50 6 : switch (kind) { 51 1 : case ACL_MEMCPY_HOST_TO_DEVICE: { 52 1 : *rtKind = RT_MEMCPY_HOST_TO_DEVICE; 53 1 : break; 54 : } 55 1 : case ACL_MEMCPY_DEVICE_TO_DEVICE: { 56 1 : *rtKind = RT_MEMCPY_DEVICE_TO_DEVICE; 57 1 : break; 58 : } 59 1 : case ACL_MEMCPY_DEVICE_TO_HOST: { 60 1 : *rtKind = RT_MEMCPY_DEVICE_TO_HOST; 61 1 : break; 62 : } 63 2 : case ACL_MEMCPY_HOST_TO_HOST: { 64 2 : *rtKind = RT_MEMCPY_HOST_TO_HOST; 65 2 : break; 66 : } 67 1 : default: { 68 1 : return ACL_ERROR_INVALID_PARAM; 69 : } 70 : } 71 5 : return ACL_SUCCESS; 72 : } 73 : 74 8 : aclError aclrtMemcpy(void *dst, size_t destMax, const void *src, size_t count, aclrtMemcpyKind kind) { 75 8 : if (dst == NULL || src == NULL) { 76 2 : ACL_LOG_ERROR("%s", dst == NULL? "dst is NULL": "src is NULL."); 77 2 : return ACL_ERROR_INVALID_PARAM; 78 : } 79 6 : rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED; 80 6 : const aclError ret = MemcpyKindTranslate(kind, &rtKind); 81 6 : if (ret != ACL_SUCCESS) { 82 1 : ACL_LOG_INNER_ERROR("invalid kind[%d]", (int32_t)(kind)); 83 1 : return ret; 84 : } 85 5 : return rtMemcpy(dst, destMax, src, count, rtKind); 86 : } 87 : 88 3 : aclError aclrtMemset(void *devPtr, size_t maxCount, int32_t value, size_t count) { 89 3 : if (devPtr == NULL) { 90 1 : ACL_LOG_ERROR("devPtr is NULL."); 91 1 : return ACL_ERROR_INVALID_PARAM; 92 : } 93 2 : return rtMemset(devPtr, maxCount, (uint32_t)(value), count); 94 : } 95 : 96 4 : aclError aclrtGetMemInfo(aclrtMemAttr attr, size_t *free, size_t *total) { 97 4 : if (free == NULL || total == NULL) { 98 2 : ACL_LOG_ERROR("%s", free == NULL? "free is NULL" : "total is NULL."); 99 2 : return ACL_ERROR_INVALID_PARAM; 100 : } 101 2 : return rtMemGetInfoEx((rtMemInfoType_t)(attr), free, total); 102 : }