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 : {
22 7 : aclrtStreamConfigHandle* configHandle = (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 : {
33 7 : if (handle == NULL) {
34 1 : ACL_LOG_ERROR("handle is NULL");
35 1 : return ACL_ERROR_INVALID_PARAM;
36 : }
37 6 : mmFree(handle);
38 6 : handle = NULL;
39 6 : return ACL_SUCCESS;
40 : }
41 :
42 : typedef aclError (*SetStreamConfigFunc)(aclrtStreamConfigHandle* const, const void* const, const size_t);
43 :
44 : typedef struct {
45 : aclrtStreamConfigAttr configAttr;
46 : SetStreamConfigFunc configParamFunc;
47 : } SetStreamConfigParamFuncMap;
48 :
49 4 : static aclError SetStreamPriority(
50 : aclrtStreamConfigHandle* const handle, const void* const attrValue, const size_t valueSize)
51 : {
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("valueSize[%zu] is invalid, it should be %zu", valueSize, sizeof(uint32_t));
56 1 : return ACL_ERROR_INVALID_PARAM;
57 : }
58 3 : const uint32_t value = *(const uint32_t*)(attrValue);
59 3 : if (!(value <= ACL_RT_MAX_PRIORITY)) {
60 1 : ACL_LOG_INNER_ERROR(
61 : "value[%u] is invalid, it should be in [%d, %d]", value, ACL_RT_MIN_PRIORITY, ACL_RT_MAX_PRIORITY);
62 1 : return ACL_ERROR_INVALID_PARAM;
63 : }
64 2 : handle->priority = value;
65 2 : return ACL_SUCCESS;
66 : }
67 :
68 3 : static aclError SetStreamFlag(
69 : aclrtStreamConfigHandle* const handle, const void* const attrValue, const size_t valueSize)
70 : {
71 3 : if (valueSize != sizeof(size_t)) {
72 1 : ACL_LOG_INNER_ERROR("valueSize[%zu] is invalid, it should be %zu", valueSize, sizeof(size_t));
73 1 : return ACL_ERROR_INVALID_PARAM;
74 : }
75 2 : const size_t value = *(const size_t*)(attrValue);
76 2 : handle->flag = value;
77 2 : return ACL_SUCCESS;
78 : }
79 :
80 3 : static aclError SetStreamWorkPtr(
81 : aclrtStreamConfigHandle* const handle, const void* const attrValue, const size_t valueSize)
82 : {
83 3 : if (valueSize != sizeof(void*)) {
84 1 : ACL_LOG_INNER_ERROR("valueSize[%zu] is invalid, it should be %zu", valueSize, sizeof(void*));
85 1 : return ACL_ERROR_INVALID_PARAM;
86 : }
87 2 : handle->workptr = *(void* const*)attrValue;
88 2 : return ACL_SUCCESS;
89 : }
90 :
91 3 : static aclError SetStreamWorkSize(
92 : aclrtStreamConfigHandle* const handle, const void* const attrValue, const size_t valueSize)
93 : {
94 3 : if (valueSize != sizeof(size_t)) {
95 1 : ACL_LOG_INNER_ERROR("valueSize[%zu] is invalid, it should be %zu", valueSize, sizeof(size_t));
96 1 : return ACL_ERROR_INVALID_PARAM;
97 : }
98 2 : const size_t value = *(const size_t*)(attrValue);
99 2 : handle->workSize = value;
100 2 : return ACL_SUCCESS;
101 : }
102 :
103 : static SetStreamConfigParamFuncMap g_setStreamConfigMap[ACL_RT_STREAM_PRIORITY + 1] = {
104 : {ACL_RT_STREAM_WORK_ADDR_PTR, &SetStreamWorkPtr},
105 : {ACL_RT_STREAM_WORK_SIZE, &SetStreamWorkSize},
106 : {ACL_RT_STREAM_FLAG, &SetStreamFlag},
107 : {ACL_RT_STREAM_PRIORITY, &SetStreamPriority}};
108 :
109 17 : aclError aclrtSetStreamConfigOpt(
110 : aclrtStreamConfigHandle* handle, aclrtStreamConfigAttr attr, const void* attrValue, size_t valueSize)
111 : {
112 17 : if (handle == NULL || attrValue == NULL) {
113 2 : ACL_LOG_ERROR("%s", handle == NULL ? "handle is NULL" : "attrValue is NULL");
114 2 : return ACL_ERROR_INVALID_PARAM;
115 : }
116 15 : SetStreamConfigFunc paramFunc = NULL;
117 15 : uint32_t attrCount = sizeof(g_setStreamConfigMap) / sizeof(SetStreamConfigParamFuncMap);
118 15 : if (attr >= attrCount) {
119 2 : ACL_LOG_INNER_ERROR("attr set invalid.");
120 2 : return ACL_ERROR_INVALID_PARAM;
121 : }
122 13 : paramFunc = g_setStreamConfigMap[attr].configParamFunc;
123 13 : aclError ret = paramFunc(handle, attrValue, valueSize);
124 13 : if (ret != ACL_SUCCESS) {
125 5 : return ret;
126 : }
127 8 : return ACL_SUCCESS;
128 : }
129 :
130 4 : aclError aclrtCreateStreamV2(aclrtStream* stream, const aclrtStreamConfigHandle* handle)
131 : {
132 4 : if (stream == NULL) {
133 1 : ACL_LOG_ERROR("stream is NULL");
134 1 : return ACL_ERROR_INVALID_PARAM;
135 : }
136 3 : rtStream_t rtStream = NULL;
137 : rtStreamConfigHandle rtHandle;
138 3 : memset_s(&rtHandle, sizeof(rtStreamConfigHandle), 0, sizeof(rtStreamConfigHandle));
139 3 : if (handle != NULL) {
140 1 : rtHandle.workPtr = handle->workptr;
141 1 : rtHandle.workSize = handle->workSize;
142 1 : rtHandle.flag = handle->flag;
143 1 : rtHandle.priority = handle->priority;
144 : }
145 3 : const rtError_t rtErr = rtStreamCreateWithConfig(&rtStream, &rtHandle);
146 3 : if (rtErr != RT_ERROR_NONE) {
147 1 : ACL_LOG_CALL_ERROR("create stream failed ret=%d", (int32_t)(rtErr));
148 1 : return rtErr;
149 : }
150 :
151 2 : *stream = (aclrtStream)(rtStream);
152 2 : return ACL_SUCCESS;
153 : }
154 :
155 4 : aclError aclrtDestroyStream(aclrtStream stream)
156 : {
157 4 : if (stream == NULL) {
158 1 : ACL_LOG_ERROR("stream is NULL");
159 1 : return ACL_ERROR_INVALID_PARAM;
160 : }
161 3 : const rtError_t rtErr = rtStreamDestroy((rtStream_t)(stream));
162 3 : if (rtErr != RT_ERROR_NONE) {
163 1 : ACL_LOG_CALL_ERROR("destroy stream failed ret=%d", (int32_t)(rtErr));
164 1 : return rtErr;
165 : }
166 2 : return ACL_SUCCESS;
167 : }
168 :
169 3 : aclError aclrtSynchronizeStream(aclrtStream stream)
170 : {
171 3 : const rtError_t rtErr = rtStreamSynchronize((rtStream_t)(stream));
172 3 : if (rtErr != RT_ERROR_NONE) {
173 1 : ACL_LOG_CALL_ERROR("synchronize stream failed ret=%d", (int32_t)(rtErr));
174 1 : return rtErr;
175 : }
176 2 : return ACL_SUCCESS;
177 : }
178 :
179 : #if defined(__cplusplus)
180 : }
181 : #endif
|