LCOV - code coverage report
Current view: top level - acl/aclrt_impl - memory.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.0 % 1688 1638
Test Date: 2026-08-31 10:05:54 Functions: 99.3 % 144 143

            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 0
     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 0
     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 0
     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 0, 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 0, 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 = %s", destMax, count,
     712              :         acl::GetMemcpyKindDesc(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              :     // 乘法前检查 N * 4 是否溢出,避免回绕为 0 绕过 memSize 校验
     792            6 :     if (N > (SIZE_MAX / sizeof(uint32_t))) {
     793            0 :         ACL_LOG_ERROR("[Check][PARAM]N × 4 overflow, N=%zu, memSize=%zu", N, memSize);
     794            0 :         const std::string nVal = std::to_string(N);
     795              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     796            0 :             "N × 4 overflows, N (%zu) is too large, which does not meet the requirement", N);
     797            0 :         acl::AclErrorLogManager::ReportInputError(
     798            0 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     799            0 :             std::vector<const char*>({"aclrtMemsetD32", nVal.c_str(), "N", errMsg.c_str()}));
     800            0 :         return ACL_ERROR_INVALID_PARAM;
     801            0 :     }
     802            6 :     const size_t requiredBytes = N * sizeof(uint32_t);
     803            6 :     if (memSize < requiredBytes) {
     804            2 :         ACL_LOG_ERROR(
     805              :             "[Check][PARAM]N * 4 must be less than or equal to memSize, but N=%zu, memSize=%zu, requiredBytes=%zu", N,
     806              :             memSize, requiredBytes);
     807            2 :         const std::string nVal = std::to_string(N);
     808              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     809            2 :             "N × 4 (%zu) is greater than memSize (%zu), which does not meet the requirement", requiredBytes, memSize);
     810            2 :         acl::AclErrorLogManager::ReportInputError(
     811            4 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     812            4 :             std::vector<const char*>({"aclrtMemsetD32", nVal.c_str(), "N", errMsg.c_str()}));
     813            2 :         return ACL_ERROR_INVALID_PARAM;
     814            2 :     }
     815              : 
     816            4 :     bool isAclMem = false;
     817            4 :     const aclError ret = IsAclPinnedMemory(ptr, isAclMem);
     818            4 :     if (ret != ACL_SUCCESS) {
     819            0 :         ACL_LOG_INNER_ERROR("Failed to check memory type, ret=%d", ret);
     820            0 :         return ret;
     821              :     }
     822            4 :     if (!isAclMem) {
     823            1 :         ACL_LOG_INNER_ERROR("Only memory allocated by aclrtMalloc or aclrtMallocHost is supported.");
     824            1 :         return ACL_ERROR_INVALID_PARAM;
     825              :     }
     826              : 
     827            3 :     const rtError_t rtErr = rtMemsetD32(ptr, static_cast<uint64_t>(memSize), value, N);
     828            3 :     if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
     829            0 :         ACL_LOG_WARN("rtMemsetD32 does not support this feature, runtime result = %d", rtErr);
     830            3 :     } else if (rtErr != RT_ERROR_NONE) {
     831            1 :         return ACL_GET_ERRCODE_RTS(rtErr);
     832              :     }
     833              : 
     834            2 :     return ACL_SUCCESS;
     835           11 : }
     836              : 
     837            9 : aclError aclrtMemsetD32AsyncImpl(void* ptr, size_t memSize, uint32_t value, size_t N, aclrtStream stream)
     838              : {
     839            9 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemsetD32Async);
     840              : 
     841            9 :     ACL_LOG_DEBUG("start to execute aclrtMemsetD32Async, memSize = %zu, N = %zu, value = 0x%x", memSize, N, value);
     842              : 
     843            9 :     if (N == 0UL) {
     844            1 :         ACL_LOG_INFO("zero-size memsetD32 async, no memory set will be performed");
     845            1 :         return ACL_SUCCESS;
     846              :     }
     847            8 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
     848            7 :     ACL_REQUIRES_POSITIVE(N);
     849              : 
     850              :     // Check byte alignment
     851            7 :     if ((reinterpret_cast<uintptr_t>(ptr) & ALIGNMENT_4BYTE_MASK) != 0) {
     852            1 :         ACL_LOG_ERROR("Pointer ptr=%p is not 4-byte aligned", ptr);
     853            1 :         return ACL_ERROR_INVALID_PARAM;
     854              :     }
     855              : 
     856              :     // 乘法前检查 N * 4 是否溢出,避免回绕为 0 绕过 memSize 校验
     857            6 :     if (N > (SIZE_MAX / sizeof(uint32_t))) {
     858            0 :         ACL_LOG_ERROR("[Check][PARAM]N × 4 overflow, N=%zu, memSize=%zu", N, memSize);
     859            0 :         const std::string nVal = std::to_string(N);
     860              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     861            0 :             "N × 4 overflows, N (%zu) is too large, which does not meet the requirement", N);
     862            0 :         acl::AclErrorLogManager::ReportInputError(
     863            0 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     864            0 :             std::vector<const char*>({"aclrtMemsetD32Async", nVal.c_str(), "N", errMsg.c_str()}));
     865            0 :         return ACL_ERROR_INVALID_PARAM;
     866            0 :     }
     867            6 :     const size_t requiredBytes = N * sizeof(uint32_t);
     868            6 :     if (memSize < requiredBytes) {
     869            1 :         ACL_LOG_ERROR(
     870              :             "[Check][PARAM]N * 4 must be less than or equal to memSize, but N=%zu, memSize=%zu, requiredBytes=%zu", N,
     871              :             memSize, requiredBytes);
     872            1 :         const std::string nVal = std::to_string(N);
     873              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     874            1 :             "N × 4 (%zu) is greater than memSize (%zu), which does not meet the requirement", requiredBytes, memSize);
     875            1 :         acl::AclErrorLogManager::ReportInputError(
     876            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     877            2 :             std::vector<const char*>({"aclrtMemsetD32Async", nVal.c_str(), "N", errMsg.c_str()}));
     878            1 :         return ACL_ERROR_INVALID_PARAM;
     879            1 :     }
     880              : 
     881            5 :     bool isAclMem = false;
     882            5 :     const aclError ret = IsAclPinnedMemory(ptr, isAclMem);
     883            5 :     if (ret != ACL_SUCCESS) {
     884            0 :         ACL_LOG_INNER_ERROR("Failed to check memory type, ret=%d", ret);
     885            0 :         return ret;
     886              :     }
     887            5 :     if (!isAclMem) {
     888            1 :         ACL_LOG_INNER_ERROR("Only memory allocated by aclrtMalloc or aclrtMallocHost is supported.");
     889            1 :         return ACL_ERROR_INVALID_PARAM;
     890              :     }
     891              : 
     892              :     const rtError_t rtErr =
     893            4 :         rtMemsetD32Async(ptr, static_cast<uint64_t>(memSize), value, N, static_cast<rtStream_t>(stream));
     894            4 :     if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
     895            0 :         ACL_LOG_WARN("rtMemsetD32Async does not support this feature, runtime result = %d", rtErr);
     896            4 :     } else if (rtErr != RT_ERROR_NONE) {
     897            1 :         return ACL_GET_ERRCODE_RTS(rtErr);
     898              :     }
     899              : 
     900            3 :     return ACL_SUCCESS;
     901            9 : }
     902              : 
     903            8 : aclError aclrtDeviceCanAccessPeerImpl(int32_t* canAccessPeer, int32_t deviceId, int32_t peerDeviceId)
     904              : {
     905            8 :     ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceCanAccessPeer);
     906            8 :     ACL_LOG_INFO("start to execute aclrtDeviceCanAccessPeer");
     907              : 
     908            8 :     if (deviceId == peerDeviceId) {
     909            2 :         ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     910            2 :         const std::string deviceIdVal = std::to_string(deviceId);
     911              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     912            2 :             "deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     913            2 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
     914            2 :         acl::AclErrorLogManager::ReportInputError(
     915            4 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     916            4 :             std::vector<const char*>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
     917            2 :         return ACL_ERROR_INVALID_PARAM;
     918            2 :     }
     919              : 
     920            6 :     uint32_t peerPhyId = 0U;
     921            6 :     ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
     922              : 
     923            4 :     ACL_REQUIRES_RTS_OK(rtDeviceCanAccessPeer(canAccessPeer, static_cast<uint32_t>(deviceId), peerPhyId));
     924              : 
     925            2 :     return ACL_SUCCESS;
     926            8 : }
     927              : 
     928           10 : aclError aclrtDeviceEnablePeerAccessImpl(int32_t peerDeviceId, uint32_t flags)
     929              : {
     930           10 :     ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceEnablePeerAccess);
     931           10 :     ACL_LOG_INFO("start to execute aclrtDeviceEnablePeerAccess");
     932           16 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0U, ACL_ERROR_FEATURE_UNSUPPORTED);
     933              : 
     934            8 :     int32_t deviceId = 0;
     935            8 :     ACL_REQUIRES_RTS_OK(rtGetDevice(&deviceId));
     936              : 
     937            6 :     if (deviceId == peerDeviceId) {
     938            1 :         ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     939            1 :         const std::string deviceIdVal = std::to_string(deviceId);
     940              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     941            1 :             "deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     942            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
     943            1 :         acl::AclErrorLogManager::ReportInputError(
     944            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     945            2 :             std::vector<const char*>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
     946            1 :         return ACL_ERROR_INVALID_PARAM;
     947            1 :     }
     948              : 
     949            5 :     uint32_t peerPhyId = 0U;
     950            5 :     ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
     951              : 
     952            3 :     ACL_REQUIRES_RTS_OK(rtEnableP2P(static_cast<uint32_t>(deviceId), peerPhyId, flags));
     953              : 
     954            1 :     return ACL_SUCCESS;
     955           10 : }
     956              : 
     957            9 : aclError aclrtDeviceDisablePeerAccessImpl(int32_t peerDeviceId)
     958              : {
     959            9 :     ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceDisablePeerAccess);
     960            9 :     ACL_LOG_INFO("start to execute aclrtDeviceDisablePeerAccess");
     961              : 
     962            9 :     int32_t deviceId = 0;
     963            9 :     ACL_REQUIRES_RTS_OK(rtGetDevice(&deviceId));
     964              : 
     965            7 :     if (deviceId == peerDeviceId) {
     966            2 :         ACL_LOG_ERROR("deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     967            2 :         const std::string deviceIdVal = std::to_string(deviceId);
     968              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
     969            2 :             "deviceId %d cannot be equal to peerDeviceId %d", deviceId, peerDeviceId);
     970            2 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
     971            2 :         acl::AclErrorLogManager::ReportInputError(
     972            4 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
     973            4 :             std::vector<const char*>({funcName.c_str(), deviceIdVal.c_str(), "deviceId", errMsg.c_str()}));
     974            2 :         return ACL_ERROR_INVALID_PARAM;
     975            2 :     }
     976              : 
     977            5 :     uint32_t peerPhyId = 0U;
     978            5 :     ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(peerDeviceId), &peerPhyId));
     979              : 
     980            3 :     ACL_REQUIRES_RTS_OK(rtDisableP2P(static_cast<uint32_t>(deviceId), peerPhyId));
     981              : 
     982            1 :     return ACL_SUCCESS;
     983            9 : }
     984              : 
     985            7 : aclError aclrtGetMemInfoImpl(aclrtMemAttr attr, size_t* free, size_t* total)
     986              : {
     987            7 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemInfo);
     988            7 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(free);
     989            7 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(total);
     990            7 :     ACL_LOG_DEBUG("start to execute aclrtGetMemInfo, memory attribute = %s", acl::GetMemAttrDesc(attr));
     991              : 
     992            7 :     ACL_REQUIRES_RTS_OK(rtMemGetInfoEx(static_cast<rtMemInfoType_t>(attr), free, total));
     993              : 
     994            5 :     ACL_LOG_DEBUG(
     995              :         "successfully execute aclrtGetMemInfo, memory attribute = %s, free memory = %zu bytes, "
     996              :         "total memory = %zu bytes",
     997              :         acl::GetMemAttrDesc(attr), *free, *total);
     998            5 :     return ACL_SUCCESS;
     999            7 : }
    1000              : 
    1001            2 : aclError aclrtGetMemUsageInfoImpl(int32_t deviceId, aclrtMemUsageInfo* memUsageInfo, size_t inputNum, size_t* outputNum)
    1002              : {
    1003            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemUsageInfo);
    1004            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memUsageInfo);
    1005            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(outputNum);
    1006            2 :     ACL_LOG_DEBUG(
    1007              :         "start to execute aclrtGetMemUsageInfo, deviceId = %d, inputNum = %zu", static_cast<int32_t>(deviceId),
    1008              :         inputNum);
    1009              : 
    1010            2 :     ACL_REQUIRES_RTS_OK(rtGetMemUsageInfo(
    1011              :         static_cast<uint32_t>(deviceId), reinterpret_cast<rtMemUsageInfo_t*>(memUsageInfo), inputNum, outputNum));
    1012              : 
    1013            1 :     ACL_LOG_DEBUG("successfully execute aclrtGetMemUsageInfo, deviceId = %d, inputNum = %zu", deviceId, inputNum);
    1014            1 :     return ACL_SUCCESS;
    1015            2 : }
    1016              : 
    1017           14 : aclError aclrtMemcpy2dImpl(
    1018              :     void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind)
    1019              : {
    1020           14 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2d);
    1021           14 :     ACL_LOG_DEBUG(
    1022              :         "start to execute aclrtMemcpy2d, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, kind = %s", dpitch,
    1023              :         spitch, width, height, acl::GetMemcpyKindDesc(kind));
    1024              : 
    1025           14 :     rtMemcpyKind_t rtKind = RT_MEMCPY_RESERVED;
    1026           14 :     const aclError ret = CheckMemcpy2dParam(dst, dpitch, src, spitch, width, height, kind, rtKind);
    1027           14 :     if (ret != ACL_SUCCESS) {
    1028            5 :         return ret;
    1029              :     }
    1030              : 
    1031            9 :     ACL_REQUIRES_RTS_OK(rtMemcpy2d(dst, dpitch, src, spitch, width, height, rtKind));
    1032              : 
    1033            8 :     ACL_LOG_DEBUG(
    1034              :         "Successfully execute aclrtMemcpy2d, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, "
    1035              :         "kind = %s",
    1036              :         dpitch, spitch, width, height, acl::GetMemcpyKindDesc(kind));
    1037            8 :     return ACL_SUCCESS;
    1038           14 : }
    1039              : 
    1040           15 : aclError aclrtMemcpy2dAsyncImpl(
    1041              :     void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind,
    1042              :     aclrtStream stream)
    1043              : {
    1044           15 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2dAsync);
    1045           15 :     ACL_LOG_DEBUG(
    1046              :         "start to execute aclrtMemcpy2dAsync, dpitch = %zu, spitch = %zu, width = %zu, height = %zu,"
    1047              :         " kind = %s",
    1048              :         dpitch, spitch, width, height, acl::GetMemcpyKindDesc(kind));
    1049              : 
    1050           15 :     rtMemcpyKind_t rtKindVal = RT_MEMCPY_RESERVED;
    1051           15 :     const aclError ret = CheckMemcpy2dParam(dst, dpitch, src, spitch, width, height, kind, rtKindVal);
    1052           15 :     if (ret != ACL_SUCCESS) {
    1053            5 :         return ret;
    1054              :     }
    1055              : 
    1056           10 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1057              :         rtMemcpy2dAsync(dst, dpitch, src, spitch, width, height, rtKindVal, stream), rtMemcpy2dAsync);
    1058              : 
    1059            9 :     ACL_LOG_DEBUG(
    1060              :         "Successfully execute aclrtMemcpy2dAsync, dpitch = %zu, spitch = %zu, width = %zu, height = %zu, "
    1061              :         "kind = %s",
    1062              :         dpitch, spitch, width, height, acl::GetMemcpyKindDesc(kind));
    1063            9 :     return ACL_SUCCESS;
    1064           15 : }
    1065              : 
    1066           10 : aclError aclrtReserveMemAddressImpl(void** virPtr, size_t size, size_t alignment, void* expectPtr, uint64_t flags)
    1067              : {
    1068           10 :     ACL_PROFILING_REG(acl::AclProfType::AclrtReserveMemAddress);
    1069           10 :     ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
    1070           10 :     ACL_LOG_DEBUG("start to execute aclrtReserveMemAddress, size = %zu", size);
    1071           10 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1072              : 
    1073           13 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1074              :     // flags参数取1,为了早期接口兼容性保留
    1075           12 :     ACL_CHECK_INVALID_VALUE_WITH_EXPECT((flags == 0ULL) || (flags == 1ULL), flags, "0");
    1076              : 
    1077            8 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1078              :         rtReserveMemAddress(virPtr, size, alignment, expectPtr, flags), rtReserveMemAddress);
    1079            6 :     ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
    1080            6 :     return ACL_SUCCESS;
    1081           10 : }
    1082              : 
    1083            8 : aclError aclrtReleaseMemAddressImpl(void* virPtr)
    1084              : {
    1085            8 :     ACL_PROFILING_REG(acl::AclProfType::AclrtReleaseMemAddress);
    1086            8 :     ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
    1087            8 :     ACL_LOG_DEBUG("start to execute aclrtReleaseMemAddress");
    1088            8 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1089              : 
    1090            8 :     ACL_REQUIRES_RTS_OK(rtReleaseMemAddress(virPtr));
    1091            6 :     ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_RESERVE_RELEASE_MEMORY_ADDRESS);
    1092            6 :     return ACL_SUCCESS;
    1093            8 : }
    1094              : 
    1095           35 : aclError aclrtMallocPhysicalImpl(
    1096              :     aclrtDrvMemHandle* handle, size_t size, const aclrtPhysicalMemProp* prop, uint64_t flags)
    1097              : {
    1098           35 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMallocPhysical);
    1099           35 :     ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
    1100           35 :     ACL_LOG_DEBUG("start to execute aclrtMallocPhysical, size = %zu", size);
    1101           35 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1102           35 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
    1103              : 
    1104           38 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1105           37 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
    1106           36 :     ACL_REQUIRES_PARAM_EQUAL_REPORT(prop->handleType, ACL_MEM_HANDLE_TYPE_NONE);
    1107           38 :     ACL_REQUIRES_PARAM_EQUAL_REPORT(prop->allocationType, ACL_MEM_ALLOCATION_TYPE_PINNED);
    1108           30 :     if (prop->location.type == ACL_MEM_LOCATION_TYPE_UNREGISTERED ||
    1109           30 :         prop->location.type == ACL_MEM_LOCATION_TYPE_MANAGED) {
    1110            1 :         ACL_LOG_ERROR(
    1111              :             "[Check][PARAM]prop->location.type is invalid, location type does not support %s. value=%s",
    1112              :             acl::GetMemLocationTypeDesc(prop->location.type), acl::GetMemLocationTypeDesc(prop->location.type));
    1113              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
    1114            1 :             "location type does not support %s", acl::GetMemLocationTypeDesc(prop->location.type));
    1115            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1116            1 :         acl::AclErrorLogManager::ReportInputError(
    1117            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1118            1 :             std::vector<const char*>(
    1119            1 :                 {funcName.c_str(), acl::GetMemLocationTypeDesc(prop->location.type), "prop->location.type",
    1120            2 :                  errMsg.c_str()}));
    1121            1 :         return ACL_ERROR_INVALID_PARAM;
    1122            1 :     }
    1123              : 
    1124           29 :     rtDrvMemProp_t rtProp = {};
    1125           29 :     rtProp.side = prop->location.type;
    1126           29 :     rtProp.devid = prop->location.id;
    1127           29 :     rtProp.module_id = acl::APP_MODE_ID_U16;
    1128           29 :     rtProp.reserve = prop->reserve;
    1129              : 
    1130              :     // device alloc
    1131           29 :     const bool isDeviceAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_DEVICE);
    1132           29 :     if (isDeviceAlloc && ((prop->memAttr == ACL_DDR_MEM_HUGE) || (prop->memAttr == ACL_DDR_MEM_NORMAL) ||
    1133           18 :                           (prop->memAttr == ACL_DDR_MEM_P2P_HUGE) || (prop->memAttr == ACL_DDR_MEM_P2P_NORMAL))) {
    1134            1 :         ACL_LOG_ERROR(
    1135              :             "memAttr [%s] only support MEM_LOCATION_TYPE_HOST(0) or MEM_LOCATION_TYPE_HOST_NUMA(4).",
    1136              :             acl::GetMemAttrDesc(prop->memAttr));
    1137            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1138            1 :         acl::AclErrorLogManager::ReportInputError(
    1139            2 :             acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
    1140            1 :             std::vector<const char*>(
    1141            1 :                 {funcName.c_str(), acl::GetMemAttrDesc(prop->memAttr), "memAttr",
    1142            2 :                  "MEM_LOCATION_TYPE_HOST(0) or MEM_LOCATION_TYPE_HOST_NUMA(4)"}));
    1143            1 :         return ACL_ERROR_INVALID_PARAM;
    1144            1 :     }
    1145           28 :     auto it = memAttrHandlers.find(static_cast<int32_t>(prop->memAttr));
    1146           28 :     if (it != memAttrHandlers.end()) {
    1147              :         // host alloc
    1148           52 :         const bool isHostAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) ||
    1149           26 :                                  (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
    1150           26 :         it->second(rtProp, isHostAlloc, isDeviceAlloc);
    1151              :     } else {
    1152            2 :         ACL_LOG_ERROR(
    1153              :             "memAttr [%s] is not supported. "
    1154              :             "For details, please refer to the manual.",
    1155              :             acl::GetMemAttrDesc(prop->memAttr));
    1156            2 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1157            2 :         acl::AclErrorLogManager::ReportInputError(
    1158            4 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1159            2 :             std::vector<const char*>(
    1160            2 :                 {funcName.c_str(), acl::GetMemAttrDesc(prop->memAttr), "memAttr",
    1161            4 :                  "The current physical memory attribute is not supported"}));
    1162            2 :         return ACL_ERROR_INVALID_PARAM;
    1163            2 :     }
    1164              : 
    1165           26 :     ACL_REQUIRES_RTS_OK(rtMallocPhysical(reinterpret_cast<rtDrvMemHandle*>(handle), size, &rtProp, flags));
    1166           25 :     ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
    1167           25 :     return ACL_SUCCESS;
    1168           35 : }
    1169              : 
    1170            6 : aclError aclrtFreePhysicalImpl(aclrtDrvMemHandle handle)
    1171              : {
    1172            6 :     ACL_PROFILING_REG(acl::AclProfType::AclrtFreePhysical);
    1173            6 :     ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
    1174            6 :     ACL_LOG_DEBUG("start to execute aclrtFreePhysical");
    1175            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1176              : 
    1177            6 :     ACL_REQUIRES_RTS_OK(rtFreePhysical(reinterpret_cast<rtDrvMemHandle>(handle)));
    1178            5 :     ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MALLOC_FREE_PHYSICAL_MEMORY);
    1179            5 :     return ACL_SUCCESS;
    1180            6 : }
    1181              : 
    1182            6 : aclError aclrtMapMemImpl(void* virPtr, size_t size, size_t offset, aclrtDrvMemHandle handle, uint64_t flags)
    1183              : {
    1184            6 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMapMem);
    1185            6 :     ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1186            6 :     ACL_LOG_DEBUG("start to execute aclrtMapMem, size = %zu, offset = %zu", size, offset);
    1187            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1188            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1189              : 
    1190            9 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1191            8 :     ACL_REQUIRES_PARAM_EQUAL_REPORT(flags, 0);
    1192            4 :     ACL_REQUIRES_RTS_OK(rtMapMem(virPtr, size, offset, reinterpret_cast<rtDrvMemHandle>(handle), flags));
    1193            3 :     ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1194            3 :     return ACL_SUCCESS;
    1195            6 : }
    1196              : 
    1197            4 : aclError aclrtUnmapMemImpl(void* virPtr)
    1198              : {
    1199            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtUnmapMem);
    1200            4 :     ACL_ADD_RELEASE_TOTAL_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1201            4 :     ACL_LOG_DEBUG("start to execute aclrtUnmapMem");
    1202            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1203              : 
    1204            4 :     ACL_REQUIRES_RTS_OK(rtUnmapMem(virPtr));
    1205            3 :     ACL_ADD_RELEASE_SUCCESS_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1206            3 :     return ACL_SUCCESS;
    1207            4 : }
    1208              : 
    1209            7 : aclError aclrtMemMapNoAccessImpl(void* virPtr, size_t size, size_t offset, aclrtDrvMemHandle handle, uint64_t flags)
    1210              : {
    1211            7 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemMapNoAccess);
    1212            7 :     ACL_ADD_APPLY_TOTAL_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1213            7 :     ACL_LOG_DEBUG(
    1214              :         "start to execute aclrtMemMapNoAccess, virPtr = %p, size = %zu, offset = %zu, flags = %llu", virPtr, size,
    1215              :         offset, static_cast<unsigned long long>(flags));
    1216            7 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1217            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1218            8 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1219            7 :     ACL_REQUIRES_PARAM_EQUAL_REPORT(flags, 0);
    1220            3 :     ACL_REQUIRES_RTS_OK(rtMemMapNoAccess(virPtr, size, offset, reinterpret_cast<rtDrvMemHandle>(handle), flags));
    1221            1 :     ACL_ADD_APPLY_SUCCESS_COUNT(acl::ACL_STATISTICS_MAP_UNMAP_MEMORY);
    1222            1 :     return ACL_SUCCESS;
    1223            7 : }
    1224              : 
    1225            2 : aclError aclrtMemGetAccessImpl(void* virPtr, aclrtMemLocation* location, uint64_t* flag)
    1226              : {
    1227            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAccess);
    1228            2 :     ACL_LOG_DEBUG("start to execute aclrtMemGetAccess");
    1229            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1230            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(location);
    1231            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(flag);
    1232              : 
    1233            2 :     ACL_REQUIRES_RTS_OK(rtMemGetAccess(virPtr, reinterpret_cast<rtMemLocation*>(location), flag));
    1234            1 :     return ACL_SUCCESS;
    1235            2 : }
    1236              : 
    1237            4 : aclError aclrtMemExportToShareableHandleImpl(
    1238              :     aclrtDrvMemHandle handle, aclrtMemHandleType handleType, uint64_t flags, uint64_t* shareableHandle)
    1239              : {
    1240            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemExportToShareableHandle);
    1241            4 :     ACL_LOG_DEBUG("start to execute aclrtMemExportToShareableHandle");
    1242            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1243            6 :     ACL_REQUIRES_PARAM_EQUAL_REPORT(handleType, ACL_MEM_HANDLE_TYPE_NONE);
    1244            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
    1245              : 
    1246            2 :     ACL_REQUIRES_RTS_OK(rtsMemExportToShareableHandle(
    1247              :         reinterpret_cast<rtDrvMemHandle>(handle), RT_MEM_HANDLE_TYPE_NONE, flags, shareableHandle));
    1248            1 :     return ACL_SUCCESS;
    1249            4 : }
    1250              : 
    1251            3 : aclError aclrtMemExportToShareableHandleV2Impl(
    1252              :     aclrtDrvMemHandle handle, uint64_t flags, aclrtMemSharedHandleType shareType, void* shareableHandle)
    1253              : {
    1254            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemExportToShareableHandleV2);
    1255            3 :     ACL_LOG_DEBUG("start to execute aclrtMemExportToShareableHandleV2");
    1256            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1257            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
    1258              : 
    1259            2 :     ACL_REQUIRES_RTS_OK(rtMemExportToShareableHandleV2(
    1260              :         reinterpret_cast<rtDrvMemHandle>(handle), static_cast<rtMemSharedHandleType>(shareType), flags,
    1261              :         shareableHandle));
    1262            1 :     return ACL_SUCCESS;
    1263            3 : }
    1264              : 
    1265            3 : aclError aclrtMemImportFromShareableHandleImpl(uint64_t shareableHandle, int32_t deviceId, aclrtDrvMemHandle* handle)
    1266              : {
    1267            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemImportFromShareableHandle);
    1268            3 :     ACL_LOG_DEBUG("start to execute aclrtMemImportFromShareableHandle");
    1269            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1270              : 
    1271            2 :     ACL_REQUIRES_RTS_OK(
    1272              :         rtMemImportFromShareableHandle(shareableHandle, deviceId, reinterpret_cast<rtDrvMemHandle*>(handle)));
    1273            1 :     return ACL_SUCCESS;
    1274            3 : }
    1275              : 
    1276            4 : aclError aclrtMemImportFromShareableHandleV2Impl(
    1277              :     void* shareableHandle, aclrtMemSharedHandleType shareType, uint64_t flags, aclrtDrvMemHandle* handle)
    1278              : {
    1279            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemImportFromShareableHandleV2);
    1280            4 :     ACL_LOG_DEBUG("start to execute aclrtMemImportFromShareableHandleV2");
    1281            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
    1282            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1283            6 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
    1284              : 
    1285            2 :     int32_t deviceId = 0;
    1286            2 :     const rtError_t rtRet = rtsGetDevice(&deviceId);
    1287            2 :     if (rtRet != ACL_RT_SUCCESS) {
    1288            0 :         return rtRet;
    1289              :     }
    1290              : 
    1291            2 :     ACL_REQUIRES_RTS_OK(rtMemImportFromShareableHandleV2(
    1292              :         shareableHandle, static_cast<rtMemSharedHandleType>(shareType), flags, deviceId,
    1293              :         reinterpret_cast<rtDrvMemHandle*>(handle)));
    1294            1 :     return ACL_SUCCESS;
    1295            4 : }
    1296              : 
    1297            4 : aclError aclrtMemSetPidToShareableHandleImpl(uint64_t shareableHandle, int32_t* pid, size_t pidNum)
    1298              : {
    1299            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetPidToShareableHandle);
    1300            4 :     ACL_LOG_DEBUG("start to execute aclrtMemSetPidToShareableHandle");
    1301            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
    1302            6 :     ACL_REQUIRES_POSITIVE_REPORT(pidNum);
    1303            2 :     ACL_REQUIRES_RTS_OK(rtMemSetPidToShareableHandle(shareableHandle, pid, static_cast<uint32_t>(pidNum)));
    1304            1 :     return ACL_SUCCESS;
    1305            4 : }
    1306              : 
    1307            4 : aclError aclrtMemSetPidToShareableHandleV2Impl(
    1308              :     void* shareableHandle, aclrtMemSharedHandleType shareType, int32_t* pid, size_t pidNum)
    1309              : {
    1310            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetPidToShareableHandleV2);
    1311            4 :     ACL_LOG_DEBUG("start to execute AclrtMemSetPidToShareableHandleV2");
    1312            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(shareableHandle);
    1313            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
    1314            6 :     ACL_REQUIRES_POSITIVE_REPORT(pidNum);
    1315              : 
    1316            2 :     ACL_REQUIRES_RTS_OK(rtMemSetPidToShareableHandleV2(
    1317              :         shareableHandle, static_cast<rtMemSharedHandleType>(shareType), pid, static_cast<uint32_t>(pidNum)));
    1318            1 :     return ACL_SUCCESS;
    1319            4 : }
    1320              : 
    1321           11 : aclError aclrtMemGetAllocationGranularityImpl(
    1322              :     aclrtPhysicalMemProp* prop, aclrtMemGranularityOptions option, size_t* granularity)
    1323              : {
    1324           11 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAllocationGranularity);
    1325           11 :     ACL_LOG_DEBUG("start to execute aclrtMemGetAllocationGranularity");
    1326           11 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
    1327           10 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(granularity);
    1328              : 
    1329            9 :     rtDrvMemProp_t rtProp1 = {};
    1330            9 :     rtProp1.side = prop->location.type;
    1331            9 :     rtProp1.devid = prop->location.id;
    1332            9 :     rtProp1.module_id = acl::APP_MODE_ID_U16;
    1333            9 :     rtProp1.reserve = prop->reserve;
    1334              : 
    1335              :     // device alloc
    1336            9 :     const bool isDeviceAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_DEVICE);
    1337            9 :     if (isDeviceAlloc && ((prop->memAttr == ACL_DDR_MEM_HUGE) || (prop->memAttr == ACL_DDR_MEM_NORMAL) ||
    1338            2 :                           (prop->memAttr == ACL_DDR_MEM_P2P_HUGE) || (prop->memAttr == ACL_DDR_MEM_P2P_NORMAL))) {
    1339            4 :         ACL_LOG_ERROR(
    1340              :             "memAttr [%s] only support MEM_LOCATION_TYPE_HOST(0) or MEM_LOCATION_TYPE_HOST_NUMA(4).",
    1341              :             acl::GetMemAttrDesc(prop->memAttr));
    1342            4 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1343            4 :         acl::AclErrorLogManager::ReportInputError(
    1344            8 :             acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
    1345            4 :             std::vector<const char*>(
    1346            4 :                 {funcName.c_str(), acl::GetMemAttrDesc(prop->memAttr), "memAttr",
    1347            8 :                  "MEM_LOCATION_TYPE_HOST(0) or MEM_LOCATION_TYPE_HOST_NUMA(4)"}));
    1348            4 :         return ACL_ERROR_INVALID_PARAM;
    1349            4 :     }
    1350            5 :     auto it = memAttrHandlers.find(static_cast<int32_t>(prop->memAttr));
    1351            5 :     if (it != memAttrHandlers.end()) {
    1352              :         // host alloc
    1353            4 :         const bool isHostAlloc = (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) ||
    1354            0 :                                  (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
    1355            4 :         it->second(rtProp1, isHostAlloc, isDeviceAlloc);
    1356              :     } else {
    1357            1 :         ACL_LOG_ERROR(
    1358              :             "memAttr [%s] is not supported. "
    1359              :             "For details, please refer to the manual.",
    1360              :             acl::GetMemAttrDesc(prop->memAttr));
    1361            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1362            1 :         acl::AclErrorLogManager::ReportInputError(
    1363            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1364            1 :             std::vector<const char*>(
    1365            1 :                 {funcName.c_str(), acl::GetMemAttrDesc(prop->memAttr), "memAttr",
    1366            2 :                  "The current physical memory attribute is not supported"}));
    1367            1 :         return ACL_ERROR_INVALID_PARAM;
    1368            1 :     }
    1369              : 
    1370            4 :     ACL_REQUIRES_RTS_OK(
    1371              :         rtMemGetAllocationGranularity(&rtProp1, static_cast<rtDrvMemGranularityOptions>(option), granularity));
    1372            3 :     return ACL_SUCCESS;
    1373           11 : }
    1374              : 
    1375            3 : aclError aclrtDeviceGetBareTgidImpl(int32_t* pid)
    1376              : {
    1377            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceGetBareTgid);
    1378            3 :     ACL_LOG_DEBUG("start to execute aclrtDeviceGetBareTgid");
    1379            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
    1380              : 
    1381            2 :     ACL_REQUIRES_RTS_OK(rtDeviceGetBareTgid(reinterpret_cast<uint32_t*>(pid)));
    1382            1 :     return ACL_SUCCESS;
    1383            3 : }
    1384              : 
    1385            4 : aclError aclrtCmoAsyncImpl(void* src, size_t size, aclrtCmoType cmoType, aclrtStream stream)
    1386              : {
    1387            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsync);
    1388            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
    1389            6 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1390            2 :     const rtCmoOpCode_t type = static_cast<rtCmoOpCode_t>(
    1391              :         static_cast<uint32_t>(cmoType) +
    1392              :         (static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH)));
    1393            2 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtCmoAsync(src, size, type, stream), rtCmoAsync);
    1394            1 :     return ACL_SUCCESS;
    1395            4 : }
    1396              : 
    1397            2 : aclError aclrtGetMemcpyDescSizeImpl(aclrtMemcpyKind kind, size_t* descSize)
    1398              : {
    1399            2 :     ACL_LOG_INFO("start to execute aclrtGetMemcpyDescSize");
    1400            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(descSize);
    1401            2 :     ACL_CHECK_INVALID_VALUE_WITH_DESC(
    1402              :         static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX), acl::GetMemcpyKindDesc(kind), "kind",
    1403              :         "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)", ACL_ERROR_INVALID_PARAM);
    1404            2 :     const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<uint32_t>(kind));
    1405            2 :     ACL_REQUIRES_RTS_OK(rtsGetMemcpyDescSize(rt_mem_kind, descSize));
    1406            1 :     return ACL_SUCCESS;
    1407              : }
    1408              : 
    1409            6 : aclError aclrtSetMemcpyDescImpl(
    1410              :     void* desc, aclrtMemcpyKind kind, void* srcAddr, void* dstAddr, size_t count, void* config)
    1411              : {
    1412            6 :     ACL_LOG_INFO("start to execute aclrtSetMemcpyDesc");
    1413            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(desc);
    1414            5 :     ACL_CHECK_INVALID_VALUE_WITH_DESC(
    1415              :         static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX), acl::GetMemcpyKindDesc(kind), "kind",
    1416              :         "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)", ACL_ERROR_INVALID_PARAM);
    1417            5 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(srcAddr);
    1418            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dstAddr);
    1419            6 :     ACL_REQUIRES_POSITIVE_REPORT(count);
    1420            2 :     ACL_CHECK_INVALID_PARAM_NO_VALUE(
    1421              :         config == nullptr, "reserve", "config is a reserved parameter and must be nullptr");
    1422              : 
    1423            2 :     const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<uint32_t>(kind));
    1424            2 :     ACL_REQUIRES_RTS_OK(
    1425              :         rtsSetMemcpyDesc(static_cast<rtMemcpyDesc_t>(desc), rt_mem_kind, srcAddr, dstAddr, count, nullptr));
    1426            1 :     return ACL_SUCCESS;
    1427              : }
    1428              : 
    1429            4 : aclError aclrtMemcpyAsyncWithDescImpl(void* desc, aclrtMemcpyKind kind, aclrtStream stream)
    1430              : {
    1431            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithDesc);
    1432            4 :     ACL_LOG_INFO("start to execute aclrtMemcpyAsyncWithDesc");
    1433            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(desc);
    1434            3 :     ACL_CHECK_INVALID_VALUE_WITH_DESC(
    1435              :         static_cast<uint32_t>(kind) < static_cast<uint32_t>(RT_MEMCPY_KIND_MAX), acl::GetMemcpyKindDesc(kind), "kind",
    1436              :         "[RT_MEMCPY_KIND_HOST_TO_HOST, RT_MEMCPY_KIND_MAX)", ACL_ERROR_INVALID_PARAM);
    1437            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);
    1438              : 
    1439            2 :     const auto rt_mem_kind = static_cast<rtMemcpyKind>(static_cast<int32_t>(kind));
    1440            2 :     ACL_REQUIRES_RTS_OK(rtsMemcpyAsyncWithDesc(static_cast<rtMemcpyDesc_t>(desc), rt_mem_kind, nullptr, stream));
    1441            1 :     return ACL_SUCCESS;
    1442            4 : }
    1443              : 
    1444            6 : aclError aclrtMemcpyAsyncWithOffsetImpl(
    1445              :     void** dst, size_t destMax, size_t dstDataOffset, const void** src, size_t count, size_t srcDataOffset,
    1446              :     aclrtMemcpyKind kind, aclrtStream stream)
    1447              : {
    1448            6 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithOffset);
    1449            6 :     ACL_LOG_INFO("start to execute aclrtMemcpyAsyncWithOffset");
    1450            6 :     if (count == 0UL) {
    1451            2 :         ACL_LOG_INFO("zero-size memcpy, no memory copy async with offset will be performed");
    1452            2 :         return ACL_SUCCESS;
    1453              :     }
    1454              : 
    1455            4 :     const auto memKind = static_cast<rtMemcpyKind>(static_cast<int32_t>(kind));
    1456            4 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1457              :         rtMemcpyAsyncWithOffset(dst, destMax, dstDataOffset, src, count, srcDataOffset, memKind, stream),
    1458              :         rtMemcpyAsyncWithOffset);
    1459            1 :     ACL_LOG_INFO("successfully execute aclrtMemcpyAsyncWithOffset");
    1460            1 :     return ACL_SUCCESS;
    1461            6 : }
    1462              : 
    1463            3 : aclError aclrtValueWriteImpl(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
    1464              : {
    1465            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtValueWrite);
    1466            3 :     ACL_LOG_INFO("start to execute aclrtValueWrite");
    1467            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devAddr);
    1468              : 
    1469            2 :     ACL_REQUIRES_RTS_OK(rtsValueWrite(devAddr, value, flag, static_cast<rtStream_t>(stream)));
    1470            1 :     return ACL_SUCCESS;
    1471            3 : }
    1472              : 
    1473            3 : aclError aclrtValueWaitImpl(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
    1474              : {
    1475            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtValueWait);
    1476            3 :     ACL_LOG_INFO("start to execute aclrtValueWait");
    1477            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devAddr);
    1478              : 
    1479            2 :     ACL_REQUIRES_RTS_OK(rtsValueWait(devAddr, value, flag, static_cast<rtStream_t>(stream)));
    1480            1 :     return ACL_SUCCESS;
    1481            3 : }
    1482              : 
    1483           15 : aclError aclrtReduceAsyncImpl(
    1484              :     void* dst, const void* src, uint64_t count, aclrtReduceKind kind, aclDataType type, aclrtStream stream,
    1485              :     void* reserve)
    1486              : {
    1487           15 :     ACL_PROFILING_REG(acl::AclProfType::AclrtReduceAsync);
    1488           15 :     ACL_LOG_DEBUG(
    1489              :         "start to execute aclrtReduceAsync, count = [%lu], kind = [%s], type = [%s]", count,
    1490              :         acl::GetReduceKindDesc(kind), acl::GetDataTypeDesc(type));
    1491           15 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dst);
    1492           14 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
    1493           13 :     ACL_CHECK_INVALID_PARAM_NO_VALUE(
    1494              :         reserve == nullptr, "reserve", "reserve is a reserved parameter and must be nullptr");
    1495              : 
    1496              :     rtDataType dataType;
    1497           12 :     if (kMapDataType.count(type) > 0) {
    1498           11 :         dataType = kMapDataType.at(type);
    1499              :     } else {
    1500            1 :         ACL_LOG_ERROR("[Check][param]param type [%s] is invalid.", acl::GetDataTypeDesc(type));
    1501            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1502            1 :         acl::AclErrorLogManager::ReportInputError(
    1503            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1504            1 :             std::vector<const char*>(
    1505            2 :                 {funcName.c_str(), acl::GetDataTypeDesc(type), "type", "The data type is currently not supported"}));
    1506            1 :         return ACL_ERROR_INVALID_PARAM;
    1507            1 :     }
    1508              : 
    1509              :     rtReduceInfo_t reduceInfo;
    1510           11 :     reduceInfo.dst = dst;
    1511           11 :     reduceInfo.src = const_cast<void*>(src);
    1512           11 :     reduceInfo.count = static_cast<size_t>(count);
    1513           11 :     reduceInfo.kind = static_cast<rtReduceKind>(kind);
    1514           11 :     reduceInfo.type = dataType;
    1515           11 :     ACL_REQUIRES_RTS_OK(rtsLaunchReduceAsyncTask(&reduceInfo, static_cast<rtStream_t>(stream), reserve));
    1516           10 :     return ACL_SUCCESS;
    1517           15 : }
    1518              : 
    1519            1 : aclError aclrtGetBufFromChainImpl(aclrtMbuf headBuf, uint32_t index, aclrtMbuf* buf)
    1520              : {
    1521            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufFromChain);
    1522            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
    1523            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1524            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainGetMbuf(headBuf, index, buf), rtMbufChainGetMbuf);
    1525            1 :     return ACL_SUCCESS;
    1526            1 : }
    1527              : 
    1528            1 : aclError aclrtGetBufChainNumImpl(aclrtMbuf headBuf, uint32_t* num)
    1529              : {
    1530            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufChainNum);
    1531            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
    1532            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(num);
    1533            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainGetMbufNum(headBuf, num), rtMbufChainGetMbufNum);
    1534            1 :     return ACL_SUCCESS;
    1535            1 : }
    1536              : 
    1537            1 : aclError aclrtAppendBufChainImpl(aclrtMbuf headBuf, aclrtMbuf buf)
    1538              : {
    1539            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtAppendBufChain);
    1540            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(headBuf);
    1541            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1542            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufChainAppend(headBuf, buf), rtMbufChainAppend);
    1543            1 :     return ACL_SUCCESS;
    1544            1 : }
    1545              : 
    1546            1 : aclError aclrtCopyBufRefImpl(const aclrtMbuf buf, aclrtMbuf* newBuf)
    1547              : {
    1548            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCopyBufRef);
    1549            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1550            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(newBuf);
    1551            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufCopyBufRef(buf, newBuf), rtMbufCopyBufRef);
    1552            1 :     return ACL_SUCCESS;
    1553            1 : }
    1554              : 
    1555            3 : aclError aclrtGetBufUserDataImpl(const aclrtMbuf buf, void* dataPtr, size_t size, size_t offset)
    1556              : {
    1557            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufUserData);
    1558              :     // The current default private data area size is 96B, if offset+size exceeds 96, an error is reported
    1559            3 :     if (size + offset > MEM_SIZE_MAX) {
    1560            1 :         ACL_LOG_ERROR(
    1561              :             "%s failed because the sum of size and offset is greater than %u, size=%zu, offset=%zu.", __func__,
    1562              :             MEM_SIZE_MAX, size, offset);
    1563            1 :         const std::string sizeVal = std::to_string(size);
    1564              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
    1565            1 :             "the sum of size and offset is greater than %u, size=%zu, offset=%zu.", MEM_SIZE_MAX, size, offset);
    1566            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1567            1 :         acl::AclErrorLogManager::ReportInputError(
    1568            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1569            2 :             std::vector<const char*>({funcName.c_str(), sizeVal.c_str(), "size", errMsg.c_str()}));
    1570            1 :         return ACL_ERROR_INVALID_PARAM;
    1571            1 :     }
    1572              : 
    1573            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1574            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
    1575            2 :     uint64_t bufSize = 0U;
    1576            2 :     void* tmpDataPtr = nullptr;
    1577            2 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetPrivInfo(buf, &tmpDataPtr, &bufSize), rtMbufGetPrivInfo);
    1578            2 :     ACL_CHECK_LESS_UINT(size + offset, static_cast<size_t>(bufSize));
    1579            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(tmpDataPtr);
    1580            2 :     const void* const srcAddr = static_cast<uint8_t*>(tmpDataPtr) + offset;
    1581            2 :     const auto ret = memcpy_s(dataPtr, size, srcAddr, size);
    1582            2 :     if (ret != EOK) {
    1583            1 :         const std::string retVal = std::to_string(ret);
    1584            1 :         std::stringstream ss;
    1585            1 :         ss << std::hex << "src=0x" << reinterpret_cast<uintptr_t>(srcAddr) << ", dataPtr=0x"
    1586            1 :            << reinterpret_cast<uintptr_t>(dataPtr) << std::dec << ", size=" << size << ", count=" << size << ".";
    1587            1 :         const std::string extendInfo = ss.str();
    1588            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1589            1 :         acl::AclErrorLogManager::ReportInputError(
    1590              :             acl::STANDARD_FUNC_FAILED_MSG,
    1591            2 :             std::vector<const char*>({"func1", "func2", "ret_code", "reason", "extend_info"}),
    1592            1 :             std::vector<const char*>(
    1593            2 :                 {funcName.c_str(), "memcpy_s", retVal.c_str(), strerror(ret), extendInfo.c_str()}));
    1594            1 :         ACL_LOG_ERROR(
    1595              :             "call memcpy_s failed, result = %d, size = %zu, bufSize = %lu, offset = %zu", ret, size, bufSize, offset);
    1596            1 :         return ACL_ERROR_FAILURE;
    1597            1 :     }
    1598            1 :     return ACL_SUCCESS;
    1599            3 : }
    1600              : 
    1601            3 : aclError aclrtSetBufUserDataImpl(aclrtMbuf buf, const void* dataPtr, size_t size, size_t offset)
    1602              : {
    1603            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtSetBufUserData);
    1604              :     // The current default private data area size is 96B, if offset+size exceeds 96, an error is reported
    1605            3 :     if (size + offset > MEM_SIZE_MAX) {
    1606            1 :         ACL_LOG_ERROR(
    1607              :             "%s failed because the sum of size and offset is greater than %u, size=%zu, offset=%zu.", __func__,
    1608              :             MEM_SIZE_MAX, size, offset);
    1609            1 :         const std::string sizeVal = std::to_string(size);
    1610              :         std::string errMsg = acl::AclErrorLogManager::FormatStr(
    1611            1 :             "the sum of size and offset is greater than %u, size=%zu, offset=%zu", MEM_SIZE_MAX, size, offset);
    1612            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1613            1 :         acl::AclErrorLogManager::ReportInputError(
    1614            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1615            2 :             std::vector<const char*>({funcName.c_str(), sizeVal.c_str(), "size", errMsg.c_str()}));
    1616            1 :         return ACL_ERROR_INVALID_PARAM;
    1617            1 :     }
    1618            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1619            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
    1620            2 :     uint64_t bufSize = 0U;
    1621            2 :     void* tmpDataPtr = nullptr;
    1622            2 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetPrivInfo(buf, &tmpDataPtr, &bufSize), rtMbufGetPrivInfo);
    1623            2 :     ACL_CHECK_LESS_UINT(size + offset, static_cast<size_t>(bufSize));
    1624            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(tmpDataPtr);
    1625            2 :     void* const destAddr = static_cast<uint8_t*>(tmpDataPtr) + offset;
    1626            2 :     const size_t destMax = static_cast<size_t>(bufSize) - offset;
    1627            2 :     const auto ret = memcpy_s(destAddr, destMax, dataPtr, size);
    1628            2 :     if (ret != EOK) {
    1629            1 :         const std::string retVal = std::to_string(ret);
    1630            1 :         std::stringstream ss;
    1631            1 :         ss << std::hex << "dataPtr=0x" << reinterpret_cast<uintptr_t>(dataPtr) << ", dest=0x"
    1632            1 :            << reinterpret_cast<uintptr_t>(destAddr) << std::dec << ", dest_max=" << destMax << ", size=" << size << ".";
    1633            1 :         const std::string extendInfo = ss.str();
    1634            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    1635            1 :         acl::AclErrorLogManager::ReportInputError(
    1636              :             acl::STANDARD_FUNC_FAILED_MSG,
    1637            2 :             std::vector<const char*>({"func1", "func2", "ret_code", "reason", "extend_info"}),
    1638            1 :             std::vector<const char*>(
    1639            2 :                 {funcName.c_str(), "memcpy_s", retVal.c_str(), strerror(ret), extendInfo.c_str()}));
    1640            1 :         ACL_LOG_ERROR(
    1641              :             "call memcpy_s failed, result = %d, size = %zu, bufSize = %lu, offset = %zu", ret, size, bufSize, offset);
    1642            1 :         return ACL_ERROR_FAILURE;
    1643            1 :     }
    1644            1 :     return ACL_SUCCESS;
    1645            3 : }
    1646              : 
    1647            4 : aclError aclrtGetBufDataImpl(const aclrtMbuf buf, void** dataPtr, size_t* size)
    1648              : {
    1649            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufData);
    1650            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1651            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dataPtr);
    1652            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(size);
    1653            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetBuffAddr(buf, dataPtr), rtMbufGetBuffAddr);
    1654            1 :     uint64_t bufSize = 0U;
    1655            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetBuffSize(buf, &bufSize), rtMbufGetBuffSize);
    1656            1 :     *size = static_cast<size_t>(bufSize);
    1657            1 :     return ACL_SUCCESS;
    1658            4 : }
    1659              : 
    1660            1 : aclError aclrtGetBufDataLenImpl(aclrtMbuf buf, size_t* len)
    1661              : {
    1662            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetBufDataLen);
    1663            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1664            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(len);
    1665            1 :     uint64_t dataLen = 0U;
    1666            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufGetDataLen(buf, &dataLen), rtMbufGetDataLen);
    1667            1 :     *len = static_cast<size_t>(dataLen);
    1668            1 :     return ACL_SUCCESS;
    1669            1 : }
    1670              : 
    1671            1 : aclError aclrtSetBufDataLenImpl(aclrtMbuf buf, size_t len)
    1672              : {
    1673            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtSetBufDataLen);
    1674            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1675            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtMbufSetDataLen(buf, len), rtMbufSetDataLen);
    1676            1 :     return ACL_SUCCESS;
    1677            1 : }
    1678              : 
    1679            8 : aclError aclrtFreeBufImpl(aclrtMbuf buf)
    1680              : {
    1681            8 :     ACL_PROFILING_REG(acl::AclProfType::AclrtFreeBuf);
    1682            8 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1683            8 :     ACL_REQUIRES_RTS_OK(rtMbufFree(buf));
    1684            8 :     buf = nullptr;
    1685            8 :     return ACL_SUCCESS;
    1686            8 : }
    1687              : 
    1688            4 : aclError aclrtAllocBufImpl(aclrtMbuf* buf, size_t size)
    1689              : {
    1690            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtAllocBuf);
    1691            4 :     ACL_LOG_INFO("start to execute aclrtAllocBuf, size is [%zu]", size);
    1692            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(buf);
    1693              :     // size must be greater than 0
    1694            6 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    1695            2 :     ACL_REQUIRES_RTS_OK(rtMbufAlloc(buf, size));
    1696            1 :     return ACL_SUCCESS;
    1697            4 : }
    1698              : 
    1699            3 : aclError aclrtCmoAsyncWithBarrierImpl(
    1700              :     void* src, size_t size, aclrtCmoType cmoType, uint32_t barrierId, aclrtStream stream)
    1701              : {
    1702            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsyncWithBarrier);
    1703            3 :     ACL_LOG_INFO(
    1704              :         "start to execute aclrtCmoAsyncWithBarrier, size is [%zu], cmoType is [%s], barrierId is [%u]", size,
    1705              :         acl::GetCmoTypeDesc(cmoType), barrierId);
    1706            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(src);
    1707            2 :     const rtCmoOpCode rtCmoType = static_cast<rtCmoOpCode>(
    1708              :         static_cast<uint32_t>(cmoType) +
    1709              :         (static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH)));
    1710            2 :     ACL_REQUIRES_RTS_OK(rtsCmoAsyncWithBarrier(src, size, rtCmoType, barrierId, static_cast<rtStream_t>(stream)));
    1711            1 :     return ACL_SUCCESS;
    1712            3 : }
    1713              : 
    1714           22 : static aclError ValidateMemcpyBatchParams(
    1715              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1716              :     size_t* attrsIndexes, size_t numAttrs)
    1717              : {
    1718           22 :     constexpr const char_t* funcDesc = "Checking the batch memory copy parameter validity";
    1719           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(dsts, funcDesc);
    1720           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(destMaxs, funcDesc);
    1721           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(srcs, funcDesc);
    1722           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(sizes, funcDesc);
    1723           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(attrs, funcDesc);
    1724           22 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(attrsIndexes, funcDesc);
    1725              : 
    1726          126 :     for (size_t i = 0UL; i < numBatches; i++) {
    1727          108 :         if (destMaxs[i] < sizes[i]) {
    1728            4 :             ACL_LOG_ERROR("element of destMaxs must be equal to or greater than corresponding element of sizes");
    1729            4 :             const std::string destMaxsVal = std::to_string(destMaxs[i]);
    1730              :             std::string errMsg = acl::AclErrorLogManager::FormatStr(
    1731            4 :                 "The memory copy size %zu at index %zu exceeds the size %zu of the destination buffer", sizes[i], i,
    1732            4 :                 destMaxs[i]);
    1733            4 :             acl::AclErrorLogManager::ReportInputError(
    1734            8 :                 acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    1735            8 :                 std::vector<const char*>({funcDesc, destMaxsVal.c_str(), "destMaxs", errMsg.c_str()}));
    1736            4 :             return ACL_ERROR_INVALID_PARAM;
    1737            4 :         }
    1738              :     }
    1739              : 
    1740           18 :     constexpr uint32_t rsvMaxSize = sizeof(aclrtMemcpyBatchAttr::rsv) / sizeof(uint8_t);
    1741           32 :     for (size_t idx = 0UL; idx < numAttrs; idx++) {
    1742          242 :         for (uint32_t i = 0U; i < rsvMaxSize; i++) {
    1743          228 :             if (attrs[idx].rsv[i] != 0U) {
    1744            4 :                 ACL_LOG_ERROR("rsv field of attrs[%zu] must be 0", idx);
    1745            4 :                 const std::string rsvVal = std::to_string(attrs[idx].rsv[i]);
    1746            4 :                 acl::AclErrorLogManager::ReportInputError(
    1747            8 :                     acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
    1748            8 :                     std::vector<const char*>({funcDesc, rsvVal.c_str(), "attrs.rsv", "0"}));
    1749            4 :                 return ACL_ERROR_INVALID_PARAM;
    1750            4 :             }
    1751              :         }
    1752              :     }
    1753              : 
    1754           14 :     return ACL_SUCCESS;
    1755              : }
    1756              : 
    1757           42 : static aclError MemcpyBatchImpl(
    1758              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1759              :     size_t* attrsIndexes, size_t numAttrs, size_t* failIndex, aclrtStream stream, bool async, const char* apiName)
    1760              : {
    1761              :     // 判断 sizes == nullptr, count == 0 报参数异常
    1762           42 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(sizes, "Batch memory copy");
    1763           58 :     ACL_REQUIRES_POSITIVE_REPORT_WITH_FUNC_DESC(numBatches, "Batch memory copy");
    1764              :     // 如果所有批次的 size 都为 0,则不执行 memcpy,直接返回成功,不需要校验其他参数
    1765           26 :     if (IsAllZeroSizeBatch(sizes, numBatches)) {
    1766            4 :         if (failIndex != nullptr) {
    1767            2 :             *failIndex = SIZE_MAX;
    1768              :         }
    1769            4 :         ACL_LOG_INFO("successfully execute %s", apiName);
    1770            4 :         return ACL_SUCCESS;
    1771              :     }
    1772              :     const aclError ret =
    1773           22 :         ValidateMemcpyBatchParams(dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs);
    1774           22 :     if (ret != ACL_SUCCESS) {
    1775            8 :         return ret;
    1776              :     }
    1777              : 
    1778           14 :     if (async) {
    1779            7 :         ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1780              :             rtsMemcpyBatchAsync(
    1781              :                 dsts, destMaxs, srcs, sizes, numBatches, reinterpret_cast<rtMemcpyBatchAttr*>(attrs), attrsIndexes,
    1782              :                 numAttrs, failIndex, stream),
    1783              :             rtsMemcpyBatchAsync);
    1784            3 :         ACL_LOG_INFO("successfully execute %s", apiName);
    1785              :     } else {
    1786            7 :         ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1787              :             rtsMemcpyBatch(
    1788              :                 dsts, srcs, sizes, numBatches, reinterpret_cast<rtMemcpyBatchAttr*>(attrs), attrsIndexes, numAttrs,
    1789              :                 failIndex),
    1790              :             rtsMemcpyBatch);
    1791            3 :         ACL_LOG_INFO("successfully execute %s", apiName);
    1792              :     }
    1793              : 
    1794            6 :     return ACL_SUCCESS;
    1795              : }
    1796              : 
    1797            4 : aclError aclrtIpcMemGetExportKeyImpl(void* devPtr, size_t size, char* key, size_t len, uint64_t flags)
    1798              : {
    1799            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemGetExportKey);
    1800            4 :     ACL_LOG_INFO(
    1801              :         "start to execute aclrtIpcMemGetExportKey, size is [%zu], len is [%zu], flags is [%lu]", size, len, flags);
    1802            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
    1803            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
    1804            2 :     ACL_REQUIRES_RTS_OK(rtsIpcMemGetExportKey(devPtr, size, key, static_cast<uint32_t>(len), flags));
    1805            1 :     return ACL_SUCCESS;
    1806            4 : }
    1807              : 
    1808            3 : aclError aclrtIpcMemCloseImpl(const char* key)
    1809              : {
    1810            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemClose);
    1811            3 :     ACL_LOG_INFO("start to execute aclrtIpcMemClose");
    1812            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
    1813            2 :     ACL_REQUIRES_RTS_OK(rtsIpcMemClose(key));
    1814            1 :     return ACL_SUCCESS;
    1815            3 : }
    1816              : 
    1817            4 : aclError aclrtIpcMemImportByKeyImpl(void** devPtr, const char* key, uint64_t flags)
    1818              : {
    1819            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemImportByKey);
    1820            4 :     ACL_LOG_INFO("start to execute aclrtIpcMemImportByKey, flags is [%lu]", flags);
    1821            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
    1822            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
    1823            2 :     const auto rtErr = rtsIpcMemImportByKey(devPtr, key, flags);
    1824            2 :     if (rtErr == RT_ERROR_NONE) {
    1825            1 :         return ACL_SUCCESS;
    1826              :     }
    1827              : 
    1828            1 :     if (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
    1829            0 :         ACL_LOG_WARN("call rtsIpcMemImportByKey failed, runtime result = %d.", static_cast<int32_t>(rtErr));
    1830              :     } else {
    1831            1 :         ACL_LOG_CALL_ERROR("call rtsIpcMemImportByKey failed, runtime result = %d", rtErr);
    1832              :     }
    1833            1 :     return ACL_GET_ERRCODE_RTS(rtErr);
    1834            4 : }
    1835              : 
    1836           16 : aclError aclrtMemcpyBatchImpl(
    1837              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1838              :     size_t* attrsIndexes, size_t numAttrs, size_t* failIndex)
    1839              : {
    1840           16 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatch);
    1841           16 :     ACL_LOG_INFO("start to execute aclrtMemcpyBatch");
    1842           16 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(failIndex);
    1843            9 :     return MemcpyBatchImpl(
    1844              :         dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, failIndex, nullptr, false,
    1845            9 :         "aclrtMemcpyBatch");
    1846           16 : }
    1847              : 
    1848           12 : aclError aclrtMemcpyBatchV2Impl(
    1849              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1850              :     size_t* attrsIndexes, size_t numAttrs)
    1851              : {
    1852           12 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchV2);
    1853           12 :     ACL_LOG_INFO("start to execute aclrtMemcpyBatchV2");
    1854           12 :     return MemcpyBatchImpl(
    1855              :         dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, nullptr, nullptr, false,
    1856           24 :         "aclrtMemcpyBatchV2");
    1857           12 : }
    1858              : 
    1859           16 : aclError aclrtMemcpyBatchAsyncImpl(
    1860              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1861              :     size_t* attrsIndexes, size_t numAttrs, size_t* failIndex, aclrtStream stream)
    1862              : {
    1863           16 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchAsync);
    1864           16 :     ACL_LOG_INFO("start to execute aclrtMemcpyBatchAsync");
    1865           16 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(failIndex);
    1866            9 :     return MemcpyBatchImpl(
    1867              :         dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, failIndex, stream, true,
    1868            9 :         "aclrtMemcpyBatchAsync");
    1869           16 : }
    1870              : 
    1871           12 : aclError aclrtMemcpyBatchAsyncV2Impl(
    1872              :     void** dsts, size_t* destMaxs, void** srcs, size_t* sizes, size_t numBatches, aclrtMemcpyBatchAttr* attrs,
    1873              :     size_t* attrsIndexes, size_t numAttrs, aclrtStream stream)
    1874              : {
    1875           12 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyBatchAsyncV2);
    1876           12 :     ACL_LOG_INFO("start to execute aclrtMemcpyBatchAsyncV2");
    1877           12 :     return MemcpyBatchImpl(
    1878              :         dsts, destMaxs, srcs, sizes, numBatches, attrs, attrsIndexes, numAttrs, nullptr, stream, true,
    1879           24 :         "aclrtMemcpyBatchAsyncV2");
    1880           12 : }
    1881              : 
    1882            4 : aclError aclrtIpcMemSetImportPidImpl(const char* key, int32_t* pid, size_t num)
    1883              : {
    1884            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemSetImportPid);
    1885            4 :     ACL_LOG_INFO("start to execute aclrtIpcMemSetImportPid, num is [%zu]", num);
    1886            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(key);
    1887            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(pid);
    1888            2 :     ACL_REQUIRES_RTS_OK(rtsIpcMemSetImportPid(key, pid, static_cast<int32_t>(num)));
    1889            1 :     return ACL_SUCCESS;
    1890            4 : }
    1891              : 
    1892            2 : aclError aclrtIpcMemSetAttrImpl(const char* key, aclrtIpcMemAttrType type, uint64_t attr)
    1893              : {
    1894            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemSetAttr);
    1895            2 :     ACL_LOG_INFO(
    1896              :         "start to execute aclrtIpcMemSetAttr, type is [%s], attr is [%lu]", acl::GetIpcMemAttrTypeDesc(type), attr);
    1897            2 :     const auto rtErr = rtIpcSetMemoryAttr(key, type, attr);
    1898            2 :     if (rtErr == RT_ERROR_NONE) {
    1899            1 :         ACL_LOG_INFO("successfully execute aclrtIpcMemSetAttr");
    1900            1 :         return ACL_SUCCESS;
    1901              :     }
    1902              : 
    1903            1 :     if ((rtErr == ACL_ERROR_RT_LINK_TYPE_NOT_SUPPORTED) || (rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT)) {
    1904            0 :         ACL_LOG_WARN("call rtIpcSetMemoryAttr failed, runtime result = %d.", static_cast<int32_t>(rtErr));
    1905            0 :     } else {
    1906            1 :         ACL_LOG_CALL_ERROR("call rtIpcSetMemoryAttr failed, runtime result = %d.", static_cast<int32_t>(rtErr));
    1907              :     }
    1908            1 :     return ACL_GET_ERRCODE_RTS(rtErr);
    1909            2 : }
    1910              : 
    1911            2 : aclError aclrtIpcMemImportPidInterServerImpl(const char* key, aclrtServerPid* serverPids, size_t num)
    1912              : {
    1913            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtIpcMemImportPidInterServer);
    1914            2 :     ACL_LOG_INFO("start to execute aclrtIpcMemImportPidInterServer, num is [%zu]", num);
    1915            2 :     ACL_REQUIRES_RTS_OK(rtIpcMemImportPidInterServer(key, reinterpret_cast<const rtServerPid*>(serverPids), num));
    1916            1 :     ACL_LOG_INFO("successfully execute aclrtIpcMemImportPidInterServer");
    1917            1 :     return ACL_SUCCESS;
    1918            2 : }
    1919              : 
    1920            1 : aclError aclrtCheckMemTypeImpl(
    1921              :     void** addrList, uint32_t size, uint32_t memType, uint32_t* checkResult, uint32_t reserve)
    1922              : {
    1923            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCheckMemType);
    1924            1 :     ACL_LOG_INFO(
    1925              :         "start to execute AclrtCheckMemType, size is [%u], memType is [%u], reserve is [%u]", size, memType, reserve);
    1926            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(addrList);
    1927            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(checkResult);
    1928            1 :     ACL_REQUIRES_RTS_OK(rtsCheckMemType(addrList, size, memType, checkResult, reserve));
    1929            1 :     return ACL_SUCCESS;
    1930            1 : }
    1931              : 
    1932            3 : aclError aclrtDevicePeerAccessStatusImpl(int32_t deviceId, int32_t peerDeviceId, int32_t* status)
    1933              : {
    1934            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtDevicePeerAccessStatus);
    1935            3 :     ACL_LOG_INFO("start to execute aclrtDevicePeerAccessStatus");
    1936            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(status);
    1937            2 :     ACL_REQUIRES_RTS_OK(rtsGetP2PStatus(
    1938              :         static_cast<uint32_t>(deviceId), static_cast<uint32_t>(peerDeviceId), reinterpret_cast<uint32_t*>(status)));
    1939            1 :     ACL_LOG_INFO("successfully execute aclrtDevicePeerAccessStatus");
    1940            1 :     return ACL_SUCCESS;
    1941            3 : }
    1942              : 
    1943            2 : aclError aclrtCmoGetDescSizeImpl(size_t* size)
    1944              : {
    1945            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCmoGetDescSize);
    1946            2 :     ACL_LOG_DEBUG("start to execute aclrtCmoGetDescSize");
    1947            2 :     ACL_REQUIRES_RTS_OK(rtsGetCmoDescSize(size));
    1948            1 :     ACL_LOG_INFO("successfully execute aclrtCmoGetDescSize");
    1949            1 :     return ACL_SUCCESS;
    1950            2 : }
    1951              : 
    1952            2 : aclError aclrtCmoSetDescImpl(void* cmoDesc, void* src, size_t size)
    1953              : {
    1954            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCmoSetDesc);
    1955            2 :     ACL_LOG_DEBUG("start to execute aclrtCmoSetDesc, memLen =%zu", size);
    1956            2 :     ACL_REQUIRES_RTS_OK(rtsSetCmoDesc(cmoDesc, src, size));
    1957            1 :     ACL_LOG_INFO("successfully execute aclrtCmoSetDesc");
    1958            1 :     return ACL_SUCCESS;
    1959            2 : }
    1960              : 
    1961            2 : static rtCmoOpCode ConvertCmoType(aclrtCmoType cmoType)
    1962              : {
    1963            2 :     constexpr uint32_t offset =
    1964              :         static_cast<uint32_t>(RT_CMO_PREFETCH) - static_cast<uint32_t>(ACL_RT_CMO_TYPE_PREFETCH);
    1965            2 :     return static_cast<rtCmoOpCode>(static_cast<uint32_t>(cmoType) + offset);
    1966              : }
    1967              : 
    1968            2 : aclError aclrtCmoAsyncWithDescImpl(void* cmoDesc, aclrtCmoType cmoType, aclrtStream stream, const void* reserve)
    1969              : {
    1970            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsyncWithDesc);
    1971            2 :     ACL_LOG_DEBUG("start to execute aclrtCmoAsyncWithDesc");
    1972            2 :     const rtCmoOpCode rtCmoType = ConvertCmoType(cmoType);
    1973            2 :     ACL_REQUIRES_RTS_OK(rtsLaunchCmoAddrTask(cmoDesc, stream, rtCmoType, reserve));
    1974            1 :     ACL_LOG_INFO("successfully execute aclrtCmoAsyncWithDesc");
    1975            1 :     return ACL_SUCCESS;
    1976            2 : }
    1977              : 
    1978            4 : aclError aclrtMemSetAccessImpl(void* virPtr, size_t size, aclrtMemAccessDesc* desc, size_t count)
    1979              : {
    1980            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetAccess);
    1981            4 :     ACL_LOG_INFO("start to execute aclrtMemSetAccess");
    1982              : 
    1983            4 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    1984              :         rtMemSetAccess(virPtr, size, reinterpret_cast<rtMemAccessDesc*>(desc), count), rtMemSetAccess);
    1985            2 :     ACL_LOG_INFO("successfully execute aclrtMemSetAccess");
    1986            2 :     return ACL_SUCCESS;
    1987            4 : }
    1988              : 
    1989            3 : aclError aclrtMemRetainAllocationHandleImpl(void* virPtr, aclrtDrvMemHandle* handle)
    1990              : {
    1991            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemRetainAllocationHandle);
    1992            3 :     ACL_LOG_DEBUG("start to execute aclrtMemRetainAllocationHandle");
    1993            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    1994            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    1995              : 
    1996            2 :     ACL_REQUIRES_RTS_OK(rtMemRetainAllocationHandle(virPtr, reinterpret_cast<rtDrvMemHandle*>(handle)));
    1997            1 :     return ACL_SUCCESS;
    1998            3 : }
    1999              : 
    2000              : // initialize the mapping table
    2001              : static const MemAttrMapping mapping[]{
    2002              :     {HUGE_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_HUGE},
    2003              :     {NORMAL_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_NORMAL},
    2004              :     {HUGE1G_PAGE_TYPE, HBM_TYPE, true, ACL_HBM_MEM_HUGE1G},
    2005              :     {NORMAL_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_DDR_MEM_P2P_NORMAL},
    2006              :     {NORMAL_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_NORMAL},
    2007              :     {HUGE_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_HUGE},
    2008              :     {HUGE1G_PAGE_TYPE, DDR_TYPE, true, ACL_MEM_HUGE1G},
    2009              :     {HUGE_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_MEM_P2P_HUGE},
    2010              :     {HUGE1G_PAGE_TYPE, P2P_DDR_TYPE, true, ACL_MEM_P2P_HUGE1G},
    2011              :     {NORMAL_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_NORMAL},
    2012              :     {HUGE_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_HUGE},
    2013              :     {HUGE1G_PAGE_TYPE, HBM_TYPE, false, ACL_HBM_MEM_HUGE1G},
    2014              :     {NORMAL_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_NORMAL},
    2015              :     {HUGE_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_HUGE},
    2016              :     {HUGE1G_PAGE_TYPE, DDR_TYPE, false, ACL_MEM_HUGE1G},
    2017              :     {NORMAL_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_NORMAL},
    2018              :     {HUGE_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_HUGE},
    2019              :     {HUGE1G_PAGE_TYPE, P2P_HBM_TYPE, false, ACL_MEM_P2P_HUGE1G}};
    2020              : 
    2021            5 : aclError aclrtMemGetAllocationPropertiesFromHandleImpl(aclrtDrvMemHandle handle, aclrtPhysicalMemProp* prop)
    2022              : {
    2023            5 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAllocationPropertiesFromHandle);
    2024            5 :     ACL_LOG_DEBUG("start to execute AclrtMemGetAllocationPropertiesFromHandle");
    2025            5 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    2026            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prop);
    2027              : 
    2028            4 :     rtDrvMemProp_t rtProp = {};
    2029            4 :     ACL_REQUIRES_RTS_OK(rtMemGetAllocationPropertiesFromHandle(reinterpret_cast<rtDrvMemHandle>(handle), &rtProp));
    2030              : 
    2031            3 :     prop->handleType = ACL_MEM_HANDLE_TYPE_NONE;
    2032            3 :     prop->allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED;
    2033            3 :     if (rtProp.side == DRV_MEM_HOST_NUMA_SIDE) {
    2034              :         // convert drv side to acl locationtype
    2035            0 :         prop->location.type = ACL_MEM_LOCATION_TYPE_HOST_NUMA;
    2036              :     } else {
    2037            3 :         prop->location.type = static_cast<aclrtMemLocationType>(rtProp.side);
    2038              :     }
    2039            3 :     prop->location.id = rtProp.devid;
    2040            3 :     prop->reserve = rtProp.reserve;
    2041              : 
    2042              :     // host alloc
    2043            3 :     const bool isHostAlloc =
    2044            3 :         (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST) || (prop->location.type == ACL_MEM_LOCATION_TYPE_HOST_NUMA);
    2045              : 
    2046              :     const auto& it =
    2047            9 :         std::find_if(std::begin(mapping), std::end(mapping), [rtProp, isHostAlloc](const MemAttrMapping& entry) {
    2048            9 :             return (entry.pgType == rtProp.pg_type) && (entry.memType == rtProp.mem_type) &&
    2049            9 :                    (entry.isHostAlloc == isHostAlloc);
    2050            3 :         });
    2051            6 :     if (it != std::end(mapping)) {
    2052            3 :         prop->memAttr = it->memAttr;
    2053              :     } else {
    2054            0 :         ACL_LOG_ERROR(
    2055              :             "memAttr not found for pg_type=%u, mem_type=%u, isHostAlloc=%u", rtProp.pg_type, rtProp.mem_type,
    2056              :             isHostAlloc);
    2057            0 :         return ACL_ERROR_INVALID_PARAM;
    2058              :     }
    2059            3 :     return ACL_SUCCESS;
    2060            5 : }
    2061              : 
    2062            5 : aclError aclrtReserveMemAddressNoUCMemoryImpl(
    2063              :     void** virPtr, size_t size, size_t alignment, void* expectPtr, uint64_t flags)
    2064              : {
    2065            5 :     ACL_PROFILING_REG(acl::AclProfType::AclrtReserveMemAddressNoUCMemory);
    2066            5 :     ACL_LOG_DEBUG("start to execute aclrtReserveMemAddressNoUCMemory, size = %zu", size);
    2067            5 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtr);
    2068              : 
    2069            8 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    2070              :     // flags参数取1,为了早期接口兼容性保留
    2071            7 :     ACL_CHECK_INVALID_VALUE_WITH_EXPECT((flags == 0ULL) || (flags == 1ULL), flags, "0");
    2072              : 
    2073            3 :     flags = flags | FLAG_START_DYNAMIC_ALLOC_MEM; // bit 9置1
    2074            3 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    2075              :         rtReserveMemAddress(virPtr, size, alignment, expectPtr, flags), rtReserveMemAddress);
    2076            1 :     return ACL_SUCCESS;
    2077            5 : }
    2078              : 
    2079            3 : aclError aclrtMemGetAddressRangeImpl(void* ptr, void** pbase, size_t* psize)
    2080              : {
    2081            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAddressRange);
    2082            3 :     ACL_LOG_DEBUG("start to execute aclrtMemGetAddressRange");
    2083            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
    2084            2 :     ACL_REQUIRES_RTS_OK(rtMemGetAddressRange(ptr, pbase, psize));
    2085            1 :     ACL_LOG_INFO("successfully execute aclrtMemGetAddressRange");
    2086            1 :     return ACL_SUCCESS;
    2087            3 : }
    2088              : 
    2089            6 : aclError aclrtMemP2PMapImpl(void* devPtr, size_t size, int32_t dstDevId, uint64_t flags)
    2090              : {
    2091            6 :     ACL_PROFILING_REG(acl::AclProfType::aclrtMemP2PMap);
    2092            6 :     ACL_LOG_INFO("start to execute aclrtMemP2PMap");
    2093            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
    2094            8 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    2095            7 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
    2096            3 :     uint32_t phyId = 0U;
    2097            3 :     ACL_REQUIRES_RTS_OK(rtGetDevicePhyIdByIndex(static_cast<uint32_t>(dstDevId), &phyId));
    2098            2 :     ACL_REQUIRES_RTS_OK(rtMemPrefetchToDevice(devPtr, size, phyId));
    2099            1 :     ACL_LOG_INFO("successfully execute aclrtMemP2PMap");
    2100            1 :     return ACL_SUCCESS;
    2101            6 : }
    2102              : 
    2103            3 : aclError aclrtMemPoolCreateImpl(aclrtMemPool* memPool, const aclrtMemPoolProps* poolProps)
    2104              : {
    2105            3 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolCreate);
    2106            3 :     ACL_LOG_INFO("start to execute aclrtMemPoolCreate.");
    2107            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2108            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(poolProps);
    2109              : 
    2110            3 :     ACL_CHECK_INVALID_VALUE_WITH_DESC(
    2111              :         poolProps->allocType == aclrtMemAllocationType::ACL_MEM_ALLOCATION_TYPE_PINNED,
    2112              :         acl::GetMemAllocationTypeDesc(poolProps->allocType), "poolProps->allocType", "MEM_ALLOCATION_TYPE_PINNED(0)",
    2113              :         ACL_ERROR_INVALID_PARAM);
    2114              : 
    2115            6 :     ACL_CHECK_INVALID_VALUE_WITH_DESC(
    2116              :         poolProps->location.type == aclrtMemLocationType::ACL_MEM_LOCATION_TYPE_DEVICE,
    2117              :         acl::GetMemLocationTypeDesc(poolProps->location.type), "poolProps->location.type",
    2118              :         "MEM_LOCATION_TYPE_DEVICE(1)", ACL_ERROR_INVALID_PARAM);
    2119              : 
    2120              :     rtMemPoolProps rtPoolProps;
    2121            2 :     rtPoolProps.side = ACL_MEM_LOCATION_TYPE_DEVICE;
    2122            2 :     rtPoolProps.devId = poolProps->location.id;
    2123            2 :     rtPoolProps.handleType = static_cast<rtDrvMemHandleType>(poolProps->handleType);
    2124            2 :     rtPoolProps.maxSize = poolProps->maxSize;
    2125            2 :     rtPoolProps.reserve = 0;
    2126              : 
    2127            2 :     uint8_t zeros[sizeof(poolProps->reserved)] = {0};
    2128            2 :     ACL_CHECK_INVALID_PARAM_NO_VALUE(
    2129              :         memcmp(poolProps->reserved, zeros, sizeof(poolProps->reserved)) == 0, "poolProps->reserved",
    2130              :         "poolProps->reserved is a reserved parameter and must be nullptr");
    2131              : 
    2132            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolCreate(reinterpret_cast<rtMemPool_t*>(memPool), &rtPoolProps));
    2133            1 :     return ACL_SUCCESS;
    2134            3 : }
    2135              : 
    2136            1 : aclError aclrtMemPoolDestroyImpl(const aclrtMemPool memPool)
    2137              : {
    2138            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolDestroy);
    2139            1 :     ACL_LOG_INFO("start to execute aclrtMemPoolDestroy.");
    2140            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2141              : 
    2142            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolDestroy(static_cast<rtMemPool_t>(memPool)));
    2143            1 :     return ACL_SUCCESS;
    2144            1 : }
    2145              : 
    2146            1 : aclError aclrtMemPoolSetAttrImpl(aclrtMemPool memPool, aclrtMemPoolAttr attr, void* value)
    2147              : {
    2148            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolSetAttr);
    2149            1 :     ACL_LOG_INFO("start to execute aclrtMemPoolSetAttr.");
    2150            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2151              : 
    2152            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolSetAttr(static_cast<rtMemPool_t>(memPool), static_cast<rtMemPoolAttr>(attr), value));
    2153            1 :     return ACL_SUCCESS;
    2154            1 : }
    2155              : 
    2156            1 : aclError aclrtMemPoolGetAttrImpl(aclrtMemPool memPool, aclrtMemPoolAttr attr, void* value)
    2157              : {
    2158            1 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolGetAttr);
    2159            1 :     ACL_LOG_INFO("start to execute aclrtMemPoolGetAttr.");
    2160            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2161              : 
    2162            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolGetAttr(static_cast<rtMemPool_t>(memPool), static_cast<rtMemPoolAttr>(attr), value));
    2163            1 :     return ACL_SUCCESS;
    2164            1 : }
    2165              : 
    2166            2 : aclError aclrtMemPoolMallocAsyncImpl(void** ptr, size_t size, aclrtMemPool memPool, aclrtStream stream)
    2167              : {
    2168            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolMallocAsync);
    2169            2 :     ACL_LOG_INFO("Start to execute aclrtMemPoolMallocAsync.");
    2170            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
    2171            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2172            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);
    2173            2 :     if (size == 0) {
    2174            1 :         return ACL_SUCCESS;
    2175              :     }
    2176              : 
    2177            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolMallocAsync(ptr, size, memPool, stream));
    2178              : 
    2179            1 :     return ACL_SUCCESS;
    2180            2 : }
    2181              : 
    2182            2 : aclError aclrtMemPoolFreeAsyncImpl(void* ptr, aclrtStream stream)
    2183              : {
    2184            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolFreeAsync);
    2185            2 :     ACL_LOG_INFO("Start to execute aclrtMemPoolFreeAsync.");
    2186            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
    2187              : 
    2188            1 :     ACL_REQUIRES_RTS_OK(rtMemPoolFreeAsync(ptr, stream));
    2189            1 :     return ACL_SUCCESS;
    2190            2 : }
    2191              : 
    2192            2 : aclError aclrtMemPoolTrimToImpl(aclrtMemPool memPool, size_t minBytesToKeep)
    2193              : {
    2194            2 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemPoolTrimTo);
    2195            2 :     ACL_LOG_INFO("Start to execute aclrtMemPoolTrimTo.");
    2196            2 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(memPool);
    2197              : 
    2198            2 :     ACL_REQUIRES_RTS_OK(rtMemPoolTrimTo(static_cast<rtMemPool_t>(memPool), minBytesToKeep));
    2199            1 :     return ACL_SUCCESS;
    2200            2 : }
    2201              : 
    2202            9 : static rtMemManagedLocationType ConvertMemManagedLocationType(aclrtMemManagedLocationType const locationType)
    2203              : {
    2204            9 :     switch (locationType) {
    2205            2 :         case ACL_MEM_LOCATIONTYPE_DEVICE:
    2206            2 :             return rtMemLocationTypeDevice;
    2207            3 :         case ACL_MEM_LOCATIONTYPE_HOST:
    2208            3 :             return rtMemLocationTypeHost;
    2209            2 :         case ACL_MEM_LOCATIONTYPE_HOST_NUMA:
    2210            2 :             return rtMemLocationTypeHostNuma;
    2211            2 :         case ACL_MEM_LOCATIONTYPE_HOST_NUMA_CURRENT:
    2212            2 :             return rtMemLocationTypeHostNumaCurrent;
    2213            0 :         default:
    2214            0 :             return rtMemLocationTypeInvalid;
    2215              :     }
    2216              : }
    2217              : 
    2218            4 : aclError aclrtMemManagedPrefetchAsyncImpl(
    2219              :     const void* ptr, size_t size, aclrtMemManagedLocation location, uint32_t flags, aclrtStream stream)
    2220              : {
    2221            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedPrefetchAsync);
    2222            4 :     ACL_LOG_DEBUG("start to execute aclrtMemManagedPrefetchAsync");
    2223            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
    2224            6 :     ACL_REQUIRES_POSITIVE_REPORT(size);
    2225            5 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
    2226            1 :     rtMemManagedLocation uvmLocation = {ConvertMemManagedLocationType(location.type), location.id};
    2227            1 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    2228              :         rtMemManagedPrefetchAsync(ptr, size, uvmLocation, flags, static_cast<rtStream_t>(stream)),
    2229              :         rtMemManagedPrefetchAsync);
    2230            1 :     return ACL_SUCCESS;
    2231            4 : }
    2232              : 
    2233            9 : aclError aclrtMemManagedPrefetchBatchAsyncImpl(
    2234              :     const void** ptrs, size_t* sizes, size_t count, aclrtMemManagedLocation* prefetchLocs, size_t* prefetchLocIdxs,
    2235              :     size_t numPrefetchLocs, uint64_t flags, aclrtStream stream)
    2236              : {
    2237            9 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemManagedPrefetchBatchAsync);
    2238            9 :     ACL_LOG_DEBUG("start to execute aclrtMemManagedPrefetchBatchAsync");
    2239            9 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptrs);
    2240            8 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(sizes);
    2241            7 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prefetchLocs);
    2242            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(prefetchLocIdxs);
    2243              : 
    2244            9 :     ACL_REQUIRES_POSITIVE_REPORT(count);
    2245            8 :     ACL_REQUIRES_POSITIVE_REPORT(numPrefetchLocs);
    2246            7 :     ACL_CHECK_RESERVED_PARAM_REPORT_RET(flags, 0, ACL_ERROR_INVALID_PARAM);
    2247            3 :     if (count < numPrefetchLocs) {
    2248            1 :         ACL_LOG_ERROR("[Check][PARAM]count must be greater than or equal to numPrefetchLocs");
    2249            1 :         const std::string countVal = std::to_string(count);
    2250              :         std::string errMsg =
    2251            1 :             acl::AclErrorLogManager::FormatStr("must be greater than or equal to numPrefetchLocs %zu", numPrefetchLocs);
    2252            1 :         std::string funcName = acl::AclErrorLogManager::GetFuncNameWithoutImplSuffix(__func__);
    2253            1 :         acl::AclErrorLogManager::ReportInputError(
    2254            2 :             acl::INVALID_PARAM_REASON_MSG, std::vector<const char*>({"func", "value", "param", "reason"}),
    2255            2 :             std::vector<const char*>({funcName.c_str(), countVal.c_str(), "count", errMsg.c_str()}));
    2256            1 :         return ACL_ERROR_INVALID_PARAM;
    2257            1 :     }
    2258              : 
    2259            2 :     rtMemManagedLocation* uvmPrefetchLocs = new (std::nothrow) rtMemManagedLocation[numPrefetchLocs];
    2260            2 :     ACL_CHECK_MALLOC_RESULT_REPORT_RET(
    2261              :         uvmPrefetchLocs, sizeof(rtMemManagedLocation) * numPrefetchLocs, "new", ACL_ERROR_BAD_ALLOC);
    2262              : 
    2263           10 :     for (size_t numPrefetchIdx = 0; numPrefetchIdx < numPrefetchLocs; numPrefetchIdx++) {
    2264            8 :         uvmPrefetchLocs[numPrefetchIdx].id = prefetchLocs[numPrefetchIdx].id;
    2265            8 :         uvmPrefetchLocs[numPrefetchIdx].type = ConvertMemManagedLocationType(prefetchLocs[numPrefetchIdx].type);
    2266              :     }
    2267              : 
    2268            2 :     const rtError_t rtErr = rtMemManagedPrefetchBatchAsync(
    2269              :         ptrs, sizes, count, uvmPrefetchLocs, prefetchLocIdxs, numPrefetchLocs, flags, static_cast<rtStream_t>(stream));
    2270            2 :     ACL_DELETE_ARRAY_AND_SET_NULL(uvmPrefetchLocs);
    2271            2 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(rtErr, rtMemManagedPrefetchBatchAsync);
    2272            2 :     return ACL_SUCCESS;
    2273            9 : }
    2274              : 
    2275            4 : aclError aclrtGetSymbolAddressImpl(const void* symbol, void** devPtr)
    2276              : {
    2277            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetSymbolAddress);
    2278            4 :     ACL_LOG_DEBUG("start to execute aclrtGetSymbolAddress.");
    2279              : 
    2280            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(symbol);
    2281            3 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(devPtr);
    2282              : 
    2283            2 :     size_t size = 0UL;
    2284            2 :     ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, devPtr, &size));
    2285            1 :     return ACL_SUCCESS;
    2286            4 : }
    2287              : 
    2288            0 : aclError aclrtGetSymbolSizeImpl(const void* symbol, size_t* size)
    2289              : {
    2290            0 :     ACL_PROFILING_REG(acl::AclProfType::AclrtGetSymbolSize);
    2291            0 :     ACL_LOG_DEBUG("start to execute aclrtGetSymbolSize.");
    2292              : 
    2293            0 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(symbol);
    2294            0 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(size);
    2295              : 
    2296            0 :     void* devPtr = nullptr;
    2297            0 :     ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, &devPtr, size));
    2298            0 :     return ACL_SUCCESS;
    2299            0 : }
    2300              : 
    2301           22 : static aclError GetSymbolInfo(const void* symbol, size_t count, size_t offset, void** symbolAddr, size_t* symbolSize)
    2302              : {
    2303           22 :     *symbolAddr = nullptr;
    2304           22 :     *symbolSize = 0UL;
    2305           22 :     ACL_REQUIRES_RTS_OK(rtSymbolLookup(symbol, symbolAddr, symbolSize));
    2306              : 
    2307           18 :     size_t totalSize = 0UL;
    2308           18 :     ACL_CHECK_ASSIGN_SIZET_ADD(offset, count, totalSize);
    2309           14 :     if (totalSize > *symbolSize) {
    2310            6 :         ACL_LOG_ERROR(
    2311              :             "[Check][Offset]offset[%zu] + count[%zu] must be <= symbolSize[%zu].", offset, count, *symbolSize);
    2312            6 :         acl::AclErrorLogManager::ReportInputError(
    2313           12 :             acl::INVALID_PARAM_MSG, std::vector<const char*>({"param", "value", "reason"}),
    2314           12 :             std::vector<const char*>({"offset+count", std::to_string(totalSize).c_str(), "must be <= symbolSize"}));
    2315            6 :         return ACL_ERROR_INVALID_PARAM;
    2316              :     }
    2317              : 
    2318            8 :     return ACL_SUCCESS;
    2319              : }
    2320              : 
    2321           19 : static aclError CheckMemcpyFromSymbol(void* dst, const void* symbol, size_t count, size_t dstMax, aclrtMemcpyKind kind)
    2322              : {
    2323           19 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(symbol, "Checking the synchronous memory copy parameter");
    2324           19 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(dst, "Checking the synchronous memory copy parameter");
    2325              : 
    2326           17 :     if (count > dstMax) {
    2327            2 :         ACL_LOG_ERROR("[Check][Count]count[%zu] must not be greater than dstMax[%zu].", count, dstMax);
    2328            2 :         acl::AclErrorLogManager::ReportInputError(
    2329            4 :             acl::INVALID_PARAM_MSG, std::vector<const char*>({"param", "value", "reason"}),
    2330            4 :             std::vector<const char*>({"count", std::to_string(count).c_str(), "must not be greater than dstMax"}));
    2331            2 :         return ACL_ERROR_INVALID_PARAM;
    2332              :     }
    2333              : 
    2334           15 :     if ((kind != ACL_MEMCPY_DEVICE_TO_HOST) && (kind != ACL_MEMCPY_DEFAULT)) {
    2335            4 :         ACL_LOG_ERROR(
    2336              :             "[Check][Kind]kind[%s] only support ACL_MEMCPY_DEVICE_TO_HOST or ACL_MEMCPY_DEFAULT",
    2337              :             acl::GetMemcpyKindDesc(kind));
    2338            4 :         acl::AclErrorLogManager::ReportInputError(
    2339            8 :             acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
    2340            4 :             std::vector<const char*>(
    2341            8 :                 {"Checking the synchronous memory copy parameter", acl::GetMemcpyKindDesc(kind), "kind",
    2342            8 :                  "ACL_MEMCPY_DEVICE_TO_HOST or ACL_MEMCPY_DEFAULT"}));
    2343            4 :         return ACL_ERROR_INVALID_PARAM;
    2344              :     }
    2345              : 
    2346           11 :     return ACL_SUCCESS;
    2347              : }
    2348              : 
    2349           10 : aclError aclrtMemcpyFromSymbolImpl(
    2350              :     void* dst, size_t dstMax, const void* symbol, size_t count, size_t offset, aclrtMemcpyKind kind)
    2351              : {
    2352           10 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyFromSymbol);
    2353           10 :     ACL_LOG_DEBUG("start to execute aclrtMemcpyFromSymbol, count = %zu, offset = %zu.", count, offset);
    2354           10 :     if (count == 0) {
    2355            1 :         ACL_LOG_INFO("count is 0, no need to execute mem copy from symbol, just return success.");
    2356            1 :         return ACL_SUCCESS;
    2357              :     }
    2358            9 :     aclError ret = CheckMemcpyFromSymbol(dst, symbol, count, dstMax, kind);
    2359            9 :     if (ret != ACL_SUCCESS) {
    2360            4 :         return ret;
    2361              :     }
    2362              : 
    2363            5 :     void* symbolAddr = nullptr;
    2364            5 :     size_t symbolSize = 0UL;
    2365            5 :     ret = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
    2366            5 :     if (ret != ACL_SUCCESS) {
    2367            3 :         return ret;
    2368              :     }
    2369              : 
    2370            2 :     void* srcAddr = static_cast<void*>(static_cast<uint8_t*>(symbolAddr) + offset);
    2371            2 :     ACL_REQUIRES_RTS_OK(rtMemcpy(dst, dstMax, srcAddr, count, RT_MEMCPY_DEVICE_TO_HOST));
    2372            2 :     return ACL_SUCCESS;
    2373           10 : }
    2374              : 
    2375           11 : aclError aclrtMemcpyFromSymbolAsyncImpl(
    2376              :     void* dst, size_t dstMax, const void* symbol, size_t count, size_t offset, aclrtMemcpyKind kind, aclrtStream stream)
    2377              : {
    2378           11 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyFromSymbolAsync);
    2379           11 :     ACL_LOG_DEBUG("start to execute aclrtMemcpyFromSymbolAsync, count = %zu, offset = %zu.", count, offset);
    2380           11 :     if (count == 0) {
    2381            1 :         ACL_LOG_INFO("count is 0, no need to execute mem copy from symbol async, just return success.");
    2382            1 :         return ACL_SUCCESS;
    2383              :     }
    2384              : 
    2385           10 :     aclError aclErr = CheckMemcpyFromSymbol(dst, symbol, count, dstMax, kind);
    2386           10 :     if (aclErr != ACL_SUCCESS) {
    2387            4 :         return aclErr;
    2388              :     }
    2389              : 
    2390            6 :     void* symbolAddr = nullptr;
    2391            6 :     size_t symbolSize = 0UL;
    2392            6 :     aclErr = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
    2393            6 :     if (aclErr != ACL_SUCCESS) {
    2394            4 :         return aclErr;
    2395              :     }
    2396              : 
    2397            2 :     void* srcAddr = static_cast<void*>(static_cast<uint8_t*>(symbolAddr) + offset);
    2398            2 :     ACL_REQUIRES_RTS_OK(rtMemcpyAsync(dst, dstMax, srcAddr, count, RT_MEMCPY_DEVICE_TO_HOST, stream));
    2399            2 :     return ACL_SUCCESS;
    2400           11 : }
    2401              : 
    2402           17 : static aclError CheckMemcpyToSymbol(const void* symbol, const void* src, aclrtMemcpyKind kind)
    2403              : {
    2404           17 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(symbol, "Checking the synchronous memory copy parameter");
    2405           17 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT_AND_FUNC_DESC(src, "Checking the synchronous memory copy parameter");
    2406              : 
    2407           15 :     if ((kind != ACL_MEMCPY_HOST_TO_DEVICE) && (kind != ACL_MEMCPY_DEFAULT)) {
    2408            4 :         ACL_LOG_ERROR(
    2409              :             "[Check][Kind]kind[%s] only support ACL_MEMCPY_HOST_TO_DEVICE or ACL_MEMCPY_DEFAULT",
    2410              :             acl::GetMemcpyKindDesc(kind));
    2411            4 :         acl::AclErrorLogManager::ReportInputError(
    2412            8 :             acl::INVALID_VALUE_MSG, std::vector<const char*>({"func", "value", "param", "expect"}),
    2413            4 :             std::vector<const char*>(
    2414            8 :                 {"Checking the synchronous memory copy parameter", acl::GetMemcpyKindDesc(kind), "kind",
    2415            8 :                  "ACL_MEMCPY_HOST_TO_DEVICE or ACL_MEMCPY_DEFAULT"}));
    2416            4 :         return ACL_ERROR_INVALID_PARAM;
    2417              :     }
    2418           11 :     return ACL_SUCCESS;
    2419              : }
    2420              : 
    2421            9 : aclError aclrtMemcpyToSymbolImpl(const void* symbol, const void* src, size_t count, size_t offset, aclrtMemcpyKind kind)
    2422              : {
    2423            9 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyToSymbol);
    2424            9 :     ACL_LOG_DEBUG("start to execute aclrtMemcpyToSymbol, count = %zu, offset = %zu.", count, offset);
    2425            9 :     if (count == 0) {
    2426            1 :         ACL_LOG_INFO("count is 0, no need to execute mem copy to symbol, just return success.");
    2427            1 :         return ACL_SUCCESS;
    2428              :     }
    2429              : 
    2430            8 :     aclError ret = CheckMemcpyToSymbol(symbol, src, kind);
    2431            8 :     if (ret != ACL_SUCCESS) {
    2432            3 :         return ret;
    2433              :     }
    2434              : 
    2435            5 :     void* symbolAddr = nullptr;
    2436            5 :     size_t symbolSize = 0UL;
    2437            5 :     ret = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
    2438            5 :     if (ret != ACL_SUCCESS) {
    2439            3 :         return ret;
    2440              :     }
    2441              : 
    2442            2 :     void* dstAddr = static_cast<void*>(static_cast<uint8_t*>(symbolAddr) + offset);
    2443            2 :     ACL_REQUIRES_RTS_OK(rtMemcpy(dstAddr, symbolSize - offset, src, count, RT_MEMCPY_HOST_TO_DEVICE));
    2444            2 :     return ACL_SUCCESS;
    2445            9 : }
    2446              : 
    2447           10 : aclError aclrtMemcpyToSymbolAsyncImpl(
    2448              :     const void* symbol, const void* src, size_t count, size_t offset, aclrtMemcpyKind kind, aclrtStream stream)
    2449              : {
    2450           10 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyToSymbolAsync);
    2451           10 :     ACL_LOG_DEBUG("start to execute aclrtMemcpyToSymbolAsync, count = %zu, offset = %zu.", count, offset);
    2452           10 :     if (count == 0) {
    2453            1 :         ACL_LOG_INFO("count is 0, no need to execute mem copy to symbol async, just return success.");
    2454            1 :         return ACL_SUCCESS;
    2455              :     }
    2456              : 
    2457            9 :     aclError aclErr = CheckMemcpyToSymbol(symbol, src, kind);
    2458            9 :     if (aclErr != ACL_SUCCESS) {
    2459            3 :         return aclErr;
    2460              :     }
    2461              : 
    2462            6 :     void* symbolAddr = nullptr;
    2463            6 :     size_t symbolSize = 0UL;
    2464            6 :     aclErr = GetSymbolInfo(symbol, count, offset, &symbolAddr, &symbolSize);
    2465            6 :     if (aclErr != ACL_SUCCESS) {
    2466            4 :         return aclErr;
    2467              :     }
    2468              : 
    2469            2 :     void* dstAddr = static_cast<void*>(static_cast<uint8_t*>(symbolAddr) + offset);
    2470            2 :     ACL_REQUIRES_RTS_OK(rtMemcpyAsync(dstAddr, symbolSize - offset, src, count, RT_MEMCPY_HOST_TO_DEVICE, stream));
    2471            2 :     return ACL_SUCCESS;
    2472           10 : }
    2473              : 
    2474            7 : aclError aclrtMemMapSelectedLinkImpl(void* virPtrDst, size_t size, void* virPtrSrc, uint32_t linkIdx)
    2475              : {
    2476            7 :     ACL_PROFILING_REG(acl::AclProfType::AclrtMemMapSelectedLink);
    2477            7 :     ACL_LOG_INFO("start to execute aclrtMemMapSelectedLink.");
    2478            7 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtrDst);
    2479            6 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(virPtrSrc);
    2480            5 :     if (size == 0UL) {
    2481            1 :         ACL_LOG_ERROR("size is [%zu], size must be greater than 0", size);
    2482            1 :         acl::AclErrorLogManager::ReportInputError(
    2483            2 :             acl::INVALID_PARAM_MSG, std::vector<const char*>({"param", "value", "reason"}),
    2484            2 :             std::vector<const char*>({"size", std::to_string(size).c_str(), "size must be greater than 0"}));
    2485            1 :         return ACL_ERROR_INVALID_PARAM;
    2486              :     }
    2487            4 :     if (linkIdx > ACL_RT_MEM_LINK_IDX_1) {
    2488            1 :         ACL_LOG_ERROR("linkIdx is [%u], linkIdx in aclrtMemMapSelectedLink must be 0 or 1", linkIdx);
    2489            1 :         acl::AclErrorLogManager::ReportInputError(
    2490            2 :             acl::INVALID_PARAM_MSG, std::vector<const char*>({"param", "value", "reason"}),
    2491            1 :             std::vector<const char*>(
    2492            2 :                 {"linkIdx", std::to_string(linkIdx).c_str(), "linkIdx in aclrtMemMapSelectedLink must be 0 or 1"}));
    2493            1 :         return ACL_ERROR_INVALID_PARAM;
    2494              :     }
    2495              : 
    2496            3 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    2497              :         rtMemMapSelectedLink(virPtrDst, size, virPtrSrc, linkIdx), rtMemMapSelectedLink);
    2498            1 :     ACL_LOG_INFO("successfully execute aclrtMemMapSelectedLink");
    2499            1 :     return ACL_SUCCESS;
    2500            7 : }
    2501              : 
    2502            5 : aclError aclrtMemMapSetLinkImpl(aclrtDrvMemHandle handle, aclrtMemLinkType adviceLink)
    2503              : {
    2504            5 :     ACL_LOG_INFO("start to execute aclrtMemMapSetLink, adviceLink=%s.", acl::GetMemLinkTypeDesc(adviceLink));
    2505            5 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(handle);
    2506            4 :     const auto rtErr = rtMemMapSetLink(static_cast<rtDrvMemHandle>(handle), static_cast<rtMemLinkType>(adviceLink));
    2507            4 :     if (rtErr == RT_ERROR_NONE) {
    2508            1 :         ACL_LOG_INFO("successfully execute aclrtMemMapSetLink.");
    2509            1 :         return ACL_SUCCESS;
    2510              :     }
    2511              : 
    2512            3 :     if ((rtErr == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) || (rtErr == ACL_ERROR_RT_LINK_TYPE_NOT_SUPPORTED)) {
    2513            2 :         ACL_LOG_WARN("call aclrtMemMapSetLink failed, runtime result = %d.", rtErr);
    2514            2 :     } else {
    2515            1 :         ACL_LOG_CALL_ERROR("call aclrtMemMapSetLink failed, runtime result = %d.", rtErr);
    2516              :     }
    2517            3 :     return ACL_GET_ERRCODE_RTS(rtErr);
    2518              : }
    2519              : 
    2520            4 : aclError aclrtHostGetDevicePointerAddrRangeImpl(aclrtAddrRange* addrRange, uint32_t* count)
    2521              : {
    2522            4 :     ACL_PROFILING_REG(acl::AclProfType::AclrtHostGetDevicePointerAddrRange);
    2523            4 :     ACL_LOG_DEBUG("start to execute aclrtHostGetDevicePointerAddrRange");
    2524            4 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(count);
    2525            3 :     ACL_REQUIRES_RTS_OK_WARN_NOT_SUPPORT(
    2526              :         rtHostGetDevicePointerAddrRange(reinterpret_cast<rtAddrRange*>(addrRange), count),
    2527              :         rtHostGetDevicePointerAddrRange);
    2528            2 :     ACL_LOG_INFO("successfully execute aclrtHostGetDevicePointerAddrRange");
    2529            2 :     return ACL_SUCCESS;
    2530            4 : }
    2531              : #ifdef __cplusplus
    2532              : }
    2533              : #endif // __cplusplus
        

Generated by: LCOV version 2.0-1