LCOV - code coverage report
Current view: top level - acl/aclrt_c/runtime - stream.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 91 91 100.0 %
Date: 2026-08-27 13:24:42 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3             :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4             :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5             :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6             :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7             :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8             :  * See LICENSE in the root of the software repository for the full text of the License.
       9             :  */
      10             : #include "acl/acl_rt.h"
      11             : #include "acl/acl_base.h"
      12             : #include "log_inner.h"
      13             : #include "runtime/rt.h"
      14             : #include "securec.h"
      15             : 
      16             : #ifdef __cplusplus
      17             : extern "C" {
      18             : #endif
      19             : 
      20           7 : aclrtStreamConfigHandle *aclrtCreateStreamConfigHandle(void) {
      21             :   aclrtStreamConfigHandle *configHandle =
      22           7 :       (aclrtStreamConfigHandle *)mmMalloc(sizeof(aclrtStreamConfigHandle));
      23           7 :   if (configHandle == NULL) {
      24           1 :     ACL_LOG_INNER_ERROR("malloc memory failed, create config handle failed.");
      25           1 :     return NULL;
      26             :   }
      27           6 :   memset_s(configHandle, sizeof(aclrtStreamConfigHandle), 0, sizeof(aclrtStreamConfigHandle));
      28           6 :   return configHandle;
      29             : }
      30             : 
      31           7 : aclError aclrtDestroyStreamConfigHandle(aclrtStreamConfigHandle *handle) {
      32           7 :   if (handle == NULL) {
      33           1 :     ACL_LOG_ERROR("handle is NULL");
      34           1 :     return ACL_ERROR_INVALID_PARAM;
      35             :   }
      36           6 :   mmFree(handle);
      37           6 :   handle = NULL;
      38           6 :   return ACL_SUCCESS;
      39             : }
      40             : 
      41             : typedef aclError (*SetStreamConfigFunc)(aclrtStreamConfigHandle *const,
      42             :                                         const void *const, const size_t);
      43             : 
      44             : typedef struct {
      45             :   aclrtStreamConfigAttr configAttr;
      46             :   SetStreamConfigFunc configParamFunc;
      47             : } SetStreamConfigParamFuncMap;
      48             : 
      49           4 : static aclError SetStreamPriority(aclrtStreamConfigHandle *const handle,
      50             :                            const void *const attrValue,
      51             :                            const size_t valueSize) {
      52             : #define ACL_RT_MIN_PRIORITY 0
      53             : #define ACL_RT_MAX_PRIORITY 7
      54           4 :   if (valueSize != sizeof(uint32_t)) {
      55           1 :     ACL_LOG_INNER_ERROR(
      56             :         "valueSize[%zu] is invalid, it should be %zu",
      57             :         valueSize, sizeof(uint32_t));
      58           1 :     return ACL_ERROR_INVALID_PARAM;
      59             :   }
      60           3 :   const uint32_t value = *(const uint32_t *)(attrValue);
      61           3 :   if (!(value <= ACL_RT_MAX_PRIORITY)) {
      62           1 :     ACL_LOG_INNER_ERROR(
      63             :         "value[%u] is invalid, it should be in [%d, %d]", value,
      64             :         ACL_RT_MIN_PRIORITY, ACL_RT_MAX_PRIORITY);
      65           1 :     return ACL_ERROR_INVALID_PARAM;
      66             :   }
      67           2 :   handle->priority = value;
      68           2 :   return ACL_SUCCESS;
      69             : }
      70             : 
      71           3 : static aclError SetStreamFlag(aclrtStreamConfigHandle *const handle,
      72             :                        const void *const attrValue, const size_t valueSize) {
      73           3 :   if (valueSize != sizeof(size_t)) {
      74           1 :     ACL_LOG_INNER_ERROR(
      75             :         "valueSize[%zu] is invalid, it should be %zu",
      76             :         valueSize, sizeof(size_t));
      77           1 :     return ACL_ERROR_INVALID_PARAM;
      78             :   }
      79           2 :   const size_t value = *(const size_t *)(attrValue);
      80           2 :   handle->flag = value;
      81           2 :   return ACL_SUCCESS;
      82             : }
      83             : 
      84           3 : static aclError SetStreamWorkPtr(aclrtStreamConfigHandle *const handle,
      85             :                           const void *const attrValue, const size_t valueSize) {
      86           3 :   if (valueSize != sizeof(void *)) {
      87           1 :     ACL_LOG_INNER_ERROR(
      88             :         "valueSize[%zu] is invalid, it should be %zu",
      89             :         valueSize, sizeof(void *));
      90           1 :     return ACL_ERROR_INVALID_PARAM;
      91             :   }
      92           2 :   handle->workptr = *(void *const *)attrValue;
      93           2 :   return ACL_SUCCESS;
      94             : }
      95             : 
      96           3 : static aclError SetStreamWorkSize(aclrtStreamConfigHandle *const handle,
      97             :                            const void *const attrValue,
      98             :                            const size_t valueSize) {
      99           3 :   if (valueSize != sizeof(size_t)) {
     100           1 :     ACL_LOG_INNER_ERROR(
     101             :         "valueSize[%zu] is invalid, it should be %zu",
     102             :         valueSize, sizeof(size_t));
     103           1 :     return ACL_ERROR_INVALID_PARAM;
     104             :   }
     105           2 :   const size_t value = *(const size_t *)(attrValue);
     106           2 :   handle->workSize = value;
     107           2 :   return ACL_SUCCESS;
     108             : }
     109             : 
     110             : static SetStreamConfigParamFuncMap
     111             :     g_setStreamConfigMap[ACL_RT_STREAM_PRIORITY + 1] = {
     112             :         {ACL_RT_STREAM_WORK_ADDR_PTR, &SetStreamWorkPtr},
     113             :         {ACL_RT_STREAM_WORK_SIZE, &SetStreamWorkSize},
     114             :         {ACL_RT_STREAM_FLAG, &SetStreamFlag},
     115             :         {ACL_RT_STREAM_PRIORITY, &SetStreamPriority}};
     116             : 
     117          17 : aclError aclrtSetStreamConfigOpt(aclrtStreamConfigHandle *handle,
     118             :                                  aclrtStreamConfigAttr attr,
     119             :                                  const void *attrValue, size_t valueSize) {
     120          17 :   if (handle == NULL || attrValue == NULL) {
     121           2 :     ACL_LOG_ERROR("%s", handle == NULL  ? "handle is NULL" : "attrValue is NULL");
     122           2 :     return ACL_ERROR_INVALID_PARAM;
     123             :   }
     124          15 :   SetStreamConfigFunc paramFunc = NULL;
     125          15 :   uint32_t attrCount =
     126             :     sizeof(g_setStreamConfigMap) / sizeof(SetStreamConfigParamFuncMap);
     127          15 :   if (attr >= attrCount) {
     128           2 :     ACL_LOG_INNER_ERROR("attr set invalid.");
     129           2 :     return ACL_ERROR_INVALID_PARAM;
     130             :   }
     131          13 :   paramFunc = g_setStreamConfigMap[attr].configParamFunc;
     132          13 :   aclError ret = paramFunc(handle, attrValue, valueSize);
     133          13 :   if (ret != ACL_SUCCESS) {
     134           5 :     return ret;
     135             :   }
     136           8 :   return ACL_SUCCESS;
     137             : }
     138             : 
     139           4 : aclError aclrtCreateStreamV2(aclrtStream *stream,
     140             :                              const aclrtStreamConfigHandle *handle) {
     141           4 :   if (stream == NULL) {
     142           1 :     ACL_LOG_ERROR("stream is NULL");
     143           1 :     return ACL_ERROR_INVALID_PARAM;
     144             :   }
     145           3 :   rtStream_t rtStream = NULL;
     146           3 :   rtStreamConfigHandle rtHandle;
     147           3 :   memset_s(&rtHandle, sizeof(rtStreamConfigHandle), 0, sizeof(rtStreamConfigHandle));
     148           3 :   if (handle != NULL) {
     149           1 :       rtHandle.workPtr = handle->workptr;
     150           1 :       rtHandle.workSize = handle->workSize;
     151           1 :       rtHandle.flag = handle->flag;
     152           1 :       rtHandle.priority = handle->priority;
     153             :   }
     154           3 :   const rtError_t rtErr = rtStreamCreateWithConfig(&rtStream, &rtHandle);
     155           3 :   if (rtErr != RT_ERROR_NONE) {
     156           1 :     ACL_LOG_CALL_ERROR("create stream failed ret=%d",
     157             :                        (int32_t)(rtErr));
     158           1 :     return rtErr;
     159             :   }
     160             : 
     161           2 :   *stream = (aclrtStream)(rtStream);
     162           2 :   return ACL_SUCCESS;
     163             : }
     164             : 
     165           4 : aclError aclrtDestroyStream(aclrtStream stream) {
     166           4 :   if (stream == NULL) {
     167           1 :     ACL_LOG_ERROR("stream is NULL");
     168           1 :     return ACL_ERROR_INVALID_PARAM;
     169             :   }
     170           3 :   const rtError_t rtErr = rtStreamDestroy((rtStream_t)(stream));
     171           3 :   if (rtErr != RT_ERROR_NONE) {
     172           1 :     ACL_LOG_CALL_ERROR("destroy stream failed ret=%d",
     173             :                        (int32_t)(rtErr));
     174           1 :     return rtErr;
     175             :   }
     176           2 :   return ACL_SUCCESS;
     177             : }
     178             : 
     179           3 : aclError aclrtSynchronizeStream(aclrtStream stream) {
     180           3 :   const rtError_t rtErr = rtStreamSynchronize((rtStream_t)(stream));
     181           3 :   if (rtErr != RT_ERROR_NONE) {
     182           1 :     ACL_LOG_CALL_ERROR("synchronize stream failed ret=%d",
     183             :                        (int32_t)(rtErr));
     184           1 :     return rtErr;
     185             :   }
     186           2 :   return ACL_SUCCESS;
     187             : }
     188             : 
     189             : #if defined(__cplusplus)
     190             : }
     191             : #endif

Generated by: LCOV version 1.14