LCOV - code coverage report
Current view: top level - acl/aclrt_impl - memory.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.1 % 1639 1608
Test Date: 2026-07-28 10:53:01 Functions: 99.3 % 142 141

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

Generated by: LCOV version 2.0-1