LCOV - code coverage report
Current view: top level - src/acl_stub - ascendc_acl_stub.cpp (source / functions) Coverage Total Hit
Test: coverage.info_filtered Lines: 98.6 % 143 141
Test Date: 2026-07-27 14:41:20 Functions: 100.0 % 34 34

            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              : /*!
      12              :  * \file ascendc_acl_stub.cpp
      13              :  * \brief
      14              :  */
      15              : #ifdef ASCENDC_CPU_DEBUG
      16              : #include <link.h>
      17              : #include <string.h>
      18              : #include "securec.h"
      19              : #include "acl/acl.h"
      20              : #include "acl/acl_prof.h"
      21              : #include "stub_def.h"
      22              : #include "kernel_fp16.h"
      23              : #include "kernel_elf_parser.h"
      24              : 
      25              : #ifdef __cplusplus
      26              : extern "C" {
      27              : #endif
      28              : 
      29            4 : aclError aclprofInit(const char* profilerResultPath, size_t length)
      30              : {
      31              :     (void)profilerResultPath;
      32              :     (void)length;
      33            4 :     return ACL_SUCCESS;
      34              : }
      35              : 
      36            4 : aclError aclprofSetConfig(aclprofConfigType configType, const char* config, size_t configLength)
      37              : {
      38              :     (void)configType;
      39              :     (void)config;
      40              :     (void)configLength;
      41            4 :     return ACL_SUCCESS;
      42              : }
      43              : 
      44            4 : aclError aclprofStart(const aclprofConfig* profilerConfig)
      45              : {
      46              :     (void)profilerConfig;
      47            4 :     return ACL_SUCCESS;
      48              : }
      49              : 
      50            4 : aclError aclprofStop(const aclprofConfig* profilerConfig)
      51              : {
      52              :     (void)profilerConfig;
      53            4 :     return ACL_SUCCESS;
      54              : }
      55              : 
      56            4 : aclError aclprofFinalize() { return ACL_SUCCESS; }
      57              : 
      58            4 : aclError aclInit(const char* configPath)
      59              : {
      60              :     (void)configPath;
      61            4 :     return ACL_SUCCESS;
      62              : }
      63              : 
      64            4 : aclError aclFinalize() { return ACL_SUCCESS; }
      65              : 
      66            4 : aclError aclrtGetVersion(int32_t* majorVersion, int32_t* minorVersion, int32_t* patchVersion)
      67              : {
      68              :     (void)majorVersion;
      69              :     (void)minorVersion;
      70              :     (void)patchVersion;
      71            4 :     return ACL_SUCCESS;
      72              : }
      73              : 
      74           52 : size_t aclDataTypeSize(aclDataType dataType)
      75              : {
      76           52 :     switch (dataType) {
      77            4 :         case ACL_FLOAT:
      78            4 :             return 4;
      79            4 :         case ACL_FLOAT16:
      80            4 :             return 2;
      81            4 :         case ACL_INT8:
      82            4 :             return 1;
      83            4 :         case ACL_INT16:
      84            4 :             return 2;
      85            4 :         case ACL_INT32:
      86            4 :             return 4;
      87            4 :         case ACL_INT64:
      88            4 :             return 8;
      89            4 :         case ACL_UINT8:
      90            4 :             return 1;
      91            4 :         case ACL_UINT16:
      92            4 :             return 2;
      93            4 :         case ACL_UINT32:
      94            4 :             return 4;
      95            4 :         case ACL_UINT64:
      96            4 :             return 8;
      97            4 :         case ACL_DOUBLE:
      98            4 :             return 8;
      99            4 :         case ACL_BOOL:
     100            4 :             return 1;
     101            4 :         default:
     102            4 :             return 0;
     103              :     }
     104              : }
     105              : 
     106           12 : float aclFloat16ToFloat(aclFloat16 value) { return static_cast<float>(half(value)); }
     107              : 
     108           12 : aclFloat16 aclFloatToFloat16(float value) { return static_cast<aclFloat16>(half(value)); }
     109              : 
     110            8 : aclError aclrtSetDevice(int32_t deviceId)
     111              : {
     112              :     (void)deviceId;
     113            8 :     return ACL_SUCCESS;
     114              : }
     115              : 
     116            4 : aclError aclrtResetDevice(int32_t deviceId)
     117              : {
     118              :     (void)deviceId;
     119            4 :     return ACL_SUCCESS;
     120              : }
     121              : 
     122            4 : aclError aclrtCreateStream(aclrtStream* stream)
     123              : {
     124              :     (void)stream;
     125            4 :     return ACL_SUCCESS;
     126              : }
     127              : 
     128            4 : aclError aclrtCreateStreamWithConfig(aclrtStream* stream, uint32_t priority, uint32_t flag)
     129              : {
     130              :     (void)priority;
     131              :     (void)flag;
     132              :     (void)stream;
     133            4 :     return ACL_SUCCESS;
     134              : }
     135              : 
     136            4 : aclError aclrtDestroyStream(aclrtStream stream)
     137              : {
     138              :     (void)stream;
     139            4 :     return ACL_SUCCESS;
     140              : }
     141              : 
     142            4 : aclError aclrtDestroyStreamForce(aclrtStream stream)
     143              : {
     144              :     (void)stream;
     145            4 :     return ACL_SUCCESS;
     146              : }
     147              : 
     148            4 : aclError aclrtSynchronizeStream(aclrtStream stream)
     149              : {
     150              :     (void)stream;
     151            4 :     return ACL_SUCCESS;
     152              : }
     153              : 
     154           12 : aclError aclrtMalloc(void** devPtr, size_t size, aclrtMemMallocPolicy policy)
     155              : {
     156              :     (void)policy;
     157           12 :     if (size == 0 || devPtr == nullptr) {
     158            8 :         printf("[ERROR] The parameter is invalid.\n");
     159            8 :         return ACL_ERROR_INVALID_PARAM;
     160              :     }
     161            4 :     *devPtr = AscendC::GmAlloc(size);
     162            4 :     return ACL_SUCCESS;
     163              : }
     164              : 
     165            8 : aclError aclrtFree(void* devPtr)
     166              : {
     167            8 :     if (devPtr == nullptr) {
     168            4 :         printf("[ERROR] The parameter is invalid.\n");
     169            4 :         return ACL_ERROR_INVALID_PARAM;
     170              :     }
     171            4 :     AscendC::GmFree(devPtr);
     172            4 :     return ACL_SUCCESS;
     173              : }
     174              : 
     175           12 : aclError aclrtMallocHost(void** hostPtr, size_t size)
     176              : {
     177           12 :     if (size == 0 || hostPtr == nullptr) {
     178            8 :         printf("[ERROR] The parameter is invalid.\n");
     179            8 :         return ACL_ERROR_INVALID_PARAM;
     180              :     }
     181            4 :     *hostPtr = AscendC::GmAlloc(size);
     182            4 :     return ACL_SUCCESS;
     183              : }
     184              : 
     185            8 : aclError aclrtFreeHost(void* hostPtr)
     186              : {
     187            8 :     if (hostPtr == nullptr) {
     188            4 :         printf("[ERROR] The parameter is invalid.\n");
     189            4 :         return ACL_ERROR_INVALID_PARAM;
     190              :     }
     191            4 :     AscendC::GmFree(hostPtr);
     192            4 :     return ACL_SUCCESS;
     193              : }
     194              : 
     195           24 : aclError aclrtMemset(void* devPtr, size_t maxCount, int32_t value, size_t count)
     196              : {
     197           24 :     if (devPtr == nullptr) {
     198            8 :         printf("[ERROR] The parameter is invalid.\n");
     199            8 :         return ACL_ERROR_INVALID_PARAM;
     200              :     }
     201           16 :     if (count > maxCount) {
     202            8 :         printf("[ERROR] The maxCount is %ld", maxCount);
     203              :     }
     204           16 :     auto ret = memset_s(devPtr, maxCount, value, count);
     205           16 :     if (ret != EOK) {
     206            8 :         return ACL_ERROR_BAD_ALLOC;
     207              :     }
     208            8 :     return ACL_SUCCESS;
     209              : }
     210              : 
     211           12 : aclError aclrtMemsetAsync(void* devPtr, size_t maxCount, int32_t value, size_t count, aclrtStream stream)
     212              : {
     213              :     (void)stream;
     214           12 :     return aclrtMemset(devPtr, maxCount, value, count);
     215              : }
     216              : 
     217            8 : aclError aclrtMemcpy(void* dst, size_t destMax, const void* src, size_t count, aclrtMemcpyKind kind)
     218              : {
     219              :     (void)kind;
     220            8 :     auto ret = memcpy_s(dst, destMax, src, count);
     221            8 :     if (ret != EOK) {
     222            4 :         return ACL_ERROR_BAD_ALLOC;
     223              :     }
     224            4 :     return ACL_SUCCESS;
     225              : }
     226              : 
     227            8 : aclError aclrtMemcpyAsync(
     228              :     void* dst, size_t destMax, const void* src, size_t count, aclrtMemcpyKind kind, aclrtStream stream)
     229              : {
     230              :     (void)kind;
     231              :     (void)stream;
     232            8 :     auto ret = memcpy_s(dst, destMax, src, count);
     233            8 :     if (ret != EOK) {
     234            4 :         return ACL_ERROR_BAD_ALLOC;
     235              :     }
     236            4 :     return ACL_SUCCESS;
     237              : }
     238              : 
     239           36 : aclError aclrtMemcpy2d(
     240              :     void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind)
     241              : {
     242              :     (void)kind;
     243           36 :     if (src == nullptr) {
     244            8 :         printf("[ERROR] The parameter is invalid.\n");
     245            8 :         return ACL_ERROR_INVALID_PARAM;
     246              :     }
     247           28 :     auto* dstBytes = static_cast<char*>(dst);
     248           28 :     const auto* srcBytes = static_cast<const char*>(src);
     249           72 :     for (size_t i = 0; i < height; ++i) {
     250           52 :         auto ret = memcpy_s(dstBytes + i * dpitch, dpitch, srcBytes + i * spitch, width);
     251           52 :         if (ret != EOK) {
     252            8 :             return ACL_ERROR_BAD_ALLOC;
     253              :         }
     254              :     }
     255           20 :     return ACL_SUCCESS;
     256              : }
     257              : 
     258           16 : aclError aclrtMemcpy2dAsync(
     259              :     void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, aclrtMemcpyKind kind,
     260              :     aclrtStream stream)
     261              : {
     262              :     (void)stream;
     263           16 :     return aclrtMemcpy2d(dst, dpitch, src, spitch, width, height, kind);
     264              : }
     265              : 
     266            4 : aclError aclrtCreateContext(aclrtContext* context, int32_t deviceId)
     267              : {
     268              :     (void)context;
     269              :     (void)deviceId;
     270            4 :     return ACL_SUCCESS;
     271              : }
     272              : 
     273            4 : aclError aclrtDestroyContext(aclrtContext context)
     274              : {
     275              :     (void)context;
     276            4 :     return ACL_SUCCESS;
     277              : }
     278              : 
     279           12 : aclError aclrtBinaryLoadFromData(
     280              :     const void* data, size_t length, const aclrtBinaryLoadOptions* options, aclrtBinHandle* binHandle)
     281              : {
     282              :     (void)options;
     283              :     (void)binHandle;
     284           12 :     if (data == nullptr || length == 0) {
     285            4 :         printf(
     286              :             "[ERROR] During binary load, get invalid elfData or data length, may be caused by a compilation error.\n");
     287            4 :         return ACL_ERROR_INVALID_PARAM;
     288              :     }
     289              : 
     290              :     try {
     291            8 :         AscendC::RegisterKernelElf(reinterpret_cast<const uint8_t*>(data), length);
     292            4 :     } catch (const std::exception& e) {
     293            4 :         printf("[ERROR] During binary load, failed to register kernel elf data: %s\n", e.what());
     294            4 :         return ACL_ERROR_INVALID_PARAM;
     295            4 :     }
     296            4 :     return ACL_SUCCESS;
     297              : }
     298              : 
     299            4 : aclError aclrtSynchronizeDevice() { return ACL_SUCCESS; }
     300              : #ifdef __cplusplus
     301              : }
     302              : #endif // __cplusplus
     303              : 
     304              : namespace {
     305           72 : static int callback(struct dl_phdr_info* info, size_t size, void* data)
     306              : {
     307              :     (void)size;
     308              :     (void)data;
     309           72 :     std::string soName = std::string(info->dlpi_name);
     310           72 :     if (soName.find("libascendcl.so") != std::string::npos) {
     311            0 :         printf(
     312              :             "[ERROR] The support of ACL interfaces in CPU debug is limited; Only the following list is supported currently.\n\
     313              :         [aclprofInit, aclprofSetConfig, aclprofStart, aclprofStop, aclprofFinalize, aclInit, aclFinalize,\n\
     314              :         aclrtGetVersion, aclDataTypeSize, aclFloat16ToFloat, aclFloatToFloat16, aclrtSetDevice, aclrtResetDevice,\n\
     315              :         aclrtCreateStream, aclrtCreateStreamWithConfig, aclrtDestroyStream, aclrtDestroyStreamForce, aclrtSynchronizeStream,\n\
     316              :         aclrtMalloc, aclrtFree, aclrtMallocHost, aclrtFreeHost, aclrtMemset, aclrtMemsetAsync, aclrtMemcpy, aclrtMemcpyAsync,\n\
     317              :         aclrtMemcpy2d, aclrtMemcpy2dAsync, aclrtCreateContext, aclrtDestroyContext, aclrtBinaryLoadFromData,\n\
     318              :         aclrtSynchronizeDevice]\n");
     319            0 :         abort();
     320              :     }
     321           72 :     return 0;
     322           72 : }
     323              : 
     324            4 : __attribute__((constructor)) void check_interface()
     325              : {
     326            4 :     printf("[%s][%s:%d]\n", __FILE__, __FUNCTION__, __LINE__);
     327            4 :     dl_iterate_phdr(callback, nullptr);
     328            4 : }
     329              : } // namespace
     330              : 
     331              : #endif // ASCENDC_CPU_DEBUG
        

Generated by: LCOV version 2.0-1