已合并
【PR】: feat:remove all exclude #4195
duanpengliang创建于 18 天前
【PR】: feat:remove all exclude #4195
已合并
共 101 个文件变更+1693-1778
| @@ -7,66 +7,13 @@ repos: | |||
| 7 | files: \.(c|h|cpp|hpp|cc|hh|cxx|hxx)$ | 7 | files: \.(c|h|cpp|hpp|cc|hh|cxx|hxx)$ |
| 8 | exclude: | | 8 | exclude: | |
| 9 | (?x)^( | 9 | (?x)^( |
| 10 | - docs | 10 | + include/external/acl/(?: |
| 11 | - | example | ||
| 12 | - | include/external/acl/(?: | ||
| 13 | acl_prof | 11 | acl_prof |
| 14 | | acl_rt | 12 | | acl_rt |
| 15 | | acl_rt_allocator | 13 | | acl_rt_allocator |
| 16 | | acl_tdt | 14 | | acl_tdt |
| 17 | | acl_tdt_queue | 15 | | acl_tdt_queue |
| 18 | - )\.h | 16 | + )\.h$ |
| 19 | - | include(?: | ||
| 20 | - /(?! | ||
| 21 | - (?: | ||
| 22 | - external | ||
| 23 | - | driver | ||
| 24 | - | tsd_client | ||
| 25 | - | dfx/base/err_msg.h | ||
| 26 | - )(?:/|$) | ||
| 27 | - ) | ||
| 28 | - | $ | ||
| 29 | - ) | ||
| 30 | - | pkg_inc(?: | ||
| 31 | - /(?! | ||
| 32 | - (?: | ||
| 33 | - aicpu_sched | ||
| 34 | - | base/err_mgr.h | ||
| 35 | - | driver | ||
| 36 | - | mmpa | ||
| 37 | - | queue_schedule | ||
| 38 | - | runtime | ||
| 39 | - | tsd | ||
| 40 | - )(?:/|$) | ||
| 41 | - ) | ||
| 42 | - | $ | ||
| 43 | - ) | ||
| 44 | - | scripts | ||
| 45 | - | src/( | ||
| 46 | - dfx/adump(?!(?:/adcore)(?:/|$)) | ||
| 47 | - | dfx/log | ||
| 48 | - | dfx/trace/(?:atrace|common|cpu_detect|inc) | ||
| 49 | - )(/|$) | ||
| 50 | - | stub | ||
| 51 | - | tests(?: | ||
| 52 | - /(?! | ||
| 53 | - (?: | ||
| 54 | - cmodel_test | ||
| 55 | - | depends/mmpa | ||
| 56 | - | depends/runtime | ||
| 57 | - | depends/tdt | ||
| 58 | - | ut/acl | ||
| 59 | - | ut/aicpu_sched | ||
| 60 | - | ut/error_manager | ||
| 61 | - | ut/mmpa | ||
| 62 | - | ut/queue_schedule | ||
| 63 | - | ut/runtime | ||
| 64 | - | ut/tsd | ||
| 65 | - )(?:/|$) | ||
| 66 | - | depends/acl_stub\.h$ | ||
| 67 | - ) | ||
| 68 | - | $ | ||
| 69 | - ) | ||
| 70 | ) | 17 | ) |
| 71 | args: | 18 | args: |
| 72 | - "--style=file" | 19 | - "--style=file" |
| @@ -15,15 +15,9 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | namespace { | 17 | namespace { |
| 18 | -const char *SafeString(const char *message) | 18 | +const char* SafeString(const char* message) { return message != nullptr ? message : "<null>"; } |
| 19 | -{ | ||
| 20 | - return message != nullptr ? message : "<null>"; | ||
| 21 | -} | ||
| 22 | 19 | ||
| 23 | -const char *RunModeToString(aclrtRunMode runMode) | 20 | +const char* RunModeToString(aclrtRunMode runMode) { return runMode == ACL_HOST ? "ACL_HOST" : "ACL_DEVICE"; } |
| 24 | -{ | ||
| 25 | - return runMode == ACL_HOST ? "ACL_HOST" : "ACL_DEVICE"; | ||
| 26 | -} | ||
| 27 | 21 | ||
| 28 | void PrintVerboseErrorInfo(int32_t deviceId) | 22 | void PrintVerboseErrorInfo(int32_t deviceId) |
| 29 | { | 23 | { |
| @@ -34,20 +28,19 @@ void PrintVerboseErrorInfo(int32_t deviceId) | |||
| 34 | return; | 28 | return; |
| 35 | } | 29 | } |
| 36 | 30 | ||
| 37 | - INFO_LOG("Verbose error info: errorType=%d, tryRepair=%u, hasDetail=%u", | 31 | + INFO_LOG( |
| 38 | - static_cast<int32_t>(errorInfo.errorType), | 32 | + "Verbose error info: errorType=%d, tryRepair=%u, hasDetail=%u", static_cast<int32_t>(errorInfo.errorType), |
| 39 | - static_cast<uint32_t>(errorInfo.tryRepair), | 33 | + static_cast<uint32_t>(errorInfo.tryRepair), static_cast<uint32_t>(errorInfo.hasDetail)); |
| 40 | - static_cast<uint32_t>(errorInfo.hasDetail)); | ||
| 41 | } | 34 | } |
| 42 | } // namespace | 35 | } // namespace |
| 43 | 36 | ||
| 44 | -#define CHECK_ACL_RETURN(call) \ | 37 | +#define CHECK_ACL_RETURN(call) \ |
| 45 | - do { \ | 38 | + do { \ |
| 46 | - aclError ret = (call); \ | 39 | + aclError ret = (call); \ |
| 47 | - if (ret != ACL_SUCCESS) { \ | 40 | + if (ret != ACL_SUCCESS) { \ |
| 48 | ERROR_LOG("%s failed: ret=%d", | 41 | ERROR_LOG("%s failed: ret=%d", |
| 49 | - return -1; \ | 42 | + return -1; \ |
| 50 | - } \ | 43 | + } \ |
| 51 | } while (0) | 44 | } while (0) |
| 52 | 45 | ||
| 53 | int main() | 46 | int main() |
| @@ -72,22 +65,19 @@ int main() | |||
| 72 | } else { | 65 | } else { |
| 73 | aclError peekError = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); | 66 | aclError peekError = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); |
| 74 | aclError lastError = aclrtGetLastError(ACL_RT_THREAD_LEVEL); | 67 | aclError lastError = aclrtGetLastError(ACL_RT_THREAD_LEVEL); |
| 75 | - const char *recentErrMsg = aclGetRecentErrMsg(); | 68 | + const char* recentErrMsg = aclGetRecentErrMsg(); |
| 76 | - ERROR_LOG("Diagnostics: ret=%d, peekErr=%d, lastErr=%d, recentErrMsg=%s", | 69 | + ERROR_LOG( |
| 77 | - static_cast<int32_t>(expectedRet), | 70 | + "Diagnostics: ret=%d, peekErr=%d, lastErr=%d, recentErrMsg=%s", static_cast<int32_t>(expectedRet), |
| 78 | - static_cast<int32_t>(peekError), | 71 | + static_cast<int32_t>(peekError), static_cast<int32_t>(lastError), SafeString(recentErrMsg)); |
| 79 | - static_cast<int32_t>(lastError), | ||
| 80 | - SafeString(recentErrMsg)); | ||
| 81 | PrintVerboseErrorInfo(deviceId); | 72 | PrintVerboseErrorInfo(deviceId); |
| 82 | 73 | ||
| 83 | // Read the diagnostics again to show the consumed state. | 74 | // Read the diagnostics again to show the consumed state. |
| 84 | aclError clearedPeek = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); | 75 | aclError clearedPeek = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); |
| 85 | aclError clearedLast = aclrtGetLastError(ACL_RT_THREAD_LEVEL); | 76 | aclError clearedLast = aclrtGetLastError(ACL_RT_THREAD_LEVEL); |
| 86 | - const char *clearedErrMsg = aclGetRecentErrMsg(); | 77 | + const char* clearedErrMsg = aclGetRecentErrMsg(); |
| 87 | - INFO_LOG("After diagnostics are consumed once: peekErr=%d, lastErr=%d, recentErrMsg=%s", | 78 | + INFO_LOG( |
| 88 | - static_cast<int32_t>(clearedPeek), | 79 | + "After diagnostics are consumed once: peekErr=%d, lastErr=%d, recentErrMsg=%s", |
| 89 | - static_cast<int32_t>(clearedLast), | 80 | + static_cast<int32_t>(clearedPeek), static_cast<int32_t>(clearedLast), SafeString(clearedErrMsg)); |
| 90 | - SafeString(clearedErrMsg)); | ||
| 91 | } | 81 | } |
| 92 | 82 | ||
| 93 | // Release ACL resources. | 83 | // Release ACL resources. |
| @@ -14,10 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace { | 16 | namespace { |
| 17 | -const char *RunModeToString(aclrtRunMode runMode) | 17 | +const char* RunModeToString(aclrtRunMode runMode) { return runMode == ACL_HOST ? "ACL_HOST" : "ACL_DEVICE"; } |
| 18 | -{ | ||
| 19 | - return runMode == ACL_HOST ? "ACL_HOST" : "ACL_DEVICE"; | ||
| 20 | -} | ||
| 21 | 18 | ||
| 22 | aclError PrintCannVersionInfo() | 19 | aclError PrintCannVersionInfo() |
| 23 | { | 20 | { |
| @@ -58,12 +55,12 @@ int main() | |||
| 58 | float originalValue = 1.625f; | 55 | float originalValue = 1.625f; |
| 59 | aclFloat16 fp16Value = aclFloatToFloat16(originalValue); | 56 | aclFloat16 fp16Value = aclFloatToFloat16(originalValue); |
| 60 | float restoredValue = aclFloat16ToFloat(fp16Value); | 57 | float restoredValue = aclFloat16ToFloat(fp16Value); |
| 61 | - INFO_LOG("Float conversion: %.6f -> 0x%04x -> %.6f", originalValue, static_cast<unsigned int>(fp16Value), | 58 | + INFO_LOG( |
| 62 | - restoredValue); | 59 | + "Float conversion: %.6f -> 0x%04x -> %.6f", originalValue, static_cast<unsigned int>(fp16Value), restoredValue); |
| 63 | 60 | ||
| 64 | - INFO_LOG("Data type size: ACL_FLOAT=%zu, ACL_FLOAT16=%zu, ACL_INT64=%zu", | 61 | + INFO_LOG( |
| 65 | - aclDataTypeSize(aclDataType::ACL_FLOAT), aclDataTypeSize(aclDataType::ACL_FLOAT16), | 62 | + "Data type size: ACL_FLOAT=%zu, ACL_FLOAT16=%zu, ACL_INT64=%zu", aclDataTypeSize(aclDataType::ACL_FLOAT), |
| 66 | - aclDataTypeSize(aclDataType::ACL_INT64)); | 63 | + aclDataTypeSize(aclDataType::ACL_FLOAT16), aclDataTypeSize(aclDataType::ACL_INT64)); |
| 67 | 64 | ||
| 68 | // Finalize ACL after all queries finish. | 65 | // Finalize ACL after all queries finish. |
| 69 | CHECK_ERROR(aclFinalize()); | 66 | CHECK_ERROR(aclFinalize()); |
| @@ -16,7 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace { | 18 | namespace { |
| 19 | -const char *CannAttrToString(aclCannAttr attr) | 19 | +const char* CannAttrToString(aclCannAttr attr) |
| 20 | { | 20 | { |
| 21 | switch (attr) { | 21 | switch (attr) { |
| 22 | case ACL_CANN_ATTR_INF_NAN: | 22 | case ACL_CANN_ATTR_INF_NAN: |
| @@ -45,7 +45,7 @@ int QueryCannVersion() | |||
| 45 | 45 | ||
| 46 | int QueryCannAttributes() | 46 | int QueryCannAttributes() |
| 47 | { | 47 | { |
| 48 | - const aclCannAttr *attrList = nullptr; | 48 | + const aclCannAttr* attrList = nullptr; |
| 49 | size_t attrCount = 0; | 49 | size_t attrCount = 0; |
| 50 | CHECK_ERROR(aclGetCannAttributeList(&attrList, &attrCount)); | 50 | CHECK_ERROR(aclGetCannAttributeList(&attrList, &attrCount)); |
| 51 | INFO_LOG("CANN attribute count: %zu", attrCount); | 51 | INFO_LOG("CANN attribute count: %zu", attrCount); |
| @@ -61,7 +61,7 @@ int QueryCannAttributes() | |||
| 61 | 61 | ||
| 62 | int QueryCompatibilityAndCapability(int32_t deviceId) | 62 | int QueryCompatibilityAndCapability(int32_t deviceId) |
| 63 | { | 63 | { |
| 64 | - const char *socName = aclrtGetSocName(); | 64 | + const char* socName = aclrtGetSocName(); |
| 65 | if (socName == nullptr || std::strlen(socName) == 0U) { | 65 | if (socName == nullptr || std::strlen(socName) == 0U) { |
| 66 | WARN_LOG("aclrtGetSocName returned empty soc name. Skip architecture compatibility check."); | 66 | WARN_LOG("aclrtGetSocName returned empty soc name. Skip architecture compatibility check."); |
| 67 | } else { | 67 | } else { |
| @@ -75,7 +75,7 @@ int QueryCompatibilityAndCapability(int32_t deviceId) | |||
| 75 | INFO_LOG("Device capability ACL_FEATURE_TSCPU_TASK_UPDATE_SUPPORT_AIC_AIV: %d", capability); | 75 | INFO_LOG("Device capability ACL_FEATURE_TSCPU_TASK_UPDATE_SUPPORT_AIC_AIV: %d", capability); |
| 76 | return 0; | 76 | return 0; |
| 77 | } | 77 | } |
| 78 | -} // namespace | 78 | +} // namespace |
| 79 | 79 | ||
| 80 | int32_t main() | 80 | int32_t main() |
| 81 | { | 81 | { |
| @@ -94,7 +94,8 @@ int32_t main() | |||
| 94 | 94 | ||
| 95 | aclError cleanupRet = aclrtResetDeviceForce(deviceId); | 95 | aclError cleanupRet = aclrtResetDeviceForce(deviceId); |
| 96 | if (cleanupRet != ACL_SUCCESS) { | 96 | if (cleanupRet != ACL_SUCCESS) { |
| 97 | - ERROR_LOG("Operation failed: aclrtResetDeviceForce(deviceId) returned error code %d", | 97 | + ERROR_LOG( |
| 98 | + "Operation failed: aclrtResetDeviceForce(deviceId) returned error code %d", | ||
| 98 | static_cast<int32_t>(cleanupRet)); | 99 | static_cast<int32_t>(cleanupRet)); |
| 99 | ret = ret == 0 ? -1 : ret; | 100 | ret = ret == 0 ? -1 : ret; |
| 100 | } | 101 | } |
| @@ -16,10 +16,10 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | -#define CHECK_ERROR(ret) \ | 19 | +#define CHECK_ERROR(ret) \ |
| 20 | - if ((ret) != ACL_SUCCESS) { \ | 20 | + if ((ret) != ACL_SUCCESS) { \ |
| 21 | printf("Error at line %d, ret = %d\n", __LINE__, static_cast<int32_t>(ret)); \ | 21 | printf("Error at line %d, ret = %d\n", __LINE__, static_cast<int32_t>(ret)); \ |
| 22 | - return -1; \ | 22 | + return -1; \ |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | namespace { | 25 | namespace { |
| @@ -23,9 +23,7 @@ extern "C" __global__ __aicore__ void VectorAddKernel( | |||
| 23 | // A5 编译时,生成器会在 kernel tail 中追加 dci()。关闭自动 DCCI 后,scalar store 产生的脏数据 | 23 | // A5 编译时,生成器会在 kernel tail 中追加 dci()。关闭自动 DCCI 后,scalar store 产生的脏数据 |
| 24 | // 尚未回写到 GM 就被 dci() 失效,导致输出全为 0,因此需要显式调用 dcci() 将数据回写到 GM。 | 24 | // 尚未回写到 GM 就被 dci() 失效,导致输出全为 0,因此需要显式调用 dcci() 将数据回写到 GM。 |
| 25 | 25 | ||
| 26 | - dcci(reinterpret_cast<__gm__ int64_t*>(dst), | 26 | + dcci(reinterpret_cast<__gm__ int64_t*>(dst), cache_line_t::ENTIRE_DATA_CACHE, dcci_dst_t::CACHELINE_OUT); |
| 27 | - cache_line_t::ENTIRE_DATA_CACHE, | ||
| 28 | - dcci_dst_t::CACHELINE_OUT); | ||
| 29 | 27 | ||
| 30 | } | 28 | } |
| 31 | 29 | ||
| @@ -27,8 +27,8 @@ public: | |||
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | - ContextGuard(const ContextGuard &) = delete; | 30 | + ContextGuard(const ContextGuard&) = delete; |
| 31 | - ContextGuard &operator=(const ContextGuard &) = delete; | 31 | + ContextGuard& operator=(const ContextGuard&) = delete; |
| 32 | 32 | ||
| 33 | aclError Destroy() | 33 | aclError Destroy() |
| 34 | { | 34 | { |
| @@ -79,7 +79,7 @@ int32_t RunContextQuerySample() | |||
| 79 | INFO_LOG("[SUCCESS] Context query sample completed successfully"); | 79 | INFO_LOG("[SUCCESS] Context query sample completed successfully"); |
| 80 | return 0; | 80 | return 0; |
| 81 | } | 81 | } |
| 82 | -} // namespace | 82 | +} // namespace |
| 83 | 83 | ||
| 84 | int32_t main() | 84 | int32_t main() |
| 85 | { | 85 | { |
| @@ -15,70 +15,73 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | namespace { | 17 | namespace { |
| 18 | - int Init(int32_t deviceId, aclrtStream* stream) | 18 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 19 | - { | 19 | +{ |
| 20 | - CHECK_ERROR(aclInit(nullptr)); | 20 | + CHECK_ERROR(aclInit(nullptr)); |
| 21 | - CHECK_ERROR(aclrtSetDevice(deviceId)); | 21 | + CHECK_ERROR(aclrtSetDevice(deviceId)); |
| 22 | - CHECK_ERROR(aclrtCreateStream(stream)); | 22 | + CHECK_ERROR(aclrtCreateStream(stream)); |
| 23 | - return 0; | 23 | + return 0; |
| 24 | +} | ||
| 25 | + | ||
| 26 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) | ||
| 27 | +{ | ||
| 28 | + int64_t shape_size = 1; | ||
| 29 | + for (auto i : shape) { | ||
| 30 | + shape_size *= i; | ||
| 31 | + } | ||
| 32 | + return shape_size; | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +template <typename T> | ||
| 36 | +int CreateAclTensor( | ||
| 37 | + const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | ||
| 38 | + aclTensor** tensor) | ||
| 39 | +{ | ||
| 40 | + auto size = GetShapeSize(shape) * sizeof(T); | ||
| 41 | + | ||
| 42 | + // Allocate memory on the device | ||
| 43 | + CHECK_ERROR(aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 44 | + | ||
| 45 | + // Copy memory from host to device synchronously | ||
| 46 | + CHECK_ERROR(aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 47 | + | ||
| 48 | + // Calculate the strides | ||
| 49 | + std::vector<int64_t> strides(shape.size(), 1); | ||
| 50 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { | ||
| 51 | + strides[i] = shape[i + 1] * strides[i + 1]; | ||
| 24 | } | 52 | } |
| 25 | 53 | ||
| 26 | - int64_t GetShapeSize(const std::vector<int64_t> &shape) | 54 | + // Create the tensor |
| 27 | - { | 55 | + *tensor = aclCreateTensor( |
| 28 | - int64_t shape_size = 1; | 56 | + shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), |
| 29 | - for (auto i : shape) { | 57 | + *deviceAddr); |
| 30 | - shape_size *= i; | 58 | + return 0; |
| 31 | - } | 59 | +} |
| 32 | - return shape_size; | ||
| 33 | - } | ||
| 34 | 60 | ||
| 35 | - template <typename T> | 61 | +void DestroyTensorResources(aclTensor* self, aclTensor* other, aclScalar* alpha, aclTensor* out) |
| 36 | - int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr, | 62 | +{ |
| 37 | - aclDataType dataType, aclTensor **tensor) | 63 | + aclDestroyTensor(self); |
| 38 | - { | 64 | + aclDestroyTensor(other); |
| 39 | - auto size = GetShapeSize(shape) * sizeof(T); | 65 | + aclDestroyScalar(alpha); |
| 66 | + aclDestroyTensor(out); | ||
| 67 | +} | ||
| 40 | 68 | ||
| 41 | - // Allocate memory on the device | 69 | +int GetAndPrintResult(void* outDeviceAddr, const std::vector<int64_t>& outShape) |
| 42 | - CHECK_ERROR(aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 70 | +{ |
| 71 | + auto size = GetShapeSize(outShape); | ||
| 72 | + std::vector<float> resultData(size, 0); | ||
| 73 | + CHECK_ERROR(aclrtMemcpy( | ||
| 74 | + resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | ||
| 75 | + ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 43 | 76 | ||
| 44 | - // Copy memory from host to device synchronously | 77 | + for (int64_t i = 0; i < size; i++) { |
| 45 | - CHECK_ERROR(aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 46 | - | ||
| 47 | - // Calculate the strides | ||
| 48 | - std::vector<int64_t> strides(shape.size(), 1); | ||
| 49 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | ||
| 50 | - strides[i] = shape[i + 1] * strides[i + 1]; | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - // Create the tensor | ||
| 54 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | ||
| 55 | - shape.data(), shape.size(), *deviceAddr); | ||
| 56 | - return 0; | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - void DestroyTensorResources(aclTensor *self, aclTensor *other, aclScalar *alpha, aclTensor *out) | ||
| 60 | - { | ||
| 61 | - aclDestroyTensor(self); | ||
| 62 | - aclDestroyTensor(other); | ||
| 63 | - aclDestroyScalar(alpha); | ||
| 64 | - aclDestroyTensor(out); | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - int GetAndPrintResult(void *outDeviceAddr, const std::vector<int64_t> &outShape) | ||
| 68 | - { | ||
| 69 | - auto size = GetShapeSize(outShape); | ||
| 70 | - std::vector<float> resultData(size, 0); | ||
| 71 | - CHECK_ERROR(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | ||
| 72 | - size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 73 | - | ||
| 74 | - for (int64_t i = 0; i < size; i++) { | ||
| 75 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); | 78 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); |
| 76 | - } | ||
| 77 | - return 0; | ||
| 78 | } | 79 | } |
| 80 | + return 0; | ||
| 81 | +} | ||
| 79 | } // namespace | 82 | } // namespace |
| 80 | 83 | ||
| 81 | -int32_t main(int argc, char const *argv[]) | 84 | +int32_t main(int argc, char const* argv[]) |
| 82 | { | 85 | { |
| 83 | INFO_LOG("Start to run device_normal sample."); | 86 | INFO_LOG("Start to run device_normal sample."); |
| 84 | int32_t deviceId = 0; | 87 | int32_t deviceId = 0; |
| @@ -88,7 +91,7 @@ int32_t main(int argc, char const *argv[]) | |||
| 88 | std::vector<int64_t> selfShape{4, 2}, otherShape{4, 2}, outShape{4, 2}; | 91 | std::vector<int64_t> selfShape{4, 2}, otherShape{4, 2}, outShape{4, 2}; |
| 89 | void *selfDeviceAddr = nullptr, *otherDeviceAddr = nullptr, *outDeviceAddr = nullptr; | 92 | void *selfDeviceAddr = nullptr, *otherDeviceAddr = nullptr, *outDeviceAddr = nullptr; |
| 90 | aclTensor *self = nullptr, *other = nullptr, *out = nullptr; | 93 | aclTensor *self = nullptr, *other = nullptr, *out = nullptr; |
| 91 | - aclScalar *alpha = nullptr; | 94 | + aclScalar* alpha = nullptr; |
| 92 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 95 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 93 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; | 96 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; |
| 94 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 97 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -99,21 +102,21 @@ int32_t main(int argc, char const *argv[]) | |||
| 99 | 102 | ||
| 100 | alpha = aclCreateScalar(&alphaValue, aclDataType::ACL_FLOAT); | 103 | alpha = aclCreateScalar(&alphaValue, aclDataType::ACL_FLOAT); |
| 101 | if (alpha == nullptr) { | 104 | if (alpha == nullptr) { |
| 102 | - ERROR_LOG("Create alpha Scalar failed."); | 105 | + ERROR_LOG("Create alpha Scalar failed."); |
| 103 | - return -1; | 106 | + return -1; |
| 104 | } | 107 | } |
| 105 | 108 | ||
| 106 | CHECK_ERROR(CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out)); | 109 | CHECK_ERROR(CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out)); |
| 107 | 110 | ||
| 108 | // Call the CANN operator library API | 111 | // Call the CANN operator library API |
| 109 | uint64_t workspaceSize = 0; | 112 | uint64_t workspaceSize = 0; |
| 110 | - aclOpExecutor *executor; | 113 | + aclOpExecutor* executor; |
| 111 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); | 114 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); |
| 112 | 115 | ||
| 113 | // Allocate device memory based on the calculation results | 116 | // Allocate device memory based on the calculation results |
| 114 | - void *workspaceAddr = nullptr; | 117 | + void* workspaceAddr = nullptr; |
| 115 | if (workspaceSize > 0lu) { | 118 | if (workspaceSize > 0lu) { |
| 116 | - CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 119 | + CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 117 | } | 120 | } |
| 118 | // Call the Add operator | 121 | // Call the Add operator |
| 119 | CHECK_ERROR(aclnnAdd(workspaceAddr, workspaceSize, executor, stream)); | 122 | CHECK_ERROR(aclnnAdd(workspaceAddr, workspaceSize, executor, stream)); |
| @@ -133,7 +136,7 @@ int32_t main(int argc, char const *argv[]) | |||
| 133 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); | 136 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); |
| 134 | CHECK_ERROR(aclrtFree(outDeviceAddr)); | 137 | CHECK_ERROR(aclrtFree(outDeviceAddr)); |
| 135 | if (workspaceSize > 0lu) { | 138 | if (workspaceSize > 0lu) { |
| 136 | - CHECK_ERROR(aclrtFree(workspaceAddr)); | 139 | + CHECK_ERROR(aclrtFree(workspaceAddr)); |
| 137 | } | 140 | } |
| 138 | CHECK_ERROR(aclrtDestroyStream(stream)); | 141 | CHECK_ERROR(aclrtDestroyStream(stream)); |
| 139 | CHECK_ERROR(aclrtResetDeviceForce(deviceId)); | 142 | CHECK_ERROR(aclrtResetDeviceForce(deviceId)); |
| @@ -141,4 +144,4 @@ int32_t main(int argc, char const *argv[]) | |||
| 141 | INFO_LOG("Run the device_normal sample successfully."); | 144 | INFO_LOG("Run the device_normal sample successfully."); |
| 142 | 145 | ||
| 143 | return 0; | 146 | return 0; |
| 144 | -} | 147 | +} |
| @@ -17,123 +17,124 @@ | |||
| 17 | using namespace std; | 17 | using namespace std; |
| 18 | 18 | ||
| 19 | namespace { | 19 | namespace { |
| 20 | - int Init(aclrtStream* stream) | 20 | +int Init(aclrtStream* stream) |
| 21 | - { | 21 | +{ |
| 22 | - CHECK_ERROR(aclrtSetDevice(0)); | 22 | + CHECK_ERROR(aclrtSetDevice(0)); |
| 23 | - CHECK_ERROR(aclrtCreateStream(stream)); | 23 | + CHECK_ERROR(aclrtCreateStream(stream)); |
| 24 | - return 0; | 24 | + return 0; |
| 25 | +} | ||
| 26 | + | ||
| 27 | +int DeviceInfoQuery() | ||
| 28 | +{ | ||
| 29 | + aclrtRunMode runMode; | ||
| 30 | + int64_t vectorCoreNum = 0; | ||
| 31 | + uint32_t deviceCount = 0; | ||
| 32 | + aclrtDeviceStatus deviceStatus; | ||
| 33 | + | ||
| 34 | + string socName = aclrtGetSocName(); | ||
| 35 | + INFO_LOG("Current Ascend chipset platform is: %s.", socName.c_str()); | ||
| 36 | + | ||
| 37 | + CHECK_ERROR(aclrtGetDeviceCount(&deviceCount)); | ||
| 38 | + INFO_LOG("Get device count success. deviceCount: %d.", deviceCount); | ||
| 39 | + | ||
| 40 | + CHECK_ERROR(aclrtQueryDeviceStatus(0, &deviceStatus)); | ||
| 41 | + INFO_LOG("Query device status success. deviceStatus: %d.", deviceStatus); | ||
| 42 | + | ||
| 43 | + CHECK_ERROR(aclrtGetRunMode(&runMode)); | ||
| 44 | + if (runMode == 0) { | ||
| 45 | + INFO_LOG("RunMode is ACL_DEVICE."); | ||
| 46 | + } else if (runMode == 1) { | ||
| 47 | + INFO_LOG("RunMode is ACL_HOST."); | ||
| 48 | + } else { | ||
| 49 | + INFO_LOG("RunMode is invalid."); | ||
| 25 | } | 50 | } |
| 26 | 51 | ||
| 27 | - int DeviceInfoQuery() | 52 | + aclrtUtilizationInfo utilizationInfo; |
| 28 | - { | 53 | + utilizationInfo.utilizationExtend = nullptr; |
| 29 | - aclrtRunMode runMode; | 54 | + CHECK_ERROR(aclrtGetDeviceUtilizationRate(0, &utilizationInfo)); |
| 30 | - int64_t vectorCoreNum = 0; | 55 | + INFO_LOG( |
| 31 | - uint32_t deviceCount = 0; | 56 | + "Get device utilizationRate success. cubeUtilization %d, vectorUtilization %d, aicpuUtilization %d," |
| 32 | - aclrtDeviceStatus deviceStatus; | 57 | + "memoryUtilization %d.", |
| 58 | + utilizationInfo.cubeUtilization, utilizationInfo.vectorUtilization, utilizationInfo.aicpuUtilization, | ||
| 59 | + utilizationInfo.memoryUtilization); | ||
| 33 | 60 | ||
| 34 | - string socName = aclrtGetSocName(); | 61 | + int32_t leastPriority; |
| 35 | - INFO_LOG("Current Ascend chipset platform is: %s.", socName.c_str()); | 62 | + int32_t greatestPriority; |
| 63 | + CHECK_ERROR(aclrtDeviceGetStreamPriorityRange(&leastPriority, &greatestPriority)); | ||
| 64 | + INFO_LOG( | ||
| 65 | + "Get stream priorityRange success. leastPriority %d, greatestPriority %d.", leastPriority, greatestPriority); | ||
| 36 | 66 | ||
| 37 | - CHECK_ERROR(aclrtGetDeviceCount(&deviceCount)); | 67 | + CHECK_ERROR(aclrtGetDeviceInfo(0, ACL_DEV_ATTR_VECTOR_CORE_NUM, &vectorCoreNum)); |
| 38 | - INFO_LOG("Get device count success. deviceCount: %d.", deviceCount); | 68 | + INFO_LOG("Get device info success. vectorCoreNum %ld.", vectorCoreNum); |
| 69 | + return 0; | ||
| 70 | +} | ||
| 39 | 71 | ||
| 40 | - CHECK_ERROR(aclrtQueryDeviceStatus(0, &deviceStatus)); | 72 | +void RunThread() |
| 41 | - INFO_LOG("Query device status success. deviceStatus: %d.", deviceStatus); | 73 | +{ |
| 74 | + aclrtStream stream = nullptr; | ||
| 75 | + uint32_t resLimitValue = 0; | ||
| 42 | 76 | ||
| 43 | - CHECK_ERROR(aclrtGetRunMode(&runMode)); | 77 | + auto ret = Init(&stream); |
| 44 | - if (runMode == 0) { | 78 | + if (ret != 0) { |
| 45 | - INFO_LOG("RunMode is ACL_DEVICE."); | 79 | + return; |
| 46 | - } else if (runMode == 1) { | ||
| 47 | - INFO_LOG("RunMode is ACL_HOST."); | ||
| 48 | - } else { | ||
| 49 | - INFO_LOG("RunMode is invalid."); | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - aclrtUtilizationInfo utilizationInfo; | ||
| 53 | - utilizationInfo.utilizationExtend = nullptr; | ||
| 54 | - CHECK_ERROR(aclrtGetDeviceUtilizationRate(0, &utilizationInfo)); | ||
| 55 | - INFO_LOG("Get device utilizationRate success. cubeUtilization %d, vectorUtilization %d, aicpuUtilization %d," | ||
| 56 | - "memoryUtilization %d.", | ||
| 57 | - utilizationInfo.cubeUtilization, utilizationInfo.vectorUtilization, utilizationInfo.aicpuUtilization, | ||
| 58 | - utilizationInfo.memoryUtilization); | ||
| 59 | - | ||
| 60 | - int32_t leastPriority; | ||
| 61 | - int32_t greatestPriority; | ||
| 62 | - CHECK_ERROR(aclrtDeviceGetStreamPriorityRange(&leastPriority, &greatestPriority)); | ||
| 63 | - INFO_LOG("Get stream priorityRange success. leastPriority %d, greatestPriority %d.", leastPriority, | ||
| 64 | - greatestPriority); | ||
| 65 | - | ||
| 66 | - CHECK_ERROR(aclrtGetDeviceInfo(0, ACL_DEV_ATTR_VECTOR_CORE_NUM, &vectorCoreNum)); | ||
| 67 | - INFO_LOG("Get device info success. vectorCoreNum %ld.", vectorCoreNum); | ||
| 68 | - return 0; | ||
| 69 | } | 80 | } |
| 70 | 81 | ||
| 71 | - void RunThread() | 82 | + ret = DeviceInfoQuery(); |
| 72 | - { | 83 | + if (ret != 0) { |
| 73 | - aclrtStream stream = nullptr; | 84 | + return; |
| 74 | - uint32_t resLimitValue = 0; | ||
| 75 | - | ||
| 76 | - auto ret = Init(&stream); | ||
| 77 | - if (ret != 0) { | ||
| 78 | - return; | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - ret = DeviceInfoQuery(); | ||
| 82 | - if (ret != 0) { | ||
| 83 | - return; | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - // Define the parameters of the kernel function | ||
| 87 | - const uint32_t TOTAL_SIZE = 1024; | ||
| 88 | - const uint32_t DATA_SIZE = TOTAL_SIZE * static_cast<uint32_t>(sizeof(float)); | ||
| 89 | - | ||
| 90 | - // Allocate memory on the host | ||
| 91 | - float *hostSrcA, *hostSrcB, *hostDst; | ||
| 92 | - aclrtMallocHost(reinterpret_cast<void **>(&hostSrcA), DATA_SIZE); | ||
| 93 | - aclrtMallocHost(reinterpret_cast<void **>(&hostSrcB), DATA_SIZE); | ||
| 94 | - aclrtMallocHost(reinterpret_cast<void **>(&hostDst), DATA_SIZE); | ||
| 95 | - | ||
| 96 | - // Allocate memory on the device | ||
| 97 | - float *devSrcA, *devSrcB, *devDst; | ||
| 98 | - aclrtMalloc(reinterpret_cast<void **>(&devSrcA), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 99 | - aclrtMalloc(reinterpret_cast<void **>(&devSrcB), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 100 | - aclrtMalloc(reinterpret_cast<void **>(&devDst), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 101 | - | ||
| 102 | - // Initialize the test data | ||
| 103 | - for (uint32_t i = 0; i < TOTAL_SIZE; ++i) { | ||
| 104 | - hostSrcA[i] = static_cast<float>(i); | ||
| 105 | - hostSrcB[i] = static_cast<float>(i); | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - aclrtMemcpy(devSrcA, DATA_SIZE, hostSrcA, DATA_SIZE, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 109 | - aclrtMemcpy(devSrcB, DATA_SIZE, hostSrcB, DATA_SIZE, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 110 | - | ||
| 111 | - aclrtGetDeviceResLimit(0, ACL_RT_DEV_RES_VECTOR_CORE, &resLimitValue); | ||
| 112 | - INFO_LOG("Get device resLimit success. VECTOR_CORE %d.", resLimitValue); | ||
| 113 | - AddDo(resLimitValue, stream, devSrcA, devSrcB, devDst, TOTAL_SIZE); | ||
| 114 | - aclrtSynchronizeStream(stream); | ||
| 115 | - aclrtMemcpy(hostDst, DATA_SIZE, devDst, DATA_SIZE, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 116 | - | ||
| 117 | - INFO_LOG("The results (first 10 elements) of the kernel function:"); | ||
| 118 | - for (int i = 0; i < 10; ++i) { // Display the first 10 elements | ||
| 119 | - INFO_LOG("Result: hostDst[%d]: %f Expected value: %f", i, hostDst[i], hostSrcA[i] + hostSrcB[i]); | ||
| 120 | - } | ||
| 121 | - | ||
| 122 | - aclrtResetDeviceResLimit(0); | ||
| 123 | - | ||
| 124 | - aclrtFree(devSrcA); | ||
| 125 | - aclrtFree(devSrcB); | ||
| 126 | - aclrtFree(devDst); | ||
| 127 | - aclrtFreeHost(hostSrcA); | ||
| 128 | - aclrtFreeHost(hostSrcB); | ||
| 129 | - aclrtFreeHost(hostDst); | ||
| 130 | - | ||
| 131 | - aclrtDestroyStream(stream); | ||
| 132 | - aclrtResetDeviceForce(0); | ||
| 133 | } | 85 | } |
| 86 | + | ||
| 87 | + // Define the parameters of the kernel function | ||
| 88 | + const uint32_t TOTAL_SIZE = 1024; | ||
| 89 | + const uint32_t DATA_SIZE = TOTAL_SIZE * static_cast<uint32_t>(sizeof(float)); | ||
| 90 | + | ||
| 91 | + // Allocate memory on the host | ||
| 92 | + float *hostSrcA, *hostSrcB, *hostDst; | ||
| 93 | + aclrtMallocHost(reinterpret_cast<void**>(&hostSrcA), DATA_SIZE); | ||
| 94 | + aclrtMallocHost(reinterpret_cast<void**>(&hostSrcB), DATA_SIZE); | ||
| 95 | + aclrtMallocHost(reinterpret_cast<void**>(&hostDst), DATA_SIZE); | ||
| 96 | + | ||
| 97 | + // Allocate memory on the device | ||
| 98 | + float *devSrcA, *devSrcB, *devDst; | ||
| 99 | + aclrtMalloc(reinterpret_cast<void**>(&devSrcA), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 100 | + aclrtMalloc(reinterpret_cast<void**>(&devSrcB), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 101 | + aclrtMalloc(reinterpret_cast<void**>(&devDst), DATA_SIZE, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 102 | + | ||
| 103 | + // Initialize the test data | ||
| 104 | + for (uint32_t i = 0; i < TOTAL_SIZE; ++i) { | ||
| 105 | + hostSrcA[i] = static_cast<float>(i); | ||
| 106 | + hostSrcB[i] = static_cast<float>(i); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + aclrtMemcpy(devSrcA, DATA_SIZE, hostSrcA, DATA_SIZE, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 110 | + aclrtMemcpy(devSrcB, DATA_SIZE, hostSrcB, DATA_SIZE, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 111 | + | ||
| 112 | + aclrtGetDeviceResLimit(0, ACL_RT_DEV_RES_VECTOR_CORE, &resLimitValue); | ||
| 113 | + INFO_LOG("Get device resLimit success. VECTOR_CORE %d.", resLimitValue); | ||
| 114 | + AddDo(resLimitValue, stream, devSrcA, devSrcB, devDst, TOTAL_SIZE); | ||
| 115 | + aclrtSynchronizeStream(stream); | ||
| 116 | + aclrtMemcpy(hostDst, DATA_SIZE, devDst, DATA_SIZE, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 117 | + | ||
| 118 | + INFO_LOG("The results (first 10 elements) of the kernel function:"); | ||
| 119 | + for (int i = 0; i < 10; ++i) { // Display the first 10 elements | ||
| 120 | + INFO_LOG("Result: hostDst[%d]: %f Expected value: %f", i, hostDst[i], hostSrcA[i] + hostSrcB[i]); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + aclrtResetDeviceResLimit(0); | ||
| 124 | + | ||
| 125 | + aclrtFree(devSrcA); | ||
| 126 | + aclrtFree(devSrcB); | ||
| 127 | + aclrtFree(devDst); | ||
| 128 | + aclrtFreeHost(hostSrcA); | ||
| 129 | + aclrtFreeHost(hostSrcB); | ||
| 130 | + aclrtFreeHost(hostDst); | ||
| 131 | + | ||
| 132 | + aclrtDestroyStream(stream); | ||
| 133 | + aclrtResetDeviceForce(0); | ||
| 134 | +} | ||
| 134 | } // namespace | 135 | } // namespace |
| 135 | 136 | ||
| 136 | -int32_t main(int argc, char const *argv[]) | 137 | +int32_t main(int argc, char const* argv[]) |
| 137 | { | 138 | { |
| 138 | INFO_LOG("Start to run device_multi_thread sample."); | 139 | INFO_LOG("Start to run device_multi_thread sample."); |
| 139 | CHECK_ERROR(aclInit(nullptr)); | 140 | CHECK_ERROR(aclInit(nullptr)); |
| @@ -16,7 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | constexpr uint32_t UINT8_DATA_LEN = 256; | 17 | constexpr uint32_t UINT8_DATA_LEN = 256; |
| 18 | 18 | ||
| 19 | -int32_t main(int argc, const char *argv[]) | 19 | +int32_t main(int argc, const char* argv[]) |
| 20 | { | 20 | { |
| 21 | INFO_LOG("Start to run device_P2P sample."); | 21 | INFO_LOG("Start to run device_P2P sample."); |
| 22 | CHECK_ERROR(aclInit(NULL)); | 22 | CHECK_ERROR(aclInit(NULL)); |
| @@ -62,7 +62,7 @@ int32_t main(int argc, const char *argv[]) | |||
| 62 | CHECK_ERROR(aclrtMallocHost(&host, memSize)); | 62 | CHECK_ERROR(aclrtMallocHost(&host, memSize)); |
| 63 | CHECK_ERROR(aclrtMemset(host, memSize, 0, memSize)); | 63 | CHECK_ERROR(aclrtMemset(host, memSize, 0, memSize)); |
| 64 | 64 | ||
| 65 | - char *char_ptr = (char *)host; | 65 | + char* char_ptr = (char*)host; |
| 66 | CHECK_ERROR(aclrtMemcpy(host, memSize, dev1, memSize, ACL_MEMCPY_DEVICE_TO_HOST)); | 66 | CHECK_ERROR(aclrtMemcpy(host, memSize, dev1, memSize, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 67 | INFO_LOG("Device 1 to host memcpy success."); | 67 | INFO_LOG("Device 1 to host memcpy success."); |
| 68 | 68 | ||
| @@ -21,7 +21,7 @@ int main() | |||
| 21 | int32_t deviceId = 0; | 21 | int32_t deviceId = 0; |
| 22 | uint32_t blockDim = 1; | 22 | uint32_t blockDim = 1; |
| 23 | uint32_t num = 0; | 23 | uint32_t num = 0; |
| 24 | - uint32_t *numDevice = nullptr; | 24 | + uint32_t* numDevice = nullptr; |
| 25 | size_t size = sizeof(uint32_t); | 25 | size_t size = sizeof(uint32_t); |
| 26 | aclrtContext context; | 26 | aclrtContext context; |
| 27 | aclrtStream stream = nullptr; | 27 | aclrtStream stream = nullptr; |
| @@ -33,17 +33,17 @@ int main() | |||
| 33 | CHECK_ERROR(aclrtSetDevice(deviceId)); | 33 | CHECK_ERROR(aclrtSetDevice(deviceId)); |
| 34 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); | 34 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); |
| 35 | CHECK_ERROR(aclrtCreateStream(&stream)); | 35 | CHECK_ERROR(aclrtCreateStream(&stream)); |
| 36 | - | 36 | + |
| 37 | // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 | 37 | // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 |
| 38 | CHECK_ERROR(aclrtSetStreamFailureMode(stream, ACL_STOP_ON_FAILURE)); | 38 | CHECK_ERROR(aclrtSetStreamFailureMode(stream, ACL_STOP_ON_FAILURE)); |
| 39 | CHECK_ERROR(aclrtCreateEvent(&event)); | 39 | CHECK_ERROR(aclrtCreateEvent(&event)); |
| 40 | - | 40 | + |
| 41 | // 查询创建后的event状态 | 41 | // 查询创建后的event状态 |
| 42 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); | 42 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); |
| 43 | INFO_LOG("After create event, current event status is %d.", eventStatus); | 43 | INFO_LOG("After create event, current event status is %d.", eventStatus); |
| 44 | - | 44 | + |
| 45 | // 开始做长任务 | 45 | // 开始做长任务 |
| 46 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 46 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 47 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 47 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 48 | INFO_LOG("Applied resource successfully, begin assigning task."); | 48 | INFO_LOG("Applied resource successfully, begin assigning task."); |
| 49 | INFO_LOG("Begin a long task."); | 49 | INFO_LOG("Begin a long task."); |
| @@ -54,7 +54,7 @@ int main() | |||
| 54 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); | 54 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); |
| 55 | INFO_LOG("0 is incompleted, 1 is completed."); | 55 | INFO_LOG("0 is incompleted, 1 is completed."); |
| 56 | INFO_LOG("After record but before synchronize, current event status is %d.", eventStatus); | 56 | INFO_LOG("After record but before synchronize, current event status is %d.", eventStatus); |
| 57 | - | 57 | + |
| 58 | // 查询同步后的event状态 | 58 | // 查询同步后的event状态 |
| 59 | CHECK_ERROR(aclrtSynchronizeEvent(event)); | 59 | CHECK_ERROR(aclrtSynchronizeEvent(event)); |
| 60 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); | 60 | CHECK_ERROR(aclrtQueryEventStatus(event, &eventStatus)); |
| @@ -23,7 +23,7 @@ int main() | |||
| 23 | int32_t deviceId = 0; | 23 | int32_t deviceId = 0; |
| 24 | uint32_t blockDim = 1; | 24 | uint32_t blockDim = 1; |
| 25 | uint32_t num = 0; | 25 | uint32_t num = 0; |
| 26 | - uint32_t *numDevice = nullptr; | 26 | + uint32_t* numDevice = nullptr; |
| 27 | size_t size = sizeof(uint32_t); | 27 | size_t size = sizeof(uint32_t); |
| 28 | uint64_t time = 0; | 28 | uint64_t time = 0; |
| 29 | float useTime = 0; | 29 | float useTime = 0; |
| @@ -43,7 +43,7 @@ int main() | |||
| 43 | CHECK_ERROR(aclrtCreateEvent(&endEvent)); | 43 | CHECK_ERROR(aclrtCreateEvent(&endEvent)); |
| 44 | 44 | ||
| 45 | // 先做一个短时任务查看耗时 | 45 | // 先做一个短时任务查看耗时 |
| 46 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 46 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 47 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 47 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 48 | INFO_LOG("Begin a short task."); | 48 | INFO_LOG("Begin a short task."); |
| 49 | // 做任务前记录一次,做任务后再记录一次,相差时间则为任务耗时。 | 49 | // 做任务前记录一次,做任务后再记录一次,相差时间则为任务耗时。 |
| @@ -62,10 +62,10 @@ int main() | |||
| 62 | INFO_LOG("End event timestamp: %ld.", time); | 62 | INFO_LOG("End event timestamp: %ld.", time); |
| 63 | CHECK_ERROR(aclrtEventElapsedTime(&useTime, startEvent, endEvent)); | 63 | CHECK_ERROR(aclrtEventElapsedTime(&useTime, startEvent, endEvent)); |
| 64 | INFO_LOG("The short task consume time %f ms.", useTime); | 64 | INFO_LOG("The short task consume time %f ms.", useTime); |
| 65 | - CHECK_ERROR(aclrtFree(numDevice));// 释放短任务分配的内存 | 65 | + CHECK_ERROR(aclrtFree(numDevice)); // 释放短任务分配的内存 |
| 66 | 66 | ||
| 67 | // 再做一个长耗时任务查看耗时 | 67 | // 再做一个长耗时任务查看耗时 |
| 68 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 68 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 69 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 69 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 70 | INFO_LOG("Begin a long task."); | 70 | INFO_LOG("Begin a long task."); |
| 71 | CHECK_ERROR(aclrtRecordEvent(startEvent, stream)); | 71 | CHECK_ERROR(aclrtRecordEvent(startEvent, stream)); |
| @@ -26,8 +26,8 @@ int32_t main() | |||
| 26 | 26 | ||
| 27 | // Allocate memory on the host | 27 | // Allocate memory on the host |
| 28 | uint64_t size = 1 * 1024 * 1024; | 28 | uint64_t size = 1 * 1024 * 1024; |
| 29 | - int *hostPtrA; | 29 | + int* hostPtrA; |
| 30 | - int *hostPtrB; | 30 | + int* hostPtrB; |
| 31 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); | 31 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); |
| 32 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); | 32 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); |
| 33 | 33 | ||
| @@ -31,7 +31,7 @@ int32_t main() | |||
| 31 | 31 | ||
| 32 | // Allocate memory on the device | 32 | // Allocate memory on the device |
| 33 | uint64_t size = 1 * 1024 * 1024; | 33 | uint64_t size = 1 * 1024 * 1024; |
| 34 | - int *devPtr; | 34 | + int* devPtr; |
| 35 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 35 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 36 | INFO_LOG("Allocate memory on the device %p successfully", devPtr); | 36 | INFO_LOG("Allocate memory on the device %p successfully", devPtr); |
| 37 | 37 | ||
| @@ -43,7 +43,7 @@ int32_t main() | |||
| 43 | INFO_LOG("Process B: get the shareable memory identifier successfully, shareable identifier = %s", memName); | 43 | INFO_LOG("Process B: get the shareable memory identifier successfully, shareable identifier = %s", memName); |
| 44 | 44 | ||
| 45 | // Return a device memory pointer accessible to Process B | 45 | // Return a device memory pointer accessible to Process B |
| 46 | - void *devPtr = nullptr; | 46 | + void* devPtr = nullptr; |
| 47 | uint64_t size = 1 * 1024 * 1024; | 47 | uint64_t size = 1 * 1024 * 1024; |
| 48 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); | 48 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); |
| 49 | 49 | ||
| @@ -31,7 +31,7 @@ int32_t main() | |||
| 31 | 31 | ||
| 32 | // Allocate memory on the device | 32 | // Allocate memory on the device |
| 33 | uint64_t size = 1 * 1024 * 1024; | 33 | uint64_t size = 1 * 1024 * 1024; |
| 34 | - int *devPtr; | 34 | + int* devPtr; |
| 35 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 35 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 36 | INFO_LOG("Allocate memory on the device %p successfully", devPtr); | 36 | INFO_LOG("Allocate memory on the device %p successfully", devPtr); |
| 37 | 37 | ||
| @@ -33,7 +33,7 @@ int32_t main() | |||
| 33 | INFO_LOG("Process B: get the shareable memory identifier successfully, shareable identifier = %s", memName); | 33 | INFO_LOG("Process B: get the shareable memory identifier successfully, shareable identifier = %s", memName); |
| 34 | 34 | ||
| 35 | // Return a device memory pointer accessible to Process B | 35 | // Return a device memory pointer accessible to Process B |
| 36 | - void *devPtr = nullptr; | 36 | + void* devPtr = nullptr; |
| 37 | uint64_t size = 1 * 1024 * 1024; | 37 | uint64_t size = 1 * 1024 * 1024; |
| 38 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); | 38 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); |
| 39 | 39 | ||
| @@ -72,7 +72,7 @@ int32_t main(int argc, char* argv[]) | |||
| 72 | aclrtMemFabricHandle shareableHandle = {}; | 72 | aclrtMemFabricHandle shareableHandle = {}; |
| 73 | aclrtDrvMemHandle handle = nullptr; | 73 | aclrtDrvMemHandle handle = nullptr; |
| 74 | socklen_t server_len = sizeof(server_addr); | 74 | socklen_t server_len = sizeof(server_addr); |
| 75 | - int recv_len = | 75 | + int recv_len = |
| 76 | recvfrom(sockfd, &shareableHandle, sizeof(shareableHandle), 0, (struct sockaddr*)&server_addr, &server_len); | 76 | recvfrom(sockfd, &shareableHandle, sizeof(shareableHandle), 0, (struct sockaddr*)&server_addr, &server_len); |
| 77 | if (recv_len < 0) { | 77 | if (recv_len < 0) { |
| 78 | close(sockfd); | 78 | close(sockfd); |
| @@ -80,8 +80,8 @@ int32_t main(int argc, char* argv[]) | |||
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | // Import shareable handle from server | 82 | // Import shareable handle from server |
| 83 | - CHECK_ERROR(aclrtMemImportFromShareableHandleV2(&shareableHandle, ACL_MEM_SHARE_HANDLE_TYPE_FABRIC, | 83 | + CHECK_ERROR(aclrtMemImportFromShareableHandleV2( |
| 84 | - ACL_RT_IPC_MEM_EXPORT_FLAG_DEFAULT, &handle)); | 84 | + &shareableHandle, ACL_MEM_SHARE_HANDLE_TYPE_FABRIC, ACL_RT_IPC_MEM_EXPORT_FLAG_DEFAULT, &handle)); |
| 85 | INFO_LOG("Client: import shareable handle successfully"); | 85 | INFO_LOG("Client: import shareable handle successfully"); |
| 86 | 86 | ||
| 87 | const size_t data_size = 1024 * sizeof(float); | 87 | const size_t data_size = 1024 * sizeof(float); |
| @@ -85,8 +85,8 @@ int32_t main(int argc, char* argv[]) | |||
| 85 | // Export a shareable handle | 85 | // Export a shareable handle |
| 86 | aclrtMemFabricHandle shareableHandle = {}; | 86 | aclrtMemFabricHandle shareableHandle = {}; |
| 87 | aclrtMemSharedHandleType shareType = ACL_MEM_SHARE_HANDLE_TYPE_FABRIC; | 87 | aclrtMemSharedHandleType shareType = ACL_MEM_SHARE_HANDLE_TYPE_FABRIC; |
| 88 | - CHECK_ERROR(aclrtMemExportToShareableHandleV2(handle, ACL_RT_IPC_MEM_EXPORT_FLAG_DISABLE_PID_VALIDATION, | 88 | + CHECK_ERROR(aclrtMemExportToShareableHandleV2( |
| 89 | - shareType, &shareableHandle)); | 89 | + handle, ACL_RT_IPC_MEM_EXPORT_FLAG_DISABLE_PID_VALIDATION, shareType, &shareableHandle)); |
| 90 | INFO_LOG("Server: export shareable handle successfully"); | 90 | INFO_LOG("Server: export shareable handle successfully"); |
| 91 | 91 | ||
| 92 | // Transfer the shareable handle to client by UDP | 92 | // Transfer the shareable handle to client by UDP |
| @@ -143,7 +143,7 @@ int32_t main(int argc, char* argv[]) | |||
| 143 | close(sockfd); | 143 | close(sockfd); |
| 144 | INFO_LOG("Server: ipc close successfully"); | 144 | INFO_LOG("Server: ipc close successfully"); |
| 145 | 145 | ||
| 146 | - // Release memory resources | 146 | + // Release memory resources |
| 147 | CHECK_ERROR(aclrtUnmapMem(virPtr)); | 147 | CHECK_ERROR(aclrtUnmapMem(virPtr)); |
| 148 | CHECK_ERROR(aclrtReleaseMemAddress(virPtr)); | 148 | CHECK_ERROR(aclrtReleaseMemAddress(virPtr)); |
| 149 | CHECK_ERROR(aclrtFreePhysical(handle)); | 149 | CHECK_ERROR(aclrtFreePhysical(handle)); |
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the host and device | 28 | // Allocate memory on the host and device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *hostPtrA; | 30 | + int* hostPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); | 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); |
| 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); | 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); |
| 34 | 34 | ||
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the host and device | 28 | // Allocate memory on the host and device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *hostPtrA; | 30 | + int* hostPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); | 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); |
| 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); | 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); |
| 34 | 34 | ||
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the host and device | 28 | // Allocate memory on the host and device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *hostPtrA; | 30 | + int* hostPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); | 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); |
| 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); | 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); |
| 34 | 34 | ||
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the host and device | 28 | // Allocate memory on the host and device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *hostPtrA; | 30 | + int* hostPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); | 32 | CHECK_ERROR(aclrtMallocHost((void**)&hostPtrA, size)); |
| 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); | 33 | INFO_LOG("Allocate memory on the host memory %p successfully", hostPtrA); |
| 34 | 34 | ||
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the device | 28 | // Allocate memory on the device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *devPtrA; | 30 | + int* devPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMalloc((void**)&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 32 | CHECK_ERROR(aclrtMalloc((void**)&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 33 | INFO_LOG("Allocate memory on the device memory %p successfully", devPtrA); | 33 | INFO_LOG("Allocate memory on the device memory %p successfully", devPtrA); |
| 34 | 34 | ||
| @@ -27,8 +27,8 @@ int32_t main() | |||
| 27 | 27 | ||
| 28 | // Allocate memory on the device | 28 | // Allocate memory on the device |
| 29 | uint64_t size = 1 * 1024 * 1024; | 29 | uint64_t size = 1 * 1024 * 1024; |
| 30 | - int *devPtrA; | 30 | + int* devPtrA; |
| 31 | - int *devPtrB; | 31 | + int* devPtrB; |
| 32 | CHECK_ERROR(aclrtMalloc((void**)&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 32 | CHECK_ERROR(aclrtMalloc((void**)&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 33 | INFO_LOG("Allocate memory on the device memory %p successfully", devPtrA); | 33 | INFO_LOG("Allocate memory on the device memory %p successfully", devPtrA); |
| 34 | 34 | ||
| @@ -38,10 +38,12 @@ int32_t main() | |||
| 38 | 38 | ||
| 39 | size_t granularity = 0UL; | 39 | size_t granularity = 0UL; |
| 40 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); | 40 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); |
| 41 | - INFO_LOG("Process A: get memory allocation granularity successfully, granularity = %d", static_cast<int32_t>(granularity)); | 41 | + INFO_LOG( |
| 42 | + "Process A: get memory allocation granularity successfully, granularity = %d", | ||
| 43 | + static_cast<int32_t>(granularity)); | ||
| 42 | 44 | ||
| 43 | // Start allocating physical memory based on memory allocation granularity | 45 | // Start allocating physical memory based on memory allocation granularity |
| 44 | - size_t alignedSize = ((dataSize + granularity - 1U) / granularity) * granularity; | 46 | + size_t alignedSize = ((dataSize + granularity - 1U) / granularity) * granularity; |
| 45 | INFO_LOG("Process A: aligned size = %zu", alignedSize); | 47 | INFO_LOG("Process A: aligned size = %zu", alignedSize); |
| 46 | 48 | ||
| 47 | aclrtDrvMemHandle handle = nullptr; | 49 | aclrtDrvMemHandle handle = nullptr; |
| @@ -49,7 +51,7 @@ int32_t main() | |||
| 49 | INFO_LOG("Process A: allocate physical memory successfully"); | 51 | INFO_LOG("Process A: allocate physical memory successfully"); |
| 50 | 52 | ||
| 51 | // Reserve virtual memory | 53 | // Reserve virtual memory |
| 52 | - void *virPtr; | 54 | + void* virPtr; |
| 53 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); | 55 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); |
| 54 | INFO_LOG("Process A: reserve virtual memory successfully"); | 56 | INFO_LOG("Process A: reserve virtual memory successfully"); |
| 55 | 57 | ||
| @@ -68,13 +70,15 @@ int32_t main() | |||
| 68 | // Write the value to the virtual memory | 70 | // Write the value to the virtual memory |
| 69 | constexpr uint32_t blockDim = 1; | 71 | constexpr uint32_t blockDim = 1; |
| 70 | int writeValue = 123; | 72 | int writeValue = 123; |
| 71 | - WriteDo(blockDim, stream, (int *)virPtr, writeValue); | 73 | + WriteDo(blockDim, stream, (int*)virPtr, writeValue); |
| 72 | INFO_LOG("Write data %d to the virtual address %p", writeValue, virPtr); | 74 | INFO_LOG("Write data %d to the virtual address %p", writeValue, virPtr); |
| 73 | 75 | ||
| 74 | // Export a shareable handle | 76 | // Export a shareable handle |
| 75 | uint64_t shareableHandle = 0ULL; | 77 | uint64_t shareableHandle = 0ULL; |
| 76 | CHECK_ERROR(aclrtMemExportToShareableHandle(handle, ACL_MEM_HANDLE_TYPE_NONE, 0, &shareableHandle)); | 78 | CHECK_ERROR(aclrtMemExportToShareableHandle(handle, ACL_MEM_HANDLE_TYPE_NONE, 0, &shareableHandle)); |
| 77 | - INFO_LOG("Process A: export a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | 79 | + INFO_LOG( |
| 80 | + "Process A: export a shareable handle successfully, shareable handle = %d", | ||
| 81 | + static_cast<int32_t>(shareableHandle)); | ||
| 78 | 82 | ||
| 79 | // Read Process B's pid from the file | 83 | // Read Process B's pid from the file |
| 80 | int32_t pid = 0; | 84 | int32_t pid = 0; |
| @@ -39,7 +39,8 @@ int32_t main() | |||
| 39 | uint64_t shareableHandle = 0ULL; | 39 | uint64_t shareableHandle = 0ULL; |
| 40 | aclrtDrvMemHandle handle = nullptr; | 40 | aclrtDrvMemHandle handle = nullptr; |
| 41 | memory::ReadFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); | 41 | memory::ReadFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); |
| 42 | - INFO_LOG("Process B: get a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | 42 | + INFO_LOG( |
| 43 | + "Process B: get a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | ||
| 43 | 44 | ||
| 44 | CHECK_ERROR(aclrtMemImportFromShareableHandle(shareableHandle, deviceId, &handle)); | 45 | CHECK_ERROR(aclrtMemImportFromShareableHandle(shareableHandle, deviceId, &handle)); |
| 45 | 46 | ||
| @@ -54,10 +55,12 @@ int32_t main() | |||
| 54 | 55 | ||
| 55 | size_t granularity = 0UL; | 56 | size_t granularity = 0UL; |
| 56 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); | 57 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); |
| 57 | - INFO_LOG("Process B: get memory allocation granularity successfully, granularity = %d", static_cast<int32_t>(granularity)); | 58 | + INFO_LOG( |
| 59 | + "Process B: get memory allocation granularity successfully, granularity = %d", | ||
| 60 | + static_cast<int32_t>(granularity)); | ||
| 58 | 61 | ||
| 59 | // Reserve virtual memory | 62 | // Reserve virtual memory |
| 60 | - void *virPtr = nullptr; | 63 | + void* virPtr = nullptr; |
| 61 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); | 64 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); |
| 62 | INFO_LOG("Process B: reserve virtual memory successfully"); | 65 | INFO_LOG("Process B: reserve virtual memory successfully"); |
| 63 | 66 | ||
| @@ -74,7 +77,7 @@ int32_t main() | |||
| 74 | INFO_LOG("Process B: set memory access permissions successfully"); | 77 | INFO_LOG("Process B: set memory access permissions successfully"); |
| 75 | 78 | ||
| 76 | // Copy memory from device to host | 79 | // Copy memory from device to host |
| 77 | - int *hostPtrA; | 80 | + int* hostPtrA; |
| 78 | CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&hostPtrA), granularity)); | 81 | CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&hostPtrA), granularity)); |
| 79 | CHECK_ERROR(aclrtMemcpy(hostPtrA, granularity, virPtr, granularity, ACL_MEMCPY_DEVICE_TO_HOST)); | 82 | CHECK_ERROR(aclrtMemcpy(hostPtrA, granularity, virPtr, granularity, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 80 | INFO_LOG("Process B: copy memory from device address %p to host address %p", virPtr, hostPtrA); | 83 | INFO_LOG("Process B: copy memory from device address %p to host address %p", virPtr, hostPtrA); |
| @@ -102,4 +105,4 @@ int32_t main() | |||
| 102 | aclrtResetDeviceForce(deviceId); | 105 | aclrtResetDeviceForce(deviceId); |
| 103 | aclFinalize(); | 106 | aclFinalize(); |
| 104 | return 0; | 107 | return 0; |
| 105 | -} | 108 | +} |
| @@ -13,7 +13,6 @@ | |||
| 13 | * using two independent processes (i.e., Process A and Process B) on a single device. | 13 | * using two independent processes (i.e., Process A and Process B) on a single device. |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | - | ||
| 17 | 16 | ||
| 18 | 17 | ||
| 19 | 18 | ||
| @@ -39,7 +38,9 @@ int32_t main() | |||
| 39 | 38 | ||
| 40 | size_t granularity = 0UL; | 39 | size_t granularity = 0UL; |
| 41 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); | 40 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); |
| 42 | - INFO_LOG("Process A: get memory allocation granularity successfully, granularity = %d", static_cast<int32_t>(granularity)); | 41 | + INFO_LOG( |
| 42 | + "Process A: get memory allocation granularity successfully, granularity = %d", | ||
| 43 | + static_cast<int32_t>(granularity)); | ||
| 43 | 44 | ||
| 44 | // Start allocating physical memory based on memory allocation granularity | 45 | // Start allocating physical memory based on memory allocation granularity |
| 45 | size_t alignedSize = ((dataSize + granularity - 1U) / granularity) * granularity; | 46 | size_t alignedSize = ((dataSize + granularity - 1U) / granularity) * granularity; |
| @@ -50,7 +51,7 @@ int32_t main() | |||
| 50 | INFO_LOG("Process A: allocate physical memory successfully"); | 51 | INFO_LOG("Process A: allocate physical memory successfully"); |
| 51 | 52 | ||
| 52 | // Reserve virtual memory | 53 | // Reserve virtual memory |
| 53 | - void *virPtr; | 54 | + void* virPtr; |
| 54 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); | 55 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); |
| 55 | INFO_LOG("Process A: reserve virtual memory successfully"); | 56 | INFO_LOG("Process A: reserve virtual memory successfully"); |
| 56 | 57 | ||
| @@ -69,13 +70,15 @@ int32_t main() | |||
| 69 | // Write the value to the virtual memory | 70 | // Write the value to the virtual memory |
| 70 | constexpr uint32_t blockDim = 1; | 71 | constexpr uint32_t blockDim = 1; |
| 71 | int writeValue = 123; | 72 | int writeValue = 123; |
| 72 | - WriteDo(blockDim, stream, (int *)virPtr, writeValue); | 73 | + WriteDo(blockDim, stream, (int*)virPtr, writeValue); |
| 73 | INFO_LOG("Write the data %d to the virtual address %p", writeValue, virPtr); | 74 | INFO_LOG("Write the data %d to the virtual address %p", writeValue, virPtr); |
| 74 | 75 | ||
| 75 | // Export a shareable handle | 76 | // Export a shareable handle |
| 76 | uint64_t shareableHandle = 0ULL; | 77 | uint64_t shareableHandle = 0ULL; |
| 77 | CHECK_ERROR(aclrtMemExportToShareableHandle(handle, ACL_MEM_HANDLE_TYPE_NONE, 1, &shareableHandle)); | 78 | CHECK_ERROR(aclrtMemExportToShareableHandle(handle, ACL_MEM_HANDLE_TYPE_NONE, 1, &shareableHandle)); |
| 78 | - INFO_LOG("Process A: export a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | 79 | + INFO_LOG( |
| 80 | + "Process A: export a shareable handle successfully, shareable handle = %d", | ||
| 81 | + static_cast<int32_t>(shareableHandle)); | ||
| 79 | 82 | ||
| 80 | // Transfer the shareable handle to Process B by writing it to the file | 83 | // Transfer the shareable handle to Process B by writing it to the file |
| 81 | memory::WriteFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); | 84 | memory::WriteFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); |
| @@ -13,7 +13,6 @@ | |||
| 13 | * using two independent processes (i.e., Process A and Process B) on a single device. | 13 | * using two independent processes (i.e., Process A and Process B) on a single device. |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | - | ||
| 17 | 16 | ||
| 18 | 17 | ||
| 19 | 18 | ||
| @@ -30,7 +29,8 @@ int32_t main() | |||
| 30 | uint64_t shareableHandle = 0ULL; | 29 | uint64_t shareableHandle = 0ULL; |
| 31 | aclrtDrvMemHandle handle = nullptr; | 30 | aclrtDrvMemHandle handle = nullptr; |
| 32 | memory::ReadFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); | 31 | memory::ReadFile("file/handle.bin", "file/handle.bin.done", &shareableHandle, sizeof(shareableHandle)); |
| 33 | - INFO_LOG("Process B: get a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | 32 | + INFO_LOG( |
| 33 | + "Process B: get a shareable handle successfully, shareable handle = %d", static_cast<int32_t>(shareableHandle)); | ||
| 34 | 34 | ||
| 35 | CHECK_ERROR(aclrtMemImportFromShareableHandle(shareableHandle, deviceId, &handle)); | 35 | CHECK_ERROR(aclrtMemImportFromShareableHandle(shareableHandle, deviceId, &handle)); |
| 36 | 36 | ||
| @@ -45,10 +45,12 @@ int32_t main() | |||
| 45 | 45 | ||
| 46 | size_t granularity = 0UL; | 46 | size_t granularity = 0UL; |
| 47 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); | 47 | CHECK_ERROR(aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_MINIMUM, &granularity)); |
| 48 | - INFO_LOG("Process B: get memory allocation granularity successfully, granularity = %d", static_cast<int32_t>(granularity)); | 48 | + INFO_LOG( |
| 49 | + "Process B: get memory allocation granularity successfully, granularity = %d", | ||
| 50 | + static_cast<int32_t>(granularity)); | ||
| 49 | 51 | ||
| 50 | // Reserve virtual memory | 52 | // Reserve virtual memory |
| 51 | - void *virPtr = nullptr; | 53 | + void* virPtr = nullptr; |
| 52 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); | 54 | CHECK_ERROR(aclrtReserveMemAddress(&virPtr, granularity, 0, nullptr, 0)); |
| 53 | INFO_LOG("Process B: reserve virtual memory successfully"); | 55 | INFO_LOG("Process B: reserve virtual memory successfully"); |
| 54 | 56 | ||
| @@ -65,7 +67,7 @@ int32_t main() | |||
| 65 | INFO_LOG("Process B: set memory access permissions successfully"); | 67 | INFO_LOG("Process B: set memory access permissions successfully"); |
| 66 | 68 | ||
| 67 | // Copy memory from device to host | 69 | // Copy memory from device to host |
| 68 | - int *hostPtrA; | 70 | + int* hostPtrA; |
| 69 | CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&hostPtrA), granularity)); | 71 | CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&hostPtrA), granularity)); |
| 70 | CHECK_ERROR(aclrtMemcpy(hostPtrA, granularity, virPtr, granularity, ACL_MEMCPY_DEVICE_TO_HOST)); | 72 | CHECK_ERROR(aclrtMemcpy(hostPtrA, granularity, virPtr, granularity, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 71 | INFO_LOG("Process B: copy memory from device address %p to host address %p", virPtr, hostPtrA); | 73 | INFO_LOG("Process B: copy memory from device address %p to host address %p", virPtr, hostPtrA); |
| @@ -16,7 +16,8 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace memory { | 18 | namespace memory { |
| 19 | -void WriteFileEx(const char* file, void* data, size_t size) { | 19 | +void WriteFileEx(const char* file, void* data, size_t size) |
| 20 | +{ | ||
| 20 | INFO_LOG("Start writing to %s", file); | 21 | INFO_LOG("Start writing to %s", file); |
| 21 | 22 | ||
| 22 | int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | 23 | int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| @@ -39,19 +40,20 @@ void WriteFileEx(const char* file, void* data, size_t size) { | |||
| 39 | } // namespace memory | 40 | } // namespace memory |
| 40 | 41 | ||
| 41 | namespace memory { | 42 | namespace memory { |
| 42 | -void ReadFileEx(const char* file, void* data, size_t size) { | 43 | +void ReadFileEx(const char* file, void* data, size_t size) |
| 44 | +{ | ||
| 43 | INFO_LOG("Start reading from %s", file); | 45 | INFO_LOG("Start reading from %s", file); |
| 44 | 46 | ||
| 45 | // Read data from the target file | 47 | // Read data from the target file |
| 46 | int fd = open(file, O_RDONLY); | 48 | int fd = open(file, O_RDONLY); |
| 47 | - if (fd == -1){ | 49 | + if (fd == -1) { |
| 48 | ERROR_LOG("Open file %s failed", file); | 50 | ERROR_LOG("Open file %s failed", file); |
| 49 | (void)close(fd); | 51 | (void)close(fd); |
| 50 | return; | 52 | return; |
| 51 | } | 53 | } |
| 52 | 54 | ||
| 53 | ssize_t readSize = read(fd, data, size); | 55 | ssize_t readSize = read(fd, data, size); |
| 54 | - if (readSize != static_cast<ssize_t>(size)){ | 56 | + if (readSize != static_cast<ssize_t>(size)) { |
| 55 | ERROR_LOG("Partial read"); | 57 | ERROR_LOG("Partial read"); |
| 56 | (void)close(fd); | 58 | (void)close(fd); |
| 57 | return; | 59 | return; |
| @@ -13,7 +13,6 @@ | |||
| 13 | * using two independent streams (i.e., Stream A and Stream B). | 13 | * using two independent streams (i.e., Stream A and Stream B). |
| 14 | */ | 14 | */ |
| 15 | 15 | ||
| 16 | - | ||
| 17 | 16 | ||
| 18 | 17 | ||
| 19 | 18 | ||
| @@ -23,7 +22,8 @@ | |||
| 23 | 22 | ||
| 24 | 23 | ||
| 25 | 24 | ||
| 26 | -int ThreadWait(aclrtStream stream, int32_t deviceId, void* devPtr, uint64_t valueCompare, const char* filePath){ | 25 | +int ThreadWait(aclrtStream stream, int32_t deviceId, void* devPtr, uint64_t valueCompare, const char* filePath) |
| 26 | +{ | ||
| 27 | // Wait for the data in the specified memory to meet the condition | 27 | // Wait for the data in the specified memory to meet the condition |
| 28 | aclrtSetDevice(deviceId); | 28 | aclrtSetDevice(deviceId); |
| 29 | 29 | ||
| @@ -42,7 +42,8 @@ int ThreadWait(aclrtStream stream, int32_t deviceId, void* devPtr, uint64_t valu | |||
| 42 | return 0; | 42 | return 0; |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | -int ThreadWrite(aclrtStream stream, int32_t deviceId, void* devPtr, uint64_t valueWrite, const char* filePath){ | 45 | +int ThreadWrite(aclrtStream stream, int32_t deviceId, void* devPtr, uint64_t valueWrite, const char* filePath) |
| 46 | +{ | ||
| 46 | // The writing thread writes a flag to check whether the waiting thread is blocked before the writing thread starts | 47 | // The writing thread writes a flag to check whether the waiting thread is blocked before the writing thread starts |
| 47 | int32_t writeFlag = 123; | 48 | int32_t writeFlag = 123; |
| 48 | memory::WriteFileEx(filePath, &writeFlag, sizeof(writeFlag)); | 49 | memory::WriteFileEx(filePath, &writeFlag, sizeof(writeFlag)); |
| @@ -67,7 +68,7 @@ int32_t main() | |||
| 67 | 68 | ||
| 68 | // Allocate memory on the device | 69 | // Allocate memory on the device |
| 69 | uint64_t size = 1 * 1024 * 1024; | 70 | uint64_t size = 1 * 1024 * 1024; |
| 70 | - void *devPtrA; | 71 | + void* devPtrA; |
| 71 | CHECK_ERROR(aclrtMalloc(&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 72 | CHECK_ERROR(aclrtMalloc(&devPtrA, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 72 | INFO_LOG("Allocate memory on the device successfully"); | 73 | INFO_LOG("Allocate memory on the device successfully"); |
| 73 | 74 | ||
| @@ -7,7 +7,7 @@ | |||
| 7 | * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 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. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | 13 | ||
| @@ -17,7 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | namespace { | 19 | namespace { |
| 20 | -bool WriteBufferToFile(int fd, const void *data, size_t size) | 20 | +bool WriteBufferToFile(int fd, const void* data, size_t size) |
| 21 | { | 21 | { |
| 22 | const ssize_t writeSize = write(fd, data, size); | 22 | const ssize_t writeSize = write(fd, data, size); |
| 23 | if (writeSize == static_cast<ssize_t>(size)) { | 23 | if (writeSize == static_cast<ssize_t>(size)) { |
| @@ -27,7 +27,7 @@ bool WriteBufferToFile(int fd, const void *data, size_t size) | |||
| 27 | return false; | 27 | return false; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | -bool ReadBufferFromFile(int fd, void *data, size_t size) | 30 | +bool ReadBufferFromFile(int fd, void* data, size_t size) |
| 31 | { | 31 | { |
| 32 | const ssize_t readSize = read(fd, data, size); | 32 | const ssize_t readSize = read(fd, data, size); |
| 33 | if (readSize == static_cast<ssize_t>(size)) { | 33 | if (readSize == static_cast<ssize_t>(size)) { |
| @@ -39,7 +39,8 @@ bool ReadBufferFromFile(int fd, void *data, size_t size) | |||
| 39 | } // namespace | 39 | } // namespace |
| 40 | 40 | ||
| 41 | namespace memory { | 41 | namespace memory { |
| 42 | -void WriteFile(const char* filePath, const char* doneFile, void* data, size_t size) { | 42 | +void WriteFile(const char* filePath, const char* doneFile, void* data, size_t size) |
| 43 | +{ | ||
| 43 | INFO_LOG("Start writing to %s", filePath); | 44 | INFO_LOG("Start writing to %s", filePath); |
| 44 | 45 | ||
| 45 | int fd = open(filePath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | 46 | int fd = open(filePath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| @@ -66,7 +67,8 @@ void WriteFile(const char* filePath, const char* doneFile, void* data, size_t si | |||
| 66 | } // namespace memory | 67 | } // namespace memory |
| 67 | 68 | ||
| 68 | namespace memory { | 69 | namespace memory { |
| 69 | -void ReadFile(const char* filePath, const char* doneFile, void* data, size_t bufferSize) { | 70 | +void ReadFile(const char* filePath, const char* doneFile, void* data, size_t bufferSize) |
| 71 | +{ | ||
| 70 | INFO_LOG("Start reading from %s", filePath); | 72 | INFO_LOG("Start reading from %s", filePath); |
| 71 | constexpr uint32_t waitTime = 1000000; | 73 | constexpr uint32_t waitTime = 1000000; |
| 72 | int attempts = 0; | 74 | int attempts = 0; |
| @@ -82,7 +84,7 @@ void ReadFile(const char* filePath, const char* doneFile, void* data, size_t buf | |||
| 82 | attempts++; | 84 | attempts++; |
| 83 | WARN_LOG("Data writing not yet complete"); | 85 | WARN_LOG("Data writing not yet complete"); |
| 84 | if (attempts >= attemptsMax) { | 86 | if (attempts >= attemptsMax) { |
| 85 | - ERROR_LOG("Open file %s failed", doneFile); | 87 | + ERROR_LOG("Open file %s failed", doneFile); |
| 86 | return; | 88 | return; |
| 87 | } | 89 | } |
| 88 | (void)usleep(waitTime); | 90 | (void)usleep(waitTime); |
| @@ -91,7 +93,7 @@ void ReadFile(const char* filePath, const char* doneFile, void* data, size_t buf | |||
| 91 | 93 | ||
| 92 | // Read data from the target file | 94 | // Read data from the target file |
| 93 | int fd = open(filePath, O_RDONLY); | 95 | int fd = open(filePath, O_RDONLY); |
| 94 | - if (fd == -1){ | 96 | + if (fd == -1) { |
| 95 | ERROR_LOG("Open file %s failed", filePath); | 97 | ERROR_LOG("Open file %s failed", filePath); |
| 96 | return; | 98 | return; |
| 97 | } | 99 | } |
| @@ -104,9 +106,9 @@ void ReadFile(const char* filePath, const char* doneFile, void* data, size_t buf | |||
| 104 | } | 106 | } |
| 105 | size_t fileSize = static_cast<size_t>(fileStat.st_size); | 107 | size_t fileSize = static_cast<size_t>(fileStat.st_size); |
| 106 | if (fileSize > bufferSize) { | 108 | if (fileSize > bufferSize) { |
| 107 | - ERROR_LOG("File size %zu exceeds buffer size %zu", fileSize, bufferSize); | 109 | + ERROR_LOG("File size %zu exceeds buffer size %zu", fileSize, bufferSize); |
| 108 | - (void)close(fd); | 110 | + (void)close(fd); |
| 109 | - return; | 111 | + return; |
| 110 | } | 112 | } |
| 111 | 113 | ||
| 112 | if (!ReadBufferFromFile(fd, data, fileSize)) { | 114 | if (!ReadBufferFromFile(fd, data, fileSize)) { |
| @@ -14,9 +14,9 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace memory { | 16 | namespace memory { |
| 17 | - void WriteFile(const char* filePath, const char* doneFile, void* data, size_t size); | 17 | +void WriteFile(const char* filePath, const char* doneFile, void* data, size_t size); |
| 18 | - void ReadFile(const char* filePath, const char* doneFile, void* data, size_t bufferSize); | 18 | +void ReadFile(const char* filePath, const char* doneFile, void* data, size_t bufferSize); |
| 19 | - void WriteFileEx(const char* filePath, void* data, size_t size); | 19 | +void WriteFileEx(const char* filePath, void* data, size_t size); |
| 20 | - void ReadFileEx(const char* filePath, void* data, size_t size); | 20 | +void ReadFileEx(const char* filePath, void* data, size_t size); |
| 21 | -} | 21 | +} // namespace memory |
| 22 | -#endif | 22 | +#endif |
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace memory { | 16 | namespace memory { |
| 17 | -inline int InitDeviceAndCreateStream(int32_t deviceId, aclrtStream *stream) | 17 | +inline int InitDeviceAndCreateStream(int32_t deviceId, aclrtStream* stream) |
| 18 | { | 18 | { |
| 19 | return static_cast<int>(runtime::InitRuntimeAndCreateStream(deviceId, stream)); | 19 | return static_cast<int>(runtime::InitRuntimeAndCreateStream(deviceId, stream)); |
| 20 | } | 20 | } |
| @@ -24,7 +24,7 @@ int main() | |||
| 24 | int32_t deviceId = 0; | 24 | int32_t deviceId = 0; |
| 25 | uint32_t blockDim = 1; | 25 | uint32_t blockDim = 1; |
| 26 | uint32_t num = 0; | 26 | uint32_t num = 0; |
| 27 | - uint32_t *numDevice = nullptr; | 27 | + uint32_t* numDevice = nullptr; |
| 28 | size_t size = sizeof(uint32_t); | 28 | size_t size = sizeof(uint32_t); |
| 29 | aclrtStream stream = nullptr; | 29 | aclrtStream stream = nullptr; |
| 30 | aclrtStreamStatus streamStatus; | 30 | aclrtStreamStatus streamStatus; |
| @@ -54,7 +54,7 @@ int main() | |||
| 54 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 54 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 55 | CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); | 55 | CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 56 | INFO_LOG("After assigning the task through the created stream, the current result is: %d.", num); | 56 | INFO_LOG("After assigning the task through the created stream, the current result is: %d.", num); |
| 57 | - | 57 | + |
| 58 | INFO_LOG("Begin 3000 task."); | 58 | INFO_LOG("Begin 3000 task."); |
| 59 | // 循环下发任务并检查下发状态 | 59 | // 循环下发任务并检查下发状态 |
| 60 | for (int i = 0; i < count; i++) { | 60 | for (int i = 0; i < count; i++) { |
| @@ -14,13 +14,13 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace { | 16 | namespace { |
| 17 | -void StreamStateCallback(aclrtStream stream, aclrtStreamState state, void *args) | 17 | +void StreamStateCallback(aclrtStream stream, aclrtStreamState state, void* args) |
| 18 | { | 18 | { |
| 19 | - const char *sampleName = args == nullptr ? "stream_failure_mode" : static_cast<const char *>(args); | 19 | + const char* sampleName = args == nullptr ? "stream_failure_mode" : static_cast<const char*>(args); |
| 20 | INFO_LOG("Stream state callback from %s: stream=%p state=%d", sampleName, stream, static_cast<int32_t>(state)); | 20 | INFO_LOG("Stream state callback from %s: stream=%p state=%d", sampleName, stream, static_cast<int32_t>(state)); |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | -void LogOptionalResult(const char *apiName, aclError ret) | 23 | +void LogOptionalResult(const char* apiName, aclError ret) |
| 24 | { | 24 | { |
| 25 | if (ret == ACL_SUCCESS) { | 25 | if (ret == ACL_SUCCESS) { |
| 26 | INFO_LOG("%s returned %d.", apiName, static_cast<int32_t>(ret)); | 26 | INFO_LOG("%s returned %d.", apiName, static_cast<int32_t>(ret)); |
| @@ -34,28 +34,29 @@ void LogAbortSyncResult(aclError ret) | |||
| 34 | if (ret == ACL_SUCCESS) { | 34 | if (ret == ACL_SUCCESS) { |
| 35 | INFO_LOG("aclrtSynchronizeStream(after abort) returned %d.", static_cast<int32_t>(ret)); | 35 | INFO_LOG("aclrtSynchronizeStream(after abort) returned %d.", static_cast<int32_t>(ret)); |
| 36 | } else { | 36 | } else { |
| 37 | - INFO_LOG("aclrtSynchronizeStream(after abort) returned %d after stream abort, which is expected.", | 37 | + INFO_LOG( |
| 38 | - static_cast<int32_t>(ret)); | 38 | + "aclrtSynchronizeStream(after abort) returned %d after stream abort, which is expected.", |
| 39 | + static_cast<int32_t>(ret)); | ||
| 39 | } | 40 | } |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | -void FreeOptionalBuffer(uint32_t *buffer, const char *name) | 43 | +void FreeOptionalBuffer(uint32_t* buffer, const char* name) |
| 43 | { | 44 | { |
| 44 | if (buffer != nullptr) { | 45 | if (buffer != nullptr) { |
| 45 | LogOptionalResult(name, aclrtFree(buffer)); | 46 | LogOptionalResult(name, aclrtFree(buffer)); |
| 46 | } | 47 | } |
| 47 | } | 48 | } |
| 48 | 49 | ||
| 49 | -void DestroyOptionalStream(aclrtStream stream, const char *name) | 50 | +void DestroyOptionalStream(aclrtStream stream, const char* name) |
| 50 | { | 51 | { |
| 51 | if (stream != nullptr) { | 52 | if (stream != nullptr) { |
| 52 | LogOptionalResult(name, aclrtDestroyStreamForce(stream)); | 53 | LogOptionalResult(name, aclrtDestroyStreamForce(stream)); |
| 53 | } | 54 | } |
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | -bool PrepareScratchBuffer(uint32_t **scratch, size_t size, const char *demoName) | 57 | +bool PrepareScratchBuffer(uint32_t** scratch, size_t size, const char* demoName) |
| 57 | { | 58 | { |
| 58 | - aclError ret = aclrtMalloc(reinterpret_cast<void **>(scratch), size, ACL_MEM_MALLOC_HUGE_FIRST); | 59 | + aclError ret = aclrtMalloc(reinterpret_cast<void**>(scratch), size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 59 | if (ret != ACL_SUCCESS) { | 60 | if (ret != ACL_SUCCESS) { |
| 60 | WARN_LOG("Skip %s demo: aclrtMalloc returned %d.", demoName, static_cast<int32_t>(ret)); | 61 | WARN_LOG("Skip %s demo: aclrtMalloc returned %d.", demoName, static_cast<int32_t>(ret)); |
| 61 | return false; | 62 | return false; |
| @@ -77,7 +78,7 @@ void DemonstrateStreamStop() | |||
| 77 | constexpr size_t kScratchSize = sizeof(uint32_t); | 78 | constexpr size_t kScratchSize = sizeof(uint32_t); |
| 78 | constexpr uint32_t kBlockDim = 1; | 79 | constexpr uint32_t kBlockDim = 1; |
| 79 | aclrtStream stopStream = nullptr; | 80 | aclrtStream stopStream = nullptr; |
| 80 | - uint32_t *stopScratch = nullptr; | 81 | + uint32_t* stopScratch = nullptr; |
| 81 | 82 | ||
| 82 | aclError ret = aclrtCreateStreamWithConfig(&stopStream, 0, ACL_STREAM_DEVICE_USE_ONLY); | 83 | aclError ret = aclrtCreateStreamWithConfig(&stopStream, 0, ACL_STREAM_DEVICE_USE_ONLY); |
| 83 | if (ret != ACL_SUCCESS) { | 84 | if (ret != ACL_SUCCESS) { |
| @@ -103,7 +104,7 @@ void DemonstrateStreamAbort() | |||
| 103 | constexpr size_t kScratchSize = sizeof(uint32_t); | 104 | constexpr size_t kScratchSize = sizeof(uint32_t); |
| 104 | constexpr uint32_t kBlockDim = 1; | 105 | constexpr uint32_t kBlockDim = 1; |
| 105 | aclrtStream abortStream = nullptr; | 106 | aclrtStream abortStream = nullptr; |
| 106 | - uint32_t *abortScratch = nullptr; | 107 | + uint32_t* abortScratch = nullptr; |
| 107 | 108 | ||
| 108 | aclError ret = aclrtCreateStream(&abortStream); | 109 | aclError ret = aclrtCreateStream(&abortStream); |
| 109 | if (ret != ACL_SUCCESS) { | 110 | if (ret != ACL_SUCCESS) { |
| @@ -138,7 +139,7 @@ int main() | |||
| 138 | const int32_t deviceId = 0; | 139 | const int32_t deviceId = 0; |
| 139 | const uint32_t blockDim = 1; | 140 | const uint32_t blockDim = 1; |
| 140 | uint32_t num = 0; | 141 | uint32_t num = 0; |
| 141 | - uint32_t *numDevice = nullptr; | 142 | + uint32_t* numDevice = nullptr; |
| 142 | const size_t size = sizeof(uint32_t); | 143 | const size_t size = sizeof(uint32_t); |
| 143 | aclrtStream stream = nullptr; | 144 | aclrtStream stream = nullptr; |
| 144 | aclrtContext context = nullptr; | 145 | aclrtContext context = nullptr; |
| @@ -150,7 +151,7 @@ int main() | |||
| 150 | CHECK_ERROR(aclrtSetDevice(deviceId)); | 151 | CHECK_ERROR(aclrtSetDevice(deviceId)); |
| 151 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); | 152 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); |
| 152 | CHECK_ERROR(aclrtCreateStream(&stream)); | 153 | CHECK_ERROR(aclrtCreateStream(&stream)); |
| 153 | - CHECK_ERROR(aclrtMalloc(reinterpret_cast<void **>(&numDevice), size, ACL_MEM_MALLOC_HUGE_FIRST)); | 154 | + CHECK_ERROR(aclrtMalloc(reinterpret_cast<void**>(&numDevice), size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 154 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 155 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 155 | 156 | ||
| 156 | INFO_LOG("Assigning task without failure mode."); | 157 | INFO_LOG("Assigning task without failure mode."); |
| @@ -7,7 +7,7 @@ | |||
| 7 | * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 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. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | 13 | ||
| @@ -18,180 +18,180 @@ using namespace std; | |||
| 18 | // 耗时较短的核函数,功能为x自乘2 | 18 | // 耗时较短的核函数,功能为x自乘2 |
| 19 | 19 | ||
| 20 | namespace { | 20 | namespace { |
| 21 | - constexpr int kWaitTimeMs = 1000; | 21 | +constexpr int kWaitTimeMs = 1000; |
| 22 | - constexpr int32_t kDeviceId = 0; | 22 | +constexpr int32_t kDeviceId = 0; |
| 23 | - constexpr uint32_t kBlockDim = 1; | 23 | +constexpr uint32_t kBlockDim = 1; |
| 24 | 24 | ||
| 25 | - // Event同步 | 25 | +// Event同步 |
| 26 | - int EventSynchronization() | 26 | +int EventSynchronization() |
| 27 | - { | 27 | +{ |
| 28 | - uint32_t num = 0; | 28 | + uint32_t num = 0; |
| 29 | - uint32_t *numDevice = nullptr; | 29 | + uint32_t* numDevice = nullptr; |
| 30 | - size_t size = sizeof(uint32_t); | 30 | + size_t size = sizeof(uint32_t); |
| 31 | - aclrtContext context = nullptr; | 31 | + aclrtContext context = nullptr; |
| 32 | - aclrtStream stream1 = nullptr; | 32 | + aclrtStream stream1 = nullptr; |
| 33 | - aclrtStream stream2 = nullptr; | 33 | + aclrtStream stream2 = nullptr; |
| 34 | - aclrtEvent event = nullptr; | 34 | + aclrtEvent event = nullptr; |
| 35 | - aclInit(nullptr); | 35 | + aclInit(nullptr); |
| 36 | 36 | ||
| 37 | - // 申请设备 | 37 | + // 申请设备 |
| 38 | - CHECK_ERROR(aclrtSetDevice(kDeviceId)); | 38 | + CHECK_ERROR(aclrtSetDevice(kDeviceId)); |
| 39 | - CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); | 39 | + CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); |
| 40 | - CHECK_ERROR(aclrtCreateStream(&stream1)); | 40 | + CHECK_ERROR(aclrtCreateStream(&stream1)); |
| 41 | - CHECK_ERROR(aclrtCreateStream(&stream2)); | 41 | + CHECK_ERROR(aclrtCreateStream(&stream2)); |
| 42 | - // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 | 42 | + // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 |
| 43 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); | 43 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); |
| 44 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); | 44 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); |
| 45 | - CHECK_ERROR(aclrtCreateEvent(&event)); | 45 | + CHECK_ERROR(aclrtCreateEvent(&event)); |
| 46 | - INFO_LOG("Use event synchronize."); | 46 | + INFO_LOG("Use event synchronize."); |
| 47 | - // 开始做长任务 | 47 | + // 开始做长任务 |
| 48 | - CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 48 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 49 | - CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 49 | + CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 50 | - INFO_LOG("Applied resource successfully, begin assigning task."); | 50 | + INFO_LOG("Applied resource successfully, begin assigning task."); |
| 51 | - INFO_LOG("Begin a long task, num += 1."); | 51 | + INFO_LOG("Begin a long task, num += 1."); |
| 52 | - LongOP(kBlockDim, stream1, numDevice); | 52 | + LongOP(kBlockDim, stream1, numDevice); |
| 53 | - CHECK_ERROR(aclrtRecordEvent(event, stream1)); | 53 | + CHECK_ERROR(aclrtRecordEvent(event, stream1)); |
| 54 | - INFO_LOG("Event synchronize."); | 54 | + INFO_LOG("Event synchronize."); |
| 55 | - CHECK_ERROR(aclrtStreamWaitEvent(stream2, event)); | 55 | + CHECK_ERROR(aclrtStreamWaitEvent(stream2, event)); |
| 56 | 56 | ||
| 57 | - // 复位event,确保在同步后再复位,可以重复使用event,节省资源。 | 57 | + // 复位event,确保在同步后再复位,可以重复使用event,节省资源。 |
| 58 | - CHECK_ERROR(aclrtResetEvent(event, stream2)); | 58 | + CHECK_ERROR(aclrtResetEvent(event, stream2)); |
| 59 | - INFO_LOG("Begin a short task, num *= 2."); | 59 | + INFO_LOG("Begin a short task, num *= 2."); |
| 60 | - ShortOP(kBlockDim, stream2, numDevice); | 60 | + ShortOP(kBlockDim, stream2, numDevice); |
| 61 | - CHECK_ERROR(aclrtRecordEvent(event, stream2)); | 61 | + CHECK_ERROR(aclrtRecordEvent(event, stream2)); |
| 62 | - CHECK_ERROR(aclrtSynchronizeEvent(event)); | 62 | + CHECK_ERROR(aclrtSynchronizeEvent(event)); |
| 63 | - CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); | 63 | + CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 64 | - // 按照顺序执行结果应该为2 | 64 | + // 按照顺序执行结果应该为2 |
| 65 | - INFO_LOG("The answer is %d.", num); | 65 | + INFO_LOG("The answer is %d.", num); |
| 66 | 66 | ||
| 67 | - // event超时等待 | 67 | + // event超时等待 |
| 68 | - INFO_LOG("Simulate timeout, wait 1000ms, the task will fail."); | 68 | + INFO_LOG("Simulate timeout, wait 1000ms, the task will fail."); |
| 69 | - LongOP(kBlockDim, stream1, numDevice); | 69 | + LongOP(kBlockDim, stream1, numDevice); |
| 70 | - CHECK_ERROR(aclrtRecordEvent(event, stream1)); | 70 | + CHECK_ERROR(aclrtRecordEvent(event, stream1)); |
| 71 | - // 流的超时等待,如果超时直接报函数错误,将时间变长则不会报错。 | 71 | + // 流的超时等待,如果超时直接报函数错误,将时间变长则不会报错。 |
| 72 | - CHECK_ERROR_WITHOUT_RETURN(aclrtSynchronizeEventWithTimeout(event, kWaitTimeMs)); | 72 | + CHECK_ERROR_WITHOUT_RETURN(aclrtSynchronizeEventWithTimeout(event, kWaitTimeMs)); |
| 73 | 73 | ||
| 74 | - // 释放资源 | 74 | + // 释放资源 |
| 75 | - CHECK_ERROR(aclrtFree(numDevice)); | 75 | + CHECK_ERROR(aclrtFree(numDevice)); |
| 76 | - CHECK_ERROR(aclrtDestroyEvent(event)); | 76 | + CHECK_ERROR(aclrtDestroyEvent(event)); |
| 77 | - CHECK_ERROR(aclrtDestroyStreamForce(stream1)); | 77 | + CHECK_ERROR(aclrtDestroyStreamForce(stream1)); |
| 78 | - CHECK_ERROR(aclrtDestroyStreamForce(stream2)); | 78 | + CHECK_ERROR(aclrtDestroyStreamForce(stream2)); |
| 79 | - CHECK_ERROR(aclrtDestroyContext(context)); | 79 | + CHECK_ERROR(aclrtDestroyContext(context)); |
| 80 | - CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); | 80 | + CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); |
| 81 | - aclFinalize(); | 81 | + aclFinalize(); |
| 82 | - INFO_LOG("Resource cleanup completed."); | 82 | + INFO_LOG("Resource cleanup completed."); |
| 83 | - return 0; | 83 | + return 0; |
| 84 | - } | 84 | +} |
| 85 | 85 | ||
| 86 | - // Stream同步 | 86 | +// Stream同步 |
| 87 | - int StreamSynchronization() | 87 | +int StreamSynchronization() |
| 88 | - { | 88 | +{ |
| 89 | - uint32_t num = 0; | 89 | + uint32_t num = 0; |
| 90 | - uint32_t *numDevice = nullptr; | 90 | + uint32_t* numDevice = nullptr; |
| 91 | - size_t size = sizeof(uint32_t); | 91 | + size_t size = sizeof(uint32_t); |
| 92 | - aclrtContext context = nullptr; | 92 | + aclrtContext context = nullptr; |
| 93 | - aclrtStream stream1 = nullptr; | 93 | + aclrtStream stream1 = nullptr; |
| 94 | - aclrtStream stream2 = nullptr; | 94 | + aclrtStream stream2 = nullptr; |
| 95 | - aclInit(nullptr); | 95 | + aclInit(nullptr); |
| 96 | 96 | ||
| 97 | - // 申请设备 | 97 | + // 申请设备 |
| 98 | - CHECK_ERROR(aclrtSetDevice(kDeviceId)); | 98 | + CHECK_ERROR(aclrtSetDevice(kDeviceId)); |
| 99 | - CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); | 99 | + CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); |
| 100 | - CHECK_ERROR(aclrtCreateStream(&stream1)); | 100 | + CHECK_ERROR(aclrtCreateStream(&stream1)); |
| 101 | - CHECK_ERROR(aclrtCreateStream(&stream2)); | 101 | + CHECK_ERROR(aclrtCreateStream(&stream2)); |
| 102 | - // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 | 102 | + // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 |
| 103 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); | 103 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); |
| 104 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); | 104 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); |
| 105 | - INFO_LOG("Use stream synchronize."); | 105 | + INFO_LOG("Use stream synchronize."); |
| 106 | - // 开始做长任务 | 106 | + // 开始做长任务 |
| 107 | - CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 107 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 108 | - CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 108 | + CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 109 | - INFO_LOG("Applied resource successfully, begin assigning task."); | 109 | + INFO_LOG("Applied resource successfully, begin assigning task."); |
| 110 | - INFO_LOG("Begin a long task, num += 1."); | 110 | + INFO_LOG("Begin a long task, num += 1."); |
| 111 | - LongOP(kBlockDim, stream1, numDevice); | 111 | + LongOP(kBlockDim, stream1, numDevice); |
| 112 | - INFO_LOG("Stream synchronize."); | 112 | + INFO_LOG("Stream synchronize."); |
| 113 | - CHECK_ERROR(aclrtSynchronizeStream(stream1)); | 113 | + CHECK_ERROR(aclrtSynchronizeStream(stream1)); |
| 114 | 114 | ||
| 115 | - // 复位event,确保在同步后再复位,可以重复使用event,节省资源。 | 115 | + // 复位event,确保在同步后再复位,可以重复使用event,节省资源。 |
| 116 | - INFO_LOG("Begin a short task, num *= 2."); | 116 | + INFO_LOG("Begin a short task, num *= 2."); |
| 117 | - ShortOP(kBlockDim, stream2, numDevice); | 117 | + ShortOP(kBlockDim, stream2, numDevice); |
| 118 | - CHECK_ERROR(aclrtSynchronizeStream(stream2)); | 118 | + CHECK_ERROR(aclrtSynchronizeStream(stream2)); |
| 119 | - CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); | 119 | + CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 120 | - // 按照顺序执行结果应该为2 | 120 | + // 按照顺序执行结果应该为2 |
| 121 | - INFO_LOG("The answer is %d.", num); | 121 | + INFO_LOG("The answer is %d.", num); |
| 122 | 122 | ||
| 123 | - // event超时等待 | 123 | + // event超时等待 |
| 124 | - INFO_LOG("Simulate timeout, wait 1000ms, the task will fail."); | 124 | + INFO_LOG("Simulate timeout, wait 1000ms, the task will fail."); |
| 125 | - LongOP(kBlockDim, stream1, numDevice); | 125 | + LongOP(kBlockDim, stream1, numDevice); |
| 126 | - // 流的超时等待,如果超时直接报函数错误,将时间变长则不会报错。 | 126 | + // 流的超时等待,如果超时直接报函数错误,将时间变长则不会报错。 |
| 127 | - CHECK_ERROR_WITHOUT_RETURN(aclrtSynchronizeStreamWithTimeout(stream1, kWaitTimeMs)); | 127 | + CHECK_ERROR_WITHOUT_RETURN(aclrtSynchronizeStreamWithTimeout(stream1, kWaitTimeMs)); |
| 128 | 128 | ||
| 129 | - // 释放资源 | 129 | + // 释放资源 |
| 130 | - CHECK_ERROR(aclrtFree(numDevice)); | 130 | + CHECK_ERROR(aclrtFree(numDevice)); |
| 131 | - CHECK_ERROR(aclrtDestroyStreamForce(stream1)); | 131 | + CHECK_ERROR(aclrtDestroyStreamForce(stream1)); |
| 132 | - CHECK_ERROR(aclrtDestroyStreamForce(stream2)); | 132 | + CHECK_ERROR(aclrtDestroyStreamForce(stream2)); |
| 133 | - CHECK_ERROR(aclrtDestroyContext(context)); | 133 | + CHECK_ERROR(aclrtDestroyContext(context)); |
| 134 | - CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); | 134 | + CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); |
| 135 | - aclFinalize(); | 135 | + aclFinalize(); |
| 136 | - INFO_LOG("Resource cleanup completed."); | 136 | + INFO_LOG("Resource cleanup completed."); |
| 137 | - return 0; | 137 | + return 0; |
| 138 | - } | 138 | +} |
| 139 | 139 | ||
| 140 | - // Notify同步 | 140 | +// Notify同步 |
| 141 | - int NotifySynchronization() | 141 | +int NotifySynchronization() |
| 142 | - { | 142 | +{ |
| 143 | - uint32_t num = 0; | 143 | + uint32_t num = 0; |
| 144 | - uint32_t *numDevice = nullptr; | 144 | + uint32_t* numDevice = nullptr; |
| 145 | - size_t size = sizeof(uint32_t); | 145 | + size_t size = sizeof(uint32_t); |
| 146 | - aclrtContext context = nullptr; | 146 | + aclrtContext context = nullptr; |
| 147 | - aclrtNotify notify; | 147 | + aclrtNotify notify; |
| 148 | - aclrtStream stream1 = nullptr; | 148 | + aclrtStream stream1 = nullptr; |
| 149 | - aclrtStream stream2 = nullptr; | 149 | + aclrtStream stream2 = nullptr; |
| 150 | - aclInit(nullptr); | 150 | + aclInit(nullptr); |
| 151 | 151 | ||
| 152 | - // 申请设备 | 152 | + // 申请设备 |
| 153 | - CHECK_ERROR(aclrtSetDevice(kDeviceId)); | 153 | + CHECK_ERROR(aclrtSetDevice(kDeviceId)); |
| 154 | - CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); | 154 | + CHECK_ERROR(aclrtCreateContext(&context, kDeviceId)); |
| 155 | - CHECK_ERROR(aclrtCreateStream(&stream1)); | 155 | + CHECK_ERROR(aclrtCreateStream(&stream1)); |
| 156 | - CHECK_ERROR(aclrtCreateStream(&stream2)); | 156 | + CHECK_ERROR(aclrtCreateStream(&stream2)); |
| 157 | - CHECK_ERROR(aclrtCreateNotify(¬ify, 0x00U)); | 157 | + CHECK_ERROR(aclrtCreateNotify(¬ify, 0x00U)); |
| 158 | - // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 | 158 | + // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行。 |
| 159 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); | 159 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream1, ACL_STOP_ON_FAILURE)); |
| 160 | - CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); | 160 | + CHECK_ERROR(aclrtSetStreamFailureMode(stream2, ACL_STOP_ON_FAILURE)); |
| 161 | - INFO_LOG("Use notify synchronize."); | 161 | + INFO_LOG("Use notify synchronize."); |
| 162 | - // 开始做长任务 | 162 | + // 开始做长任务 |
| 163 | - CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 163 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 164 | - CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 164 | + CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 165 | - INFO_LOG("Applied resource successfully, begin assigning task."); | 165 | + INFO_LOG("Applied resource successfully, begin assigning task."); |
| 166 | - INFO_LOG("Begin a long task, num += 1."); | 166 | + INFO_LOG("Begin a long task, num += 1."); |
| 167 | - LongOP(kBlockDim, stream1, numDevice); | 167 | + LongOP(kBlockDim, stream1, numDevice); |
| 168 | - INFO_LOG("Stream synchronize."); | 168 | + INFO_LOG("Stream synchronize."); |
| 169 | - CHECK_ERROR(aclrtRecordNotify(notify, stream1)); | 169 | + CHECK_ERROR(aclrtRecordNotify(notify, stream1)); |
| 170 | 170 | ||
| 171 | - CHECK_ERROR(aclrtWaitAndResetNotify(notify, stream2, 0)); | 171 | + CHECK_ERROR(aclrtWaitAndResetNotify(notify, stream2, 0)); |
| 172 | - INFO_LOG("Begin a short task, num *= 2."); | 172 | + INFO_LOG("Begin a short task, num *= 2."); |
| 173 | - ShortOP(kBlockDim, stream2, numDevice); | 173 | + ShortOP(kBlockDim, stream2, numDevice); |
| 174 | - CHECK_ERROR(aclrtSynchronizeStream(stream2)); | 174 | + CHECK_ERROR(aclrtSynchronizeStream(stream2)); |
| 175 | - CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); | 175 | + CHECK_ERROR(aclrtMemcpy(&num, size, numDevice, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 176 | - // 按照顺序执行结果应该为2 | 176 | + // 按照顺序执行结果应该为2 |
| 177 | - INFO_LOG("The answer is %d.", num); | 177 | + INFO_LOG("The answer is %d.", num); |
| 178 | - // 释放资源 | 178 | + // 释放资源 |
| 179 | - CHECK_ERROR(aclrtFree(numDevice)); | 179 | + CHECK_ERROR(aclrtFree(numDevice)); |
| 180 | - CHECK_ERROR(aclrtDestroyNotify(notify)); | 180 | + CHECK_ERROR(aclrtDestroyNotify(notify)); |
| 181 | - CHECK_ERROR(aclrtDestroyStreamForce(stream1)); | 181 | + CHECK_ERROR(aclrtDestroyStreamForce(stream1)); |
| 182 | - CHECK_ERROR(aclrtDestroyStreamForce(stream2)); | 182 | + CHECK_ERROR(aclrtDestroyStreamForce(stream2)); |
| 183 | - CHECK_ERROR(aclrtDestroyContext(context)); | 183 | + CHECK_ERROR(aclrtDestroyContext(context)); |
| 184 | - CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); | 184 | + CHECK_ERROR(aclrtResetDeviceForce(kDeviceId)); |
| 185 | - aclFinalize(); | 185 | + aclFinalize(); |
| 186 | - INFO_LOG("Resource cleanup completed."); | 186 | + INFO_LOG("Resource cleanup completed."); |
| 187 | - return 0; | 187 | + return 0; |
| 188 | - } | 188 | +} |
| 189 | } // namespace | 189 | } // namespace |
| 190 | 190 | ||
| 191 | int main() | 191 | int main() |
| 192 | { | 192 | { |
| 193 | // 每个函数代表一种同步的方法 | 193 | // 每个函数代表一种同步的方法 |
| 194 | - | 194 | + |
| 195 | (void)StreamSynchronization(); | 195 | (void)StreamSynchronization(); |
| 196 | (void)EventSynchronization(); | 196 | (void)EventSynchronization(); |
| 197 | (void)NotifySynchronization(); | 197 | (void)NotifySynchronization(); |
| @@ -21,7 +21,7 @@ constexpr int32_t kDeviceId = 0; | |||
| 21 | constexpr uint32_t kStreamFlags = 0; | 21 | constexpr uint32_t kStreamFlags = 0; |
| 22 | constexpr uint32_t kStreamPriority = 0; | 22 | constexpr uint32_t kStreamPriority = 0; |
| 23 | 23 | ||
| 24 | -int CheckAcl(aclError ret, const char *expr) | 24 | +int CheckAcl(aclError ret, const char* expr) |
| 25 | { | 25 | { |
| 26 | if (ret != ACL_SUCCESS) { | 26 | if (ret != ACL_SUCCESS) { |
| 27 | fprintf(stderr, "[ERROR] Operation failed: %s returned error code %d\n", expr, static_cast<int32_t>(ret)); | 27 | fprintf(stderr, "[ERROR] Operation failed: %s returned error code %d\n", expr, static_cast<int32_t>(ret)); |
| @@ -31,10 +31,10 @@ int CheckAcl(aclError ret, const char *expr) | |||
| 31 | return 0; | 31 | return 0; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | -using CreateStreamConfigHandleFunc = aclrtStreamConfigHandle *(*)(); | 34 | +using CreateStreamConfigHandleFunc = aclrtStreamConfigHandle* (*)(); |
| 35 | -using DestroyStreamConfigHandleFunc = aclError (*)(aclrtStreamConfigHandle *); | 35 | +using DestroyStreamConfigHandleFunc = aclError (*)(aclrtStreamConfigHandle*); |
| 36 | -using SetStreamConfigOptFunc = aclError (*)(aclrtStreamConfigHandle *, aclrtStreamConfigAttr, const void *, size_t); | 36 | +using SetStreamConfigOptFunc = aclError (*)(aclrtStreamConfigHandle*, aclrtStreamConfigAttr, const void*, size_t); |
| 37 | -using CreateStreamV2Func = aclError (*)(aclrtStream *, const aclrtStreamConfigHandle *); | 37 | +using CreateStreamV2Func = aclError (*)(aclrtStream*, const aclrtStreamConfigHandle*); |
| 38 | 38 | ||
| 39 | struct StreamConfigApi { | 39 | struct StreamConfigApi { |
| 40 | CreateStreamConfigHandleFunc createConfigHandle; | 40 | CreateStreamConfigHandleFunc createConfigHandle; |
| @@ -45,10 +45,7 @@ struct StreamConfigApi { | |||
| 45 | 45 | ||
| 46 | class StreamConfigHandleGuard { | 46 | class StreamConfigHandleGuard { |
| 47 | public: | 47 | public: |
| 48 | - StreamConfigHandleGuard(const StreamConfigApi &api, aclrtStreamConfigHandle *handle) | 48 | + StreamConfigHandleGuard(const StreamConfigApi& api, aclrtStreamConfigHandle* handle) : api_(api), handle_(handle) {} |
| 49 | - : api_(api), handle_(handle) | ||
| 50 | - { | ||
| 51 | - } | ||
| 52 | 49 | ||
| 53 | ~StreamConfigHandleGuard() | 50 | ~StreamConfigHandleGuard() |
| 54 | { | 51 | { |
| @@ -57,8 +54,8 @@ public: | |||
| 57 | } | 54 | } |
| 58 | } | 55 | } |
| 59 | 56 | ||
| 60 | - StreamConfigHandleGuard(const StreamConfigHandleGuard &) = delete; | 57 | + StreamConfigHandleGuard(const StreamConfigHandleGuard&) = delete; |
| 61 | - StreamConfigHandleGuard &operator=(const StreamConfigHandleGuard &) = delete; | 58 | + StreamConfigHandleGuard& operator=(const StreamConfigHandleGuard&) = delete; |
| 62 | 59 | ||
| 63 | aclError Destroy() | 60 | aclError Destroy() |
| 64 | { | 61 | { |
| @@ -74,8 +71,8 @@ public: | |||
| 74 | } | 71 | } |
| 75 | 72 | ||
| 76 | private: | 73 | private: |
| 77 | - const StreamConfigApi &api_; | 74 | + const StreamConfigApi& api_; |
| 78 | - aclrtStreamConfigHandle *handle_; | 75 | + aclrtStreamConfigHandle* handle_; |
| 79 | }; | 76 | }; |
| 80 | 77 | ||
| 81 | class StreamGuard { | 78 | class StreamGuard { |
| @@ -89,8 +86,8 @@ public: | |||
| 89 | } | 86 | } |
| 90 | } | 87 | } |
| 91 | 88 | ||
| 92 | - StreamGuard(const StreamGuard &) = delete; | 89 | + StreamGuard(const StreamGuard&) = delete; |
| 93 | - StreamGuard &operator=(const StreamGuard &) = delete; | 90 | + StreamGuard& operator=(const StreamGuard&) = delete; |
| 94 | 91 | ||
| 95 | aclError Destroy() | 92 | aclError Destroy() |
| 96 | { | 93 | { |
| @@ -110,7 +107,7 @@ private: | |||
| 110 | }; | 107 | }; |
| 111 | 108 | ||
| 112 | template <typename Func> | 109 | template <typename Func> |
| 113 | -bool LoadSymbol(void *handle, const char *name, Func *func) | 110 | +bool LoadSymbol(void* handle, const char* name, Func* func) |
| 114 | { | 111 | { |
| 115 | *func = reinterpret_cast<Func>(dlsym(handle, name)); | 112 | *func = reinterpret_cast<Func>(dlsym(handle, name)); |
| 116 | if (*func == nullptr) { | 113 | if (*func == nullptr) { |
| @@ -120,7 +117,7 @@ bool LoadSymbol(void *handle, const char *name, Func *func) | |||
| 120 | return true; | 117 | return true; |
| 121 | } | 118 | } |
| 122 | 119 | ||
| 123 | -bool LoadStreamConfigApi(StreamConfigApi *api) | 120 | +bool LoadStreamConfigApi(StreamConfigApi* api) |
| 124 | { | 121 | { |
| 125 | bool ok = true; | 122 | bool ok = true; |
| 126 | ok = LoadSymbol(RTLD_DEFAULT, "aclrtCreateStreamConfigHandle", &api->createConfigHandle) && ok; | 123 | ok = LoadSymbol(RTLD_DEFAULT, "aclrtCreateStreamConfigHandle", &api->createConfigHandle) && ok; |
| @@ -130,14 +127,16 @@ bool LoadStreamConfigApi(StreamConfigApi *api) | |||
| 130 | return ok; | 127 | return ok; |
| 131 | } | 128 | } |
| 132 | 129 | ||
| 133 | -int SetStreamConfigOptions(const StreamConfigApi &api, aclrtStreamConfigHandle *configHandle) | 130 | +int SetStreamConfigOptions(const StreamConfigApi& api, aclrtStreamConfigHandle* configHandle) |
| 134 | { | 131 | { |
| 135 | - if (CheckAcl(api.setConfigOpt(configHandle, ACL_RT_STREAM_FLAG, &kStreamFlags, sizeof(kStreamFlags)), | 132 | + if (CheckAcl( |
| 136 | - "aclrtSetStreamConfigOpt(ACL_RT_STREAM_FLAG)") != 0) { | 133 | + api.setConfigOpt(configHandle, ACL_RT_STREAM_FLAG, &kStreamFlags, sizeof(kStreamFlags)), |
| 134 | + "aclrtSetStreamConfigOpt(ACL_RT_STREAM_FLAG)") != 0) { | ||
| 137 | return -1; | 135 | return -1; |
| 138 | } | 136 | } |
| 139 | - if (CheckAcl(api.setConfigOpt(configHandle, ACL_RT_STREAM_PRIORITY, &kStreamPriority, sizeof(kStreamPriority)), | 137 | + if (CheckAcl( |
| 140 | - "aclrtSetStreamConfigOpt(ACL_RT_STREAM_PRIORITY)") != 0) { | 138 | + api.setConfigOpt(configHandle, ACL_RT_STREAM_PRIORITY, &kStreamPriority, sizeof(kStreamPriority)), |
| 139 | + "aclrtSetStreamConfigOpt(ACL_RT_STREAM_PRIORITY)") != 0) { | ||
| 141 | return -1; | 140 | return -1; |
| 142 | } | 141 | } |
| 143 | INFO_LOG("Set stream config flags=%u priority=%u", kStreamFlags, kStreamPriority); | 142 | INFO_LOG("Set stream config flags=%u priority=%u", kStreamFlags, kStreamPriority); |
| @@ -170,11 +169,12 @@ int RunStreamConfigQuerySample() | |||
| 170 | { | 169 | { |
| 171 | StreamConfigApi api = {}; | 170 | StreamConfigApi api = {}; |
| 172 | if (!LoadStreamConfigApi(&api)) { | 171 | if (!LoadStreamConfigApi(&api)) { |
| 173 | - INFO_LOG("[SKIP] Stream config query sample skipped because the current Runtime library does not export all stream config APIs."); | 172 | + INFO_LOG("[SKIP] Stream config query sample skipped because the current Runtime library does not export all " |
| 173 | + "stream config APIs."); | ||
| 174 | return 0; | 174 | return 0; |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | - aclrtStreamConfigHandle *configHandle = api.createConfigHandle(); | 177 | + aclrtStreamConfigHandle* configHandle = api.createConfigHandle(); |
| 178 | if (configHandle == nullptr) { | 178 | if (configHandle == nullptr) { |
| 179 | INFO_LOG("[SKIP] Stream config query sample skipped because aclrtCreateStreamConfigHandle returned nullptr."); | 179 | INFO_LOG("[SKIP] Stream config query sample skipped because aclrtCreateStreamConfigHandle returned nullptr."); |
| 180 | return 0; | 180 | return 0; |
| @@ -209,7 +209,7 @@ int RunStreamConfigQuerySample() | |||
| 209 | INFO_LOG("[SUCCESS] Stream config query sample completed successfully"); | 209 | INFO_LOG("[SUCCESS] Stream config query sample completed successfully"); |
| 210 | return 0; | 210 | return 0; |
| 211 | } | 211 | } |
| 212 | -} // namespace | 212 | +} // namespace |
| 213 | 213 | ||
| 214 | int32_t main() | 214 | int32_t main() |
| 215 | { | 215 | { |
| @@ -75,7 +75,7 @@ int main() | |||
| 75 | 75 | ||
| 76 | // 申请存放随机数状态 counter 的device内存 (要求 16Byte) | 76 | // 申请存放随机数状态 counter 的device内存 (要求 16Byte) |
| 77 | void* counterAddr = NULL; | 77 | void* counterAddr = NULL; |
| 78 | - CHECK_ERROR(aclrtMalloc((void**)&counterAddr, 16, ACL_MEM_MALLOC_HUGE_FIRST)); | 78 | + CHECK_ERROR(aclrtMalloc((void**)&counterAddr, 16, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 79 | 79 | ||
| 80 | printf("Gen normal distribution random num, data type: float \n"); | 80 | printf("Gen normal distribution random num, data type: float \n"); |
| 81 | float mean = 3.0; | 81 | float mean = 3.0; |
| @@ -23,10 +23,7 @@ int32_t CallBackSpace::CallBackSample::deviceId_ = 0; | |||
| 23 | 23 | ||
| 24 | CallBackSpace::CallBackSample::CallBackSample() = default; | 24 | CallBackSpace::CallBackSample::CallBackSample() = default; |
| 25 | 25 | ||
| 26 | -CallBackSpace::CallBackSample::~CallBackSample() | 26 | +CallBackSpace::CallBackSample::~CallBackSample() { (void)Destroy(); } |
| 27 | -{ | ||
| 28 | - (void)Destroy(); | ||
| 29 | -} | ||
| 30 | 27 | ||
| 31 | int CallBackSpace::CallBackSample::Init() | 28 | int CallBackSpace::CallBackSample::Init() |
| 32 | { | 29 | { |
| @@ -38,7 +35,7 @@ int CallBackSpace::CallBackSample::Init() | |||
| 38 | return 0; | 35 | return 0; |
| 39 | } | 36 | } |
| 40 | 37 | ||
| 41 | -void CallBackSpace::CallBackSample::ThreadFunc(void *arg) | 38 | +void CallBackSpace::CallBackSample::ThreadFunc(void* arg) |
| 42 | { | 39 | { |
| 43 | const int waitTime = 100; | 40 | const int waitTime = 100; |
| 44 | aclError ret = aclrtSetCurrentContext(context_); | 41 | aclError ret = aclrtSetCurrentContext(context_); |
| @@ -52,7 +49,7 @@ void CallBackSpace::CallBackSample::ThreadFunc(void *arg) | |||
| 52 | INFO_LOG("Report callback thread exit"); | 49 | INFO_LOG("Report callback thread exit"); |
| 53 | } | 50 | } |
| 54 | 51 | ||
| 55 | -void CallBackSpace::CallBackSample::HostFuncThreadFunc(void *arg) | 52 | +void CallBackSpace::CallBackSample::HostFuncThreadFunc(void* arg) |
| 56 | { | 53 | { |
| 57 | const int waitTime = 100; | 54 | const int waitTime = 100; |
| 58 | aclError ret = aclrtSetCurrentContext(context_); | 55 | aclError ret = aclrtSetCurrentContext(context_); |
| @@ -66,30 +63,30 @@ void CallBackSpace::CallBackSample::HostFuncThreadFunc(void *arg) | |||
| 66 | INFO_LOG("Hostfunc processing thread exit"); | 63 | INFO_LOG("Hostfunc processing thread exit"); |
| 67 | } | 64 | } |
| 68 | 65 | ||
| 69 | -void CallBackSpace::CallBackSample::CallBackBeforeLaunchFunc(void *arg) | 66 | +void CallBackSpace::CallBackSample::CallBackBeforeLaunchFunc(void* arg) |
| 70 | { | 67 | { |
| 71 | thread::id tid = std::this_thread::get_id(); | 68 | thread::id tid = std::this_thread::get_id(); |
| 72 | string tidStr = CallbackUtils::GetThreadId(tid); | 69 | string tidStr = CallbackUtils::GetThreadId(tid); |
| 73 | INFO_LOG("Report callback thread id is %s", tidStr.c_str()); | 70 | INFO_LOG("Report callback thread id is %s", tidStr.c_str()); |
| 74 | - int *data = static_cast<int*>(arg); | 71 | + int* data = static_cast<int*>(arg); |
| 75 | INFO_LOG("This callback before task, result: user data is: %d.", *data); | 72 | INFO_LOG("This callback before task, result: user data is: %d.", *data); |
| 76 | } | 73 | } |
| 77 | 74 | ||
| 78 | -void CallBackSpace::CallBackSample::CallBackFunc(void *arg) | 75 | +void CallBackSpace::CallBackSample::CallBackFunc(void* arg) |
| 79 | { | 76 | { |
| 80 | thread::id tid = std::this_thread::get_id(); | 77 | thread::id tid = std::this_thread::get_id(); |
| 81 | string tidStr = CallbackUtils::GetThreadId(tid); | 78 | string tidStr = CallbackUtils::GetThreadId(tid); |
| 82 | INFO_LOG("Report callback thread id is %s", tidStr.c_str()); | 79 | INFO_LOG("Report callback thread id is %s", tidStr.c_str()); |
| 83 | - int *data = static_cast<int*>(arg); | 80 | + int* data = static_cast<int*>(arg); |
| 84 | INFO_LOG("This callback after task and loop five times, result: user data is: %d.", *data); | 81 | INFO_LOG("This callback after task and loop five times, result: user data is: %d.", *data); |
| 85 | } | 82 | } |
| 86 | 83 | ||
| 87 | -void CallBackSpace::CallBackSample::HostFunc(void *arg) | 84 | +void CallBackSpace::CallBackSample::HostFunc(void* arg) |
| 88 | { | 85 | { |
| 89 | thread::id tid = std::this_thread::get_id(); | 86 | thread::id tid = std::this_thread::get_id(); |
| 90 | string tidStr = CallbackUtils::GetThreadId(tid); | 87 | string tidStr = CallbackUtils::GetThreadId(tid); |
| 91 | INFO_LOG("Hostfunc callback thread id is %s", tidStr.c_str()); | 88 | INFO_LOG("Hostfunc callback thread id is %s", tidStr.c_str()); |
| 92 | - int *data = static_cast<int*>(arg); | 89 | + int* data = static_cast<int*>(arg); |
| 93 | INFO_LOG("Hostfunc executed in subscribed thread, user data is: %d.", *data); | 90 | INFO_LOG("Hostfunc executed in subscribed thread, user data is: %d.", *data); |
| 94 | } | 91 | } |
| 95 | 92 | ||
| @@ -100,9 +97,9 @@ int CallBackSpace::CallBackSample::Callback() | |||
| 100 | bool isReportLoop = true; | 97 | bool isReportLoop = true; |
| 101 | bool isHostFuncLoop = true; | 98 | bool isHostFuncLoop = true; |
| 102 | const size_t size = sizeof(uint32_t); | 99 | const size_t size = sizeof(uint32_t); |
| 103 | - uint32_t *numDevice = nullptr; | 100 | + uint32_t* numDevice = nullptr; |
| 104 | const int count = 5; | 101 | const int count = 5; |
| 105 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 102 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 106 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 103 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 107 | 104 | ||
| 108 | thread reportThread(ThreadFunc, &isReportLoop); | 105 | thread reportThread(ThreadFunc, &isReportLoop); |
| @@ -115,7 +112,7 @@ int CallBackSpace::CallBackSample::Callback() | |||
| 115 | INFO_LOG("The created report thread id is %s", reportThreadId.c_str()); | 112 | INFO_LOG("The created report thread id is %s", reportThreadId.c_str()); |
| 116 | INFO_LOG("The created hostfunc thread id is %s", hostFuncThreadId.c_str()); | 113 | INFO_LOG("The created hostfunc thread id is %s", hostFuncThreadId.c_str()); |
| 117 | 114 | ||
| 118 | - int *userData = new int(520); | 115 | + int* userData = new int(520); |
| 119 | uint64_t reportTidInt = std::stoull(reportThreadId); | 116 | uint64_t reportTidInt = std::stoull(reportThreadId); |
| 120 | uint64_t hostFuncTidInt = std::stoull(hostFuncThreadId); | 117 | uint64_t hostFuncTidInt = std::stoull(hostFuncThreadId); |
| 121 | CHECK_ERROR(aclrtSubscribeReport(reportTidInt, stream_)); | 118 | CHECK_ERROR(aclrtSubscribeReport(reportTidInt, stream_)); |
| @@ -17,24 +17,24 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | namespace CallBackSpace { | 19 | namespace CallBackSpace { |
| 20 | - class CallBackSample { | 20 | +class CallBackSample { |
| 21 | - public: | 21 | +public: |
| 22 | - CallBackSample(); | 22 | + CallBackSample(); |
| 23 | - virtual ~CallBackSample(); | 23 | + virtual ~CallBackSample(); |
| 24 | - int Init(); | 24 | + int Init(); |
| 25 | - int Callback(); | 25 | + int Callback(); |
| 26 | - int Destroy(); | 26 | + int Destroy(); |
| 27 | - static void ThreadFunc(void *arg); | 27 | + static void ThreadFunc(void* arg); |
| 28 | - static void HostFuncThreadFunc(void *arg); | 28 | + static void HostFuncThreadFunc(void* arg); |
| 29 | - static void CallBackFunc(void *arg); | 29 | + static void CallBackFunc(void* arg); |
| 30 | - static void CallBackBeforeLaunchFunc(void *arg); | 30 | + static void CallBackBeforeLaunchFunc(void* arg); |
| 31 | - static void HostFunc(void *arg); | 31 | + static void HostFunc(void* arg); |
| 32 | 32 | ||
| 33 | - public: | 33 | +public: |
| 34 | - static int32_t deviceId_; | 34 | + static int32_t deviceId_; |
| 35 | - static aclrtContext context_; | 35 | + static aclrtContext context_; |
| 36 | - static aclrtStream stream_; | 36 | + static aclrtStream stream_; |
| 37 | - }; | 37 | +}; |
| 38 | -} | 38 | +} // namespace CallBackSpace |
| 39 | 39 | ||
| 40 | 40 | ||
| @@ -16,17 +16,14 @@ using namespace std; | |||
| 16 | 16 | ||
| 17 | // 核函数,让x自增1 | 17 | // 核函数,让x自增1 |
| 18 | namespace { | 18 | namespace { |
| 19 | - void CallBackFunc(void *arg) | 19 | +void CallBackFunc(void* arg) { INFO_LOG("Hostfunc callback!!!"); } |
| 20 | - { | 20 | +} // namespace |
| 21 | - INFO_LOG("Hostfunc callback!!!"); | ||
| 22 | - } | ||
| 23 | -} | ||
| 24 | int main() | 21 | int main() |
| 25 | { | 22 | { |
| 26 | // 初始化 | 23 | // 初始化 |
| 27 | int32_t deviceId = 0; | 24 | int32_t deviceId = 0; |
| 28 | uint32_t num = 0; | 25 | uint32_t num = 0; |
| 29 | - uint32_t *numDevice = nullptr; | 26 | + uint32_t* numDevice = nullptr; |
| 30 | size_t size = sizeof(uint32_t); | 27 | size_t size = sizeof(uint32_t); |
| 31 | uint32_t blockDim = 1; | 28 | uint32_t blockDim = 1; |
| 32 | aclrtStream stream = nullptr; | 29 | aclrtStream stream = nullptr; |
| @@ -35,7 +32,7 @@ int main() | |||
| 35 | CHECK_ERROR(aclrtSetDevice(deviceId)); | 32 | CHECK_ERROR(aclrtSetDevice(deviceId)); |
| 36 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); | 33 | CHECK_ERROR(aclrtCreateContext(&context, deviceId)); |
| 37 | // 默认流下发任务,默认流随着context创建而创建,给函数传入空即使用默认流 | 34 | // 默认流下发任务,默认流随着context创建而创建,给函数传入空即使用默认流 |
| 38 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 35 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 39 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 36 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 40 | CHECK_ERROR(aclrtCreateStream(&stream)); | 37 | CHECK_ERROR(aclrtCreateStream(&stream)); |
| 41 | // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行 | 38 | // 默认为遇错继续执行,调用该函数设置为遇错即停,当一个任务执行遇到错误,后续任务将不进行 |
| @@ -23,20 +23,16 @@ aclrtStream ExceptionCallBackSpace::ExceptionCallBackSample::stream_ = nullptr; | |||
| 23 | int32_t ExceptionCallBackSpace::ExceptionCallBackSample::deviceId_ = 0; | 23 | int32_t ExceptionCallBackSpace::ExceptionCallBackSample::deviceId_ = 0; |
| 24 | 24 | ||
| 25 | namespace { | 25 | namespace { |
| 26 | -const char *SafeString(const char *message) | 26 | +const char* SafeString(const char* message) { return message != nullptr ? message : "<null>"; } |
| 27 | -{ | ||
| 28 | - return message != nullptr ? message : "<null>"; | ||
| 29 | -} | ||
| 30 | 27 | ||
| 31 | -void LogRuntimeErrorState(int32_t deviceId, const char *stage) | 28 | +void LogRuntimeErrorState(int32_t deviceId, const char* stage) |
| 32 | { | 29 | { |
| 33 | aclrtErrorInfo errorInfo = {}; | 30 | aclrtErrorInfo errorInfo = {}; |
| 34 | aclError verboseRet = aclrtGetErrorVerbose(deviceId, &errorInfo); | 31 | aclError verboseRet = aclrtGetErrorVerbose(deviceId, &errorInfo); |
| 35 | if (verboseRet == ACL_SUCCESS) { | 32 | if (verboseRet == ACL_SUCCESS) { |
| 36 | - INFO_LOG("%s verbose error info: errorType=%d, tryRepair=%u, hasDetail=%u", | 33 | + INFO_LOG( |
| 37 | - stage, | 34 | + "%s verbose error info: errorType=%d, tryRepair=%u, hasDetail=%u", stage, |
| 38 | - static_cast<int32_t>(errorInfo.errorType), | 35 | + static_cast<int32_t>(errorInfo.errorType), static_cast<uint32_t>(errorInfo.tryRepair), |
| 39 | - static_cast<uint32_t>(errorInfo.tryRepair), | ||
| 40 | static_cast<uint32_t>(errorInfo.hasDetail)); | 36 | static_cast<uint32_t>(errorInfo.hasDetail)); |
| 41 | } else { | 37 | } else { |
| 42 | WARN_LOG("%s aclrtGetErrorVerbose failed with error code %d", stage, static_cast<int32_t>(verboseRet)); | 38 | WARN_LOG("%s aclrtGetErrorVerbose failed with error code %d", stage, static_cast<int32_t>(verboseRet)); |
| @@ -44,21 +40,16 @@ void LogRuntimeErrorState(int32_t deviceId, const char *stage) | |||
| 44 | 40 | ||
| 45 | aclError peekError = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); | 41 | aclError peekError = aclrtPeekAtLastError(ACL_RT_THREAD_LEVEL); |
| 46 | aclError lastError = aclrtGetLastError(ACL_RT_THREAD_LEVEL); | 42 | aclError lastError = aclrtGetLastError(ACL_RT_THREAD_LEVEL); |
| 47 | - const char *recentErrMsg = aclGetRecentErrMsg(); | 43 | + const char* recentErrMsg = aclGetRecentErrMsg(); |
| 48 | - ERROR_LOG("%s runtime diagnostics: peekErr=%d, lastErr=%d, recentErrMsg=%s", | 44 | + ERROR_LOG( |
| 49 | - stage, | 45 | + "%s runtime diagnostics: peekErr=%d, lastErr=%d, recentErrMsg=%s", stage, static_cast<int32_t>(peekError), |
| 50 | - static_cast<int32_t>(peekError), | 46 | + static_cast<int32_t>(lastError), SafeString(recentErrMsg)); |
| 51 | - static_cast<int32_t>(lastError), | ||
| 52 | - SafeString(recentErrMsg)); | ||
| 53 | } | 47 | } |
| 54 | } // namespace | 48 | } // namespace |
| 55 | 49 | ||
| 56 | ExceptionCallBackSpace::ExceptionCallBackSample::ExceptionCallBackSample() = default; | 50 | ExceptionCallBackSpace::ExceptionCallBackSample::ExceptionCallBackSample() = default; |
| 57 | 51 | ||
| 58 | -ExceptionCallBackSpace::ExceptionCallBackSample::~ExceptionCallBackSample() | 52 | +ExceptionCallBackSpace::ExceptionCallBackSample::~ExceptionCallBackSample() { (void)Destroy(); } |
| 59 | -{ | ||
| 60 | - (void)Destroy(); | ||
| 61 | -} | ||
| 62 | 53 | ||
| 63 | int ExceptionCallBackSpace::ExceptionCallBackSample::Init() | 54 | int ExceptionCallBackSpace::ExceptionCallBackSample::Init() |
| 64 | { | 55 | { |
| @@ -70,7 +61,7 @@ int ExceptionCallBackSpace::ExceptionCallBackSample::Init() | |||
| 70 | return 0; | 61 | return 0; |
| 71 | } | 62 | } |
| 72 | 63 | ||
| 73 | -void ExceptionCallBackSpace::ExceptionCallBackSample::ThreadFunc(void *arg) | 64 | +void ExceptionCallBackSpace::ExceptionCallBackSample::ThreadFunc(void* arg) |
| 74 | { | 65 | { |
| 75 | const int waitTime = 100; | 66 | const int waitTime = 100; |
| 76 | aclrtSetCurrentContext(context_); | 67 | aclrtSetCurrentContext(context_); |
| @@ -80,13 +71,13 @@ void ExceptionCallBackSpace::ExceptionCallBackSample::ThreadFunc(void *arg) | |||
| 80 | INFO_LOG("Thread exit"); | 71 | INFO_LOG("Thread exit"); |
| 81 | } | 72 | } |
| 82 | 73 | ||
| 83 | -void ExceptionCallBackSpace::ExceptionCallBackSample::CallBackFunc(void *arg) | 74 | +void ExceptionCallBackSpace::ExceptionCallBackSample::CallBackFunc(void* arg) |
| 84 | { | 75 | { |
| 85 | - int *data = static_cast<int*>(arg); | 76 | + int* data = static_cast<int*>(arg); |
| 86 | INFO_LOG("After error still callback, the userdata is: %d.", *data); | 77 | INFO_LOG("After error still callback, the userdata is: %d.", *data); |
| 87 | } | 78 | } |
| 88 | 79 | ||
| 89 | -void ExceptionCallBackSpace::ExceptionCallBackSample::ExceptionCallBackFunc(aclrtExceptionInfo *exceptionInfo) | 80 | +void ExceptionCallBackSpace::ExceptionCallBackSample::ExceptionCallBackFunc(aclrtExceptionInfo* exceptionInfo) |
| 90 | { | 81 | { |
| 91 | INFO_LOG("Exception occurred, callback function."); | 82 | INFO_LOG("Exception occurred, callback function."); |
| 92 | uint32_t errorMsg = aclrtGetTaskIdFromExceptionInfo(exceptionInfo); | 83 | uint32_t errorMsg = aclrtGetTaskIdFromExceptionInfo(exceptionInfo); |
| @@ -100,16 +91,16 @@ void ExceptionCallBackSpace::ExceptionCallBackSample::ExceptionCallBackFunc(aclr | |||
| 100 | errorMsg = aclrtGetErrorCodeFromExceptionInfo(exceptionInfo); | 91 | errorMsg = aclrtGetErrorCodeFromExceptionInfo(exceptionInfo); |
| 101 | INFO_LOG("The error code id is %u.", errorMsg); | 92 | INFO_LOG("The error code id is %u.", errorMsg); |
| 102 | 93 | ||
| 103 | - void *devArgsPtr = nullptr; | 94 | + void* devArgsPtr = nullptr; |
| 104 | uint32_t devArgsLen = 0; | 95 | uint32_t devArgsLen = 0; |
| 105 | aclError argsRet = aclrtGetArgsFromExceptionInfo(exceptionInfo, &devArgsPtr, &devArgsLen); | 96 | aclError argsRet = aclrtGetArgsFromExceptionInfo(exceptionInfo, &devArgsPtr, &devArgsLen); |
| 106 | - INFO_LOG("Exception args query ret=%d, devArgsPtr=%p, devArgsLen=%u", | 97 | + INFO_LOG( |
| 107 | - static_cast<int32_t>(argsRet), devArgsPtr, devArgsLen); | 98 | + "Exception args query ret=%d, devArgsPtr=%p, devArgsLen=%u", static_cast<int32_t>(argsRet), devArgsPtr, |
| 99 | + devArgsLen); | ||
| 108 | 100 | ||
| 109 | aclrtFuncHandle funcHandle = nullptr; | 101 | aclrtFuncHandle funcHandle = nullptr; |
| 110 | aclError funcRet = aclrtGetFuncHandleFromExceptionInfo(exceptionInfo, &funcHandle); | 102 | aclError funcRet = aclrtGetFuncHandleFromExceptionInfo(exceptionInfo, &funcHandle); |
| 111 | - INFO_LOG("Exception func handle query ret=%d, funcHandle=%p", | 103 | + INFO_LOG("Exception func handle query ret=%d, funcHandle=%p", static_cast<int32_t>(funcRet), funcHandle); |
| 112 | - static_cast<int32_t>(funcRet), funcHandle); | ||
| 113 | } | 104 | } |
| 114 | 105 | ||
| 115 | int ExceptionCallBackSpace::ExceptionCallBackSample::Callback() | 106 | int ExceptionCallBackSpace::ExceptionCallBackSample::Callback() |
| @@ -118,16 +109,16 @@ int ExceptionCallBackSpace::ExceptionCallBackSample::Callback() | |||
| 118 | const int blockDim = 1; | 109 | const int blockDim = 1; |
| 119 | bool isLoop = true; | 110 | bool isLoop = true; |
| 120 | const size_t size = sizeof(uint32_t); | 111 | const size_t size = sizeof(uint32_t); |
| 121 | - uint32_t *numDevice = nullptr; | 112 | + uint32_t* numDevice = nullptr; |
| 122 | uint32_t taskId = 0; | 113 | uint32_t taskId = 0; |
| 123 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 114 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 124 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); | 115 | CHECK_ERROR(aclrtMemcpy(numDevice, size, &num, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 125 | 116 | ||
| 126 | thread td(ThreadFunc, &isLoop); | 117 | thread td(ThreadFunc, &isLoop); |
| 127 | thread::id tid = td.get_id(); | 118 | thread::id tid = td.get_id(); |
| 128 | ostringstream oss; | 119 | ostringstream oss; |
| 129 | oss << tid; | 120 | oss << tid; |
| 130 | - int *userData = new int(520); | 121 | + int* userData = new int(520); |
| 131 | uint64_t tidInt = std::stoull(oss.str()); | 122 | uint64_t tidInt = std::stoull(oss.str()); |
| 132 | CHECK_ERROR(aclrtSubscribeReport(tidInt, stream_)); | 123 | CHECK_ERROR(aclrtSubscribeReport(tidInt, stream_)); |
| 133 | CHECK_ERROR(aclrtSetExceptionInfoCallback(ExceptionCallBackFunc)); | 124 | CHECK_ERROR(aclrtSetExceptionInfoCallback(ExceptionCallBackFunc)); |
| @@ -14,23 +14,22 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace ExceptionCallBackSpace { | 16 | namespace ExceptionCallBackSpace { |
| 17 | - class ExceptionCallBackSample { | 17 | +class ExceptionCallBackSample { |
| 18 | - public: | 18 | +public: |
| 19 | - ExceptionCallBackSample(); | 19 | + ExceptionCallBackSample(); |
| 20 | - virtual ~ExceptionCallBackSample(); | 20 | + virtual ~ExceptionCallBackSample(); |
| 21 | - int Init(); | 21 | + int Init(); |
| 22 | - int Callback(); | 22 | + int Callback(); |
| 23 | - int Destroy(); | 23 | + int Destroy(); |
| 24 | - static void ThreadFunc(void *arg); | 24 | + static void ThreadFunc(void* arg); |
| 25 | - static void CallBackFunc(void *arg); | 25 | + static void CallBackFunc(void* arg); |
| 26 | - static void ExceptionCallBackFunc(aclrtExceptionInfo *exceptionInfo); | 26 | + static void ExceptionCallBackFunc(aclrtExceptionInfo* exceptionInfo); |
| 27 | - | ||
| 28 | - public: | ||
| 29 | - static int32_t deviceId_; | ||
| 30 | - static aclrtContext context_; | ||
| 31 | - static aclrtStream stream_; | ||
| 32 | - }; | ||
| 33 | -} | ||
| 34 | 27 | ||
| 28 | +public: | ||
| 29 | + static int32_t deviceId_; | ||
| 30 | + static aclrtContext context_; | ||
| 31 | + static aclrtStream stream_; | ||
| 32 | +}; | ||
| 33 | +} // namespace ExceptionCallBackSpace | ||
| 35 | 34 | ||
| 36 | 35 | ||
| @@ -13,19 +13,19 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | namespace CallbackUtils { | 15 | namespace CallbackUtils { |
| 16 | - bool IsLoopFlag(void *arg) | 16 | +bool IsLoopFlag(void* arg) |
| 17 | - { | 17 | +{ |
| 18 | - if (*(static_cast<bool *>(arg))) { | 18 | + if (*(static_cast<bool*>(arg))) { |
| 19 | - return true; | 19 | + return true; |
| 20 | - } else { | 20 | + } else { |
| 21 | - return false; | 21 | + return false; |
| 22 | - } | ||
| 23 | } | 22 | } |
| 24 | - std::string GetThreadId(std::thread::id tid) | 23 | +} |
| 25 | - { | 24 | +std::string GetThreadId(std::thread::id tid) |
| 26 | - std::ostringstream oss; | 25 | +{ |
| 27 | - oss << tid; | 26 | + std::ostringstream oss; |
| 28 | - std::string tidStr = oss.str(); | 27 | + oss << tid; |
| 29 | - return tidStr; | 28 | + std::string tidStr = oss.str(); |
| 30 | - } | 29 | + return tidStr; |
| 31 | -} | 30 | +} |
| 31 | +} // namespace CallbackUtils | ||
| @@ -15,8 +15,8 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | namespace CallbackUtils { | 17 | namespace CallbackUtils { |
| 18 | - bool IsLoopFlag(void *arg); | 18 | +bool IsLoopFlag(void* arg); |
| 19 | - std::string GetThreadId(std::thread::id tid); | 19 | +std::string GetThreadId(std::thread::id tid); |
| 20 | -} | 20 | +} // namespace CallbackUtils |
| 21 | 21 | ||
| 22 | 22 | ||
| @@ -24,7 +24,8 @@ | |||
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | -int main() { | 27 | +int main() |
| 28 | +{ | ||
| 28 | CHECK_ERROR(aclInit(nullptr)); | 29 | CHECK_ERROR(aclInit(nullptr)); |
| 29 | 30 | ||
| 30 | int32_t deviceId = 0; | 31 | int32_t deviceId = 0; |
| @@ -23,7 +23,8 @@ | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | -int32_t main() { | 26 | +int32_t main() |
| 27 | +{ | ||
| 27 | CHECK_ERROR(aclInit(nullptr)); | 28 | CHECK_ERROR(aclInit(nullptr)); |
| 28 | 29 | ||
| 29 | int32_t deviceId = 0; | 30 | int32_t deviceId = 0; |
| @@ -66,7 +67,7 @@ int32_t main() { | |||
| 66 | 67 | ||
| 67 | // 5. 模拟消费者自身的工作 | 68 | // 5. 模拟消费者自身的工作 |
| 68 | INFO_LOG("Process B: doing some work..."); | 69 | INFO_LOG("Process B: doing some work..."); |
| 69 | - usleep(2000000); // 2秒 | 70 | + usleep(2000000); // 2秒 |
| 70 | 71 | ||
| 71 | // 6. 记录事件,通知生产者 | 72 | // 6. 记录事件,通知生产者 |
| 72 | CHECK_ERROR(aclrtRecordEvent(ipcEvent, stream)); | 73 | CHECK_ERROR(aclrtRecordEvent(ipcEvent, stream)); |
| @@ -36,8 +36,8 @@ int main(int argc, char* argv[]) | |||
| 36 | 36 | ||
| 37 | INFO_LOG("Available device count: %u, required device count: %d", deviceCount, requiredDeviceCount); | 37 | INFO_LOG("Available device count: %u, required device count: %d", deviceCount, requiredDeviceCount); |
| 38 | if (deviceCount < static_cast<uint32_t>(requiredDeviceCount)) { | 38 | if (deviceCount < static_cast<uint32_t>(requiredDeviceCount)) { |
| 39 | - INFO_LOG("[SKIP] Need at least %d devices, but only %u device(s) are available.", requiredDeviceCount, | 39 | + INFO_LOG( |
| 40 | - deviceCount); | 40 | + "[SKIP] Need at least %d devices, but only %u device(s) are available.", requiredDeviceCount, deviceCount); |
| 41 | return 2; | 41 | return 2; |
| 42 | } | 42 | } |
| 43 | return 0; | 43 | return 0; |
| @@ -25,7 +25,8 @@ | |||
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | // 等待所有消费者完成(通过检查每个消费者创建的完成标志文件) | 27 | // 等待所有消费者完成(通过检查每个消费者创建的完成标志文件) |
| 28 | -int WaitForConsumers(int expectedCount) { | 28 | +int WaitForConsumers(int expectedCount) |
| 29 | +{ | ||
| 29 | char flagFile[64]; | 30 | char flagFile[64]; |
| 30 | int completed = 0; | 31 | int completed = 0; |
| 31 | int timeoutSec = 30; | 32 | int timeoutSec = 30; |
| @@ -53,7 +54,8 @@ int WaitForConsumers(int expectedCount) { | |||
| 53 | } | 54 | } |
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | -int main(int argc, char* argv[]) { | 57 | +int main(int argc, char* argv[]) |
| 58 | +{ | ||
| 57 | if (argc != 2) { | 59 | if (argc != 2) { |
| 58 | ERROR_LOG("Usage: %s <number_of_consumers>", argv[0]); | 60 | ERROR_LOG("Usage: %s <number_of_consumers>", argv[0]); |
| 59 | return -1; | 61 | return -1; |
| @@ -23,7 +23,8 @@ | |||
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | -int main(int argc, char* argv[]) { | 26 | +int main(int argc, char* argv[]) |
| 27 | +{ | ||
| 27 | if (argc != 3) { | 28 | if (argc != 3) { |
| 28 | ERROR_LOG("Usage: %s <device_id> <consumer_id>", argv[0]); | 29 | ERROR_LOG("Usage: %s <device_id> <consumer_id>", argv[0]); |
| 29 | return -1; | 30 | return -1; |
| @@ -71,7 +72,7 @@ int main(int argc, char* argv[]) { | |||
| 71 | 72 | ||
| 72 | // 5. 模拟消费者自身的工作 | 73 | // 5. 模拟消费者自身的工作 |
| 73 | INFO_LOG("Consumer %d: doing some work...", consumerId); | 74 | INFO_LOG("Consumer %d: doing some work...", consumerId); |
| 74 | - usleep(1000000); // 1秒 | 75 | + usleep(1000000); // 1秒 |
| 75 | 76 | ||
| 76 | // 6. 记录事件,通知其他等待者(这里主要是为了演示,实际已无等待者) | 77 | // 6. 记录事件,通知其他等待者(这里主要是为了演示,实际已无等待者) |
| 77 | CHECK_ERROR(aclrtRecordEvent(ipcEvent, stream)); | 78 | CHECK_ERROR(aclrtRecordEvent(ipcEvent, stream)); |
| @@ -26,12 +26,12 @@ | |||
| 26 | 26 | ||
| 27 | namespace { | 27 | namespace { |
| 28 | struct KernelBuffers { | 28 | struct KernelBuffers { |
| 29 | - uint8_t *xHost = nullptr; | 29 | + uint8_t* xHost = nullptr; |
| 30 | - uint8_t *yHost = nullptr; | 30 | + uint8_t* yHost = nullptr; |
| 31 | - uint8_t *zHost = nullptr; | 31 | + uint8_t* zHost = nullptr; |
| 32 | - uint8_t *xDevice = nullptr; | 32 | + uint8_t* xDevice = nullptr; |
| 33 | - uint8_t *yDevice = nullptr; | 33 | + uint8_t* yDevice = nullptr; |
| 34 | - uint8_t *zDevice = nullptr; | 34 | + uint8_t* zDevice = nullptr; |
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| 37 | struct RuntimeResources { | 37 | struct RuntimeResources { |
| @@ -44,7 +44,7 @@ struct RuntimeResources { | |||
| 44 | bool binLoaded = false; | 44 | bool binLoaded = false; |
| 45 | }; | 45 | }; |
| 46 | 46 | ||
| 47 | -void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalResult) | 47 | +void UpdateFinalResultOnError(const char* apiName, aclError ret, int32_t& finalResult) |
| 48 | { | 48 | { |
| 49 | if (ret == ACL_SUCCESS) { | 49 | if (ret == ACL_SUCCESS) { |
| 50 | return; | 50 | return; |
| @@ -53,7 +53,7 @@ void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalR | |||
| 53 | finalResult = -1; | 53 | finalResult = -1; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | -int32_t InitializeRuntime(RuntimeResources *runtime) | 56 | +int32_t InitializeRuntime(RuntimeResources* runtime) |
| 57 | { | 57 | { |
| 58 | // Initialize ACL and create a stream on device 0. | 58 | // Initialize ACL and create a stream on device 0. |
| 59 | CHECK_ERROR(aclInit(nullptr)); | 59 | CHECK_ERROR(aclInit(nullptr)); |
| @@ -65,19 +65,19 @@ int32_t InitializeRuntime(RuntimeResources *runtime) | |||
| 65 | return 0; | 65 | return 0; |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | -int32_t AllocateKernelBuffers(size_t inputByteSize, size_t outputByteSize, KernelBuffers *buffers) | 68 | +int32_t AllocateKernelBuffers(size_t inputByteSize, size_t outputByteSize, KernelBuffers* buffers) |
| 69 | { | 69 | { |
| 70 | // Allocate host and device buffers for kernel inputs and output. | 70 | // Allocate host and device buffers for kernel inputs and output. |
| 71 | - CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void **>(&buffers->xHost), inputByteSize)); | 71 | + CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&buffers->xHost), inputByteSize)); |
| 72 | - CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void **>(&buffers->yHost), inputByteSize)); | 72 | + CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&buffers->yHost), inputByteSize)); |
| 73 | - CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void **>(&buffers->zHost), outputByteSize)); | 73 | + CHECK_ERROR(aclrtMallocHost(reinterpret_cast<void**>(&buffers->zHost), outputByteSize)); |
| 74 | - CHECK_ERROR(aclrtMalloc(reinterpret_cast<void **>(&buffers->xDevice), inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 74 | + CHECK_ERROR(aclrtMalloc(reinterpret_cast<void**>(&buffers->xDevice), inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 75 | - CHECK_ERROR(aclrtMalloc(reinterpret_cast<void **>(&buffers->yDevice), inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 75 | + CHECK_ERROR(aclrtMalloc(reinterpret_cast<void**>(&buffers->yDevice), inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 76 | - CHECK_ERROR(aclrtMalloc(reinterpret_cast<void **>(&buffers->zDevice), outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 76 | + CHECK_ERROR(aclrtMalloc(reinterpret_cast<void**>(&buffers->zDevice), outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 77 | return 0; | 77 | return 0; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | -int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers &buffers) | 80 | +int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers& buffers) |
| 81 | { | 81 | { |
| 82 | // Load generated input files and copy them to device memory. | 82 | // Load generated input files and copy them to device memory. |
| 83 | size_t xFileSize = inputByteSize; | 83 | size_t xFileSize = inputByteSize; |
| @@ -98,14 +98,17 @@ int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers &buffers) | |||
| 98 | return 0; | 98 | return 0; |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | -int32_t AppendCommonKernelArgs(aclrtArgsHandle argsHandle, uint8_t *xDevice, uint8_t *yDevice, uint8_t *zDevice) | 101 | +int32_t AppendCommonKernelArgs(aclrtArgsHandle argsHandle, uint8_t* xDevice, uint8_t* yDevice, uint8_t* zDevice) |
| 102 | { | 102 | { |
| 103 | aclrtParamHandle paramHandle1 = nullptr; | 103 | aclrtParamHandle paramHandle1 = nullptr; |
| 104 | aclrtParamHandle paramHandle2 = nullptr; | 104 | aclrtParamHandle paramHandle2 = nullptr; |
| 105 | aclrtParamHandle paramHandle3 = nullptr; | 105 | aclrtParamHandle paramHandle3 = nullptr; |
| 106 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&xDevice), sizeof(uintptr_t), ¶mHandle1)); | 106 | + CHECK_ERROR( |
| 107 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&yDevice), sizeof(uintptr_t), ¶mHandle2)); | 107 | + aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&xDevice), sizeof(uintptr_t), ¶mHandle1)); |
| 108 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&zDevice), sizeof(uintptr_t), ¶mHandle3)); | 108 | + CHECK_ERROR( |
| 109 | + aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&yDevice), sizeof(uintptr_t), ¶mHandle2)); | ||
| 110 | + CHECK_ERROR( | ||
| 111 | + aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&zDevice), sizeof(uintptr_t), ¶mHandle3)); | ||
| 109 | return 0; | 112 | return 0; |
| 110 | } | 113 | } |
| 111 | 114 | ||
| @@ -114,17 +117,17 @@ int32_t ConfigurePlaceholderArgs(aclrtArgsHandle argsHandle) | |||
| 114 | constexpr int32_t TOTAL_LENGTH = 8 * 2048; | 117 | constexpr int32_t TOTAL_LENGTH = 8 * 2048; |
| 115 | constexpr int32_t TILE_NUM = 8; | 118 | constexpr int32_t TILE_NUM = 8; |
| 116 | 119 | ||
| 117 | - int32_t *lengthHost = nullptr; | 120 | + int32_t* lengthHost = nullptr; |
| 118 | - int32_t *numHost = nullptr; | 121 | + int32_t* numHost = nullptr; |
| 119 | aclrtParamHandle paramHandle4 = nullptr; | 122 | aclrtParamHandle paramHandle4 = nullptr; |
| 120 | aclrtParamHandle paramHandle5 = nullptr; | 123 | aclrtParamHandle paramHandle5 = nullptr; |
| 121 | 124 | ||
| 122 | CHECK_ERROR(aclrtKernelArgsAppendPlaceHolder(argsHandle, ¶mHandle4)); | 125 | CHECK_ERROR(aclrtKernelArgsAppendPlaceHolder(argsHandle, ¶mHandle4)); |
| 123 | CHECK_ERROR(aclrtKernelArgsAppendPlaceHolder(argsHandle, ¶mHandle5)); | 126 | CHECK_ERROR(aclrtKernelArgsAppendPlaceHolder(argsHandle, ¶mHandle5)); |
| 124 | CHECK_ERROR(aclrtKernelArgsGetPlaceHolderBuffer( | 127 | CHECK_ERROR(aclrtKernelArgsGetPlaceHolderBuffer( |
| 125 | - argsHandle, paramHandle4, sizeof(TOTAL_LENGTH), reinterpret_cast<void **>(&lengthHost))); | 128 | + argsHandle, paramHandle4, sizeof(TOTAL_LENGTH), reinterpret_cast<void**>(&lengthHost))); |
| 126 | CHECK_ERROR(aclrtKernelArgsGetPlaceHolderBuffer( | 129 | CHECK_ERROR(aclrtKernelArgsGetPlaceHolderBuffer( |
| 127 | - argsHandle, paramHandle5, sizeof(TILE_NUM), reinterpret_cast<void **>(&numHost))); | 130 | + argsHandle, paramHandle5, sizeof(TILE_NUM), reinterpret_cast<void**>(&numHost))); |
| 128 | 131 | ||
| 129 | *lengthHost = TOTAL_LENGTH; | 132 | *lengthHost = TOTAL_LENGTH; |
| 130 | *numHost = TILE_NUM; | 133 | *numHost = TILE_NUM; |
| @@ -132,19 +135,13 @@ int32_t ConfigurePlaceholderArgs(aclrtArgsHandle argsHandle) | |||
| 132 | } | 135 | } |
| 133 | 136 | ||
| 134 | int32_t BuildKernelArgs( | 137 | int32_t BuildKernelArgs( |
| 135 | - const std::string &mode, | 138 | + const std::string& mode, uint8_t* xDevice, uint8_t* yDevice, uint8_t* zDevice, RuntimeResources* runtime, |
| 136 | - uint8_t *xDevice, | 139 | + aclrtFuncHandle* funcHandle, aclrtArgsHandle* argsHandle) |
| 137 | - uint8_t *yDevice, | ||
| 138 | - uint8_t *zDevice, | ||
| 139 | - RuntimeResources *runtime, | ||
| 140 | - aclrtFuncHandle *funcHandle, | ||
| 141 | - aclrtArgsHandle *argsHandle) | ||
| 142 | { | 140 | { |
| 143 | // Load the selected kernel binary and build the launch argument list. | 141 | // Load the selected kernel binary and build the launch argument list. |
| 144 | const bool isPlaceholder = (mode == "placeholder"); | 142 | const bool isPlaceholder = (mode == "placeholder"); |
| 145 | - const char *filePath = isPlaceholder | 143 | + const char* filePath = isPlaceholder ? "./out/fatbin/ascendc_kernels_placeholder/ascendc_kernels_placeholder.o" : |
| 146 | - ? "./out/fatbin/ascendc_kernels_placeholder/ascendc_kernels_placeholder.o" | 144 | + "./out/fatbin/ascendc_kernels_simple/ascendc_kernels_simple.o"; |
| 147 | - : "./out/fatbin/ascendc_kernels_simple/ascendc_kernels_simple.o"; | ||
| 148 | 145 | ||
| 149 | CHECK_ERROR(aclrtBinaryLoadFromFile(filePath, nullptr, &runtime->binHandle)); | 146 | CHECK_ERROR(aclrtBinaryLoadFromFile(filePath, nullptr, &runtime->binHandle)); |
| 150 | runtime->binLoaded = true; | 147 | runtime->binLoaded = true; |
| @@ -163,13 +160,8 @@ int32_t BuildKernelArgs( | |||
| 163 | } | 160 | } |
| 164 | 161 | ||
| 165 | int32_t LaunchKernelAndWriteOutput( | 162 | int32_t LaunchKernelAndWriteOutput( |
| 166 | - aclrtFuncHandle funcHandle, | 163 | + aclrtFuncHandle funcHandle, aclrtArgsHandle argsHandle, uint32_t blockDim, aclrtStream stream, uint8_t* zDevice, |
| 167 | - aclrtArgsHandle argsHandle, | 164 | + uint8_t* zHost, size_t outputByteSize) |
| 168 | - uint32_t blockDim, | ||
| 169 | - aclrtStream stream, | ||
| 170 | - uint8_t *zDevice, | ||
| 171 | - uint8_t *zHost, | ||
| 172 | - size_t outputByteSize) | ||
| 173 | { | 165 | { |
| 174 | // Launch the kernel, synchronize the stream, and write output for verification. | 166 | // Launch the kernel, synchronize the stream, and write output for verification. |
| 175 | CHECK_ERROR(aclrtLaunchKernelWithConfig(funcHandle, blockDim, stream, nullptr, argsHandle, nullptr)); | 167 | CHECK_ERROR(aclrtLaunchKernelWithConfig(funcHandle, blockDim, stream, nullptr, argsHandle, nullptr)); |
| @@ -182,7 +174,7 @@ int32_t LaunchKernelAndWriteOutput( | |||
| 182 | return 0; | 174 | return 0; |
| 183 | } | 175 | } |
| 184 | 176 | ||
| 185 | -void ReleaseKernelResources(RuntimeResources &runtime, KernelBuffers &buffers, int32_t &finalResult) | 177 | +void ReleaseKernelResources(RuntimeResources& runtime, KernelBuffers& buffers, int32_t& finalResult) |
| 186 | { | 178 | { |
| 187 | if (runtime.binLoaded) { | 179 | if (runtime.binLoaded) { |
| 188 | UpdateFinalResultOnError( | 180 | UpdateFinalResultOnError( |
| @@ -219,7 +211,7 @@ void ReleaseKernelResources(RuntimeResources &runtime, KernelBuffers &buffers, i | |||
| 219 | } | 211 | } |
| 220 | } | 212 | } |
| 221 | 213 | ||
| 222 | -int32_t RunKernelLaunchSample(const std::string &mode) | 214 | +int32_t RunKernelLaunchSample(const std::string& mode) |
| 223 | { | 215 | { |
| 224 | const uint32_t blockDim = 8; | 216 | const uint32_t blockDim = 8; |
| 225 | const size_t inputByteSize = 8 * 2048 * sizeof(uint16_t); | 217 | const size_t inputByteSize = 8 * 2048 * sizeof(uint16_t); |
| @@ -243,11 +235,12 @@ int32_t RunKernelLaunchSample(const std::string &mode) | |||
| 243 | return -1; | 235 | return -1; |
| 244 | } | 236 | } |
| 245 | if (BuildKernelArgs( | 237 | if (BuildKernelArgs( |
| 246 | - mode, buffers.xDevice, buffers.yDevice, buffers.zDevice, &runtime, &funcHandle, &argsHandle) != 0) { | 238 | + mode, buffers.xDevice, buffers.yDevice, buffers.zDevice, &runtime, &funcHandle, &argsHandle) != 0) { |
| 247 | return -1; | 239 | return -1; |
| 248 | } | 240 | } |
| 249 | if (LaunchKernelAndWriteOutput( | 241 | if (LaunchKernelAndWriteOutput( |
| 250 | - funcHandle, argsHandle, blockDim, runtime.stream, buffers.zDevice, buffers.zHost, outputByteSize) != 0) { | 242 | + funcHandle, argsHandle, blockDim, runtime.stream, buffers.zDevice, buffers.zHost, outputByteSize) != |
| 243 | + 0) { | ||
| 251 | return -1; | 244 | return -1; |
| 252 | } | 245 | } |
| 253 | INFO_LOG("Kernel launch sample runs in %s mode.", mode.c_str()); | 246 | INFO_LOG("Kernel launch sample runs in %s mode.", mode.c_str()); |
| @@ -263,7 +256,7 @@ int32_t RunKernelLaunchSample(const std::string &mode) | |||
| 263 | } | 256 | } |
| 264 | } // namespace | 257 | } // namespace |
| 265 | 258 | ||
| 266 | -int32_t main(int32_t argc, char *argv[]) | 259 | +int32_t main(int32_t argc, char* argv[]) |
| 267 | { | 260 | { |
| 268 | const std::string mode = (argc > 1) ? argv[1] : "simple"; | 261 | const std::string mode = (argc > 1) ? argv[1] : "simple"; |
| 269 | if (mode != "simple" && mode != "placeholder") { | 262 | if (mode != "simple" && mode != "placeholder") { |
| @@ -18,7 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | namespace { | 20 | namespace { |
| 21 | -void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalResult) | 21 | +void UpdateFinalResultOnError(const char* apiName, aclError ret, int32_t& finalResult) |
| 22 | { | 22 | { |
| 23 | if (ret == ACL_SUCCESS) { | 23 | if (ret == ACL_SUCCESS) { |
| 24 | return; | 24 | return; |
| @@ -17,7 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | namespace kernel { | 19 | namespace kernel { |
| 20 | -bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize) | 20 | +bool ReadFile(const std::string& filePath, size_t& fileSize, void* buffer, size_t bufferSize) |
| 21 | { | 21 | { |
| 22 | // Read data from the target file | 22 | // Read data from the target file |
| 23 | struct stat sBuf; | 23 | struct stat sBuf; |
| @@ -38,7 +38,7 @@ bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_ | |||
| 38 | return false; | 38 | return false; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | - std::filebuf *buf = file.rdbuf(); | 41 | + std::filebuf* buf = file.rdbuf(); |
| 42 | size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); | 42 | size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); |
| 43 | if (size == 0U) { | 43 | if (size == 0U) { |
| 44 | ERROR_LOG("file size is 0"); | 44 | ERROR_LOG("file size is 0"); |
| @@ -51,16 +51,15 @@ bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_ | |||
| 51 | return false; | 51 | return false; |
| 52 | } | 52 | } |
| 53 | buf->pubseekpos(0, std::ios::in); | 53 | buf->pubseekpos(0, std::ios::in); |
| 54 | - (void)buf->sgetn(static_cast<char *>(buffer), static_cast<std::streamsize>(size)); | 54 | + (void)buf->sgetn(static_cast<char*>(buffer), static_cast<std::streamsize>(size)); |
| 55 | fileSize = size; | 55 | fileSize = size; |
| 56 | file.close(); | 56 | file.close(); |
| 57 | return true; | 57 | return true; |
| 58 | } | 58 | } |
| 59 | } // namespace kernel | 59 | } // namespace kernel |
| 60 | 60 | ||
| 61 | - | ||
| 62 | namespace kernel { | 61 | namespace kernel { |
| 63 | -bool WriteFile(const std::string &filePath, const void *buffer, size_t size) | 62 | +bool WriteFile(const std::string& filePath, const void* buffer, size_t size) |
| 64 | { | 63 | { |
| 65 | // Write data to the target file | 64 | // Write data to the target file |
| 66 | if (buffer == nullptr) { | 65 | if (buffer == nullptr) { |
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace kernel { | 16 | namespace kernel { |
| 17 | - bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize); | 17 | +bool ReadFile(const std::string& filePath, size_t& fileSize, void* buffer, size_t bufferSize); |
| 18 | - bool WriteFile(const std::string &filePath, const void *buffer, size_t size); | 18 | +bool WriteFile(const std::string& filePath, const void* buffer, size_t size); |
| 19 | -} | 19 | +} // namespace kernel |
| 20 | 20 | ||
| @@ -13,7 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | namespace { | 15 | namespace { |
| 16 | -void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalResult) | 16 | +void UpdateFinalResultOnError(const char* apiName, aclError ret, int32_t& finalResult) |
| 17 | { | 17 | { |
| 18 | if (ret == ACL_SUCCESS) { | 18 | if (ret == ACL_SUCCESS) { |
| 19 | return; | 19 | return; |
| @@ -35,7 +35,7 @@ int main() | |||
| 35 | aclrtStream executeStream = nullptr; | 35 | aclrtStream executeStream = nullptr; |
| 36 | aclrtLabel labels[labelCount] = {nullptr, nullptr}; | 36 | aclrtLabel labels[labelCount] = {nullptr, nullptr}; |
| 37 | aclrtLabelList labelList = nullptr; | 37 | aclrtLabelList labelList = nullptr; |
| 38 | - uint32_t *branchIndexDevice = nullptr; | 38 | + uint32_t* branchIndexDevice = nullptr; |
| 39 | aclmdlRI modelRI = nullptr; | 39 | aclmdlRI modelRI = nullptr; |
| 40 | 40 | ||
| 41 | bool aclInitialized = false; | 41 | bool aclInitialized = false; |
| @@ -82,16 +82,12 @@ int main() | |||
| 82 | 82 | ||
| 83 | // Prepare the branch index on device memory. | 83 | // Prepare the branch index on device memory. |
| 84 | CHECK_ERROR( | 84 | CHECK_ERROR( |
| 85 | - aclrtMalloc(reinterpret_cast<void **>(&branchIndexDevice), sizeof(branchIndex), ACL_MEM_MALLOC_HUGE_FIRST)); | 85 | + aclrtMalloc(reinterpret_cast<void**>(&branchIndexDevice), sizeof(branchIndex), ACL_MEM_MALLOC_HUGE_FIRST)); |
| 86 | branchIndexAllocated = true; | 86 | branchIndexAllocated = true; |
| 87 | INFO_LOG("Allocated device memory for branch index."); | 87 | INFO_LOG("Allocated device memory for branch index."); |
| 88 | 88 | ||
| 89 | CHECK_ERROR(aclrtMemcpy( | 89 | CHECK_ERROR(aclrtMemcpy( |
| 90 | - branchIndexDevice, | 90 | + branchIndexDevice, sizeof(branchIndex), &branchIndex, sizeof(branchIndex), ACL_MEMCPY_HOST_TO_DEVICE)); |
| 91 | - sizeof(branchIndex), | ||
| 92 | - &branchIndex, | ||
| 93 | - sizeof(branchIndex), | ||
| 94 | - ACL_MEMCPY_HOST_TO_DEVICE)); | ||
| 95 | INFO_LOG("Copied branch index %u from host to device.", branchIndex); | 91 | INFO_LOG("Copied branch index %u from host to device.", branchIndex); |
| 96 | 92 | ||
| 97 | // Create labels and organize them as a label list. | 93 | // Create labels and organize them as a label list. |
| @@ -8,7 +8,6 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | - | ||
| 12 | 11 | ||
| 13 | 12 | ||
| 14 | 13 | ||
| @@ -24,17 +23,17 @@ using namespace std; | |||
| 24 | int main() | 23 | int main() |
| 25 | { | 24 | { |
| 26 | int deviceId = 0; | 25 | int deviceId = 0; |
| 27 | - void *selfDevice = nullptr; | 26 | + void* selfDevice = nullptr; |
| 28 | - void *otherDevice = nullptr; | 27 | + void* otherDevice = nullptr; |
| 29 | - void *outDevice = nullptr; | 28 | + void* outDevice = nullptr; |
| 30 | - void *outTmpDevice = nullptr; | 29 | + void* outTmpDevice = nullptr; |
| 31 | - void *selfHost = nullptr; | 30 | + void* selfHost = nullptr; |
| 32 | - void *otherHost = nullptr; | 31 | + void* otherHost = nullptr; |
| 33 | - aclTensor *self = nullptr; | 32 | + aclTensor* self = nullptr; |
| 34 | - aclTensor *other = nullptr; | 33 | + aclTensor* other = nullptr; |
| 35 | - aclScalar *alpha = nullptr; | 34 | + aclScalar* alpha = nullptr; |
| 36 | - aclTensor *out = nullptr; | 35 | + aclTensor* out = nullptr; |
| 37 | - aclTensor *outTmp = nullptr; | 36 | + aclTensor* outTmp = nullptr; |
| 38 | vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8}; | 37 | vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 39 | vector<float> otherHostData = {2, 2, 2, 2, 2, 2, 2, 2}; | 38 | vector<float> otherHostData = {2, 2, 2, 2, 2, 2, 2, 2}; |
| 40 | vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 39 | vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -43,11 +42,11 @@ int main() | |||
| 43 | float alphaValue = 1.1f; | 42 | float alphaValue = 1.1f; |
| 44 | uint64_t addWorkspaceSize = 0; | 43 | uint64_t addWorkspaceSize = 0; |
| 45 | uint64_t mulWorkspaceSize = 0; | 44 | uint64_t mulWorkspaceSize = 0; |
| 46 | - aclOpExecutor *addExecutor; | 45 | + aclOpExecutor* addExecutor; |
| 47 | - aclOpExecutor *mulExecutor; | 46 | + aclOpExecutor* mulExecutor; |
| 48 | aclrtContext context; | 47 | aclrtContext context; |
| 49 | const int loopCount = 4; | 48 | const int loopCount = 4; |
| 50 | - int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); | 49 | + int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); |
| 51 | 50 | ||
| 52 | CHECK_ERROR(aclInit(NULL)); | 51 | CHECK_ERROR(aclInit(NULL)); |
| 53 | CHECK_ERROR(aclrtSetDevice(deviceId)); | 52 | CHECK_ERROR(aclrtSetDevice(deviceId)); |
| @@ -58,16 +57,16 @@ int main() | |||
| 58 | alpha = aclCreateScalar(&alphaValue, aclDataType::ACL_FLOAT); | 57 | alpha = aclCreateScalar(&alphaValue, aclDataType::ACL_FLOAT); |
| 59 | ModelUtils::CreateAclTensor(shape, &outDevice, aclDataType::ACL_FLOAT, &out); | 58 | ModelUtils::CreateAclTensor(shape, &outDevice, aclDataType::ACL_FLOAT, &out); |
| 60 | ModelUtils::CreateAclTensor(shape, &outTmpDevice, aclDataType::ACL_FLOAT, &outTmp); | 59 | ModelUtils::CreateAclTensor(shape, &outTmpDevice, aclDataType::ACL_FLOAT, &outTmp); |
| 61 | - | 60 | + |
| 62 | // 创建workspace来执行算子,不同算子的workspace可能不同,该处计算outTmp = self + other * alpha | 61 | // 创建workspace来执行算子,不同算子的workspace可能不同,该处计算outTmp = self + other * alpha |
| 63 | aclnnAddGetWorkspaceSize(self, other, alpha, outTmp, &addWorkspaceSize, &addExecutor); | 62 | aclnnAddGetWorkspaceSize(self, other, alpha, outTmp, &addWorkspaceSize, &addExecutor); |
| 64 | - void *addWorkspaceAddr = nullptr; | 63 | + void* addWorkspaceAddr = nullptr; |
| 65 | if (addWorkspaceSize > 0) { | 64 | if (addWorkspaceSize > 0) { |
| 66 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr, addWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 65 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr, addWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 67 | } | 66 | } |
| 68 | // 该算子计算out = outTmp * other,outTmp是上一个add算子的输出。 | 67 | // 该算子计算out = outTmp * other,outTmp是上一个add算子的输出。 |
| 69 | aclnnMulGetWorkspaceSize(outTmp, other, out, &mulWorkspaceSize, &mulExecutor); | 68 | aclnnMulGetWorkspaceSize(outTmp, other, out, &mulWorkspaceSize, &mulExecutor); |
| 70 | - void *mulWorkspaceAddr = nullptr; | 69 | + void* mulWorkspaceAddr = nullptr; |
| 71 | if (mulWorkspaceSize > 0) { | 70 | if (mulWorkspaceSize > 0) { |
| 72 | CHECK_ERROR(aclrtMalloc(&mulWorkspaceAddr, mulWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 71 | CHECK_ERROR(aclrtMalloc(&mulWorkspaceAddr, mulWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 73 | } | 72 | } |
| @@ -77,7 +76,7 @@ int main() | |||
| 77 | CHECK_ERROR(aclrtCreateStream(&stream)); | 76 | CHECK_ERROR(aclrtCreateStream(&stream)); |
| 78 | CHECK_ERROR(aclrtMallocHost(&selfHost, size)); | 77 | CHECK_ERROR(aclrtMallocHost(&selfHost, size)); |
| 79 | CHECK_ERROR(aclrtMemcpy(selfHost, size, selfHostData.data(), size, ACL_MEMCPY_HOST_TO_HOST)); | 78 | CHECK_ERROR(aclrtMemcpy(selfHost, size, selfHostData.data(), size, ACL_MEMCPY_HOST_TO_HOST)); |
| 80 | - | 79 | + |
| 81 | // 标志着model的开始直到end。此处设置的mode为ACL_MODEL_RI_CAPTURE_MODE_GLOBAL,禁止执行非安全的函数 | 80 | // 标志着model的开始直到end。此处设置的mode为ACL_MODEL_RI_CAPTURE_MODE_GLOBAL,禁止执行非安全的函数 |
| 82 | // 该model执行了将内存从host复制到device侧,然后调用了一个add算子和一个mul算子 | 81 | // 该model执行了将内存从host复制到device侧,然后调用了一个add算子和一个mul算子 |
| 83 | CHECK_ERROR(aclmdlRICaptureBegin(stream, ACL_MODEL_RI_CAPTURE_MODE_GLOBAL)); | 82 | CHECK_ERROR(aclmdlRICaptureBegin(stream, ACL_MODEL_RI_CAPTURE_MODE_GLOBAL)); |
| @@ -96,9 +95,9 @@ int main() | |||
| 96 | // 标志model创建结束,且保存为modelRI,后续调用可以通过modelRI进行 | 95 | // 标志model创建结束,且保存为modelRI,后续调用可以通过modelRI进行 |
| 97 | CHECK_ERROR(aclmdlRICaptureEnd(stream, &modelRI)); | 96 | CHECK_ERROR(aclmdlRICaptureEnd(stream, &modelRI)); |
| 98 | // 打印信息可以在日志中查找 | 97 | // 打印信息可以在日志中查找 |
| 99 | - const char *jsonPath = "./modelRI.json"; | 98 | + const char* jsonPath = "./modelRI.json"; |
| 100 | CHECK_ERROR(aclmdlRIDebugJsonPrint(modelRI, jsonPath, 0)); | 99 | CHECK_ERROR(aclmdlRIDebugJsonPrint(modelRI, jsonPath, 0)); |
| 101 | - | 100 | + |
| 102 | // 循环执行刚才创建的modelRI | 101 | // 循环执行刚才创建的modelRI |
| 103 | for (int i = 0; i < loopCount; i++) { | 102 | for (int i = 0; i < loopCount; i++) { |
| 104 | INFO_LOG("execute model, loop count: %d.", i + 1); | 103 | INFO_LOG("execute model, loop count: %d.", i + 1); |
| @@ -7,7 +7,7 @@ | |||
| 7 | * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 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. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | 13 | ||
| @@ -22,16 +22,16 @@ using namespace std; | |||
| 22 | int main() | 22 | int main() |
| 23 | { | 23 | { |
| 24 | int deviceId = 0; | 24 | int deviceId = 0; |
| 25 | - void *selfDevice = nullptr; | 25 | + void* selfDevice = nullptr; |
| 26 | - void *otherDevice = nullptr; | 26 | + void* otherDevice = nullptr; |
| 27 | - void *outDevice = nullptr; | 27 | + void* outDevice = nullptr; |
| 28 | - void *outTmpDevice = nullptr; | 28 | + void* outTmpDevice = nullptr; |
| 29 | - aclTensor *self = nullptr; | 29 | + aclTensor* self = nullptr; |
| 30 | - aclTensor *other = nullptr; | 30 | + aclTensor* other = nullptr; |
| 31 | - aclScalar *alpha = nullptr; | 31 | + aclScalar* alpha = nullptr; |
| 32 | - aclScalar *updateAlpha = nullptr; | 32 | + aclScalar* updateAlpha = nullptr; |
| 33 | - aclTensor *out = nullptr; | 33 | + aclTensor* out = nullptr; |
| 34 | - aclTensor *outTmp = nullptr; | 34 | + aclTensor* outTmp = nullptr; |
| 35 | vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8}; | 35 | vector<float> selfHostData = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 36 | vector<float> otherHostData = {2, 2, 2, 2, 2, 2, 2, 2}; | 36 | vector<float> otherHostData = {2, 2, 2, 2, 2, 2, 2, 2}; |
| 37 | vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 37 | vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -42,9 +42,9 @@ int main() | |||
| 42 | uint64_t firstAddWorkspaceSize = 0; | 42 | uint64_t firstAddWorkspaceSize = 0; |
| 43 | uint64_t secondAddWorkspaceSize = 0; | 43 | uint64_t secondAddWorkspaceSize = 0; |
| 44 | uint64_t updateAddWorkspaceSize = 0; | 44 | uint64_t updateAddWorkspaceSize = 0; |
| 45 | - aclOpExecutor *firstAddExecutor; | 45 | + aclOpExecutor* firstAddExecutor; |
| 46 | - aclOpExecutor *secondAddExecutor; | 46 | + aclOpExecutor* secondAddExecutor; |
| 47 | - aclOpExecutor *updateAddExecutor; | 47 | + aclOpExecutor* updateAddExecutor; |
| 48 | aclrtContext context; | 48 | aclrtContext context; |
| 49 | const int loopCount = 2; | 49 | const int loopCount = 2; |
| 50 | int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); | 50 | int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); |
| @@ -59,23 +59,23 @@ int main() | |||
| 59 | updateAlpha = aclCreateScalar(&updateAlphaValue, aclDataType::ACL_FLOAT); | 59 | updateAlpha = aclCreateScalar(&updateAlphaValue, aclDataType::ACL_FLOAT); |
| 60 | ModelUtils::CreateAclTensor(shape, &outDevice, aclDataType::ACL_FLOAT, &out); | 60 | ModelUtils::CreateAclTensor(shape, &outDevice, aclDataType::ACL_FLOAT, &out); |
| 61 | ModelUtils::CreateAclTensor(shape, &outTmpDevice, aclDataType::ACL_FLOAT, &outTmp); | 61 | ModelUtils::CreateAclTensor(shape, &outTmpDevice, aclDataType::ACL_FLOAT, &outTmp); |
| 62 | - | 62 | + |
| 63 | // 该算子计算outTmp = self + other * alpha | 63 | // 该算子计算outTmp = self + other * alpha |
| 64 | aclnnAddGetWorkspaceSize(self, other, alpha, outTmp, &firstAddWorkspaceSize, &firstAddExecutor); | 64 | aclnnAddGetWorkspaceSize(self, other, alpha, outTmp, &firstAddWorkspaceSize, &firstAddExecutor); |
| 65 | - void *firstAddWorkspaceAddr = nullptr; | 65 | + void* firstAddWorkspaceAddr = nullptr; |
| 66 | if (firstAddWorkspaceSize > 0) { | 66 | if (firstAddWorkspaceSize > 0) { |
| 67 | CHECK_ERROR(aclrtMalloc(&firstAddWorkspaceAddr, firstAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 67 | CHECK_ERROR(aclrtMalloc(&firstAddWorkspaceAddr, firstAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | // 该算子计算outTmp = outTmp + other * alpha | 70 | // 该算子计算outTmp = outTmp + other * alpha |
| 71 | aclnnAddGetWorkspaceSize(outTmp, other, alpha, out, &secondAddWorkspaceSize, &secondAddExecutor); | 71 | aclnnAddGetWorkspaceSize(outTmp, other, alpha, out, &secondAddWorkspaceSize, &secondAddExecutor); |
| 72 | - void *secondAddWorkspaceAddr = nullptr; | 72 | + void* secondAddWorkspaceAddr = nullptr; |
| 73 | if (secondAddWorkspaceSize > 0) { | 73 | if (secondAddWorkspaceSize > 0) { |
| 74 | CHECK_ERROR(aclrtMalloc(&secondAddWorkspaceAddr, secondAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 74 | CHECK_ERROR(aclrtMalloc(&secondAddWorkspaceAddr, secondAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 75 | } | 75 | } |
| 76 | // 该算子计算outTmp = outTmp + other * updateAlpha | 76 | // 该算子计算outTmp = outTmp + other * updateAlpha |
| 77 | aclnnAddGetWorkspaceSize(outTmp, other, updateAlpha, out, &updateAddWorkspaceSize, &updateAddExecutor); | 77 | aclnnAddGetWorkspaceSize(outTmp, other, updateAlpha, out, &updateAddWorkspaceSize, &updateAddExecutor); |
| 78 | - void *updateAddWorkspaceAddr = nullptr; | 78 | + void* updateAddWorkspaceAddr = nullptr; |
| 79 | if (updateAddWorkspaceSize > 0) { | 79 | if (updateAddWorkspaceSize > 0) { |
| 80 | CHECK_ERROR(aclrtMalloc(&updateAddWorkspaceAddr, updateAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 80 | CHECK_ERROR(aclrtMalloc(&updateAddWorkspaceAddr, updateAddWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 81 | } | 81 | } |
| @@ -23,26 +23,26 @@ int main() | |||
| 23 | int deviceId = 0; | 23 | int deviceId = 0; |
| 24 | int blockDim = 1; | 24 | int blockDim = 1; |
| 25 | // 初始化数据,包括三个算子任务所需要的输入输出 | 25 | // 初始化数据,包括三个算子任务所需要的输入输出 |
| 26 | - uint32_t *numDevice = nullptr; | 26 | + uint32_t* numDevice = nullptr; |
| 27 | - void *selfDevice1 = nullptr; | 27 | + void* selfDevice1 = nullptr; |
| 28 | - void *otherDevice1 = nullptr; | 28 | + void* otherDevice1 = nullptr; |
| 29 | - void *outDevice1 = nullptr; | 29 | + void* outDevice1 = nullptr; |
| 30 | - void *selfDevice2 = nullptr; | 30 | + void* selfDevice2 = nullptr; |
| 31 | - void *otherDevice2 = nullptr; | 31 | + void* otherDevice2 = nullptr; |
| 32 | - void *outDevice2 = nullptr; | 32 | + void* outDevice2 = nullptr; |
| 33 | - void *selfDevice3 = nullptr; | 33 | + void* selfDevice3 = nullptr; |
| 34 | - void *otherDevice3 = nullptr; | 34 | + void* otherDevice3 = nullptr; |
| 35 | - void *outDevice3 = nullptr; | 35 | + void* outDevice3 = nullptr; |
| 36 | - aclTensor *self1 = nullptr; | 36 | + aclTensor* self1 = nullptr; |
| 37 | - aclTensor *other1 = nullptr; | 37 | + aclTensor* other1 = nullptr; |
| 38 | - aclScalar *alpha = nullptr; | 38 | + aclScalar* alpha = nullptr; |
| 39 | - aclTensor *out1 = nullptr; | 39 | + aclTensor* out1 = nullptr; |
| 40 | - aclTensor *self2 = nullptr; | 40 | + aclTensor* self2 = nullptr; |
| 41 | - aclTensor *other2 = nullptr; | 41 | + aclTensor* other2 = nullptr; |
| 42 | - aclTensor *out2 = nullptr; | 42 | + aclTensor* out2 = nullptr; |
| 43 | - aclTensor *self3 = nullptr; | 43 | + aclTensor* self3 = nullptr; |
| 44 | - aclTensor *other3 = nullptr; | 44 | + aclTensor* other3 = nullptr; |
| 45 | - aclTensor *out3 = nullptr; | 45 | + aclTensor* out3 = nullptr; |
| 46 | vector<float> selfHostData1 = {1, 1, 1, 1, 1, 1, 1, 1}; | 46 | vector<float> selfHostData1 = {1, 1, 1, 1, 1, 1, 1, 1}; |
| 47 | vector<float> otherHostData1 = {2, 2, 2, 2, 2, 2, 2, 2}; | 47 | vector<float> otherHostData1 = {2, 2, 2, 2, 2, 2, 2, 2}; |
| 48 | vector<float> outHostData1 = {0, 0, 0, 0, 0, 0, 0, 0}; | 48 | vector<float> outHostData1 = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -57,9 +57,9 @@ int main() | |||
| 57 | uint64_t addWorkspaceSize1 = 0; | 57 | uint64_t addWorkspaceSize1 = 0; |
| 58 | uint64_t addWorkspaceSize2 = 0; | 58 | uint64_t addWorkspaceSize2 = 0; |
| 59 | uint64_t addWorkspaceSize3 = 0; | 59 | uint64_t addWorkspaceSize3 = 0; |
| 60 | - aclOpExecutor *addExecutor1; | 60 | + aclOpExecutor* addExecutor1; |
| 61 | - aclOpExecutor *addExecutor2; | 61 | + aclOpExecutor* addExecutor2; |
| 62 | - aclOpExecutor *addExecutor3; | 62 | + aclOpExecutor* addExecutor3; |
| 63 | aclrtContext context; | 63 | aclrtContext context; |
| 64 | int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); | 64 | int64_t size = ModelUtils::GetShapeSize(shape) * sizeof(float); |
| 65 | CHECK_ERROR(aclInit(NULL)); | 65 | CHECK_ERROR(aclInit(NULL)); |
| @@ -80,37 +80,37 @@ int main() | |||
| 80 | ModelUtils::CreateAclTensor(shape, &outDevice2, aclDataType::ACL_FLOAT, &out2); | 80 | ModelUtils::CreateAclTensor(shape, &outDevice2, aclDataType::ACL_FLOAT, &out2); |
| 81 | ModelUtils::CreateAclTensor(shape, &outDevice3, aclDataType::ACL_FLOAT, &out3); | 81 | ModelUtils::CreateAclTensor(shape, &outDevice3, aclDataType::ACL_FLOAT, &out3); |
| 82 | aclnnAddGetWorkspaceSize(self1, other1, alpha, out1, &addWorkspaceSize1, &addExecutor1); | 82 | aclnnAddGetWorkspaceSize(self1, other1, alpha, out1, &addWorkspaceSize1, &addExecutor1); |
| 83 | - void *addWorkspaceAddr1 = nullptr; | 83 | + void* addWorkspaceAddr1 = nullptr; |
| 84 | if (addWorkspaceSize1 > 0) { | 84 | if (addWorkspaceSize1 > 0) { |
| 85 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr1, addWorkspaceSize1, ACL_MEM_MALLOC_HUGE_FIRST)); | 85 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr1, addWorkspaceSize1, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 86 | } | 86 | } |
| 87 | aclnnAddGetWorkspaceSize(self2, other2, alpha, out2, &addWorkspaceSize2, &addExecutor2); | 87 | aclnnAddGetWorkspaceSize(self2, other2, alpha, out2, &addWorkspaceSize2, &addExecutor2); |
| 88 | - void *addWorkspaceAddr2 = nullptr; | 88 | + void* addWorkspaceAddr2 = nullptr; |
| 89 | if (addWorkspaceSize2 > 0) { | 89 | if (addWorkspaceSize2 > 0) { |
| 90 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr2, addWorkspaceSize2, ACL_MEM_MALLOC_HUGE_FIRST)); | 90 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr2, addWorkspaceSize2, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 91 | } | 91 | } |
| 92 | aclnnAddGetWorkspaceSize(self3, other3, alpha, out3, &addWorkspaceSize3, &addExecutor3); | 92 | aclnnAddGetWorkspaceSize(self3, other3, alpha, out3, &addWorkspaceSize3, &addExecutor3); |
| 93 | - void *addWorkspaceAddr3 = nullptr; | 93 | + void* addWorkspaceAddr3 = nullptr; |
| 94 | if (addWorkspaceSize3 > 0) { | 94 | if (addWorkspaceSize3 > 0) { |
| 95 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr3, addWorkspaceSize3, ACL_MEM_MALLOC_HUGE_FIRST)); | 95 | CHECK_ERROR(aclrtMalloc(&addWorkspaceAddr3, addWorkspaceSize3, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 96 | } | 96 | } |
| 97 | // switchstream函数需要的参数为device侧数据的地址,这里创建两个device侧的地址用于比较,一个为1,一个为2 | 97 | // switchstream函数需要的参数为device侧数据的地址,这里创建两个device侧的地址用于比较,一个为1,一个为2 |
| 98 | int32_t rightValue1 = 1; | 98 | int32_t rightValue1 = 1; |
| 99 | int32_t rightValue2 = 2; | 99 | int32_t rightValue2 = 2; |
| 100 | - void *rightDevice1 = nullptr; | 100 | + void* rightDevice1 = nullptr; |
| 101 | - void *rightDevice2 = nullptr; | 101 | + void* rightDevice2 = nullptr; |
| 102 | aclrtCondition condition = ACL_RT_EQUAL; | 102 | aclrtCondition condition = ACL_RT_EQUAL; |
| 103 | aclrtCompareDataType dataType = ACL_RT_SWITCH_INT32; | 103 | aclrtCompareDataType dataType = ACL_RT_SWITCH_INT32; |
| 104 | CHECK_ERROR(aclrtMalloc(&rightDevice1, sizeof(int32_t), ACL_MEM_MALLOC_HUGE_FIRST)); | 104 | CHECK_ERROR(aclrtMalloc(&rightDevice1, sizeof(int32_t), ACL_MEM_MALLOC_HUGE_FIRST)); |
| 105 | CHECK_ERROR(aclrtMemcpy(rightDevice1, sizeof(int32_t), &rightValue1, sizeof(int32_t), ACL_MEMCPY_HOST_TO_DEVICE)); | 105 | CHECK_ERROR(aclrtMemcpy(rightDevice1, sizeof(int32_t), &rightValue1, sizeof(int32_t), ACL_MEMCPY_HOST_TO_DEVICE)); |
| 106 | CHECK_ERROR(aclrtMalloc(&rightDevice2, sizeof(int32_t), ACL_MEM_MALLOC_HUGE_FIRST)); | 106 | CHECK_ERROR(aclrtMalloc(&rightDevice2, sizeof(int32_t), ACL_MEM_MALLOC_HUGE_FIRST)); |
| 107 | CHECK_ERROR(aclrtMemcpy(rightDevice2, sizeof(int32_t), &rightValue2, sizeof(int32_t), ACL_MEMCPY_HOST_TO_DEVICE)); | 107 | CHECK_ERROR(aclrtMemcpy(rightDevice2, sizeof(int32_t), &rightValue2, sizeof(int32_t), ACL_MEMCPY_HOST_TO_DEVICE)); |
| 108 | - CHECK_ERROR(aclrtMalloc((void **)&numDevice, sizeof(uint32_t), ACL_MEM_MALLOC_HUGE_FIRST)); | 108 | + CHECK_ERROR(aclrtMalloc((void**)&numDevice, sizeof(uint32_t), ACL_MEM_MALLOC_HUGE_FIRST)); |
| 109 | uint32_t numInit = 0; | 109 | uint32_t numInit = 0; |
| 110 | CHECK_ERROR(aclrtMemcpy(numDevice, sizeof(uint32_t), &numInit, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE)); | 110 | CHECK_ERROR(aclrtMemcpy(numDevice, sizeof(uint32_t), &numInit, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE)); |
| 111 | // 通过 DeviceToDevice 异步拷贝将其置为 1,供 SwitchStream 分支验证。 | 111 | // 通过 DeviceToDevice 异步拷贝将其置为 1,供 SwitchStream 分支验证。 |
| 112 | uint32_t val1 = 1; | 112 | uint32_t val1 = 1; |
| 113 | - void *targetValDev_1 = nullptr; | 113 | + void* targetValDev_1 = nullptr; |
| 114 | CHECK_ERROR(aclrtMalloc(&targetValDev_1, sizeof(uint32_t), ACL_MEM_MALLOC_HUGE_FIRST)); | 114 | CHECK_ERROR(aclrtMalloc(&targetValDev_1, sizeof(uint32_t), ACL_MEM_MALLOC_HUGE_FIRST)); |
| 115 | CHECK_ERROR(aclrtMemcpy(targetValDev_1, sizeof(uint32_t), &val1, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE)); | 115 | CHECK_ERROR(aclrtMemcpy(targetValDev_1, sizeof(uint32_t), &val1, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE)); |
| 116 | // 创建五个流:四个工作流 + 一个汇聚流endStream | 116 | // 创建五个流:四个工作流 + 一个汇聚流endStream |
| @@ -143,7 +143,8 @@ int main() | |||
| 143 | CHECK_ERROR(aclrtMemcpyAsync(otherDevice2, size, otherHostData2.data(), size, ACL_MEMCPY_HOST_TO_DEVICE, stream2)); | 143 | CHECK_ERROR(aclrtMemcpyAsync(otherDevice2, size, otherHostData2.data(), size, ACL_MEMCPY_HOST_TO_DEVICE, stream2)); |
| 144 | CHECK_ERROR(aclrtMemcpyAsync(otherDevice3, size, otherHostData3.data(), size, ACL_MEMCPY_HOST_TO_DEVICE, stream3)); | 144 | CHECK_ERROR(aclrtMemcpyAsync(otherDevice3, size, otherHostData3.data(), size, ACL_MEMCPY_HOST_TO_DEVICE, stream3)); |
| 145 | aclnnAdd(addWorkspaceAddr1, addWorkspaceSize1, addExecutor1, stream1); | 145 | aclnnAdd(addWorkspaceAddr1, addWorkspaceSize1, addExecutor1, stream1); |
| 146 | - CHECK_ERROR(aclrtMemcpyAsync(numDevice, sizeof(uint32_t), targetValDev_1, sizeof(uint32_t), ACL_MEMCPY_DEVICE_TO_DEVICE, stream1)); | 146 | + CHECK_ERROR(aclrtMemcpyAsync( |
| 147 | + numDevice, sizeof(uint32_t), targetValDev_1, sizeof(uint32_t), ACL_MEMCPY_DEVICE_TO_DEVICE, stream1)); | ||
| 147 | // stream2的任务为下发一个aclnn算子,完成后激活endStream | 148 | // stream2的任务为下发一个aclnn算子,完成后激活endStream |
| 148 | aclnnAdd(addWorkspaceAddr2, addWorkspaceSize2, addExecutor2, stream2); | 149 | aclnnAdd(addWorkspaceAddr2, addWorkspaceSize2, addExecutor2, stream2); |
| 149 | CHECK_ERROR(aclrtActiveStream(endStream, stream2)); | 150 | CHECK_ERROR(aclrtActiveStream(endStream, stream2)); |
| @@ -181,7 +182,8 @@ int main() | |||
| 181 | // 以便让图里面录入的那句 DeviceToDevice 复制时把 2 写进 numDevice。 | 182 | // 以便让图里面录入的那句 DeviceToDevice 复制时把 2 写进 numDevice。 |
| 182 | uint32_t val2 = 2; | 183 | uint32_t val2 = 2; |
| 183 | // 使用独立的 copyStream 拷贝 2 到 targetValDev_1 中 | 184 | // 使用独立的 copyStream 拷贝 2 到 targetValDev_1 中 |
| 184 | - CHECK_ERROR(aclrtMemcpyAsync(targetValDev_1, sizeof(uint32_t), &val2, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE, copyStream)); | 185 | + CHECK_ERROR(aclrtMemcpyAsync( |
| 186 | + targetValDev_1, sizeof(uint32_t), &val2, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE, copyStream)); | ||
| 185 | CHECK_ERROR(aclrtSynchronizeStream(copyStream)); | 187 | CHECK_ERROR(aclrtSynchronizeStream(copyStream)); |
| 186 | // 将上一次的主机结果清零,以免混淆 | 188 | // 将上一次的主机结果清零,以免混淆 |
| 187 | outHostData1.assign(outHostData1.size(), 0); | 189 | outHostData1.assign(outHostData1.size(), 0); |
| @@ -12,12 +12,11 @@ | |||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | -static bool CheckResult(const std::vector<int32_t> &actual, const std::vector<int32_t> &expected) | 15 | +static bool CheckResult(const std::vector<int32_t>& actual, const std::vector<int32_t>& expected) |
| 16 | { | 16 | { |
| 17 | for (size_t i = 0; i < actual.size(); ++i) { | 17 | for (size_t i = 0; i < actual.size(); ++i) { |
| 18 | if (actual[i] != expected[i]) { | 18 | if (actual[i] != expected[i]) { |
| 19 | - ERROR_LOG("Check result failed at index %zu: actual=%d, expected=%d", | 19 | + ERROR_LOG("Check result failed at index %zu: actual=%d, expected=%d", i, actual[i], expected[i]); |
| 20 | - i, actual[i], expected[i]); | ||
| 21 | return false; | 20 | return false; |
| 22 | } | 21 | } |
| 23 | } | 22 | } |
| @@ -42,8 +41,8 @@ static int Scenario1StreamRecordGraphWaitExternal() | |||
| 42 | aclrtEvent event; | 41 | aclrtEvent event; |
| 43 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); | 42 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); |
| 44 | 43 | ||
| 45 | - void *srcDevice = nullptr; | 44 | + void* srcDevice = nullptr; |
| 46 | - void *dstDevice = nullptr; | 45 | + void* dstDevice = nullptr; |
| 47 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 46 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 48 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 47 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 49 | 48 | ||
| @@ -91,8 +90,8 @@ static int Scenario2GraphRecordExternalStreamWait() | |||
| 91 | aclrtEvent event; | 90 | aclrtEvent event; |
| 92 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); | 91 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); |
| 93 | 92 | ||
| 94 | - void *srcDevice = nullptr; | 93 | + void* srcDevice = nullptr; |
| 95 | - void *dstDevice = nullptr; | 94 | + void* dstDevice = nullptr; |
| 96 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 95 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 97 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 96 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 98 | 97 | ||
| @@ -141,9 +140,9 @@ static int Scenario3Graph1RecordGraph2WaitExternal() | |||
| 141 | aclrtEvent event; | 140 | aclrtEvent event; |
| 142 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); | 141 | CHECK_ERROR(aclrtCreateEventExWithFlag(&event, ACL_EVENT_SYNC)); |
| 143 | 142 | ||
| 144 | - void *srcDevice = nullptr; | 143 | + void* srcDevice = nullptr; |
| 145 | - void *midDevice = nullptr; | 144 | + void* midDevice = nullptr; |
| 146 | - void *dstDevice = nullptr; | 145 | + void* dstDevice = nullptr; |
| 147 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 146 | CHECK_ERROR(aclrtMalloc(&srcDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 148 | CHECK_ERROR(aclrtMalloc(&midDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 147 | CHECK_ERROR(aclrtMalloc(&midDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 149 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 148 | CHECK_ERROR(aclrtMalloc(&dstDevice, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| @@ -19,47 +19,40 @@ | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | namespace ModelUtils { | 21 | namespace ModelUtils { |
| 22 | - int64_t GetShapeSize(const std::vector<int64_t> &shape) | 22 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 23 | - { | 23 | +{ |
| 24 | - int64_t shapeSize = 1; | 24 | + int64_t shapeSize = 1; |
| 25 | - for (auto i : shape) { | 25 | + for (auto i : shape) { |
| 26 | - shapeSize *= i; | 26 | + shapeSize *= i; |
| 27 | - } | 27 | + } |
| 28 | - return shapeSize; | 28 | + return shapeSize; |
| 29 | - } | 29 | +} |
| 30 | - int CreateAclTensor(const std::vector<int64_t> &shape, void **deviceAddr, aclDataType dataType, | 30 | +int CreateAclTensor(const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, aclTensor** tensor) |
| 31 | - aclTensor **tensor) | 31 | +{ |
| 32 | - { | 32 | + auto size = GetShapeSize(shape) * sizeof(float); |
| 33 | - auto size = GetShapeSize(shape) * sizeof(float); | 33 | + // 申请Device侧内存 |
| 34 | - // 申请Device侧内存 | 34 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 35 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 35 | + // 计算连续tensor的stride |
| 36 | - // 计算连续tensor的stride | 36 | + std::vector<int64_t> strides(shape.size(), 1); |
| 37 | - std::vector<int64_t> strides(shape.size(), 1); | 37 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 38 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 38 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 39 | - strides[i] = shape[i + 1] * strides[i + 1]; | 39 | + } |
| 40 | - } | 40 | + // 调用aclCreateTensor接口创建aclTensor |
| 41 | - // 调用aclCreateTensor接口创建aclTensor | 41 | + *tensor = aclCreateTensor( |
| 42 | - *tensor = aclCreateTensor(shape.data(), | 42 | + shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), |
| 43 | - shape.size(), | 43 | + *deviceAddr); |
| 44 | - dataType, | 44 | + return 0; |
| 45 | - strides.data(), | 45 | +} |
| 46 | - 0, | 46 | +void PrintArray(const std::vector<float>& data) |
| 47 | - aclFormat::ACL_FORMAT_ND, | 47 | +{ |
| 48 | - shape.data(), | 48 | + const int size = data.size(); |
| 49 | - shape.size(), | 49 | + const int precision = 4; |
| 50 | - *deviceAddr); | 50 | + std::ostringstream oss; |
| 51 | - return 0; | 51 | + oss << std::fixed << std::setprecision(precision); |
| 52 | - } | 52 | + oss << "The vector data is: "; |
| 53 | - void PrintArray(const std::vector<float>& data) | 53 | + for (int j = 0; j < size; j++) { |
| 54 | - { | 54 | + oss << data[j] << " "; |
| 55 | - const int size = data.size(); | 55 | + } |
| 56 | - const int precision = 4; | 56 | + INFO_LOG("%s", oss.str().c_str()); |
| 57 | - std::ostringstream oss; | 57 | +} |
| 58 | - oss << std::fixed << std::setprecision(precision); | 58 | +} // namespace ModelUtils |
| 59 | - oss << "The vector data is: "; | ||
| 60 | - for (int j = 0; j < size; j++) { | ||
| 61 | - oss << data[j] <<" "; | ||
| 62 | - } | ||
| 63 | - INFO_LOG("%s", oss.str().c_str()); | ||
| 64 | - } | ||
| 65 | -} | ||
| @@ -19,12 +19,11 @@ | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | namespace ModelUtils { | 21 | namespace ModelUtils { |
| 22 | - int64_t GetShapeSize(const std::vector<int64_t> &shape); | 22 | +int64_t GetShapeSize(const std::vector<int64_t>& shape); |
| 23 | 23 | ||
| 24 | - int CreateAclTensor(const std::vector<int64_t> &shape, void **deviceAddr, aclDataType dataType, | 24 | +int CreateAclTensor(const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, aclTensor** tensor); |
| 25 | - aclTensor **tensor); | 25 | + |
| 26 | - | 26 | +void PrintArray(const std::vector<float>& data); |
| 27 | - void PrintArray(const std::vector<float>& data); | 27 | +} // namespace ModelUtils |
| 28 | -} | ||
| 29 | 28 | ||
| 30 | 29 | ||
| @@ -9,7 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | - * This sample demonstrates Inter-Process Communication (IPC) notify for | 12 | + * This sample demonstrates Inter-Process Communication (IPC) notify for |
| 13 | * task synchronization across multiple devices and processes. | 13 | * task synchronization across multiple devices and processes. |
| 14 | * This sample enables process whitelist verification. | 14 | * This sample enables process whitelist verification. |
| 15 | */ | 15 | */ |
| @@ -42,14 +42,16 @@ int32_t main() | |||
| 42 | 42 | ||
| 43 | // Allocate memory on the device | 43 | // Allocate memory on the device |
| 44 | uint64_t size = 1 * 1024 * 1024; | 44 | uint64_t size = 1 * 1024 * 1024; |
| 45 | - int *devPtr = nullptr; | 45 | + int* devPtr = nullptr; |
| 46 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 46 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 47 | 47 | ||
| 48 | // Export a shareable identifier for IPC memory sharing | 48 | // Export a shareable identifier for IPC memory sharing |
| 49 | char memName[256] = "name"; | 49 | char memName[256] = "name"; |
| 50 | size_t lenName = 65; | 50 | size_t lenName = 65; |
| 51 | CHECK_ERROR(aclrtIpcMemGetExportKey(devPtr, size, memName, lenName, 1)); | 51 | CHECK_ERROR(aclrtIpcMemGetExportKey(devPtr, size, memName, lenName, 1)); |
| 52 | - INFO_LOG("Process A: get a shareable identifier for IPC memory sharing successfully, shareable identifier = %s", memName); | 52 | + INFO_LOG( |
| 53 | + "Process A: get a shareable identifier for IPC memory sharing successfully, shareable identifier = %s", | ||
| 54 | + memName); | ||
| 53 | 55 | ||
| 54 | // Transfer the shareable identifier to Process B by writing it to the file | 56 | // Transfer the shareable identifier to Process B by writing it to the file |
| 55 | memory::WriteFile("file/memName.bin", "file/memName.bin.done", memName, strlen(memName)); | 57 | memory::WriteFile("file/memName.bin", "file/memName.bin.done", memName, strlen(memName)); |
| @@ -61,7 +63,9 @@ int32_t main() | |||
| 61 | // Export a shareable identifier for IPC notify sharing | 63 | // Export a shareable identifier for IPC notify sharing |
| 62 | char notifyName[256] = "name"; | 64 | char notifyName[256] = "name"; |
| 63 | CHECK_ERROR(aclrtNotifyGetExportKey(notify, notifyName, lenName, 0)); | 65 | CHECK_ERROR(aclrtNotifyGetExportKey(notify, notifyName, lenName, 0)); |
| 64 | - INFO_LOG("Process A: get a shareable identifier for IPC notify sharing successfully, shareable identifier = %s", notifyName); | 66 | + INFO_LOG( |
| 67 | + "Process A: get a shareable identifier for IPC notify sharing successfully, shareable identifier = %s", | ||
| 68 | + notifyName); | ||
| 65 | 69 | ||
| 66 | // Read Process B's pid from the file | 70 | // Read Process B's pid from the file |
| 67 | int32_t pid = 0; | 71 | int32_t pid = 0; |
| @@ -9,7 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | - * This sample demonstrates Inter-Process Communication (IPC) notify for | 12 | + * This sample demonstrates Inter-Process Communication (IPC) notify for |
| 13 | * task synchronization across multiple devices and processes. | 13 | * task synchronization across multiple devices and processes. |
| 14 | * This sample enables process whitelist verification. | 14 | * This sample enables process whitelist verification. |
| 15 | */ | 15 | */ |
| @@ -44,10 +44,12 @@ int32_t main() | |||
| 44 | // Get the shareable key from the file | 44 | // Get the shareable key from the file |
| 45 | char memName[256] = "name"; | 45 | char memName[256] = "name"; |
| 46 | memory::ReadFile("file/memName.bin", "file/memName.bin.done", memName, sizeof(memName)); | 46 | memory::ReadFile("file/memName.bin", "file/memName.bin.done", memName, sizeof(memName)); |
| 47 | - INFO_LOG("Process B: get the shareable identifier for IPC memory sharing successfully, shareable identifier = %s", memName); | 47 | + INFO_LOG( |
| 48 | + "Process B: get the shareable identifier for IPC memory sharing successfully, shareable identifier = %s", | ||
| 49 | + memName); | ||
| 48 | 50 | ||
| 49 | // Return a device memory pointer accessible to Process B | 51 | // Return a device memory pointer accessible to Process B |
| 50 | - void *devPtr = nullptr; | 52 | + void* devPtr = nullptr; |
| 51 | uint64_t size = 1 * 1024 * 1024; | 53 | uint64_t size = 1 * 1024 * 1024; |
| 52 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); | 54 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); |
| 53 | 55 | ||
| @@ -58,11 +60,13 @@ int32_t main() | |||
| 58 | 60 | ||
| 59 | // Write Process B's pid to the file | 61 | // Write Process B's pid to the file |
| 60 | memory::WriteFile("file/pid.bin", "file/pid.bin.done", &pid, sizeof(pid)); | 62 | memory::WriteFile("file/pid.bin", "file/pid.bin.done", &pid, sizeof(pid)); |
| 61 | - | 63 | + |
| 62 | // Get the shareable key from the file | 64 | // Get the shareable key from the file |
| 63 | char notifyName[256] = "name"; | 65 | char notifyName[256] = "name"; |
| 64 | memory::ReadFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, sizeof(notifyName)); | 66 | memory::ReadFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, sizeof(notifyName)); |
| 65 | - INFO_LOG("Process B: get the shareable identifier for IPC notify sharing successfully, shareable identifier = %s", notifyName); | 67 | + INFO_LOG( |
| 68 | + "Process B: get the shareable identifier for IPC notify sharing successfully, shareable identifier = %s", | ||
| 69 | + notifyName); | ||
| 66 | 70 | ||
| 67 | // Get key information and return the notify accessible to Process B | 71 | // Get key information and return the notify accessible to Process B |
| 68 | aclrtNotify notify = nullptr; | 72 | aclrtNotify notify = nullptr; |
| @@ -84,7 +88,7 @@ int32_t main() | |||
| 84 | // Release notify and memory resources | 88 | // Release notify and memory resources |
| 85 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 89 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 86 | CHECK_ERROR(aclrtIpcMemClose(memName)); | 90 | CHECK_ERROR(aclrtIpcMemClose(memName)); |
| 87 | - CHECK_ERROR(aclrtDestroyNotify(notify)); | 91 | + CHECK_ERROR(aclrtDestroyNotify(notify)); |
| 88 | } else { | 92 | } else { |
| 89 | ERROR_LOG("The target devices do not support data interaction"); | 93 | ERROR_LOG("The target devices do not support data interaction"); |
| 90 | } | 94 | } |
| @@ -9,7 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | - * This sample demonstrates Inter-Process Communication (IPC) notify for | 12 | + * This sample demonstrates Inter-Process Communication (IPC) notify for |
| 13 | * task synchronization across multiple devices and processes. | 13 | * task synchronization across multiple devices and processes. |
| 14 | * This sample does not enable whitelist verification. | 14 | * This sample does not enable whitelist verification. |
| 15 | */ | 15 | */ |
| @@ -42,14 +42,16 @@ int32_t main() | |||
| 42 | 42 | ||
| 43 | // Allocate memory on the device | 43 | // Allocate memory on the device |
| 44 | uint64_t size = 1 * 1024 * 1024; | 44 | uint64_t size = 1 * 1024 * 1024; |
| 45 | - int *devPtr = nullptr; | 45 | + int* devPtr = nullptr; |
| 46 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 46 | CHECK_ERROR(aclrtMalloc((void**)&devPtr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 47 | 47 | ||
| 48 | // Export a shareable identifier for IPC memory sharing | 48 | // Export a shareable identifier for IPC memory sharing |
| 49 | char memName[256] = "name"; | 49 | char memName[256] = "name"; |
| 50 | size_t lenName = 65; | 50 | size_t lenName = 65; |
| 51 | CHECK_ERROR(aclrtIpcMemGetExportKey(devPtr, size, memName, lenName, 1)); | 51 | CHECK_ERROR(aclrtIpcMemGetExportKey(devPtr, size, memName, lenName, 1)); |
| 52 | - INFO_LOG("Process A: get a shareable identifier for IPC memory sharing successfully, shareable identifier = %s", memName); | 52 | + INFO_LOG( |
| 53 | + "Process A: get a shareable identifier for IPC memory sharing successfully, shareable identifier = %s", | ||
| 54 | + memName); | ||
| 53 | 55 | ||
| 54 | // Transfer the shareable identifier to Process B by writing it to the file | 56 | // Transfer the shareable identifier to Process B by writing it to the file |
| 55 | memory::WriteFile("file/memName.bin", "file/memName.bin.done", memName, strlen(memName)); | 57 | memory::WriteFile("file/memName.bin", "file/memName.bin.done", memName, strlen(memName)); |
| @@ -62,7 +64,9 @@ int32_t main() | |||
| 62 | char notifyName[256] = "name"; | 64 | char notifyName[256] = "name"; |
| 63 | uint64_t flag = 2; | 65 | uint64_t flag = 2; |
| 64 | CHECK_ERROR(aclrtNotifyGetExportKey(notify, notifyName, lenName, flag)); | 66 | CHECK_ERROR(aclrtNotifyGetExportKey(notify, notifyName, lenName, flag)); |
| 65 | - INFO_LOG("Process A: get a shareable identifier for IPC notify sharing successfully, shareable identifier = %s", notifyName); | 67 | + INFO_LOG( |
| 68 | + "Process A: get a shareable identifier for IPC notify sharing successfully, shareable identifier = %s", | ||
| 69 | + notifyName); | ||
| 66 | 70 | ||
| 67 | // Transfer the shareable identifier to Process B by writing it to the file | 71 | // Transfer the shareable identifier to Process B by writing it to the file |
| 68 | memory::WriteFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, strlen(notifyName)); | 72 | memory::WriteFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, strlen(notifyName)); |
| @@ -9,7 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | - * This sample demonstrates Inter-Process Communication (IPC) notify for | 12 | + * This sample demonstrates Inter-Process Communication (IPC) notify for |
| 13 | * task synchronization across multiple devices and processes. | 13 | * task synchronization across multiple devices and processes. |
| 14 | * This sample does not enable whitelist verification. | 14 | * This sample does not enable whitelist verification. |
| 15 | */ | 15 | */ |
| @@ -24,7 +24,7 @@ | |||
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | namespace { | 26 | namespace { |
| 27 | -int CheckAndEnablePeerAccess(int32_t deviceId, int32_t peerDeviceId, bool *isSupported) | 27 | +int CheckAndEnablePeerAccess(int32_t deviceId, int32_t peerDeviceId, bool* isSupported) |
| 28 | { | 28 | { |
| 29 | int32_t canAccessPeer = 0; | 29 | int32_t canAccessPeer = 0; |
| 30 | aclError ret = aclrtDeviceCanAccessPeer(&canAccessPeer, deviceId, peerDeviceId); | 30 | aclError ret = aclrtDeviceCanAccessPeer(&canAccessPeer, deviceId, peerDeviceId); |
| @@ -61,17 +61,21 @@ int32_t main() | |||
| 61 | // Get the shareable identifier from the file | 61 | // Get the shareable identifier from the file |
| 62 | char memName[256] = "name"; | 62 | char memName[256] = "name"; |
| 63 | memory::ReadFile("file/memName.bin", "file/memName.bin.done", memName, sizeof(memName)); | 63 | memory::ReadFile("file/memName.bin", "file/memName.bin.done", memName, sizeof(memName)); |
| 64 | - INFO_LOG("Process B: get the shareable identifier for IPC memory sharing successfully, shareable identifier = %s", memName); | 64 | + INFO_LOG( |
| 65 | + "Process B: get the shareable identifier for IPC memory sharing successfully, shareable identifier = %s", | ||
| 66 | + memName); | ||
| 65 | 67 | ||
| 66 | // Get identifier information and return a device memory pointer accessible to Process B | 68 | // Get identifier information and return a device memory pointer accessible to Process B |
| 67 | - void *devPtr = nullptr; | 69 | + void* devPtr = nullptr; |
| 68 | uint64_t size = 1 * 1024 * 1024; | 70 | uint64_t size = 1 * 1024 * 1024; |
| 69 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); | 71 | CHECK_ERROR(aclrtIpcMemImportByKey(&devPtr, memName, 0)); |
| 70 | - | 72 | + |
| 71 | // Get the shareable identifier from the file | 73 | // Get the shareable identifier from the file |
| 72 | char notifyName[256] = "name"; | 74 | char notifyName[256] = "name"; |
| 73 | memory::ReadFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, sizeof(notifyName)); | 75 | memory::ReadFile("file/notifyName.bin", "file/notifyName.bin.done", notifyName, sizeof(notifyName)); |
| 74 | - INFO_LOG("Process B: get the shareable identifier for IPC notify sharing successfully, shareable identifier = %s", notifyName); | 76 | + INFO_LOG( |
| 77 | + "Process B: get the shareable identifier for IPC notify sharing successfully, shareable identifier = %s", | ||
| 78 | + notifyName); | ||
| 75 | 79 | ||
| 76 | // Return the notify accessible to Process B | 80 | // Return the notify accessible to Process B |
| 77 | aclrtNotify notify = nullptr; | 81 | aclrtNotify notify = nullptr; |
| @@ -93,7 +97,7 @@ int32_t main() | |||
| 93 | // Release notify and memory resources | 97 | // Release notify and memory resources |
| 94 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 98 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 95 | CHECK_ERROR(aclrtIpcMemClose(memName)); | 99 | CHECK_ERROR(aclrtIpcMemClose(memName)); |
| 96 | - CHECK_ERROR(aclrtDestroyNotify(notify)); | 100 | + CHECK_ERROR(aclrtDestroyNotify(notify)); |
| 97 | } else { | 101 | } else { |
| 98 | ERROR_LOG("The target devices do not support data interaction"); | 102 | ERROR_LOG("The target devices do not support data interaction"); |
| 99 | } | 103 | } |
| @@ -16,7 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace { | 18 | namespace { |
| 19 | -int DumpDataset(const acltdtDataset *dataset) | 19 | +int DumpDataset(const acltdtDataset* dataset) |
| 20 | { | 20 | { |
| 21 | const size_t datasetSize = acltdtGetDatasetSize(dataset); | 21 | const size_t datasetSize = acltdtGetDatasetSize(dataset); |
| 22 | INFO_LOG("Dataset size: %zu", datasetSize); | 22 | INFO_LOG("Dataset size: %zu", datasetSize); |
| @@ -24,7 +24,7 @@ int DumpDataset(const acltdtDataset *dataset) | |||
| 24 | return 0; | 24 | return 0; |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | - acltdtDataItem *item = acltdtGetDataItem(dataset, 0); | 27 | + acltdtDataItem* item = acltdtGetDataItem(dataset, 0); |
| 28 | if (item == nullptr) { | 28 | if (item == nullptr) { |
| 29 | ERROR_LOG("acltdtGetDataItem returned nullptr"); | 29 | ERROR_LOG("acltdtGetDataItem returned nullptr"); |
| 30 | return -1; | 30 | return -1; |
| @@ -36,15 +36,12 @@ int DumpDataset(const acltdtDataset *dataset) | |||
| 36 | CHECK_ERROR(acltdtGetDimsFromItem(item, dims.data(), dimNum)); | 36 | CHECK_ERROR(acltdtGetDimsFromItem(item, dims.data(), dimNum)); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | - float *tensorData = static_cast<float *>(acltdtGetDataAddrFromItem(item)); | 39 | + float* tensorData = static_cast<float*>(acltdtGetDataAddrFromItem(item)); |
| 40 | INFO_LOG( | 40 | INFO_LOG( |
| 41 | "Tensor type=%d, data type=%d, bytes=%zu, dims=(%lld, %lld), firstValue=%.3f", | 41 | "Tensor type=%d, data type=%d, bytes=%zu, dims=(%lld, %lld), firstValue=%.3f", |
| 42 | - static_cast<int32_t>(acltdtGetTensorTypeFromItem(item)), | 42 | + static_cast<int32_t>(acltdtGetTensorTypeFromItem(item)), static_cast<int32_t>(acltdtGetDataTypeFromItem(item)), |
| 43 | - static_cast<int32_t>(acltdtGetDataTypeFromItem(item)), | 43 | + acltdtGetDataSizeFromItem(item), static_cast<long long>(dimNum > 0 ? dims[0] : 0), |
| 44 | - acltdtGetDataSizeFromItem(item), | 44 | + static_cast<long long>(dimNum > 1 ? dims[1] : 0), tensorData == nullptr ? 0.0F : tensorData[0]); |
| 45 | - static_cast<long long>(dimNum > 0 ? dims[0] : 0), | ||
| 46 | - static_cast<long long>(dimNum > 1 ? dims[1] : 0), | ||
| 47 | - tensorData == nullptr ? 0.0F : tensorData[0]); | ||
| 48 | return 0; | 45 | return 0; |
| 49 | } | 46 | } |
| 50 | 47 | ||
| @@ -56,9 +53,9 @@ int main() | |||
| 56 | constexpr size_t kChannelCapacity = 2; | 53 | constexpr size_t kChannelCapacity = 2; |
| 57 | constexpr int32_t kTimeoutMs = 1000; | 54 | constexpr int32_t kTimeoutMs = 1000; |
| 58 | std::vector<float> hostTensor = {1.0F, 2.0F, 3.0F, 4.0F}; | 55 | std::vector<float> hostTensor = {1.0F, 2.0F, 3.0F, 4.0F}; |
| 59 | - acltdtDataset *sendDataset = nullptr; | 56 | + acltdtDataset* sendDataset = nullptr; |
| 60 | - acltdtDataset *recvDataset = nullptr; | 57 | + acltdtDataset* recvDataset = nullptr; |
| 61 | - acltdtChannelHandle *channel = nullptr; | 58 | + acltdtChannelHandle* channel = nullptr; |
| 62 | bool aclInitialized = false; | 59 | bool aclInitialized = false; |
| 63 | bool deviceSet = false; | 60 | bool deviceSet = false; |
| 64 | 61 | ||
| @@ -70,9 +67,8 @@ int main() | |||
| 70 | 67 | ||
| 71 | channel = acltdtCreateChannelWithCapacity(deviceId, "simple_tdt_channel", kChannelCapacity); | 68 | channel = acltdtCreateChannelWithCapacity(deviceId, "simple_tdt_channel", kChannelCapacity); |
| 72 | if (channel == nullptr) { | 69 | if (channel == nullptr) { |
| 73 | - WARN_LOG( | 70 | + WARN_LOG("acltdtCreateChannelWithCapacity returned nullptr: this sample needs a queue-backed TDT channel " |
| 74 | - "acltdtCreateChannelWithCapacity returned nullptr: this sample needs a queue-backed TDT channel " | 71 | + "so it can send and receive within one host process"); |
| 75 | - "so it can send and receive within one host process"); | ||
| 76 | return 0; | 72 | return 0; |
| 77 | } | 73 | } |
| 78 | 74 | ||
| @@ -120,8 +116,7 @@ int main() | |||
| 120 | if (deviceSet) { | 116 | if (deviceSet) { |
| 121 | tdt::UpdateFinalResultOnError( | 117 | tdt::UpdateFinalResultOnError( |
| 122 | "aclrtResetDeviceForce(static_cast<int32_t>(deviceId))", | 118 | "aclrtResetDeviceForce(static_cast<int32_t>(deviceId))", |
| 123 | - aclrtResetDeviceForce(static_cast<int32_t>(deviceId)), | 119 | + aclrtResetDeviceForce(static_cast<int32_t>(deviceId)), finalResult); |
| 124 | - finalResult); | ||
| 125 | } | 120 | } |
| 126 | if (aclInitialized) { | 121 | if (aclInitialized) { |
| 127 | tdt::UpdateFinalResultOnError("aclFinalize()", aclFinalize(), finalResult); | 122 | tdt::UpdateFinalResultOnError("aclFinalize()", aclFinalize(), finalResult); |
| @@ -26,10 +26,10 @@ int main() | |||
| 26 | std::vector<float> firstTensor = {10.0F, 20.0F}; | 26 | std::vector<float> firstTensor = {10.0F, 20.0F}; |
| 27 | std::vector<float> secondTensor = {30.0F, 40.0F}; | 27 | std::vector<float> secondTensor = {30.0F, 40.0F}; |
| 28 | std::vector<float> thirdTensor = {50.0F, 60.0F}; | 28 | std::vector<float> thirdTensor = {50.0F, 60.0F}; |
| 29 | - acltdtDataset *firstDataset = nullptr; | 29 | + acltdtDataset* firstDataset = nullptr; |
| 30 | - acltdtDataset *secondDataset = nullptr; | 30 | + acltdtDataset* secondDataset = nullptr; |
| 31 | - acltdtDataset *thirdDataset = nullptr; | 31 | + acltdtDataset* thirdDataset = nullptr; |
| 32 | - acltdtChannelHandle *channel = nullptr; | 32 | + acltdtChannelHandle* channel = nullptr; |
| 33 | bool aclInitialized = false; | 33 | bool aclInitialized = false; |
| 34 | bool deviceSet = false; | 34 | bool deviceSet = false; |
| 35 | 35 | ||
| @@ -41,9 +41,8 @@ int main() | |||
| 41 | 41 | ||
| 42 | channel = acltdtCreateChannelWithCapacity(deviceId, "capacity_tdt_channel", kChannelCapacity); | 42 | channel = acltdtCreateChannelWithCapacity(deviceId, "capacity_tdt_channel", kChannelCapacity); |
| 43 | if (channel == nullptr) { | 43 | if (channel == nullptr) { |
| 44 | - WARN_LOG( | 44 | + WARN_LOG("acltdtCreateChannelWithCapacity returned nullptr: the current runtime likely does not enable " |
| 45 | - "acltdtCreateChannelWithCapacity returned nullptr: the current runtime likely does not enable " | 45 | + "capacity-limited TDT channels on this product/build"); |
| 46 | - "capacity-limited TDT channels on this product/build"); | ||
| 47 | return 0; | 46 | return 0; |
| 48 | } | 47 | } |
| 49 | 48 | ||
| @@ -60,7 +59,7 @@ int main() | |||
| 60 | return -1; | 59 | return -1; |
| 61 | } | 60 | } |
| 62 | 61 | ||
| 63 | - acltdtDataItem *firstItem = acltdtGetDataItem(firstDataset, 0); | 62 | + acltdtDataItem* firstItem = acltdtGetDataItem(firstDataset, 0); |
| 64 | if (!tdt::CheckNotNull(firstItem, "acltdtGetDataItem")) { | 63 | if (!tdt::CheckNotNull(firstItem, "acltdtGetDataItem")) { |
| 65 | return -1; | 64 | return -1; |
| 66 | } | 65 | } |
| @@ -70,9 +69,7 @@ int main() | |||
| 70 | aclError sliceRet = acltdtGetSliceInfoFromItem(firstItem, &sliceNum, &sliceId); | 69 | aclError sliceRet = acltdtGetSliceInfoFromItem(firstItem, &sliceNum, &sliceId); |
| 71 | INFO_LOG( | 70 | INFO_LOG( |
| 72 | "Slice info ret=%d, sliceNum=%zu, sliceId=%zu, tensorType=%d, datasetName=%s", | 71 | "Slice info ret=%d, sliceNum=%zu, sliceId=%zu, tensorType=%d, datasetName=%s", |
| 73 | - static_cast<int32_t>(sliceRet), | 72 | + static_cast<int32_t>(sliceRet), sliceNum, sliceId, |
| 74 | - sliceNum, | ||
| 75 | - sliceId, | ||
| 76 | static_cast<int32_t>(acltdtGetTensorTypeFromItem(firstItem)), | 73 | static_cast<int32_t>(acltdtGetTensorTypeFromItem(firstItem)), |
| 77 | acltdtGetDatasetName(firstDataset) == nullptr ? "<null>" : acltdtGetDatasetName(firstDataset)); | 74 | acltdtGetDatasetName(firstDataset) == nullptr ? "<null>" : acltdtGetDatasetName(firstDataset)); |
| 78 | 75 | ||
| @@ -93,9 +90,7 @@ int main() | |||
| 93 | INFO_LOG( | 90 | INFO_LOG( |
| 94 | "Second send reached capacity pressure immediately: ret=%d, queried channel size=%zu, " | 91 | "Second send reached capacity pressure immediately: ret=%d, queried channel size=%zu, " |
| 95 | "configured capacity=%zu", | 92 | "configured capacity=%zu", |
| 96 | - static_cast<int32_t>(secondSendRet), | 93 | + static_cast<int32_t>(secondSendRet), channelSize, kChannelCapacity); |
| 97 | - channelSize, | ||
| 98 | - kChannelCapacity); | ||
| 99 | } else { | 94 | } else { |
| 100 | ERROR_LOG( | 95 | ERROR_LOG( |
| 101 | "Operation failed: acltdtSendTensor(channel, secondDataset, 0) returned error code %d", | 96 | "Operation failed: acltdtSendTensor(channel, secondDataset, 0) returned error code %d", |
| @@ -109,17 +104,21 @@ int main() | |||
| 109 | if (channel != nullptr) { | 104 | if (channel != nullptr) { |
| 110 | const aclError cleanRet = acltdtCleanChannel(channel); | 105 | const aclError cleanRet = acltdtCleanChannel(channel); |
| 111 | if (cleanRet != ACL_SUCCESS) { | 106 | if (cleanRet != ACL_SUCCESS) { |
| 112 | - ERROR_LOG("Operation failed: acltdtCleanChannel(channel) returned error code %d", static_cast<int32_t>(cleanRet)); | 107 | + ERROR_LOG( |
| 108 | + "Operation failed: acltdtCleanChannel(channel) returned error code %d", static_cast<int32_t>(cleanRet)); | ||
| 113 | finalResult = -1; | 109 | finalResult = -1; |
| 114 | } | 110 | } |
| 115 | const aclError stopRet = acltdtStopChannel(channel); | 111 | const aclError stopRet = acltdtStopChannel(channel); |
| 116 | if (stopRet != ACL_SUCCESS) { | 112 | if (stopRet != ACL_SUCCESS) { |
| 117 | - ERROR_LOG("Operation failed: acltdtStopChannel(channel) returned error code %d", static_cast<int32_t>(stopRet)); | 113 | + ERROR_LOG( |
| 114 | + "Operation failed: acltdtStopChannel(channel) returned error code %d", static_cast<int32_t>(stopRet)); | ||
| 118 | finalResult = -1; | 115 | finalResult = -1; |
| 119 | } | 116 | } |
| 120 | const aclError destroyRet = acltdtDestroyChannel(channel); | 117 | const aclError destroyRet = acltdtDestroyChannel(channel); |
| 121 | if (destroyRet != ACL_SUCCESS) { | 118 | if (destroyRet != ACL_SUCCESS) { |
| 122 | - ERROR_LOG("Operation failed: acltdtDestroyChannel(channel) returned error code %d", static_cast<int32_t>(destroyRet)); | 119 | + ERROR_LOG( |
| 120 | + "Operation failed: acltdtDestroyChannel(channel) returned error code %d", | ||
| 121 | + static_cast<int32_t>(destroyRet)); | ||
| 123 | finalResult = -1; | 122 | finalResult = -1; |
| 124 | } | 123 | } |
| 125 | } | 124 | } |
| @@ -18,7 +18,7 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | namespace tdt { | 20 | namespace tdt { |
| 21 | -inline bool CheckNotNull(const void *ptr, const char *name) | 21 | +inline bool CheckNotNull(const void* ptr, const char* name) |
| 22 | { | 22 | { |
| 23 | if (ptr == nullptr) { | 23 | if (ptr == nullptr) { |
| 24 | ERROR_LOG("%s is nullptr", name); | 24 | ERROR_LOG("%s is nullptr", name); |
| @@ -27,21 +27,17 @@ inline bool CheckNotNull(const void *ptr, const char *name) | |||
| 27 | return true; | 27 | return true; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | -inline acltdtDataset *CreateFloatDataset(std::vector<float> &values) | 30 | +inline acltdtDataset* CreateFloatDataset(std::vector<float>& values) |
| 31 | { | 31 | { |
| 32 | int64_t dims[] = {1, static_cast<int64_t>(values.size())}; | 32 | int64_t dims[] = {1, static_cast<int64_t>(values.size())}; |
| 33 | - acltdtDataItem *item = acltdtCreateDataItem( | 33 | + acltdtDataItem* item = acltdtCreateDataItem( |
| 34 | - ACL_TENSOR_DATA_TENSOR, | 34 | + ACL_TENSOR_DATA_TENSOR, dims, sizeof(dims) / sizeof(dims[0]), ACL_FLOAT, values.data(), |
| 35 | - dims, | ||
| 36 | - sizeof(dims) / sizeof(dims[0]), | ||
| 37 | - ACL_FLOAT, | ||
| 38 | - values.data(), | ||
| 39 | values.size() * sizeof(float)); | 35 | values.size() * sizeof(float)); |
| 40 | if (item == nullptr) { | 36 | if (item == nullptr) { |
| 41 | return nullptr; | 37 | return nullptr; |
| 42 | } | 38 | } |
| 43 | 39 | ||
| 44 | - acltdtDataset *dataset = acltdtCreateDataset(); | 40 | + acltdtDataset* dataset = acltdtCreateDataset(); |
| 45 | if (dataset == nullptr) { | 41 | if (dataset == nullptr) { |
| 46 | (void)acltdtDestroyDataItem(item); | 42 | (void)acltdtDestroyDataItem(item); |
| 47 | return nullptr; | 43 | return nullptr; |
| @@ -55,7 +51,7 @@ inline acltdtDataset *CreateFloatDataset(std::vector<float> &values) | |||
| 55 | return dataset; | 51 | return dataset; |
| 56 | } | 52 | } |
| 57 | 53 | ||
| 58 | -inline void DestroyDatasetAndItems(acltdtDataset *dataset) | 54 | +inline void DestroyDatasetAndItems(acltdtDataset* dataset) |
| 59 | { | 55 | { |
| 60 | if (dataset == nullptr) { | 56 | if (dataset == nullptr) { |
| 61 | return; | 57 | return; |
| @@ -63,7 +59,7 @@ inline void DestroyDatasetAndItems(acltdtDataset *dataset) | |||
| 63 | 59 | ||
| 64 | const size_t datasetSize = acltdtGetDatasetSize(dataset); | 60 | const size_t datasetSize = acltdtGetDatasetSize(dataset); |
| 65 | for (size_t i = 0; i < datasetSize; ++i) { | 61 | for (size_t i = 0; i < datasetSize; ++i) { |
| 66 | - acltdtDataItem *item = acltdtGetDataItem(dataset, i); | 62 | + acltdtDataItem* item = acltdtGetDataItem(dataset, i); |
| 67 | if (item != nullptr) { | 63 | if (item != nullptr) { |
| 68 | (void)acltdtDestroyDataItem(item); | 64 | (void)acltdtDestroyDataItem(item); |
| 69 | } | 65 | } |
| @@ -71,7 +67,7 @@ inline void DestroyDatasetAndItems(acltdtDataset *dataset) | |||
| 71 | (void)acltdtDestroyDataset(dataset); | 67 | (void)acltdtDestroyDataset(dataset); |
| 72 | } | 68 | } |
| 73 | 69 | ||
| 74 | -inline void DestroyDataset(acltdtDataset *dataset) | 70 | +inline void DestroyDataset(acltdtDataset* dataset) |
| 75 | { | 71 | { |
| 76 | if (dataset == nullptr) { | 72 | if (dataset == nullptr) { |
| 77 | return; | 73 | return; |
| @@ -79,7 +75,7 @@ inline void DestroyDataset(acltdtDataset *dataset) | |||
| 79 | (void)acltdtDestroyDataset(dataset); | 75 | (void)acltdtDestroyDataset(dataset); |
| 80 | } | 76 | } |
| 81 | 77 | ||
| 82 | -inline void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalResult) | 78 | +inline void UpdateFinalResultOnError(const char* apiName, aclError ret, int32_t& finalResult) |
| 83 | { | 79 | { |
| 84 | if (ret == ACL_SUCCESS) { | 80 | if (ret == ACL_SUCCESS) { |
| 85 | return; | 81 | return; |
| @@ -18,12 +18,12 @@ int main() | |||
| 18 | { | 18 | { |
| 19 | const int32_t deviceId = 0; | 19 | const int32_t deviceId = 0; |
| 20 | const char queueName[] = "simple_queue"; | 20 | const char queueName[] = "simple_queue"; |
| 21 | - const char *queueNamePtr = queueName; | 21 | + const char* queueNamePtr = queueName; |
| 22 | - constexpr size_t kQueueNamePtrSize = sizeof(const char *); | 22 | + constexpr size_t kQueueNamePtrSize = sizeof(const char*); |
| 23 | const uint32_t depth = 4; | 23 | const uint32_t depth = 4; |
| 24 | 24 | ||
| 25 | uint32_t qid = 0; | 25 | uint32_t qid = 0; |
| 26 | - acltdtQueueAttr *attr = nullptr; | 26 | + acltdtQueueAttr* attr = nullptr; |
| 27 | bool aclInitialized = false; | 27 | bool aclInitialized = false; |
| 28 | bool deviceSet = false; | 28 | bool deviceSet = false; |
| 29 | bool queueCreated = false; | 29 | bool queueCreated = false; |
| @@ -42,11 +42,12 @@ int main() | |||
| 42 | CHECK_ERROR(acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &queueNamePtr)); | 42 | CHECK_ERROR(acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &queueNamePtr)); |
| 43 | CHECK_ERROR(acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(depth), &depth)); | 43 | CHECK_ERROR(acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(depth), &depth)); |
| 44 | 44 | ||
| 45 | - const char *queriedName = nullptr; | 45 | + const char* queriedName = nullptr; |
| 46 | uint32_t queriedDepth = 0; | 46 | uint32_t queriedDepth = 0; |
| 47 | size_t realLen = 0; | 47 | size_t realLen = 0; |
| 48 | CHECK_ERROR(acltdtGetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &realLen, &queriedName)); | 48 | CHECK_ERROR(acltdtGetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &realLen, &queriedName)); |
| 49 | - CHECK_ERROR(acltdtGetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(queriedDepth), &realLen, &queriedDepth)); | 49 | + CHECK_ERROR( |
| 50 | + acltdtGetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(queriedDepth), &realLen, &queriedDepth)); | ||
| 50 | INFO_LOG("QueueAttr name=%s, depth=%u", queriedName == nullptr ? "<null>" : queriedName, queriedDepth); | 51 | INFO_LOG("QueueAttr name=%s, depth=%u", queriedName == nullptr ? "<null>" : queriedName, queriedDepth); |
| 51 | 52 | ||
| 52 | CHECK_ERROR(acltdtCreateQueue(attr, &qid)); | 53 | CHECK_ERROR(acltdtCreateQueue(attr, &qid)); |
| @@ -15,7 +15,7 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | namespace { | 17 | namespace { |
| 18 | -constexpr size_t kQueueNamePtrSize = sizeof(const char *); | 18 | +constexpr size_t kQueueNamePtrSize = sizeof(const char*); |
| 19 | 19 | ||
| 20 | struct QueueRouteSample { | 20 | struct QueueRouteSample { |
| 21 | static constexpr int32_t kDeviceId = 0; | 21 | static constexpr int32_t kDeviceId = 0; |
| @@ -29,19 +29,19 @@ struct QueueRouteSample { | |||
| 29 | bool dstQueueCreated = false; | 29 | bool dstQueueCreated = false; |
| 30 | bool routeAdded = false; | 30 | bool routeAdded = false; |
| 31 | bool bindAttempted = false; | 31 | bool bindAttempted = false; |
| 32 | - acltdtQueueRoute *route = nullptr; | 32 | + acltdtQueueRoute* route = nullptr; |
| 33 | - acltdtQueueRouteList *bindList = nullptr; | 33 | + acltdtQueueRouteList* bindList = nullptr; |
| 34 | - acltdtQueueRouteQueryInfo *queryInfo = nullptr; | 34 | + acltdtQueueRouteQueryInfo* queryInfo = nullptr; |
| 35 | - acltdtQueueRouteList *queryList = nullptr; | 35 | + acltdtQueueRouteList* queryList = nullptr; |
| 36 | }; | 36 | }; |
| 37 | 37 | ||
| 38 | -aclError CreateQueueWithAttr(const char *name, uint32_t depth, uint32_t *qid) | 38 | +aclError CreateQueueWithAttr(const char* name, uint32_t depth, uint32_t* qid) |
| 39 | { | 39 | { |
| 40 | if (qid == nullptr) { | 40 | if (qid == nullptr) { |
| 41 | return ACL_ERROR_INVALID_PARAM; | 41 | return ACL_ERROR_INVALID_PARAM; |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | - acltdtQueueAttr *attr = acltdtCreateQueueAttr(); | 44 | + acltdtQueueAttr* attr = acltdtCreateQueueAttr(); |
| 45 | if (attr == nullptr) { | 45 | if (attr == nullptr) { |
| 46 | return ACL_ERROR_INVALID_PARAM; | 46 | return ACL_ERROR_INVALID_PARAM; |
| 47 | } | 47 | } |
| @@ -49,7 +49,7 @@ aclError CreateQueueWithAttr(const char *name, uint32_t depth, uint32_t *qid) | |||
| 49 | aclError ret = acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(depth), &depth); | 49 | aclError ret = acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_DEPTH_UINT32, sizeof(depth), &depth); |
| 50 | if ((ret == ACL_SUCCESS) && (name != nullptr)) { | 50 | if ((ret == ACL_SUCCESS) && (name != nullptr)) { |
| 51 | // ACL_TDT_QUEUE_NAME_PTR expects a pointer-sized payload that points to the queue name. | 51 | // ACL_TDT_QUEUE_NAME_PTR expects a pointer-sized payload that points to the queue name. |
| 52 | - const char *namePtr = name; | 52 | + const char* namePtr = name; |
| 53 | ret = acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &namePtr); | 53 | ret = acltdtSetQueueAttr(attr, ACL_TDT_QUEUE_NAME_PTR, kQueueNamePtrSize, &namePtr); |
| 54 | } | 54 | } |
| 55 | if (ret == ACL_SUCCESS) { | 55 | if (ret == ACL_SUCCESS) { |
| @@ -60,7 +60,7 @@ aclError CreateQueueWithAttr(const char *name, uint32_t depth, uint32_t *qid) | |||
| 60 | return ret; | 60 | return ret; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | -aclError CreateQueue(const char *name, uint32_t depth, uint32_t *qid) | 63 | +aclError CreateQueue(const char* name, uint32_t depth, uint32_t* qid) |
| 64 | { | 64 | { |
| 65 | aclError ret = CreateQueueWithAttr(name, depth, qid); | 65 | aclError ret = CreateQueueWithAttr(name, depth, qid); |
| 66 | if ((ret != ACL_ERROR_INVALID_PARAM) || (name == nullptr)) { | 66 | if ((ret != ACL_ERROR_INVALID_PARAM) || (name == nullptr)) { |
| @@ -75,7 +75,7 @@ aclError CreateQueue(const char *name, uint32_t depth, uint32_t *qid) | |||
| 75 | return CreateQueueWithAttr(nullptr, depth, qid); | 75 | return CreateQueueWithAttr(nullptr, depth, qid); |
| 76 | } | 76 | } |
| 77 | 77 | ||
| 78 | -int32_t CreateQueues(QueueRouteSample &sample) | 78 | +int32_t CreateQueues(QueueRouteSample& sample) |
| 79 | { | 79 | { |
| 80 | aclError ret = CreateQueue("route_src_queue", QueueRouteSample::kQueueDepth, &sample.srcQid); | 80 | aclError ret = CreateQueue("route_src_queue", QueueRouteSample::kQueueDepth, &sample.srcQid); |
| 81 | if (ret != ACL_SUCCESS) { | 81 | if (ret != ACL_SUCCESS) { |
| @@ -99,7 +99,7 @@ int32_t CreateQueues(QueueRouteSample &sample) | |||
| 99 | return 0; | 99 | return 0; |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | -int32_t BindQueueRoute(QueueRouteSample &sample) | 102 | +int32_t BindQueueRoute(QueueRouteSample& sample) |
| 103 | { | 103 | { |
| 104 | sample.route = acltdtCreateQueueRoute(sample.srcQid, sample.dstQid); | 104 | sample.route = acltdtCreateQueueRoute(sample.srcQid, sample.dstQid); |
| 105 | if (!tdt::CheckNotNull(sample.route, "acltdtCreateQueueRoute")) { | 105 | if (!tdt::CheckNotNull(sample.route, "acltdtCreateQueueRoute")) { |
| @@ -117,7 +117,7 @@ int32_t BindQueueRoute(QueueRouteSample &sample) | |||
| 117 | return 0; | 117 | return 0; |
| 118 | } | 118 | } |
| 119 | 119 | ||
| 120 | -int32_t PrepareRouteQuery(QueueRouteSample &sample) | 120 | +int32_t PrepareRouteQuery(QueueRouteSample& sample) |
| 121 | { | 121 | { |
| 122 | sample.queryInfo = acltdtCreateQueueRouteQueryInfo(); | 122 | sample.queryInfo = acltdtCreateQueueRouteQueryInfo(); |
| 123 | if (!tdt::CheckNotNull(sample.queryInfo, "acltdtCreateQueueRouteQueryInfo")) { | 123 | if (!tdt::CheckNotNull(sample.queryInfo, "acltdtCreateQueueRouteQueryInfo")) { |
| @@ -125,21 +125,12 @@ int32_t PrepareRouteQuery(QueueRouteSample &sample) | |||
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | acltdtQueueRouteQueryMode mode = ACL_TDT_QUEUE_ROUTE_QUERY_SRC_AND_DST; | 127 | acltdtQueueRouteQueryMode mode = ACL_TDT_QUEUE_ROUTE_QUERY_SRC_AND_DST; |
| 128 | + CHECK_ERROR( | ||
| 129 | + acltdtSetQueueRouteQueryInfo(sample.queryInfo, ACL_TDT_QUEUE_ROUTE_QUERY_MODE_ENUM, sizeof(mode), &mode)); | ||
| 128 | CHECK_ERROR(acltdtSetQueueRouteQueryInfo( | 130 | CHECK_ERROR(acltdtSetQueueRouteQueryInfo( |
| 129 | - sample.queryInfo, | 131 | + sample.queryInfo, ACL_TDT_QUEUE_ROUTE_QUERY_SRC_ID_UINT32, sizeof(sample.srcQid), &sample.srcQid)); |
| 130 | - ACL_TDT_QUEUE_ROUTE_QUERY_MODE_ENUM, | ||
| 131 | - sizeof(mode), | ||
| 132 | - &mode)); | ||
| 133 | CHECK_ERROR(acltdtSetQueueRouteQueryInfo( | 132 | CHECK_ERROR(acltdtSetQueueRouteQueryInfo( |
| 134 | - sample.queryInfo, | 133 | + sample.queryInfo, ACL_TDT_QUEUE_ROUTE_QUERY_DST_ID_UINT32, sizeof(sample.dstQid), &sample.dstQid)); |
| 135 | - ACL_TDT_QUEUE_ROUTE_QUERY_SRC_ID_UINT32, | ||
| 136 | - sizeof(sample.srcQid), | ||
| 137 | - &sample.srcQid)); | ||
| 138 | - CHECK_ERROR(acltdtSetQueueRouteQueryInfo( | ||
| 139 | - sample.queryInfo, | ||
| 140 | - ACL_TDT_QUEUE_ROUTE_QUERY_DST_ID_UINT32, | ||
| 141 | - sizeof(sample.dstQid), | ||
| 142 | - &sample.dstQid)); | ||
| 143 | 134 | ||
| 144 | sample.queryList = acltdtCreateQueueRouteList(); | 135 | sample.queryList = acltdtCreateQueueRouteList(); |
| 145 | if (!tdt::CheckNotNull(sample.queryList, "acltdtCreateQueueRouteList(query)")) { | 136 | if (!tdt::CheckNotNull(sample.queryList, "acltdtCreateQueueRouteList(query)")) { |
| @@ -150,11 +141,8 @@ int32_t PrepareRouteQuery(QueueRouteSample &sample) | |||
| 150 | } | 141 | } |
| 151 | 142 | ||
| 152 | aclError GetQueueRouteParamValue( | 143 | aclError GetQueueRouteParamValue( |
| 153 | - acltdtQueueRoute *route, | 144 | + acltdtQueueRoute* route, decltype(ACL_TDT_QUEUE_ROUTE_SRC_UINT32) paramType, const char* apiName, size_t valueSize, |
| 154 | - decltype(ACL_TDT_QUEUE_ROUTE_SRC_UINT32) paramType, | 145 | + void* value) |
| 155 | - const char *apiName, | ||
| 156 | - size_t valueSize, | ||
| 157 | - void *value) | ||
| 158 | { | 146 | { |
| 159 | size_t paramSize = 0; | 147 | size_t paramSize = 0; |
| 160 | const aclError ret = acltdtGetQueueRouteParam(route, paramType, valueSize, ¶mSize, value); | 148 | const aclError ret = acltdtGetQueueRouteParam(route, paramType, valueSize, ¶mSize, value); |
| @@ -164,7 +152,7 @@ aclError GetQueueRouteParamValue( | |||
| 164 | return ret; | 152 | return ret; |
| 165 | } | 153 | } |
| 166 | 154 | ||
| 167 | -int32_t LogQueriedRoute(const QueueRouteSample &sample) | 155 | +int32_t LogQueriedRoute(const QueueRouteSample& sample) |
| 168 | { | 156 | { |
| 169 | const size_t routeNum = acltdtGetQueueRouteNum(sample.queryList); | 157 | const size_t routeNum = acltdtGetQueueRouteNum(sample.queryList); |
| 170 | INFO_LOG("Queried route count: %zu", routeNum); | 158 | INFO_LOG("Queried route count: %zu", routeNum); |
| @@ -172,7 +160,7 @@ int32_t LogQueriedRoute(const QueueRouteSample &sample) | |||
| 172 | return 0; | 160 | return 0; |
| 173 | } | 161 | } |
| 174 | 162 | ||
| 175 | - acltdtQueueRoute *routeView = acltdtCreateQueueRoute(sample.srcQid, sample.dstQid); | 163 | + acltdtQueueRoute* routeView = acltdtCreateQueueRoute(sample.srcQid, sample.dstQid); |
| 176 | if (!tdt::CheckNotNull(routeView, "routeView")) { | 164 | if (!tdt::CheckNotNull(routeView, "routeView")) { |
| 177 | return -1; | 165 | return -1; |
| 178 | } | 166 | } |
| @@ -190,26 +178,20 @@ int32_t LogQueriedRoute(const QueueRouteSample &sample) | |||
| 190 | uint32_t queriedDst = 0; | 178 | uint32_t queriedDst = 0; |
| 191 | int32_t queriedStatus = 0; | 179 | int32_t queriedStatus = 0; |
| 192 | if (GetQueueRouteParamValue( | 180 | if (GetQueueRouteParamValue( |
| 193 | - routeView, | 181 | + routeView, ACL_TDT_QUEUE_ROUTE_SRC_UINT32, |
| 194 | - ACL_TDT_QUEUE_ROUTE_SRC_UINT32, | ||
| 195 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_SRC_UINT32, sizeof(queriedSrc), ¶mSize, " | 182 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_SRC_UINT32, sizeof(queriedSrc), ¶mSize, " |
| 196 | "&queriedSrc)", | 183 | "&queriedSrc)", |
| 197 | - sizeof(queriedSrc), | 184 | + sizeof(queriedSrc), &queriedSrc) != ACL_SUCCESS || |
| 198 | - &queriedSrc) != ACL_SUCCESS || | ||
| 199 | GetQueueRouteParamValue( | 185 | GetQueueRouteParamValue( |
| 200 | - routeView, | 186 | + routeView, ACL_TDT_QUEUE_ROUTE_DST_UINT32, |
| 201 | - ACL_TDT_QUEUE_ROUTE_DST_UINT32, | ||
| 202 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_DST_UINT32, sizeof(queriedDst), ¶mSize, " | 187 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_DST_UINT32, sizeof(queriedDst), ¶mSize, " |
| 203 | "&queriedDst)", | 188 | "&queriedDst)", |
| 204 | - sizeof(queriedDst), | 189 | + sizeof(queriedDst), &queriedDst) != ACL_SUCCESS || |
| 205 | - &queriedDst) != ACL_SUCCESS || | ||
| 206 | GetQueueRouteParamValue( | 190 | GetQueueRouteParamValue( |
| 207 | - routeView, | 191 | + routeView, ACL_TDT_QUEUE_ROUTE_STATUS_INT32, |
| 208 | - ACL_TDT_QUEUE_ROUTE_STATUS_INT32, | ||
| 209 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_STATUS_INT32, sizeof(queriedStatus), " | 192 | "acltdtGetQueueRouteParam(routeView, ACL_TDT_QUEUE_ROUTE_STATUS_INT32, sizeof(queriedStatus), " |
| 210 | "¶mSize, &queriedStatus)", | 193 | "¶mSize, &queriedStatus)", |
| 211 | - sizeof(queriedStatus), | 194 | + sizeof(queriedStatus), &queriedStatus) != ACL_SUCCESS) { |
| 212 | - &queriedStatus) != ACL_SUCCESS) { | ||
| 213 | (void)acltdtDestroyQueueRoute(routeView); | 195 | (void)acltdtDestroyQueueRoute(routeView); |
| 214 | return -1; | 196 | return -1; |
| 215 | } | 197 | } |
| @@ -219,7 +201,7 @@ int32_t LogQueriedRoute(const QueueRouteSample &sample) | |||
| 219 | return 0; | 201 | return 0; |
| 220 | } | 202 | } |
| 221 | 203 | ||
| 222 | -int32_t RunSample(QueueRouteSample &sample) | 204 | +int32_t RunSample(QueueRouteSample& sample) |
| 223 | { | 205 | { |
| 224 | CHECK_ERROR(aclInit(nullptr)); | 206 | CHECK_ERROR(aclInit(nullptr)); |
| 225 | sample.aclInitialized = true; | 207 | sample.aclInitialized = true; |
| @@ -238,62 +220,49 @@ int32_t RunSample(QueueRouteSample &sample) | |||
| 238 | return LogQueriedRoute(sample); | 220 | return LogQueriedRoute(sample); |
| 239 | } | 221 | } |
| 240 | 222 | ||
| 241 | -void CleanupRouteResources(QueueRouteSample &sample, int32_t &finalResult) | 223 | +void CleanupRouteResources(QueueRouteSample& sample, int32_t& finalResult) |
| 242 | { | 224 | { |
| 243 | if (sample.bindAttempted && sample.routeAdded && (sample.bindList != nullptr)) { | 225 | if (sample.bindAttempted && sample.routeAdded && (sample.bindList != nullptr)) { |
| 244 | tdt::UpdateFinalResultOnError( | 226 | tdt::UpdateFinalResultOnError( |
| 245 | - "acltdtUnbindQueueRoutes(sample.bindList)", | 227 | + "acltdtUnbindQueueRoutes(sample.bindList)", acltdtUnbindQueueRoutes(sample.bindList), finalResult); |
| 246 | - acltdtUnbindQueueRoutes(sample.bindList), | ||
| 247 | - finalResult); | ||
| 248 | } | 228 | } |
| 249 | if (sample.queryList != nullptr) { | 229 | if (sample.queryList != nullptr) { |
| 250 | tdt::UpdateFinalResultOnError( | 230 | tdt::UpdateFinalResultOnError( |
| 251 | - "acltdtDestroyQueueRouteList(sample.queryList)", | 231 | + "acltdtDestroyQueueRouteList(sample.queryList)", acltdtDestroyQueueRouteList(sample.queryList), |
| 252 | - acltdtDestroyQueueRouteList(sample.queryList), | ||
| 253 | finalResult); | 232 | finalResult); |
| 254 | } | 233 | } |
| 255 | if (sample.queryInfo != nullptr) { | 234 | if (sample.queryInfo != nullptr) { |
| 256 | tdt::UpdateFinalResultOnError( | 235 | tdt::UpdateFinalResultOnError( |
| 257 | - "acltdtDestroyQueueRouteQueryInfo(sample.queryInfo)", | 236 | + "acltdtDestroyQueueRouteQueryInfo(sample.queryInfo)", acltdtDestroyQueueRouteQueryInfo(sample.queryInfo), |
| 258 | - acltdtDestroyQueueRouteQueryInfo(sample.queryInfo), | ||
| 259 | finalResult); | 237 | finalResult); |
| 260 | } | 238 | } |
| 261 | if (sample.bindList != nullptr) { | 239 | if (sample.bindList != nullptr) { |
| 262 | tdt::UpdateFinalResultOnError( | 240 | tdt::UpdateFinalResultOnError( |
| 263 | - "acltdtDestroyQueueRouteList(sample.bindList)", | 241 | + "acltdtDestroyQueueRouteList(sample.bindList)", acltdtDestroyQueueRouteList(sample.bindList), finalResult); |
| 264 | - acltdtDestroyQueueRouteList(sample.bindList), | ||
| 265 | - finalResult); | ||
| 266 | } | 242 | } |
| 267 | if (sample.route != nullptr) { | 243 | if (sample.route != nullptr) { |
| 268 | tdt::UpdateFinalResultOnError( | 244 | tdt::UpdateFinalResultOnError( |
| 269 | - "acltdtDestroyQueueRoute(sample.route)", | 245 | + "acltdtDestroyQueueRoute(sample.route)", acltdtDestroyQueueRoute(sample.route), finalResult); |
| 270 | - acltdtDestroyQueueRoute(sample.route), | ||
| 271 | - finalResult); | ||
| 272 | } | 246 | } |
| 273 | } | 247 | } |
| 274 | 248 | ||
| 275 | -void CleanupQueues(QueueRouteSample &sample, int32_t &finalResult) | 249 | +void CleanupQueues(QueueRouteSample& sample, int32_t& finalResult) |
| 276 | { | 250 | { |
| 277 | if (sample.dstQueueCreated) { | 251 | if (sample.dstQueueCreated) { |
| 278 | tdt::UpdateFinalResultOnError( | 252 | tdt::UpdateFinalResultOnError( |
| 279 | - "acltdtDestroyQueue(sample.dstQid)", | 253 | + "acltdtDestroyQueue(sample.dstQid)", acltdtDestroyQueue(sample.dstQid), finalResult); |
| 280 | - acltdtDestroyQueue(sample.dstQid), | ||
| 281 | - finalResult); | ||
| 282 | } | 254 | } |
| 283 | if (sample.srcQueueCreated) { | 255 | if (sample.srcQueueCreated) { |
| 284 | tdt::UpdateFinalResultOnError( | 256 | tdt::UpdateFinalResultOnError( |
| 285 | - "acltdtDestroyQueue(sample.srcQid)", | 257 | + "acltdtDestroyQueue(sample.srcQid)", acltdtDestroyQueue(sample.srcQid), finalResult); |
| 286 | - acltdtDestroyQueue(sample.srcQid), | ||
| 287 | - finalResult); | ||
| 288 | } | 258 | } |
| 289 | } | 259 | } |
| 290 | 260 | ||
| 291 | -void CleanupAclRuntime(int32_t &finalResult, const QueueRouteSample &sample) | 261 | +void CleanupAclRuntime(int32_t& finalResult, const QueueRouteSample& sample) |
| 292 | { | 262 | { |
| 293 | if (sample.deviceSet) { | 263 | if (sample.deviceSet) { |
| 294 | tdt::UpdateFinalResultOnError( | 264 | tdt::UpdateFinalResultOnError( |
| 295 | - "aclrtResetDeviceForce(QueueRouteSample::kDeviceId)", | 265 | + "aclrtResetDeviceForce(QueueRouteSample::kDeviceId)", aclrtResetDeviceForce(QueueRouteSample::kDeviceId), |
| 296 | - aclrtResetDeviceForce(QueueRouteSample::kDeviceId), | ||
| 297 | finalResult); | 266 | finalResult); |
| 298 | } | 267 | } |
| 299 | if (sample.aclInitialized) { | 268 | if (sample.aclInitialized) { |
| @@ -301,7 +270,7 @@ void CleanupAclRuntime(int32_t &finalResult, const QueueRouteSample &sample) | |||
| 301 | } | 270 | } |
| 302 | } | 271 | } |
| 303 | 272 | ||
| 304 | -void CleanupSample(QueueRouteSample &sample, int32_t &finalResult) | 273 | +void CleanupSample(QueueRouteSample& sample, int32_t& finalResult) |
| 305 | { | 274 | { |
| 306 | CleanupRouteResources(sample, finalResult); | 275 | CleanupRouteResources(sample, finalResult); |
| 307 | CleanupQueues(sample, finalResult); | 276 | CleanupQueues(sample, finalResult); |
| @@ -17,56 +17,118 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | namespace { | 19 | namespace { |
| 20 | -struct CustomBlock { void *addr; size_t size; }; | 20 | +struct CustomBlock { |
| 21 | -struct CustomAllocatorState { uint32_t allocCount; uint32_t freeCount; }; | 21 | + void* addr; |
| 22 | + size_t size; | ||
| 23 | +}; | ||
| 24 | +struct CustomAllocatorState { | ||
| 25 | + uint32_t allocCount; | ||
| 26 | + uint32_t freeCount; | ||
| 27 | +}; | ||
| 22 | 28 | ||
| 23 | -void *CustomAlloc(aclrtAllocator allocator, size_t size) | 29 | +void* CustomAlloc(aclrtAllocator allocator, size_t size) |
| 24 | { | 30 | { |
| 25 | - auto *state = static_cast<CustomAllocatorState *>(allocator); | 31 | + auto* state = static_cast<CustomAllocatorState*>(allocator); |
| 26 | - auto *block = static_cast<CustomBlock *>(std::malloc(sizeof(CustomBlock))); | 32 | + auto* block = static_cast<CustomBlock*>(std::malloc(sizeof(CustomBlock))); |
| 27 | - if (block == nullptr) { return nullptr; } | 33 | + if (block == nullptr) { |
| 34 | + return nullptr; | ||
| 35 | + } | ||
| 28 | block->addr = std::malloc(size); | 36 | block->addr = std::malloc(size); |
| 29 | - if (block->addr == nullptr) { std::free(block); return nullptr; } | 37 | + if (block->addr == nullptr) { |
| 38 | + std::free(block); | ||
| 39 | + return nullptr; | ||
| 40 | + } | ||
| 30 | block->size = size; | 41 | block->size = size; |
| 31 | - if (state != nullptr) { ++state->allocCount; } | 42 | + if (state != nullptr) { |
| 43 | + ++state->allocCount; | ||
| 44 | + } | ||
| 32 | INFO_LOG("CustomAlloc size=%zu", size); | 45 | INFO_LOG("CustomAlloc size=%zu", size); |
| 33 | return block; | 46 | return block; |
| 34 | } | 47 | } |
| 35 | 48 | ||
| 36 | void CustomFree(aclrtAllocator allocator, aclrtAllocatorBlock block) | 49 | void CustomFree(aclrtAllocator allocator, aclrtAllocatorBlock block) |
| 37 | { | 50 | { |
| 38 | - auto *state = static_cast<CustomAllocatorState *>(allocator); | 51 | + auto* state = static_cast<CustomAllocatorState*>(allocator); |
| 39 | - auto *typedBlock = static_cast<CustomBlock *>(block); | 52 | + auto* typedBlock = static_cast<CustomBlock*>(block); |
| 40 | - if (typedBlock != nullptr) { std::free(typedBlock->addr); std::free(typedBlock); } | 53 | + if (typedBlock != nullptr) { |
| 41 | - if (state != nullptr) { ++state->freeCount; } | 54 | + std::free(typedBlock->addr); |
| 55 | + std::free(typedBlock); | ||
| 56 | + } | ||
| 57 | + if (state != nullptr) { | ||
| 58 | + ++state->freeCount; | ||
| 59 | + } | ||
| 42 | INFO_LOG("CustomFree invoked"); | 60 | INFO_LOG("CustomFree invoked"); |
| 43 | } | 61 | } |
| 44 | 62 | ||
| 45 | -void *CustomAllocAdvise(aclrtAllocator allocator, size_t size, aclrtAllocatorAddr addr) | 63 | +void* CustomAllocAdvise(aclrtAllocator allocator, size_t size, aclrtAllocatorAddr addr) |
| 46 | { | 64 | { |
| 47 | (void)addr; | 65 | (void)addr; |
| 48 | INFO_LOG("CustomAllocAdvise size=%zu", size); | 66 | INFO_LOG("CustomAllocAdvise size=%zu", size); |
| 49 | return CustomAlloc(allocator, size); | 67 | return CustomAlloc(allocator, size); |
| 50 | } | 68 | } |
| 51 | 69 | ||
| 52 | -void *CustomGetAddrFromBlock(aclrtAllocatorBlock block) | 70 | +void* CustomGetAddrFromBlock(aclrtAllocatorBlock block) |
| 53 | { | 71 | { |
| 54 | - auto *typedBlock = static_cast<CustomBlock *>(block); | 72 | + auto* typedBlock = static_cast<CustomBlock*>(block); |
| 55 | return typedBlock == nullptr ? nullptr : typedBlock->addr; | 73 | return typedBlock == nullptr ? nullptr : typedBlock->addr; |
| 56 | } | 74 | } |
| 57 | -} | 75 | +} // namespace |
| 58 | -#define CHECK_NOT_NULL(ptr, name) do { if ((ptr) == nullptr) { ERROR_LOG("%s is nullptr", name); return -1; } } while (0) | 76 | +#define CHECK_NOT_NULL(ptr, name) \ |
| 77 | + do { \ | ||
| 78 | + if ((ptr) == nullptr) { \ | ||
| 79 | + ERROR_LOG("%s is nullptr", name); \ | ||
| 80 | + return -1; \ | ||
| 81 | + } \ | ||
| 82 | + } while (0) | ||
| 59 | int main() | 83 | int main() |
| 60 | { | 84 | { |
| 61 | - const int32_t deviceId = 0; aclrtContext context = nullptr; aclrtStream stream = nullptr; CustomAllocatorState state = {0, 0}; | 85 | + const int32_t deviceId = 0; |
| 62 | - CHECK_ERROR(aclInit(nullptr)); CHECK_ERROR(aclrtSetDevice(deviceId)); CHECK_ERROR(aclrtCreateContext(&context, deviceId)); CHECK_ERROR(aclrtCreateStream(&stream)); | 86 | + aclrtContext context = nullptr; |
| 63 | - aclrtAllocatorDesc desc = aclrtAllocatorCreateDesc(); CHECK_NOT_NULL(desc, "aclrtAllocatorCreateDesc"); | 87 | + aclrtStream stream = nullptr; |
| 64 | - CHECK_ERROR(aclrtAllocatorSetObjToDesc(desc, reinterpret_cast<aclrtAllocator>(&state))); CHECK_ERROR(aclrtAllocatorSetAllocFuncToDesc(desc, CustomAlloc)); CHECK_ERROR(aclrtAllocatorSetFreeFuncToDesc(desc, CustomFree)); CHECK_ERROR(aclrtAllocatorSetAllocAdviseFuncToDesc(desc, CustomAllocAdvise)); CHECK_ERROR(aclrtAllocatorSetGetAddrFromBlockFuncToDesc(desc, CustomGetAddrFromBlock)); | 88 | + CustomAllocatorState state = {0, 0}; |
| 89 | + CHECK_ERROR(aclInit(nullptr)); | ||
| 90 | + CHECK_ERROR(aclrtSetDevice(deviceId)); | ||
| 91 | + CHECK_ERROR(aclrtCreateContext(&context, deviceId)); | ||
| 92 | + CHECK_ERROR(aclrtCreateStream(&stream)); | ||
| 93 | + aclrtAllocatorDesc desc = aclrtAllocatorCreateDesc(); | ||
| 94 | + CHECK_NOT_NULL(desc, "aclrtAllocatorCreateDesc"); | ||
| 95 | + CHECK_ERROR(aclrtAllocatorSetObjToDesc(desc, reinterpret_cast<aclrtAllocator>(&state))); | ||
| 96 | + CHECK_ERROR(aclrtAllocatorSetAllocFuncToDesc(desc, CustomAlloc)); | ||
| 97 | + CHECK_ERROR(aclrtAllocatorSetFreeFuncToDesc(desc, CustomFree)); | ||
| 98 | + CHECK_ERROR(aclrtAllocatorSetAllocAdviseFuncToDesc(desc, CustomAllocAdvise)); | ||
| 99 | + CHECK_ERROR(aclrtAllocatorSetGetAddrFromBlockFuncToDesc(desc, CustomGetAddrFromBlock)); | ||
| 65 | CHECK_ERROR(aclrtAllocatorRegister(stream, desc)); | 100 | CHECK_ERROR(aclrtAllocatorRegister(stream, desc)); |
| 66 | - aclrtAllocatorDesc descOut = nullptr; aclrtAllocator allocatorOut = nullptr; aclrtAllocatorAllocFunc allocFunc = nullptr; aclrtAllocatorFreeFunc freeFunc = nullptr; aclrtAllocatorAllocAdviseFunc allocAdviseFunc = nullptr; aclrtAllocatorGetAddrFromBlockFunc getAddrFunc = nullptr; | 101 | + aclrtAllocatorDesc descOut = nullptr; |
| 67 | - CHECK_ERROR(aclrtAllocatorGetByStream(stream, &descOut, &allocatorOut, &allocFunc, &freeFunc, &allocAdviseFunc, &getAddrFunc)); CHECK_NOT_NULL(allocFunc, "allocFunc"); CHECK_NOT_NULL(freeFunc, "freeFunc"); CHECK_NOT_NULL(getAddrFunc, "getAddrFunc"); | 102 | + aclrtAllocator allocatorOut = nullptr; |
| 68 | - aclrtAllocatorBlock block = allocFunc(allocatorOut, 256); CHECK_NOT_NULL(block, "Custom block"); void *addr = getAddrFunc(block); CHECK_NOT_NULL(addr, "Custom block addr"); std::fill_n(static_cast<unsigned char *>(addr), 256, static_cast<unsigned char>(0xAB)); | 103 | + aclrtAllocatorAllocFunc allocFunc = nullptr; |
| 69 | - aclrtAllocatorBlock advisedBlock = nullptr; if (allocAdviseFunc != nullptr) { advisedBlock = allocAdviseFunc(allocatorOut, 128, nullptr); if (advisedBlock != nullptr) { freeFunc(allocatorOut, advisedBlock); } } | 104 | + aclrtAllocatorFreeFunc freeFunc = nullptr; |
| 70 | - freeFunc(allocatorOut, block); INFO_LOG("Allocator desc queried: descOut=%p allocCount=%u freeCount=%u", descOut, state.allocCount, state.freeCount); | 105 | + aclrtAllocatorAllocAdviseFunc allocAdviseFunc = nullptr; |
| 71 | - CHECK_ERROR(aclrtAllocatorUnregister(stream)); CHECK_ERROR(aclrtAllocatorDestroyDesc(desc)); CHECK_ERROR(aclrtDestroyStream(stream)); CHECK_ERROR(aclrtDestroyContext(context)); CHECK_ERROR(aclrtResetDeviceForce(deviceId)); CHECK_ERROR(aclFinalize()); return 0; | 106 | + aclrtAllocatorGetAddrFromBlockFunc getAddrFunc = nullptr; |
| 107 | + CHECK_ERROR(aclrtAllocatorGetByStream( | ||
| 108 | + stream, &descOut, &allocatorOut, &allocFunc, &freeFunc, &allocAdviseFunc, &getAddrFunc)); | ||
| 109 | + CHECK_NOT_NULL(allocFunc, "allocFunc"); | ||
| 110 | + CHECK_NOT_NULL(freeFunc, "freeFunc"); | ||
| 111 | + CHECK_NOT_NULL(getAddrFunc, "getAddrFunc"); | ||
| 112 | + aclrtAllocatorBlock block = allocFunc(allocatorOut, 256); | ||
| 113 | + CHECK_NOT_NULL(block, "Custom block"); | ||
| 114 | + void* addr = getAddrFunc(block); | ||
| 115 | + CHECK_NOT_NULL(addr, "Custom block addr"); | ||
| 116 | + std::fill_n(static_cast<unsigned char*>(addr), 256, static_cast<unsigned char>(0xAB)); | ||
| 117 | + aclrtAllocatorBlock advisedBlock = nullptr; | ||
| 118 | + if (allocAdviseFunc != nullptr) { | ||
| 119 | + advisedBlock = allocAdviseFunc(allocatorOut, 128, nullptr); | ||
| 120 | + if (advisedBlock != nullptr) { | ||
| 121 | + freeFunc(allocatorOut, advisedBlock); | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + freeFunc(allocatorOut, block); | ||
| 125 | + INFO_LOG( | ||
| 126 | + "Allocator desc queried: descOut=%p allocCount=%u freeCount=%u", descOut, state.allocCount, state.freeCount); | ||
| 127 | + CHECK_ERROR(aclrtAllocatorUnregister(stream)); | ||
| 128 | + CHECK_ERROR(aclrtAllocatorDestroyDesc(desc)); | ||
| 129 | + CHECK_ERROR(aclrtDestroyStream(stream)); | ||
| 130 | + CHECK_ERROR(aclrtDestroyContext(context)); | ||
| 131 | + CHECK_ERROR(aclrtResetDeviceForce(deviceId)); | ||
| 132 | + CHECK_ERROR(aclFinalize()); | ||
| 133 | + return 0; | ||
| 72 | } | 134 | } |
| @@ -9,8 +9,8 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | /* | 11 | /* |
| 12 | - * This example leverages the Unified Virtual Memory (UVM) mechanism to allocate memory | 12 | + * This example leverages the Unified Virtual Memory (UVM) mechanism to allocate memory |
| 13 | - * for kernel inputs and outputs via UVM allocation APIs, thereby eliminating the need | 13 | + * for kernel inputs and outputs via UVM allocation APIs, thereby eliminating the need |
| 14 | * for explicit data transfers during parameter passing and result write-back. | 14 | * for explicit data transfers during parameter passing and result write-back. |
| 15 | */ | 15 | */ |
| 16 | 16 | ||
| @@ -23,9 +23,9 @@ | |||
| 23 | 23 | ||
| 24 | namespace { | 24 | namespace { |
| 25 | struct KernelBuffers { | 25 | struct KernelBuffers { |
| 26 | - uint8_t *xPtr = nullptr; | 26 | + uint8_t* xPtr = nullptr; |
| 27 | - uint8_t *yPtr = nullptr; | 27 | + uint8_t* yPtr = nullptr; |
| 28 | - uint8_t *zPtr = nullptr; | 28 | + uint8_t* zPtr = nullptr; |
| 29 | }; | 29 | }; |
| 30 | 30 | ||
| 31 | struct RuntimeResources { | 31 | struct RuntimeResources { |
| @@ -38,7 +38,7 @@ struct RuntimeResources { | |||
| 38 | bool binLoaded = false; | 38 | bool binLoaded = false; |
| 39 | }; | 39 | }; |
| 40 | 40 | ||
| 41 | -void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalResult) | 41 | +void UpdateFinalResultOnError(const char* apiName, aclError ret, int32_t& finalResult) |
| 42 | { | 42 | { |
| 43 | if (ret == ACL_SUCCESS) { | 43 | if (ret == ACL_SUCCESS) { |
| 44 | return; | 44 | return; |
| @@ -47,7 +47,7 @@ void UpdateFinalResultOnError(const char *apiName, aclError ret, int32_t &finalR | |||
| 47 | finalResult = -1; | 47 | finalResult = -1; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | -int32_t InitializeRuntime(RuntimeResources *runtime) | 50 | +int32_t InitializeRuntime(RuntimeResources* runtime) |
| 51 | { | 51 | { |
| 52 | // Initialize ACL and create a stream on device 0. | 52 | // Initialize ACL and create a stream on device 0. |
| 53 | CHECK_ERROR(aclInit(nullptr)); | 53 | CHECK_ERROR(aclInit(nullptr)); |
| @@ -59,16 +59,19 @@ int32_t InitializeRuntime(RuntimeResources *runtime) | |||
| 59 | return 0; | 59 | return 0; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | -int32_t AllocateKernelBuffers(size_t inputByteSize, size_t outputByteSize, KernelBuffers *buffers) | 62 | +int32_t AllocateKernelBuffers(size_t inputByteSize, size_t outputByteSize, KernelBuffers* buffers) |
| 63 | { | 63 | { |
| 64 | // Allocate uvm memory for kernel inputs and output. | 64 | // Allocate uvm memory for kernel inputs and output. |
| 65 | - CHECK_ERROR(aclrtMemAllocManaged(reinterpret_cast<void **>(&buffers->xPtr), inputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); | 65 | + CHECK_ERROR( |
| 66 | - CHECK_ERROR(aclrtMemAllocManaged(reinterpret_cast<void **>(&buffers->yPtr), inputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); | 66 | + aclrtMemAllocManaged(reinterpret_cast<void**>(&buffers->xPtr), inputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); |
| 67 | - CHECK_ERROR(aclrtMemAllocManaged(reinterpret_cast<void **>(&buffers->zPtr), outputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); | 67 | + CHECK_ERROR( |
| 68 | + aclrtMemAllocManaged(reinterpret_cast<void**>(&buffers->yPtr), inputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); | ||
| 69 | + CHECK_ERROR( | ||
| 70 | + aclrtMemAllocManaged(reinterpret_cast<void**>(&buffers->zPtr), outputByteSize, ACL_RT_MEM_ATTACH_GLOBAL)); | ||
| 68 | return 0; | 71 | return 0; |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers &buffers) | 74 | +int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers& buffers) |
| 72 | { | 75 | { |
| 73 | // Load generated input files. | 76 | // Load generated input files. |
| 74 | size_t xFileSize = inputByteSize; | 77 | size_t xFileSize = inputByteSize; |
| @@ -87,27 +90,23 @@ int32_t PrepareInputData(size_t inputByteSize, const KernelBuffers &buffers) | |||
| 87 | return 0; | 90 | return 0; |
| 88 | } | 91 | } |
| 89 | 92 | ||
| 90 | -int32_t AppendCommonKernelArgs(aclrtArgsHandle argsHandle, uint8_t *xPtr, uint8_t *yPtr, uint8_t *zPtr) | 93 | +int32_t AppendCommonKernelArgs(aclrtArgsHandle argsHandle, uint8_t* xPtr, uint8_t* yPtr, uint8_t* zPtr) |
| 91 | { | 94 | { |
| 92 | aclrtParamHandle paramHandle1 = nullptr; | 95 | aclrtParamHandle paramHandle1 = nullptr; |
| 93 | aclrtParamHandle paramHandle2 = nullptr; | 96 | aclrtParamHandle paramHandle2 = nullptr; |
| 94 | aclrtParamHandle paramHandle3 = nullptr; | 97 | aclrtParamHandle paramHandle3 = nullptr; |
| 95 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&xPtr), sizeof(uintptr_t), ¶mHandle1)); | 98 | + CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&xPtr), sizeof(uintptr_t), ¶mHandle1)); |
| 96 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&yPtr), sizeof(uintptr_t), ¶mHandle2)); | 99 | + CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&yPtr), sizeof(uintptr_t), ¶mHandle2)); |
| 97 | - CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void **>(&zPtr), sizeof(uintptr_t), ¶mHandle3)); | 100 | + CHECK_ERROR(aclrtKernelArgsAppend(argsHandle, reinterpret_cast<void**>(&zPtr), sizeof(uintptr_t), ¶mHandle3)); |
| 98 | return 0; | 101 | return 0; |
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | int32_t BuildKernelArgs( | 104 | int32_t BuildKernelArgs( |
| 102 | - uint8_t *xPtr, | 105 | + uint8_t* xPtr, uint8_t* yPtr, uint8_t* zPtr, RuntimeResources* runtime, aclrtFuncHandle* funcHandle, |
| 103 | - uint8_t *yPtr, | 106 | + aclrtArgsHandle* argsHandle) |
| 104 | - uint8_t *zPtr, | ||
| 105 | - RuntimeResources *runtime, | ||
| 106 | - aclrtFuncHandle *funcHandle, | ||
| 107 | - aclrtArgsHandle *argsHandle) | ||
| 108 | { | 107 | { |
| 109 | // Load the selected kernel binary and build the launch argument list. | 108 | // Load the selected kernel binary and build the launch argument list. |
| 110 | - const char *filePath = "./out/fatbin/ascendc_kernels_simple/ascendc_kernels_simple.o"; | 109 | + const char* filePath = "./out/fatbin/ascendc_kernels_simple/ascendc_kernels_simple.o"; |
| 111 | 110 | ||
| 112 | CHECK_ERROR(aclrtBinaryLoadFromFile(filePath, nullptr, &runtime->binHandle)); | 111 | CHECK_ERROR(aclrtBinaryLoadFromFile(filePath, nullptr, &runtime->binHandle)); |
| 113 | runtime->binLoaded = true; | 112 | runtime->binLoaded = true; |
| @@ -123,11 +122,7 @@ int32_t BuildKernelArgs( | |||
| 123 | } | 122 | } |
| 124 | 123 | ||
| 125 | int32_t LaunchKernelAndWriteOutput( | 124 | int32_t LaunchKernelAndWriteOutput( |
| 126 | - aclrtFuncHandle funcHandle, | 125 | + aclrtFuncHandle funcHandle, aclrtArgsHandle argsHandle, uint32_t blockDim, aclrtStream stream, uint8_t* zPtr, |
| 127 | - aclrtArgsHandle argsHandle, | ||
| 128 | - uint32_t blockDim, | ||
| 129 | - aclrtStream stream, | ||
| 130 | - uint8_t *zPtr, | ||
| 131 | size_t outputByteSize) | 126 | size_t outputByteSize) |
| 132 | { | 127 | { |
| 133 | // Launch the kernel, synchronize the stream, and write output for verification. | 128 | // Launch the kernel, synchronize the stream, and write output for verification. |
| @@ -140,7 +135,7 @@ int32_t LaunchKernelAndWriteOutput( | |||
| 140 | return 0; | 135 | return 0; |
| 141 | } | 136 | } |
| 142 | 137 | ||
| 143 | -void ReleaseKernelResources(RuntimeResources &runtime, KernelBuffers &buffers, int32_t &finalResult) | 138 | +void ReleaseKernelResources(RuntimeResources& runtime, KernelBuffers& buffers, int32_t& finalResult) |
| 144 | { | 139 | { |
| 145 | if (runtime.binLoaded) { | 140 | if (runtime.binLoaded) { |
| 146 | UpdateFinalResultOnError( | 141 | UpdateFinalResultOnError( |
| @@ -191,12 +186,11 @@ int32_t RunKernelLaunchSample() | |||
| 191 | if (PrepareInputData(inputByteSize, buffers) != 0) { | 186 | if (PrepareInputData(inputByteSize, buffers) != 0) { |
| 192 | return -1; | 187 | return -1; |
| 193 | } | 188 | } |
| 194 | - if (BuildKernelArgs( | 189 | + if (BuildKernelArgs(buffers.xPtr, buffers.yPtr, buffers.zPtr, &runtime, &funcHandle, &argsHandle) != 0) { |
| 195 | - buffers.xPtr, buffers.yPtr, buffers.zPtr, &runtime, &funcHandle, &argsHandle) != 0) { | ||
| 196 | return -1; | 190 | return -1; |
| 197 | } | 191 | } |
| 198 | if (LaunchKernelAndWriteOutput( | 192 | if (LaunchKernelAndWriteOutput( |
| 199 | - funcHandle, argsHandle, blockDim, runtime.stream, buffers.zPtr, outputByteSize) != 0) { | 193 | + funcHandle, argsHandle, blockDim, runtime.stream, buffers.zPtr, outputByteSize) != 0) { |
| 200 | return -1; | 194 | return -1; |
| 201 | } | 195 | } |
| 202 | return 0; | 196 | return 0; |
| @@ -211,7 +205,4 @@ int32_t RunKernelLaunchSample() | |||
| 211 | } | 205 | } |
| 212 | } // namespace | 206 | } // namespace |
| 213 | 207 | ||
| 214 | -int32_t main(int32_t argc, char *argv[]) | 208 | +int32_t main(int32_t argc, char* argv[]) { return RunKernelLaunchSample(); } |
| 215 | -{ | ||
| 216 | - return RunKernelLaunchSample(); | ||
| 217 | -} | ||
| @@ -17,7 +17,7 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | namespace kernel { | 19 | namespace kernel { |
| 20 | -bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize) | 20 | +bool ReadFile(const std::string& filePath, size_t& fileSize, void* buffer, size_t bufferSize) |
| 21 | { | 21 | { |
| 22 | // Read data from the target file | 22 | // Read data from the target file |
| 23 | struct stat sBuf; | 23 | struct stat sBuf; |
| @@ -38,7 +38,7 @@ bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_ | |||
| 38 | return false; | 38 | return false; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | - std::filebuf *buf = file.rdbuf(); | 41 | + std::filebuf* buf = file.rdbuf(); |
| 42 | size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); | 42 | size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); |
| 43 | if (size == 0U) { | 43 | if (size == 0U) { |
| 44 | ERROR_LOG("file size is 0"); | 44 | ERROR_LOG("file size is 0"); |
| @@ -51,16 +51,15 @@ bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_ | |||
| 51 | return false; | 51 | return false; |
| 52 | } | 52 | } |
| 53 | buf->pubseekpos(0, std::ios::in); | 53 | buf->pubseekpos(0, std::ios::in); |
| 54 | - (void)buf->sgetn(static_cast<char *>(buffer), static_cast<std::streamsize>(size)); | 54 | + (void)buf->sgetn(static_cast<char*>(buffer), static_cast<std::streamsize>(size)); |
| 55 | fileSize = size; | 55 | fileSize = size; |
| 56 | file.close(); | 56 | file.close(); |
| 57 | return true; | 57 | return true; |
| 58 | } | 58 | } |
| 59 | } // namespace kernel | 59 | } // namespace kernel |
| 60 | 60 | ||
| 61 | - | ||
| 62 | namespace kernel { | 61 | namespace kernel { |
| 63 | -bool WriteFile(const std::string &filePath, const void *buffer, size_t size) | 62 | +bool WriteFile(const std::string& filePath, const void* buffer, size_t size) |
| 64 | { | 63 | { |
| 65 | // Write data to the target file | 64 | // Write data to the target file |
| 66 | if (buffer == nullptr) { | 65 | if (buffer == nullptr) { |
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace kernel { | 16 | namespace kernel { |
| 17 | - bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize); | 17 | +bool ReadFile(const std::string& filePath, size_t& fileSize, void* buffer, size_t bufferSize); |
| 18 | - bool WriteFile(const std::string &filePath, const void *buffer, size_t size); | 18 | +bool WriteFile(const std::string& filePath, const void* buffer, size_t size); |
| 19 | -} | 19 | +} // namespace kernel |
| 20 | 20 | ||
| @@ -17,14 +17,14 @@ | |||
| 17 | namespace { | 17 | namespace { |
| 18 | constexpr size_t kOverflowStatusBufferSize = 64; | 18 | constexpr size_t kOverflowStatusBufferSize = 64; |
| 19 | 19 | ||
| 20 | -uint32_t ReadOverflowFlag(const uint8_t *statusBuffer) | 20 | +uint32_t ReadOverflowFlag(const uint8_t* statusBuffer) |
| 21 | { | 21 | { |
| 22 | uint32_t overflowFlag = 0; | 22 | uint32_t overflowFlag = 0; |
| 23 | - std::copy_n(statusBuffer, sizeof(overflowFlag), reinterpret_cast<unsigned char *>(&overflowFlag)); | 23 | + std::copy_n(statusBuffer, sizeof(overflowFlag), reinterpret_cast<unsigned char*>(&overflowFlag)); |
| 24 | return overflowFlag; | 24 | return overflowFlag; |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | -const char *OverflowModeToString(aclrtFloatOverflowMode mode) | 27 | +const char* OverflowModeToString(aclrtFloatOverflowMode mode) |
| 28 | { | 28 | { |
| 29 | switch (mode) { | 29 | switch (mode) { |
| 30 | case ACL_RT_OVERFLOW_MODE_SATURATION: | 30 | case ACL_RT_OVERFLOW_MODE_SATURATION: |
| @@ -38,17 +38,14 @@ const char *OverflowModeToString(aclrtFloatOverflowMode mode) | |||
| 38 | } | 38 | } |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | -int32_t HandleOptionalOverflowRet(const char *apiName, aclError ret, const char *reason) | 41 | +int32_t HandleOptionalOverflowRet(const char* apiName, aclError ret, const char* reason) |
| 42 | { | 42 | { |
| 43 | if (ret == ACL_SUCCESS) { | 43 | if (ret == ACL_SUCCESS) { |
| 44 | return 0; | 44 | return 0; |
| 45 | } | 45 | } |
| 46 | if (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) { | 46 | if (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) { |
| 47 | WARN_LOG( | 47 | WARN_LOG( |
| 48 | - "%s is unavailable in the current environment (ret=%d): %s", | 48 | + "%s is unavailable in the current environment (ret=%d): %s", apiName, static_cast<int32_t>(ret), reason); |
| 49 | - apiName, | ||
| 50 | - static_cast<int32_t>(ret), | ||
| 51 | - reason); | ||
| 52 | return 1; | 49 | return 1; |
| 53 | } | 50 | } |
| 54 | ERROR_LOG("Operation failed: %s returned error code %d", apiName, static_cast<int32_t>(ret)); | 51 | ERROR_LOG("Operation failed: %s returned error code %d", apiName, static_cast<int32_t>(ret)); |
| @@ -61,7 +58,7 @@ int main() | |||
| 61 | const int32_t deviceId = 0; | 58 | const int32_t deviceId = 0; |
| 62 | aclrtContext context = nullptr; | 59 | aclrtContext context = nullptr; |
| 63 | aclrtStream stream = nullptr; | 60 | aclrtStream stream = nullptr; |
| 64 | - uint8_t *statusDevice = nullptr; | 61 | + uint8_t* statusDevice = nullptr; |
| 65 | aclrtFloatOverflowMode originalMode = ACL_RT_OVERFLOW_MODE_UNDEF; | 62 | aclrtFloatOverflowMode originalMode = ACL_RT_OVERFLOW_MODE_UNDEF; |
| 66 | aclrtFloatOverflowMode currentMode = ACL_RT_OVERFLOW_MODE_UNDEF; | 63 | aclrtFloatOverflowMode currentMode = ACL_RT_OVERFLOW_MODE_UNDEF; |
| 67 | bool aclInitialized = false; | 64 | bool aclInitialized = false; |
| @@ -82,8 +79,7 @@ int main() | |||
| 82 | contextCreated = true; | 79 | contextCreated = true; |
| 83 | 80 | ||
| 84 | const int32_t getModeStatus = HandleOptionalOverflowRet( | 81 | const int32_t getModeStatus = HandleOptionalOverflowRet( |
| 85 | - "aclrtGetDeviceSatMode(&originalMode)", | 82 | + "aclrtGetDeviceSatMode(&originalMode)", aclrtGetDeviceSatMode(&originalMode), |
| 86 | - aclrtGetDeviceSatMode(&originalMode), | ||
| 87 | "the current device/runtime does not expose device saturation mode"); | 83 | "the current device/runtime does not expose device saturation mode"); |
| 88 | if (getModeStatus < 0) { | 84 | if (getModeStatus < 0) { |
| 89 | return -1; | 85 | return -1; |
| @@ -96,7 +92,8 @@ int main() | |||
| 96 | const int32_t setModeStatus = HandleOptionalOverflowRet( | 92 | const int32_t setModeStatus = HandleOptionalOverflowRet( |
| 97 | "aclrtSetDeviceSatMode(ACL_RT_OVERFLOW_MODE_SATURATION)", | 93 | "aclrtSetDeviceSatMode(ACL_RT_OVERFLOW_MODE_SATURATION)", |
| 98 | aclrtSetDeviceSatMode(ACL_RT_OVERFLOW_MODE_SATURATION), | 94 | aclrtSetDeviceSatMode(ACL_RT_OVERFLOW_MODE_SATURATION), |
| 99 | - "stream overflow detection requires saturation mode and is only supported on specific products/runtime builds"); | 95 | + "stream overflow detection requires saturation mode and is only supported on specific products/runtime " |
| 96 | + "builds"); | ||
| 100 | if (setModeStatus < 0) { | 97 | if (setModeStatus < 0) { |
| 101 | return -1; | 98 | return -1; |
| 102 | } | 99 | } |
| @@ -108,16 +105,14 @@ int main() | |||
| 108 | 105 | ||
| 109 | CHECK_ERROR(aclrtGetDeviceSatMode(¤tMode)); | 106 | CHECK_ERROR(aclrtGetDeviceSatMode(¤tMode)); |
| 110 | INFO_LOG( | 107 | INFO_LOG( |
| 111 | - "Device saturation mode switched from %s to %s.", | 108 | + "Device saturation mode switched from %s to %s.", OverflowModeToString(originalMode), |
| 112 | - OverflowModeToString(originalMode), | ||
| 113 | OverflowModeToString(currentMode)); | 109 | OverflowModeToString(currentMode)); |
| 114 | 110 | ||
| 115 | CHECK_ERROR(aclrtCreateStream(&stream)); | 111 | CHECK_ERROR(aclrtCreateStream(&stream)); |
| 116 | streamCreated = true; | 112 | streamCreated = true; |
| 117 | 113 | ||
| 118 | const int32_t setOverflowSwitchStatus = HandleOptionalOverflowRet( | 114 | const int32_t setOverflowSwitchStatus = HandleOptionalOverflowRet( |
| 119 | - "aclrtSetStreamOverflowSwitch(stream, 1)", | 115 | + "aclrtSetStreamOverflowSwitch(stream, 1)", aclrtSetStreamOverflowSwitch(stream, 1), |
| 120 | - aclrtSetStreamOverflowSwitch(stream, 1), | ||
| 121 | "the current environment does not expose stream-level overflow detection even in saturation mode"); | 116 | "the current environment does not expose stream-level overflow detection even in saturation mode"); |
| 122 | if (setOverflowSwitchStatus < 0) { | 117 | if (setOverflowSwitchStatus < 0) { |
| 123 | return -1; | 118 | return -1; |
| @@ -132,10 +127,8 @@ int main() | |||
| 132 | CHECK_ERROR(aclrtGetStreamOverflowSwitch(stream, &queriedSwitch)); | 127 | CHECK_ERROR(aclrtGetStreamOverflowSwitch(stream, &queriedSwitch)); |
| 133 | INFO_LOG("Overflow switch=%u", queriedSwitch); | 128 | INFO_LOG("Overflow switch=%u", queriedSwitch); |
| 134 | 129 | ||
| 135 | - CHECK_ERROR(aclrtMalloc( | 130 | + CHECK_ERROR( |
| 136 | - reinterpret_cast<void **>(&statusDevice), | 131 | + aclrtMalloc(reinterpret_cast<void**>(&statusDevice), kOverflowStatusBufferSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 137 | - kOverflowStatusBufferSize, | ||
| 138 | - ACL_MEM_MALLOC_HUGE_FIRST)); | ||
| 139 | statusAllocated = true; | 132 | statusAllocated = true; |
| 140 | 133 | ||
| 141 | uint8_t statusHost[kOverflowStatusBufferSize] = {}; | 134 | uint8_t statusHost[kOverflowStatusBufferSize] = {}; |
| @@ -144,11 +137,7 @@ int main() | |||
| 144 | CHECK_ERROR(aclrtGetOverflowStatus(statusDevice, kOverflowStatusBufferSize, stream)); | 137 | CHECK_ERROR(aclrtGetOverflowStatus(statusDevice, kOverflowStatusBufferSize, stream)); |
| 145 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 138 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 146 | CHECK_ERROR(aclrtMemcpy( | 139 | CHECK_ERROR(aclrtMemcpy( |
| 147 | - statusHost, | 140 | + statusHost, sizeof(statusHost), statusDevice, kOverflowStatusBufferSize, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 148 | - sizeof(statusHost), | ||
| 149 | - statusDevice, | ||
| 150 | - kOverflowStatusBufferSize, | ||
| 151 | - ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 152 | overflowFlag = ReadOverflowFlag(statusHost); | 141 | overflowFlag = ReadOverflowFlag(statusHost); |
| 153 | INFO_LOG("Overflow status before reset=%u", overflowFlag); | 142 | INFO_LOG("Overflow status before reset=%u", overflowFlag); |
| 154 | 143 | ||
| @@ -160,11 +149,7 @@ int main() | |||
| 160 | CHECK_ERROR(aclrtGetOverflowStatus(statusDevice, kOverflowStatusBufferSize, stream)); | 149 | CHECK_ERROR(aclrtGetOverflowStatus(statusDevice, kOverflowStatusBufferSize, stream)); |
| 161 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 150 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 162 | CHECK_ERROR(aclrtMemcpy( | 151 | CHECK_ERROR(aclrtMemcpy( |
| 163 | - statusHost, | 152 | + statusHost, sizeof(statusHost), statusDevice, kOverflowStatusBufferSize, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 164 | - sizeof(statusHost), | ||
| 165 | - statusDevice, | ||
| 166 | - kOverflowStatusBufferSize, | ||
| 167 | - ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 168 | overflowFlag = ReadOverflowFlag(statusHost); | 153 | overflowFlag = ReadOverflowFlag(statusHost); |
| 169 | INFO_LOG("Overflow status after reset=%u", overflowFlag); | 154 | INFO_LOG("Overflow status after reset=%u", overflowFlag); |
| 170 | return 0; | 155 | return 0; |
| @@ -173,7 +158,8 @@ int main() | |||
| 173 | if (statusAllocated) { | 158 | if (statusAllocated) { |
| 174 | const aclError freeRet = aclrtFree(statusDevice); | 159 | const aclError freeRet = aclrtFree(statusDevice); |
| 175 | if (freeRet != ACL_SUCCESS) { | 160 | if (freeRet != ACL_SUCCESS) { |
| 176 | - ERROR_LOG("Operation failed: aclrtFree(statusDevice) returned error code %d", static_cast<int32_t>(freeRet)); | 161 | + ERROR_LOG( |
| 162 | + "Operation failed: aclrtFree(statusDevice) returned error code %d", static_cast<int32_t>(freeRet)); | ||
| 177 | finalResult = -1; | 163 | finalResult = -1; |
| 178 | } | 164 | } |
| 179 | } | 165 | } |
| @@ -18,18 +18,18 @@ | |||
| 18 | using namespace std; | 18 | using namespace std; |
| 19 | 19 | ||
| 20 | namespace { | 20 | namespace { |
| 21 | - void LogDumpPath(acldumpType dumpType, const char *fallbackPath) | 21 | +void LogDumpPath(acldumpType dumpType, const char* fallbackPath) |
| 22 | - { | 22 | +{ |
| 23 | - const char *dumpPath = acldumpGetPath(dumpType); | 23 | + const char* dumpPath = acldumpGetPath(dumpType); |
| 24 | - if (dumpPath != nullptr) { | 24 | + if (dumpPath != nullptr) { |
| 25 | - INFO_LOG("acldumpGetPath returned dump path: %s", dumpPath); | 25 | + INFO_LOG("acldumpGetPath returned dump path: %s", dumpPath); |
| 26 | - return; | 26 | + return; |
| 27 | - } | ||
| 28 | - WARN_LOG("acldumpGetPath returned null, fallback dump path is %s", fallbackPath); | ||
| 29 | } | 27 | } |
| 30 | - | 28 | + WARN_LOG("acldumpGetPath returned null, fallback dump path is %s", fallbackPath); |
| 31 | } | 29 | } |
| 32 | 30 | ||
| 31 | +} // namespace | ||
| 32 | + | ||
| 33 | int main() | 33 | int main() |
| 34 | { | 34 | { |
| 35 | // The device id | 35 | // The device id |
| @@ -51,13 +51,13 @@ int main() | |||
| 51 | std::vector<int64_t> selfShape{4, 2}; | 51 | std::vector<int64_t> selfShape{4, 2}; |
| 52 | std::vector<int64_t> otherShape{4, 2}; | 52 | std::vector<int64_t> otherShape{4, 2}; |
| 53 | std::vector<int64_t> outShape{4, 2}; | 53 | std::vector<int64_t> outShape{4, 2}; |
| 54 | - void *selfDeviceAddr = nullptr; | 54 | + void* selfDeviceAddr = nullptr; |
| 55 | - void *otherDeviceAddr = nullptr; | 55 | + void* otherDeviceAddr = nullptr; |
| 56 | - void *outDeviceAddr = nullptr; | 56 | + void* outDeviceAddr = nullptr; |
| 57 | - aclTensor *self = nullptr; | 57 | + aclTensor* self = nullptr; |
| 58 | - aclTensor *other = nullptr; | 58 | + aclTensor* other = nullptr; |
| 59 | - aclScalar *alpha = nullptr; | 59 | + aclScalar* alpha = nullptr; |
| 60 | - aclTensor *out = nullptr; | 60 | + aclTensor* out = nullptr; |
| 61 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 61 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 62 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; | 62 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; |
| 63 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 63 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -77,11 +77,11 @@ int main() | |||
| 77 | 77 | ||
| 78 | // 3. Call the CANN operator library API(Custom Implementation) | 78 | // 3. Call the CANN operator library API(Custom Implementation) |
| 79 | uint64_t workspaceSize = 0; | 79 | uint64_t workspaceSize = 0; |
| 80 | - aclOpExecutor *executor; | 80 | + aclOpExecutor* executor; |
| 81 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); | 81 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); |
| 82 | 82 | ||
| 83 | // Allocate device memory based on the calculation results | 83 | // Allocate device memory based on the calculation results |
| 84 | - void *workspaceAddr = nullptr; | 84 | + void* workspaceAddr = nullptr; |
| 85 | if (workspaceSize > 0lu) { | 85 | if (workspaceSize > 0lu) { |
| 86 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 86 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 87 | } | 87 | } |
| @@ -94,11 +94,12 @@ int main() | |||
| 94 | // 5. Obtain the execution result of the operator and copy the result from the device memory to the host | 94 | // 5. Obtain the execution result of the operator and copy the result from the device memory to the host |
| 95 | auto size = adump::GetShapeSize(outShape); | 95 | auto size = adump::GetShapeSize(outShape); |
| 96 | std::vector<float> resultData(size, 0); | 96 | std::vector<float> resultData(size, 0); |
| 97 | - CHECK_ERROR(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 97 | + CHECK_ERROR(aclrtMemcpy( |
| 98 | - size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | 98 | + resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 99 | + ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 99 | 100 | ||
| 100 | for (int64_t i = 0; i < size; i++) { | 101 | for (int64_t i = 0; i < size; i++) { |
| 101 | - INFO_LOG("result[%ld] is: %f", i, resultData[i]); | 102 | + INFO_LOG("result[%ld] is: %f", i, resultData[i]); |
| 102 | } | 103 | } |
| 103 | 104 | ||
| 104 | // 6. Release the resources(Custom Destruction) | 105 | // 6. Release the resources(Custom Destruction) |
| @@ -109,7 +110,7 @@ int main() | |||
| 109 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); | 110 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); |
| 110 | CHECK_ERROR(aclrtFree(outDeviceAddr)); | 111 | CHECK_ERROR(aclrtFree(outDeviceAddr)); |
| 111 | if (workspaceSize > 0lu) { | 112 | if (workspaceSize > 0lu) { |
| 112 | - CHECK_ERROR(aclrtFree(workspaceAddr)); | 113 | + CHECK_ERROR(aclrtFree(workspaceAddr)); |
| 113 | } | 114 | } |
| 114 | // 7. AsendCL Destroy | 115 | // 7. AsendCL Destroy |
| 115 | // Disable Dump Args Function | 116 | // Disable Dump Args Function |
| @@ -118,7 +119,9 @@ int main() | |||
| 118 | CHECK_ERROR(aclrtDestroyStream(stream)); | 119 | CHECK_ERROR(aclrtDestroyStream(stream)); |
| 119 | CHECK_ERROR(aclrtResetDeviceForce(deviceId)); | 120 | CHECK_ERROR(aclrtResetDeviceForce(deviceId)); |
| 120 | CHECK_ERROR(aclFinalize()); | 121 | CHECK_ERROR(aclFinalize()); |
| 121 | - INFO_LOG("Run the device_normal sample successfully. " | 122 | + INFO_LOG( |
| 122 | - "please make sure dump data has been in path: %s", dumpPath); | 123 | + "Run the device_normal sample successfully. " |
| 124 | + "please make sure dump data has been in path: %s", | ||
| 125 | + dumpPath); | ||
| 123 | return 0; | 126 | return 0; |
| 124 | } | 127 | } |
| @@ -24,71 +24,73 @@ | |||
| 24 | using namespace std; | 24 | using namespace std; |
| 25 | 25 | ||
| 26 | namespace { | 26 | namespace { |
| 27 | - std::mutex gCallbackMutex; | 27 | +std::mutex gCallbackMutex; |
| 28 | - std::map<std::string, int32_t> gChunkCountByFile; | 28 | +std::map<std::string, int32_t> gChunkCountByFile; |
| 29 | - int32_t gTotalChunkCount = 0; | 29 | +int32_t gTotalChunkCount = 0; |
| 30 | - int64_t gTotalBytes = 0; | 30 | +int64_t gTotalBytes = 0; |
| 31 | 31 | ||
| 32 | - void LogDumpPath(acldumpType dumpType, const char *fallbackPath) | 32 | +void LogDumpPath(acldumpType dumpType, const char* fallbackPath) |
| 33 | - { | 33 | +{ |
| 34 | - const char *dumpPath = acldumpGetPath(dumpType); | 34 | + const char* dumpPath = acldumpGetPath(dumpType); |
| 35 | - if (dumpPath != nullptr) { | 35 | + if (dumpPath != nullptr) { |
| 36 | - INFO_LOG("acldumpGetPath returned dump path: %s", dumpPath); | 36 | + INFO_LOG("acldumpGetPath returned dump path: %s", dumpPath); |
| 37 | - return; | 37 | + return; |
| 38 | - } | ||
| 39 | - WARN_LOG("acldumpGetPath returned null, fallback dump path is %s", fallbackPath); | ||
| 40 | } | 38 | } |
| 39 | + WARN_LOG("acldumpGetPath returned null, fallback dump path is %s", fallbackPath); | ||
| 40 | +} | ||
| 41 | 41 | ||
| 42 | - std::string BuildChunkPreview(const acldumpChunk *data) | 42 | +std::string BuildChunkPreview(const acldumpChunk* data) |
| 43 | - { | 43 | +{ |
| 44 | - const uint32_t previewLen = std::min<uint32_t>(data->bufLen, 16U); | 44 | + const uint32_t previewLen = std::min<uint32_t>(data->bufLen, 16U); |
| 45 | - std::ostringstream preview; | 45 | + std::ostringstream preview; |
| 46 | - preview << std::hex << std::setfill('0'); | 46 | + preview << std::hex << std::setfill('0'); |
| 47 | - for (uint32_t i = 0; i < previewLen; ++i) { | 47 | + for (uint32_t i = 0; i < previewLen; ++i) { |
| 48 | - if (i != 0) { | 48 | + if (i != 0) { |
| 49 | - preview << " "; | 49 | + preview << " "; |
| 50 | - } | ||
| 51 | - preview << std::setw(2) << static_cast<uint32_t>(data->dataBuf[i]); | ||
| 52 | } | 50 | } |
| 53 | - if (data->bufLen > previewLen) { | 51 | + preview << std::setw(2) << static_cast<uint32_t>(data->dataBuf[i]); |
| 54 | - preview << " ..."; | ||
| 55 | - } | ||
| 56 | - return preview.str(); | ||
| 57 | } | 52 | } |
| 58 | - | 53 | + if (data->bufLen > previewLen) { |
| 59 | - void PrintCallbackSummary() | 54 | + preview << " ..."; |
| 60 | - { | ||
| 61 | - std::lock_guard<std::mutex> lock(gCallbackMutex); | ||
| 62 | - INFO_LOG("Dump callback summary: total chunks=%d, total bytes=%ld, total files=%d", | ||
| 63 | - gTotalChunkCount, gTotalBytes, static_cast<int32_t>(gChunkCountByFile.size())); | ||
| 64 | - for (const auto &entry : gChunkCountByFile) { | ||
| 65 | - INFO_LOG(" file=%s, chunks=%d", entry.first.c_str(), entry.second); | ||
| 66 | - } | ||
| 67 | } | 55 | } |
| 56 | + return preview.str(); | ||
| 57 | +} | ||
| 68 | 58 | ||
| 69 | - int32_t DumpTensorCallback(const acldumpChunk *data, int32_t len) | 59 | +void PrintCallbackSummary() |
| 70 | - { | 60 | +{ |
| 71 | - if (data == nullptr) { | 61 | + std::lock_guard<std::mutex> lock(gCallbackMutex); |
| 72 | - ERROR_LOG("Callback data is null!"); | 62 | + INFO_LOG( |
| 73 | - return -1; | 63 | + "Dump callback summary: total chunks=%d, total bytes=%ld, total files=%d", gTotalChunkCount, gTotalBytes, |
| 74 | - } | 64 | + static_cast<int32_t>(gChunkCountByFile.size())); |
| 75 | - if ((sizeof(acldumpChunk) + data->bufLen) != len) { | 65 | + for (const auto& entry : gChunkCountByFile) { |
| 76 | - ERROR_LOG("Callback data is invalid. bufLen: %d, callback len: %d", data->bufLen, len); | 66 | + INFO_LOG(" file=%s, chunks=%d", entry.first.c_str(), entry.second); |
| 77 | - return -1; | ||
| 78 | - } | ||
| 79 | - const std::string preview = BuildChunkPreview(data); | ||
| 80 | - { | ||
| 81 | - std::lock_guard<std::mutex> lock(gCallbackMutex); | ||
| 82 | - ++gTotalChunkCount; | ||
| 83 | - gTotalBytes += data->bufLen; | ||
| 84 | - ++gChunkCountByFile[data->fileName]; | ||
| 85 | - } | ||
| 86 | - INFO_LOG("Receive dump tensor data success. file=%s, bufLen=%u, isLastChunk=%u, offset=%ld, flag=%d, preview=%s", | ||
| 87 | - data->fileName, data->bufLen, data->isLastChunk, data->offset, data->flag, preview.c_str()); | ||
| 88 | - return 0; | ||
| 89 | } | 67 | } |
| 90 | } | 68 | } |
| 91 | 69 | ||
| 70 | +int32_t DumpTensorCallback(const acldumpChunk* data, int32_t len) | ||
| 71 | +{ | ||
| 72 | + if (data == nullptr) { | ||
| 73 | + ERROR_LOG("Callback data is null!"); | ||
| 74 | + return -1; | ||
| 75 | + } | ||
| 76 | + if ((sizeof(acldumpChunk) + data->bufLen) != len) { | ||
| 77 | + ERROR_LOG("Callback data is invalid. bufLen: %d, callback len: %d", data->bufLen, len); | ||
| 78 | + return -1; | ||
| 79 | + } | ||
| 80 | + const std::string preview = BuildChunkPreview(data); | ||
| 81 | + { | ||
| 82 | + std::lock_guard<std::mutex> lock(gCallbackMutex); | ||
| 83 | + ++gTotalChunkCount; | ||
| 84 | + gTotalBytes += data->bufLen; | ||
| 85 | + ++gChunkCountByFile[data->fileName]; | ||
| 86 | + } | ||
| 87 | + INFO_LOG( | ||
| 88 | + "Receive dump tensor data success. file=%s, bufLen=%u, isLastChunk=%u, offset=%ld, flag=%d, preview=%s", | ||
| 89 | + data->fileName, data->bufLen, data->isLastChunk, data->offset, data->flag, preview.c_str()); | ||
| 90 | + return 0; | ||
| 91 | +} | ||
| 92 | +} // namespace | ||
| 93 | + | ||
| 92 | int main() | 94 | int main() |
| 93 | { | 95 | { |
| 94 | // The device id | 96 | // The device id |
| @@ -107,13 +109,13 @@ int main() | |||
| 107 | std::vector<int64_t> selfShape{4, 2}; | 109 | std::vector<int64_t> selfShape{4, 2}; |
| 108 | std::vector<int64_t> otherShape{4, 2}; | 110 | std::vector<int64_t> otherShape{4, 2}; |
| 109 | std::vector<int64_t> outShape{4, 2}; | 111 | std::vector<int64_t> outShape{4, 2}; |
| 110 | - void *selfDeviceAddr = nullptr; | 112 | + void* selfDeviceAddr = nullptr; |
| 111 | - void *otherDeviceAddr = nullptr; | 113 | + void* otherDeviceAddr = nullptr; |
| 112 | - void *outDeviceAddr = nullptr; | 114 | + void* outDeviceAddr = nullptr; |
| 113 | - aclTensor *self = nullptr; | 115 | + aclTensor* self = nullptr; |
| 114 | - aclTensor *out = nullptr; | 116 | + aclTensor* out = nullptr; |
| 115 | - aclTensor *other = nullptr; | 117 | + aclTensor* other = nullptr; |
| 116 | - aclScalar *alpha = nullptr; | 118 | + aclScalar* alpha = nullptr; |
| 117 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 119 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 118 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 120 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 119 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; | 121 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; |
| @@ -134,11 +136,11 @@ int main() | |||
| 134 | INFO_LOG("Get workspace size..."); | 136 | INFO_LOG("Get workspace size..."); |
| 135 | // 3. Call the CANN operator library API(Custom Implementation) | 137 | // 3. Call the CANN operator library API(Custom Implementation) |
| 136 | uint64_t workspaceSize = 0; | 138 | uint64_t workspaceSize = 0; |
| 137 | - aclOpExecutor *executor; | 139 | + aclOpExecutor* executor; |
| 138 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); | 140 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); |
| 139 | 141 | ||
| 140 | // Allocate device memory based on the calculation results. | 142 | // Allocate device memory based on the calculation results. |
| 141 | - void *workspaceAddr = nullptr; | 143 | + void* workspaceAddr = nullptr; |
| 142 | if (workspaceSize > 0lu) { | 144 | if (workspaceSize > 0lu) { |
| 143 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 145 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 144 | } | 146 | } |
| @@ -152,11 +154,12 @@ int main() | |||
| 152 | // 5. Obtain the execution result of the operator and copy the result from the device memory to the host | 154 | // 5. Obtain the execution result of the operator and copy the result from the device memory to the host |
| 153 | auto size = adump::GetShapeSize(outShape); | 155 | auto size = adump::GetShapeSize(outShape); |
| 154 | std::vector<float> resultData(size, 0); | 156 | std::vector<float> resultData(size, 0); |
| 155 | - CHECK_ERROR(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 157 | + CHECK_ERROR(aclrtMemcpy( |
| 156 | - size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | 158 | + resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 159 | + ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 157 | 160 | ||
| 158 | for (int64_t i = 0; i < size; i++) { | 161 | for (int64_t i = 0; i < size; i++) { |
| 159 | - INFO_LOG("result[%ld] is: %f ", i, resultData[i]); | 162 | + INFO_LOG("result[%ld] is: %f ", i, resultData[i]); |
| 160 | } | 163 | } |
| 161 | PrintCallbackSummary(); | 164 | PrintCallbackSummary(); |
| 162 | 165 | ||
| @@ -168,7 +171,7 @@ int main() | |||
| 168 | CHECK_ERROR(aclrtFree(outDeviceAddr)); | 171 | CHECK_ERROR(aclrtFree(outDeviceAddr)); |
| 169 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); | 172 | CHECK_ERROR(aclrtFree(otherDeviceAddr)); |
| 170 | if (workspaceSize > 0lu) { | 173 | if (workspaceSize > 0lu) { |
| 171 | - CHECK_ERROR(aclrtFree(workspaceAddr)); | 174 | + CHECK_ERROR(aclrtFree(workspaceAddr)); |
| 172 | } | 175 | } |
| 173 | // 7. AsendCL Destroy. | 176 | // 7. AsendCL Destroy. |
| 174 | // Unregister callback for dump tensor data. | 177 | // Unregister callback for dump tensor data. |
| @@ -20,14 +20,14 @@ using namespace std; | |||
| 20 | int main() | 20 | int main() |
| 21 | { | 21 | { |
| 22 | const int32_t deviceId = 0; | 22 | const int32_t deviceId = 0; |
| 23 | - const char *dumpCfgPath = "./acl.json"; | 23 | + const char* dumpCfgPath = "./acl.json"; |
| 24 | aclrtStream stream = nullptr; | 24 | aclrtStream stream = nullptr; |
| 25 | 25 | ||
| 26 | CHECK_ERROR(adump::InitRuntime(deviceId, &stream)); | 26 | CHECK_ERROR(adump::InitRuntime(deviceId, &stream)); |
| 27 | CHECK_ERROR(aclmdlInitDump()); | 27 | CHECK_ERROR(aclmdlInitDump()); |
| 28 | CHECK_ERROR(aclmdlSetDump(dumpCfgPath)); | 28 | CHECK_ERROR(aclmdlSetDump(dumpCfgPath)); |
| 29 | 29 | ||
| 30 | - const char *dumpPath = acldumpGetPath(DATA_DUMP); | 30 | + const char* dumpPath = acldumpGetPath(DATA_DUMP); |
| 31 | if (dumpPath != nullptr) { | 31 | if (dumpPath != nullptr) { |
| 32 | INFO_LOG("Configured model dump path is: %s", dumpPath); | 32 | INFO_LOG("Configured model dump path is: %s", dumpPath); |
| 33 | } else { | 33 | } else { |
| @@ -40,14 +40,14 @@ int main() | |||
| 40 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 40 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 41 | float alphaValue = 1.0f; | 41 | float alphaValue = 1.0f; |
| 42 | 42 | ||
| 43 | - void *selfDeviceAddr = nullptr; | 43 | + void* selfDeviceAddr = nullptr; |
| 44 | - void *otherDeviceAddr = nullptr; | 44 | + void* otherDeviceAddr = nullptr; |
| 45 | - void *outDeviceAddr = nullptr; | 45 | + void* outDeviceAddr = nullptr; |
| 46 | - void *workspaceAddr = nullptr; | 46 | + void* workspaceAddr = nullptr; |
| 47 | - aclTensor *self = nullptr; | 47 | + aclTensor* self = nullptr; |
| 48 | - aclTensor *other = nullptr; | 48 | + aclTensor* other = nullptr; |
| 49 | - aclTensor *out = nullptr; | 49 | + aclTensor* out = nullptr; |
| 50 | - aclScalar *alpha = nullptr; | 50 | + aclScalar* alpha = nullptr; |
| 51 | 51 | ||
| 52 | CHECK_ERROR(adump::CreateAclTensor(selfHostData, shape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self)); | 52 | CHECK_ERROR(adump::CreateAclTensor(selfHostData, shape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self)); |
| 53 | CHECK_ERROR(adump::CreateAclTensor(otherHostData, shape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other)); | 53 | CHECK_ERROR(adump::CreateAclTensor(otherHostData, shape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other)); |
| @@ -60,7 +60,7 @@ int main() | |||
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | uint64_t workspaceSize = 0; | 62 | uint64_t workspaceSize = 0; |
| 63 | - aclOpExecutor *executor = nullptr; | 63 | + aclOpExecutor* executor = nullptr; |
| 64 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); | 64 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); |
| 65 | if (workspaceSize > 0UL) { | 65 | if (workspaceSize > 0UL) { |
| 66 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 66 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| @@ -69,8 +69,9 @@ int main() | |||
| 69 | CHECK_ERROR(aclrtSynchronizeStream(stream)); | 69 | CHECK_ERROR(aclrtSynchronizeStream(stream)); |
| 70 | 70 | ||
| 71 | std::vector<float> resultData(adump::GetShapeSize(shape), 0.0f); | 71 | std::vector<float> resultData(adump::GetShapeSize(shape), 0.0f); |
| 72 | - CHECK_ERROR(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(float), outDeviceAddr, | 72 | + CHECK_ERROR(aclrtMemcpy( |
| 73 | - resultData.size() * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | 73 | + resultData.data(), resultData.size() * sizeof(float), outDeviceAddr, resultData.size() * sizeof(float), |
| 74 | + ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 74 | for (int64_t i = 0; i < static_cast<int64_t>(resultData.size()); ++i) { | 75 | for (int64_t i = 0; i < static_cast<int64_t>(resultData.size()); ++i) { |
| 75 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); | 76 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); |
| 76 | } | 77 | } |
| @@ -20,15 +20,16 @@ using namespace std; | |||
| 20 | 20 | ||
| 21 | namespace { | 21 | namespace { |
| 22 | // 将 shape 填充进 acldumpTensorInfo 的定长数组,并设置有效维度数 | 22 | // 将 shape 填充进 acldumpTensorInfo 的定长数组,并设置有效维度数 |
| 23 | -void FillTensorInfo(acldumpTensorInfo &info, acldumpTensorType type, void *deviceAddr, | 23 | +void FillTensorInfo( |
| 24 | - const std::vector<int64_t> &shape, int32_t dataType, size_t byteSize) | 24 | + acldumpTensorInfo& info, acldumpTensorType type, void* deviceAddr, const std::vector<int64_t>& shape, |
| 25 | + int32_t dataType, size_t byteSize) | ||
| 25 | { | 26 | { |
| 26 | (void)memset_s(&info, sizeof(info), 0, sizeof(info)); | 27 | (void)memset_s(&info, sizeof(info), 0, sizeof(info)); |
| 27 | info.type = type; | 28 | info.type = type; |
| 28 | info.tensorSize = byteSize; | 29 | info.tensorSize = byteSize; |
| 29 | info.format = aclFormat::ACL_FORMAT_ND; | 30 | info.format = aclFormat::ACL_FORMAT_ND; |
| 30 | info.dataType = dataType; | 31 | info.dataType = dataType; |
| 31 | - info.tensorAddr = static_cast<int64_t *>(deviceAddr); | 32 | + info.tensorAddr = static_cast<int64_t*>(deviceAddr); |
| 32 | // 本接口直接以 tensorAddr 作为 Device 上的数据地址读取数据,因此地址类型为裸地址、数据位于 Device | 33 | // 本接口直接以 tensorAddr 作为 Device 上的数据地址读取数据,因此地址类型为裸地址、数据位于 Device |
| 33 | info.addrType = ACL_DUMP_ADDR_RAW; | 34 | info.addrType = ACL_DUMP_ADDR_RAW; |
| 34 | info.placement = ACL_DUMP_PLACEMENT_DEVICE; | 35 | info.placement = ACL_DUMP_PLACEMENT_DEVICE; |
| @@ -40,14 +41,14 @@ void FillTensorInfo(acldumpTensorInfo &info, acldumpTensorType type, void *devic | |||
| 40 | info.originShape[i] = static_cast<uint64_t>(shape[i]); | 41 | info.originShape[i] = static_cast<uint64_t>(shape[i]); |
| 41 | } | 42 | } |
| 42 | } | 43 | } |
| 43 | -} // namespace | 44 | +} // namespace |
| 44 | 45 | ||
| 45 | int main() | 46 | int main() |
| 46 | { | 47 | { |
| 47 | // The device id | 48 | // The device id |
| 48 | int32_t deviceId = 0; | 49 | int32_t deviceId = 0; |
| 49 | // The dump configuration path (enables aic_err_brief_dump exception dump) | 50 | // The dump configuration path (enables aic_err_brief_dump exception dump) |
| 50 | - const char *dumpCfgPath = "./acl.json"; | 51 | + const char* dumpCfgPath = "./acl.json"; |
| 51 | aclrtStream stream = nullptr; | 52 | aclrtStream stream = nullptr; |
| 52 | 53 | ||
| 53 | // 1. AscendCL Init(加载开启 Exception Dump 的配置) | 54 | // 1. AscendCL Init(加载开启 Exception Dump 的配置) |
| @@ -69,13 +70,13 @@ int main() | |||
| 69 | std::vector<int64_t> selfShape{4, 2}; | 70 | std::vector<int64_t> selfShape{4, 2}; |
| 70 | std::vector<int64_t> otherShape{4, 2}; | 71 | std::vector<int64_t> otherShape{4, 2}; |
| 71 | std::vector<int64_t> outShape{4, 2}; | 72 | std::vector<int64_t> outShape{4, 2}; |
| 72 | - void *selfDeviceAddr = nullptr; | 73 | + void* selfDeviceAddr = nullptr; |
| 73 | - void *otherDeviceAddr = nullptr; | 74 | + void* otherDeviceAddr = nullptr; |
| 74 | - void *outDeviceAddr = nullptr; | 75 | + void* outDeviceAddr = nullptr; |
| 75 | - aclTensor *self = nullptr; | 76 | + aclTensor* self = nullptr; |
| 76 | - aclTensor *other = nullptr; | 77 | + aclTensor* other = nullptr; |
| 77 | - aclScalar *alpha = nullptr; | 78 | + aclScalar* alpha = nullptr; |
| 78 | - aclTensor *out = nullptr; | 79 | + aclTensor* out = nullptr; |
| 79 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 80 | std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 80 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; | 81 | std::vector<float> otherHostData = {1, 1, 1, 2, 2, 2, 3, 3}; |
| 81 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 82 | std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| @@ -96,9 +97,9 @@ int main() | |||
| 96 | 97 | ||
| 97 | // 4. Call the CANN operator library API(Custom Implementation) —— 跑通一个正常算子 | 98 | // 4. Call the CANN operator library API(Custom Implementation) —— 跑通一个正常算子 |
| 98 | uint64_t workspaceSize = 0; | 99 | uint64_t workspaceSize = 0; |
| 99 | - aclOpExecutor *executor; | 100 | + aclOpExecutor* executor; |
| 100 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); | 101 | CHECK_ERROR(aclnnAddGetWorkspaceSize(self, other, alpha, out, &workspaceSize, &executor)); |
| 101 | - void *workspaceAddr = nullptr; | 102 | + void* workspaceAddr = nullptr; |
| 102 | if (workspaceSize > 0lu) { | 103 | if (workspaceSize > 0lu) { |
| 103 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); | 104 | CHECK_ERROR(aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 104 | } | 105 | } |
| @@ -108,14 +109,17 @@ int main() | |||
| 108 | // 5. 组合调用 acldumpSaveExceptionInfo,将算子的 tensor 主动落盘到 Exception Dump 路径下 | 109 | // 5. 组合调用 acldumpSaveExceptionInfo,将算子的 tensor 主动落盘到 Exception Dump 路径下 |
| 109 | std::vector<acldumpTensorInfo> tensors(3); | 110 | std::vector<acldumpTensorInfo> tensors(3); |
| 110 | const size_t elemBytes = static_cast<size_t>(adump::GetShapeSize(selfShape)) * sizeof(float); | 111 | const size_t elemBytes = static_cast<size_t>(adump::GetShapeSize(selfShape)) * sizeof(float); |
| 111 | - FillTensorInfo(tensors[0], ACL_DUMP_TENSOR_INPUT, selfDeviceAddr, selfShape, | 112 | + FillTensorInfo( |
| 112 | - static_cast<int32_t>(aclDataType::ACL_FLOAT), elemBytes); | 113 | + tensors[0], ACL_DUMP_TENSOR_INPUT, selfDeviceAddr, selfShape, static_cast<int32_t>(aclDataType::ACL_FLOAT), |
| 113 | - FillTensorInfo(tensors[1], ACL_DUMP_TENSOR_INPUT, otherDeviceAddr, otherShape, | 114 | + elemBytes); |
| 114 | - static_cast<int32_t>(aclDataType::ACL_FLOAT), elemBytes); | 115 | + FillTensorInfo( |
| 115 | - FillTensorInfo(tensors[2], ACL_DUMP_TENSOR_OUTPUT, outDeviceAddr, outShape, | 116 | + tensors[1], ACL_DUMP_TENSOR_INPUT, otherDeviceAddr, otherShape, static_cast<int32_t>(aclDataType::ACL_FLOAT), |
| 116 | - static_cast<int32_t>(aclDataType::ACL_FLOAT), elemBytes); | 117 | + elemBytes); |
| 118 | + FillTensorInfo( | ||
| 119 | + tensors[2], ACL_DUMP_TENSOR_OUTPUT, outDeviceAddr, outShape, static_cast<int32_t>(aclDataType::ACL_FLOAT), | ||
| 120 | + elemBytes); | ||
| 117 | 121 | ||
| 118 | - const char *userTag = "component=demo;stage=forward;note=save_exception_info_example"; | 122 | + const char* userTag = "component=demo;stage=forward;note=save_exception_info_example"; |
| 119 | aclError saveRet = acldumpSaveExceptionInfo("save_exception_info", userTag, tensors.data(), tensors.size()); | 123 | aclError saveRet = acldumpSaveExceptionInfo("save_exception_info", userTag, tensors.data(), tensors.size()); |
| 120 | if (saveRet == ACL_SUCCESS) { | 124 | if (saveRet == ACL_SUCCESS) { |
| 121 | INFO_LOG("acldumpSaveExceptionInfo success, data has been saved under exception dump path: %s", excDumpPath); | 125 | INFO_LOG("acldumpSaveExceptionInfo success, data has been saved under exception dump path: %s", excDumpPath); |
| @@ -126,8 +130,9 @@ int main() | |||
| 126 | // 5. 校验算子结果 | 130 | // 5. 校验算子结果 |
| 127 | auto size = adump::GetShapeSize(outShape); | 131 | auto size = adump::GetShapeSize(outShape); |
| 128 | std::vector<float> resultData(size, 0); | 132 | std::vector<float> resultData(size, 0); |
| 129 | - CHECK_ERROR(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 133 | + CHECK_ERROR(aclrtMemcpy( |
| 130 | - size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | 134 | + resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 135 | + ACL_MEMCPY_DEVICE_TO_HOST)); | ||
| 131 | for (int64_t i = 0; i < size; i++) { | 136 | for (int64_t i = 0; i < size; i++) { |
| 132 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); | 137 | INFO_LOG("result[%ld] is: %f", i, resultData[i]); |
| 133 | } | 138 | } |
| @@ -16,12 +16,12 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace adump { | 18 | namespace adump { |
| 19 | -inline int InitRuntime(int32_t deviceId, aclrtStream *stream, const char *configPath = nullptr) | 19 | +inline int InitRuntime(int32_t deviceId, aclrtStream* stream, const char* configPath = nullptr) |
| 20 | { | 20 | { |
| 21 | return static_cast<int>(runtime::InitRuntimeAndCreateStream(deviceId, stream, configPath)); | 21 | return static_cast<int>(runtime::InitRuntimeAndCreateStream(deviceId, stream, configPath)); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | -inline int64_t GetShapeSize(const std::vector<int64_t> &shape) | 24 | +inline int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 25 | { | 25 | { |
| 26 | int64_t shapeSize = 1; | 26 | int64_t shapeSize = 1; |
| 27 | for (const int64_t dim : shape) { | 27 | for (const int64_t dim : shape) { |
| @@ -31,8 +31,9 @@ inline int64_t GetShapeSize(const std::vector<int64_t> &shape) | |||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | template <typename T> | 33 | template <typename T> |
| 34 | -int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr, | 34 | +int CreateAclTensor( |
| 35 | - aclDataType dataType, aclTensor **tensor) | 35 | + const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, |
| 36 | + aclTensor** tensor) | ||
| 36 | { | 37 | { |
| 37 | const size_t size = static_cast<size_t>(GetShapeSize(shape)) * sizeof(T); | 38 | const size_t size = static_cast<size_t>(GetShapeSize(shape)) * sizeof(T); |
| 38 | aclError ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 39 | aclError ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| @@ -49,18 +50,19 @@ int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> & | |||
| 49 | strides[static_cast<size_t>(i)] = shape[static_cast<size_t>(i + 1)] * strides[static_cast<size_t>(i + 1)]; | 50 | strides[static_cast<size_t>(i)] = shape[static_cast<size_t>(i + 1)] * strides[static_cast<size_t>(i + 1)]; |
| 50 | } | 51 | } |
| 51 | 52 | ||
| 52 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 53 | + *tensor = aclCreateTensor( |
| 53 | - shape.data(), shape.size(), *deviceAddr); | 54 | + shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), |
| 55 | + *deviceAddr); | ||
| 54 | if (*tensor == nullptr) { | 56 | if (*tensor == nullptr) { |
| 55 | return -1; | 57 | return -1; |
| 56 | } | 58 | } |
| 57 | return ACL_SUCCESS; | 59 | return ACL_SUCCESS; |
| 58 | } | 60 | } |
| 59 | 61 | ||
| 60 | -inline void DestroyTensorResources(aclTensor *self, aclTensor *other, aclScalar *alpha, aclTensor *out) | 62 | +inline void DestroyTensorResources(aclTensor* self, aclTensor* other, aclScalar* alpha, aclTensor* out) |
| 61 | { | 63 | { |
| 62 | - aclTensor *tensorList[] = {self, other, out}; | 64 | + aclTensor* tensorList[] = {self, other, out}; |
| 63 | - for (aclTensor *tensor : tensorList) { | 65 | + for (aclTensor* tensor : tensorList) { |
| 64 | if (tensor != nullptr) { | 66 | if (tensor != nullptr) { |
| 65 | aclDestroyTensor(tensor); | 67 | aclDestroyTensor(tensor); |
| 66 | } | 68 | } |
| @@ -15,10 +15,11 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | -int main(int argc, char *argv[]) { | 18 | +int main(int argc, char* argv[]) |
| 19 | +{ | ||
| 19 | INFO_LOG("-------- Start --------"); | 20 | INFO_LOG("-------- Start --------"); |
| 20 | // 初始化 | 21 | // 初始化 |
| 21 | - uint32_t deviceIdList[1] = {0}; // 根据实际环境的DeviceID配置 | 22 | + uint32_t deviceIdList[1] = {0}; // 根据实际环境的DeviceID配置 |
| 22 | aclrtStream stream = nullptr; | 23 | aclrtStream stream = nullptr; |
| 23 | // 申请运行时资源 | 24 | // 申请运行时资源 |
| 24 | aclInit(nullptr); | 25 | aclInit(nullptr); |
| @@ -32,14 +33,16 @@ int main(int argc, char *argv[]) { | |||
| 32 | INFO_LOG("profiling init done"); | 33 | INFO_LOG("profiling init done"); |
| 33 | // 进行profiling配置 | 34 | // 进行profiling配置 |
| 34 | // 创建配置结构体 | 35 | // 创建配置结构体 |
| 35 | - aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, | 36 | + aclprofConfig* config = aclprofCreateConfig( |
| 36 | - nullptr, ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_AICORE_METRICS | ACL_PROF_AICPU | ACL_PROF_L2CACHE | ACL_PROF_HCCL_TRACE | ACL_PROF_MSPROFTX | ACL_PROF_RUNTIME_API); | 37 | + deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr, |
| 38 | + ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_AICORE_METRICS | ACL_PROF_AICPU | ACL_PROF_L2CACHE | | ||
| 39 | + ACL_PROF_HCCL_TRACE | ACL_PROF_MSPROFTX | ACL_PROF_RUNTIME_API); | ||
| 37 | std::string memFreq = "15"; | 40 | std::string memFreq = "15"; |
| 38 | aclError ret = aclprofSetConfig(ACL_PROF_SYS_HARDWARE_MEM_FREQ, memFreq.c_str(), memFreq.length()); | 41 | aclError ret = aclprofSetConfig(ACL_PROF_SYS_HARDWARE_MEM_FREQ, memFreq.c_str(), memFreq.length()); |
| 39 | if (ret != ACL_SUCCESS) { | 42 | if (ret != ACL_SUCCESS) { |
| 40 | ERROR_LOG("profiling set config error: %d", ret); | 43 | ERROR_LOG("profiling set config error: %d", ret); |
| 41 | return -1; | 44 | return -1; |
| 42 | - } | 45 | + } |
| 43 | INFO_LOG("profiling set config done"); | 46 | INFO_LOG("profiling set config done"); |
| 44 | 47 | ||
| 45 | aclprofStart(config); | 48 | aclprofStart(config); |
| @@ -16,9 +16,9 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace { | 18 | namespace { |
| 19 | -void *CreateStampWithMessage(const std::string &message) | 19 | +void* CreateStampWithMessage(const std::string& message) |
| 20 | { | 20 | { |
| 21 | - void *stamp = aclprofCreateStamp(); | 21 | + void* stamp = aclprofCreateStamp(); |
| 22 | if (stamp == nullptr) { | 22 | if (stamp == nullptr) { |
| 23 | ERROR_LOG("aclprofCreateStamp failed for message %s", message.c_str()); | 23 | ERROR_LOG("aclprofCreateStamp failed for message %s", message.c_str()); |
| 24 | return nullptr; | 24 | return nullptr; |
| @@ -26,15 +26,15 @@ void *CreateStampWithMessage(const std::string &message) | |||
| 26 | 26 | ||
| 27 | aclError ret = aclprofSetStampTraceMessage(stamp, message.c_str(), static_cast<uint32_t>(message.length())); | 27 | aclError ret = aclprofSetStampTraceMessage(stamp, message.c_str(), static_cast<uint32_t>(message.length())); |
| 28 | if (ret != ACL_SUCCESS) { | 28 | if (ret != ACL_SUCCESS) { |
| 29 | - ERROR_LOG("aclprofSetStampTraceMessage(%s) failed with error code %d", | 29 | + ERROR_LOG( |
| 30 | - message.c_str(), static_cast<int32_t>(ret)); | 30 | + "aclprofSetStampTraceMessage(%s) failed with error code %d", message.c_str(), static_cast<int32_t>(ret)); |
| 31 | aclprofDestroyStamp(stamp); | 31 | aclprofDestroyStamp(stamp); |
| 32 | return nullptr; | 32 | return nullptr; |
| 33 | } | 33 | } |
| 34 | return stamp; | 34 | return stamp; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | -bool CheckPtr(const void *ptr, const char *name) | 37 | +bool CheckPtr(const void* ptr, const char* name) |
| 38 | { | 38 | { |
| 39 | if (ptr == nullptr) { | 39 | if (ptr == nullptr) { |
| 40 | ERROR_LOG("%s is nullptr", name); | 40 | ERROR_LOG("%s is nullptr", name); |
| @@ -44,14 +44,14 @@ bool CheckPtr(const void *ptr, const char *name) | |||
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | struct StampSet { | 46 | struct StampSet { |
| 47 | - void *loadMark = nullptr; | 47 | + void* loadMark = nullptr; |
| 48 | - void *outerRangePush = nullptr; | 48 | + void* outerRangePush = nullptr; |
| 49 | - void *innerRangePush = nullptr; | 49 | + void* innerRangePush = nullptr; |
| 50 | - void *preprocessRange = nullptr; | 50 | + void* preprocessRange = nullptr; |
| 51 | - void *execMark = nullptr; | 51 | + void* execMark = nullptr; |
| 52 | }; | 52 | }; |
| 53 | 53 | ||
| 54 | -int32_t CreateStampSet(StampSet *stampSet) | 54 | +int32_t CreateStampSet(StampSet* stampSet) |
| 55 | { | 55 | { |
| 56 | stampSet->loadMark = CreateStampWithMessage("model_load_mark"); | 56 | stampSet->loadMark = CreateStampWithMessage("model_load_mark"); |
| 57 | stampSet->outerRangePush = CreateStampWithMessage("forward_pass_push"); | 57 | stampSet->outerRangePush = CreateStampWithMessage("forward_pass_push"); |
| @@ -77,7 +77,7 @@ int32_t CreateStampSet(StampSet *stampSet) | |||
| 77 | return 0; | 77 | return 0; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | -void DestroyStampSet(const StampSet &stampSet) | 80 | +void DestroyStampSet(const StampSet& stampSet) |
| 81 | { | 81 | { |
| 82 | aclprofDestroyStamp(stampSet.loadMark); | 82 | aclprofDestroyStamp(stampSet.loadMark); |
| 83 | aclprofDestroyStamp(stampSet.outerRangePush); | 83 | aclprofDestroyStamp(stampSet.outerRangePush); |
| @@ -88,7 +88,7 @@ void DestroyStampSet(const StampSet &stampSet) | |||
| 88 | 88 | ||
| 89 | int32_t RunTraceFlow(aclrtStream stream) | 89 | int32_t RunTraceFlow(aclrtStream stream) |
| 90 | { | 90 | { |
| 91 | - aclprofStepInfo *stepInfo = aclprofCreateStepInfo(); | 91 | + aclprofStepInfo* stepInfo = aclprofCreateStepInfo(); |
| 92 | if (!CheckPtr(stepInfo, "aclprofCreateStepInfo")) { | 92 | if (!CheckPtr(stepInfo, "aclprofCreateStepInfo")) { |
| 93 | return -1; | 93 | return -1; |
| 94 | } | 94 | } |
| @@ -141,11 +141,8 @@ int32_t RunMsproftxSample() | |||
| 141 | const std::string aclProfPath = "./output"; | 141 | const std::string aclProfPath = "./output"; |
| 142 | CHECK_ERROR(aclprofInit(aclProfPath.c_str(), aclProfPath.length())); | 142 | CHECK_ERROR(aclprofInit(aclProfPath.c_str(), aclProfPath.length())); |
| 143 | 143 | ||
| 144 | - aclprofConfig *config = aclprofCreateConfig( | 144 | + aclprofConfig* config = aclprofCreateConfig( |
| 145 | - deviceIdList, | 145 | + deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr, |
| 146 | - 1, | ||
| 147 | - ACL_AICORE_ARITHMETIC_UTILIZATION, | ||
| 148 | - nullptr, | ||
| 149 | ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_MSPROFTX); | 146 | ACL_PROF_ACL_API | ACL_PROF_TASK_TIME | ACL_PROF_MSPROFTX); |
| 150 | if (!CheckPtr(config, "aclprofCreateConfig")) { | 147 | if (!CheckPtr(config, "aclprofCreateConfig")) { |
| 151 | return -1; | 148 | return -1; |
| @@ -170,7 +167,7 @@ int32_t RunMsproftxSample() | |||
| 170 | } | 167 | } |
| 171 | } // namespace | 168 | } // namespace |
| 172 | 169 | ||
| 173 | -int main(int argc, char *argv[]) | 170 | +int main(int argc, char* argv[]) |
| 174 | { | 171 | { |
| 175 | (void)argc; | 172 | (void)argc; |
| 176 | (void)argv; | 173 | (void)argv; |
| @@ -20,7 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | namespace { | 21 | namespace { |
| 22 | // 自定义函数,实现从用户内存中读取订阅数据的函数 | 22 | // 自定义函数,实现从用户内存中读取订阅数据的函数 |
| 23 | -void getModelInfo(void *data, uint32_t len) | 23 | +void getModelInfo(void* data, uint32_t len) |
| 24 | { | 24 | { |
| 25 | uint32_t opNumber = 0; | 25 | uint32_t opNumber = 0; |
| 26 | uint32_t dataLen = 0; | 26 | uint32_t dataLen = 0; |
| @@ -51,7 +51,7 @@ void getModelInfo(void *data, uint32_t len) | |||
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | // 自定义函数,实现从管道中读取数据到用户内存的函数 | 53 | // 自定义函数,实现从管道中读取数据到用户内存的函数 |
| 54 | -void *profDataRead(void *fd) | 54 | +void* profDataRead(void* fd) |
| 55 | { | 55 | { |
| 56 | // 设置每次从管道中读取的算子信息个数 | 56 | // 设置每次从管道中读取的算子信息个数 |
| 57 | uint64_t N = 10; | 57 | uint64_t N = 10; |
| @@ -60,8 +60,9 @@ void *profDataRead(void *fd) | |||
| 60 | aclprofGetOpDescSize(&bufferSize); | 60 | aclprofGetOpDescSize(&bufferSize); |
| 61 | // 计算存储算子信息的内存的大小,并且申请内存 | 61 | // 计算存储算子信息的内存的大小,并且申请内存 |
| 62 | uint64_t readbufLen = bufferSize * N; | 62 | uint64_t readbufLen = bufferSize * N; |
| 63 | - char *readbuf = new char[readbufLen]; | 63 | + char* readbuf = new char[readbufLen]; |
| 64 | - // 从管道中读取数据到申请的内存中,读取到的实际数据大小dataLen可能小于bufferSize * N,如果管道中没有数据,默认会阻塞直到读取到数据为止 | 64 | + // 从管道中读取数据到申请的内存中,读取到的实际数据大小dataLen可能小于bufferSize * |
| 65 | + // N,如果管道中没有数据,默认会阻塞直到读取到数据为止 | ||
| 65 | ssize_t dataLen = read(*(int*)fd, readbuf, readbufLen); | 66 | ssize_t dataLen = read(*(int*)fd, readbuf, readbufLen); |
| 66 | // 读取数据到readbuf成功 | 67 | // 读取数据到readbuf成功 |
| 67 | while (dataLen > 0) { | 68 | while (dataLen > 0) { |
| @@ -73,18 +74,18 @@ void *profDataRead(void *fd) | |||
| 73 | memset_s(readbuf, readbufLen, 0, readbufLen); | 74 | memset_s(readbuf, readbufLen, 0, readbufLen); |
| 74 | dataLen = read(*(int*)fd, readbuf, readbufLen); | 75 | dataLen = read(*(int*)fd, readbuf, readbufLen); |
| 75 | } | 76 | } |
| 76 | - delete []readbuf; | 77 | + delete[] readbuf; |
| 77 | return nullptr; | 78 | return nullptr; |
| 78 | } | 79 | } |
| 79 | -} | 80 | +} // namespace |
| 80 | 81 | ||
| 81 | -int main(int argc, char *argv[]) | 82 | +int main(int argc, char* argv[]) |
| 82 | { | 83 | { |
| 83 | INFO_LOG("-------- Start -------- \n"); | 84 | INFO_LOG("-------- Start -------- \n"); |
| 84 | // 调用aclInit初始化 | 85 | // 调用aclInit初始化 |
| 85 | aclInit(nullptr); | 86 | aclInit(nullptr); |
| 86 | // 申请运行管理资源 | 87 | // 申请运行管理资源 |
| 87 | - uint32_t deviceIdList[1] = {0}; // 根据实际环境的DeviceID配置 | 88 | + uint32_t deviceIdList[1] = {0}; // 根据实际环境的DeviceID配置 |
| 88 | aclrtSetDevice(deviceIdList[0]); | 89 | aclrtSetDevice(deviceIdList[0]); |
| 89 | aclrtStream stream = nullptr; | 90 | aclrtStream stream = nullptr; |
| 90 | aclrtCreateStream(&stream); | 91 | aclrtCreateStream(&stream); |
| @@ -101,7 +102,7 @@ int main(int argc, char *argv[]) | |||
| 101 | // 读管道指针指向subFd[0],写管道指针指向subFd[1] | 102 | // 读管道指针指向subFd[0],写管道指针指向subFd[1] |
| 102 | CHECK_ERROR(pipe(subFd)); | 103 | CHECK_ERROR(pipe(subFd)); |
| 103 | // 创建模型订阅的配置并且进行模型订阅 | 104 | // 创建模型订阅的配置并且进行模型订阅 |
| 104 | - aclprofSubscribeConfig *config = aclprofCreateSubscribeConfig(1, ACL_AICORE_NONE, &subFd[1]); | 105 | + aclprofSubscribeConfig* config = aclprofCreateSubscribeConfig(1, ACL_AICORE_NONE, &subFd[1]); |
| 105 | // 模型订阅需要传入模型的modelId | 106 | // 模型订阅需要传入模型的modelId |
| 106 | aclprofModelSubscribe(modelId, config); | 107 | aclprofModelSubscribe(modelId, config); |
| 107 | 108 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /** | 1 | /** |
| 2 | -* Copyright (c) 2026 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| 3 | * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 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"). | 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. | 5 | * Please refer to the License for details. You may not use this file except in compliance with the License. |
| @@ -34,4 +34,4 @@ int DeInit(int32_t deviceId, aclrtContext* context, aclrtStream* stream) | |||
| 34 | return 0; | 34 | return 0; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | -# endif // UTIL_ACL_H_ | 37 | +#endif // UTIL_ACL_H_ |
| @@ -1,5 +1,5 @@ | |||
| 1 | /** | 1 | /** |
| 2 | -* Copyright (c) 2026 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| 3 | * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 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"). | 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. | 5 | * Please refer to the License for details. You may not use this file except in compliance with the License. |
| @@ -1,5 +1,5 @@ | |||
| 1 | /** | 1 | /** |
| 2 | -* Copyright (c) 2026 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| 3 | * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 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"). | 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. | 5 | * Please refer to the License for details. You may not use this file except in compliance with the License. |
| @@ -37,8 +37,9 @@ int64_t GetShapeSize(const std::vector<int64_t>& shape) | |||
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | template <typename T> | 39 | template <typename T> |
| 40 | -int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 40 | +int CreateAclTensor( |
| 41 | - aclDataType dataType, aclTensor** tensor) | 41 | + const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, |
| 42 | + aclTensor** tensor) | ||
| 42 | { | 43 | { |
| 43 | auto size = GetShapeSize(shape) * sizeof(T); | 44 | auto size = GetShapeSize(shape) * sizeof(T); |
| 44 | ACL_CALL(aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST)); | 45 | ACL_CALL(aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| @@ -50,8 +51,9 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& | |||
| 50 | } | 51 | } |
| 51 | 52 | ||
| 52 | // 调用aclCreateTensor接口创建aclTensor | 53 | // 调用aclCreateTensor接口创建aclTensor |
| 53 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 54 | + *tensor = aclCreateTensor( |
| 54 | - shape.data(), shape.size(), *deviceAddr); | 55 | + shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), |
| 56 | + *deviceAddr); | ||
| 55 | return 0; | 57 | return 0; |
| 56 | } | 58 | } |
| 57 | 59 | ||
| @@ -96,9 +98,12 @@ int DoAclAdd(aclrtContext context, aclrtStream stream) | |||
| 96 | ACL_CALL(aclrtSynchronizeStream(stream)); | 98 | ACL_CALL(aclrtSynchronizeStream(stream)); |
| 97 | auto size = GetShapeSize(outShape); | 99 | auto size = GetShapeSize(outShape); |
| 98 | std::vector<float> resultData(size, 0); | 100 | std::vector<float> resultData(size, 0); |
| 99 | - ACL_CALL(aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 101 | + ACL_CALL(aclrtMemcpy( |
| 100 | - size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST)); | 102 | + resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 101 | - for (int64_t i = 0; i < size; i++) { LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); } | 103 | + ACL_MEMCPY_DEVICE_TO_HOST)); |
| 104 | + for (int64_t i = 0; i < size; i++) { | ||
| 105 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | ||
| 106 | + } | ||
| 102 | aclDestroyTensor(self); | 107 | aclDestroyTensor(self); |
| 103 | aclDestroyTensor(other); | 108 | aclDestroyTensor(other); |
| 104 | aclDestroyScalar(alpha); | 109 | aclDestroyScalar(alpha); |
| @@ -129,18 +134,12 @@ static int TestMstxWithDomain() | |||
| 129 | return ACL_SUCCESS; | 134 | return ACL_SUCCESS; |
| 130 | } | 135 | } |
| 131 | 136 | ||
| 132 | -void MstxDomainInit() | 137 | +void MstxDomainInit() { domainRange = mstxDomainCreateA(g_domainRangeName.c_str()); } |
| 133 | -{ | ||
| 134 | - domainRange = mstxDomainCreateA(g_domainRangeName.c_str()); | ||
| 135 | -} | ||
| 136 | 138 | ||
| 137 | -void MstxDomainDeInit() | 139 | +void MstxDomainDeInit() { mstxDomainDestroy(domainRange); } |
| 138 | -{ | 140 | +} // namespace |
| 139 | - mstxDomainDestroy(domainRange); | ||
| 140 | -} | ||
| 141 | -} | ||
| 142 | 141 | ||
| 143 | -int main(int argc, const char **argv) | 142 | +int main(int argc, const char** argv) |
| 144 | { | 143 | { |
| 145 | int32_t deviceId = 0; | 144 | int32_t deviceId = 0; |
| 146 | int ret = Init(deviceId, &context, &stream); | 145 | int ret = Init(deviceId, &context, &stream); |
| @@ -15,22 +15,22 @@ | |||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | namespace { | 17 | namespace { |
| 18 | - constexpr int32_t TOTAL_LENGTH = 8 * 2048; // total length of data | 18 | +constexpr int32_t TOTAL_LENGTH = 8 * 2048; // total length of data |
| 19 | - constexpr int32_t USE_CORE_NUM = 8; // num of core used | 19 | +constexpr int32_t USE_CORE_NUM = 8; // num of core used |
| 20 | - constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM; // length computed of each core | 20 | +constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM; // length computed of each core |
| 21 | - constexpr int32_t TILE_NUM = 8; // split data into 8 tiles for each core | 21 | +constexpr int32_t TILE_NUM = 8; // split data into 8 tiles for each core |
| 22 | - constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue | 22 | +constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue |
| 23 | - constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // separate to 2 parts, due to double buffer | 23 | +constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; // separate to 2 parts, due to double buffer |
| 24 | -} | 24 | +} // namespace |
| 25 | 25 | ||
| 26 | class KernelAdd { | 26 | class KernelAdd { |
| 27 | public: | 27 | public: |
| 28 | __aicore__ inline KernelAdd() {} | 28 | __aicore__ inline KernelAdd() {} |
| 29 | __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z) | 29 | __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z) |
| 30 | { | 30 | { |
| 31 | - xGm.SetGlobalBuffer((__gm__ half *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); | 31 | + xGm.SetGlobalBuffer((__gm__ half*)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); |
| 32 | - yGm.SetGlobalBuffer((__gm__ half *)y + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); | 32 | + yGm.SetGlobalBuffer((__gm__ half*)y + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); |
| 33 | - zGm.SetGlobalBuffer((__gm__ half *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); | 33 | + zGm.SetGlobalBuffer((__gm__ half*)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH); |
| 34 | pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half)); | 34 | pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(half)); |
| 35 | pipe.InitBuffer(inQueueY, BUFFER_NUM, TILE_LENGTH * sizeof(half)); | 35 | pipe.InitBuffer(inQueueY, BUFFER_NUM, TILE_LENGTH * sizeof(half)); |
| 36 | pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(half)); | 36 | pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(half)); |
| @@ -8,7 +8,7 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | - /* | 11 | +/* |
| 12 | * Below is the operator used for computing the addition of two matrices. | 12 | * Below is the operator used for computing the addition of two matrices. |
| 13 | * This operator allows users to configure the tiling information with placeholders. | 13 | * This operator allows users to configure the tiling information with placeholders. |
| 14 | */ | 14 | */ |
| @@ -16,9 +16,9 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | namespace { | 18 | namespace { |
| 19 | - constexpr int32_t USE_CORE_NUM = 8; // num of core used | 19 | +constexpr int32_t USE_CORE_NUM = 8; // num of core used |
| 20 | - constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue | 20 | +constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue |
| 21 | -} | 21 | +} // namespace |
| 22 | 22 | ||
| 23 | class KernelAdd { | 23 | class KernelAdd { |
| 24 | public: | 24 | public: |
| @@ -31,9 +31,9 @@ public: | |||
| 31 | return; | 31 | return; |
| 32 | } | 32 | } |
| 33 | this->tileLength = this->blockLength / tileNum / BUFFER_NUM; | 33 | this->tileLength = this->blockLength / tileNum / BUFFER_NUM; |
| 34 | - xGm.SetGlobalBuffer((__gm__ half *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); | 34 | + xGm.SetGlobalBuffer((__gm__ half*)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); |
| 35 | - yGm.SetGlobalBuffer((__gm__ half *)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); | 35 | + yGm.SetGlobalBuffer((__gm__ half*)y + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); |
| 36 | - zGm.SetGlobalBuffer((__gm__ half *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); | 36 | + zGm.SetGlobalBuffer((__gm__ half*)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength); |
| 37 | pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(half)); | 37 | pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(half)); |
| 38 | pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(half)); | 38 | pipe.InitBuffer(inQueueY, BUFFER_NUM, this->tileLength * sizeof(half)); |
| 39 | pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(half)); | 39 | pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(half)); |
| @@ -88,7 +88,8 @@ private: | |||
| 88 | uint32_t tileLength; | 88 | uint32_t tileLength; |
| 89 | }; | 89 | }; |
| 90 | 90 | ||
| 91 | -extern "C" __global__ __aicore__ void add_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z, __gm__ int32_t* tilingLength, __gm__ int32_t* tilingNum) | 91 | +extern "C" __global__ __aicore__ void add_custom( |
| 92 | + GM_ADDR x, GM_ADDR y, GM_ADDR z, __gm__ int32_t* tilingLength, __gm__ int32_t* tilingNum) | ||
| 92 | { | 93 | { |
| 93 | KernelAdd op; | 94 | KernelAdd op; |
| 94 | uint32_t tileLength = *tilingLength; | 95 | uint32_t tileLength = *tilingLength; |
| @@ -1,29 +1,24 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - | 13 | + |
| 14 | -// 简单核函数,让x自增1 | 14 | +// 简单核函数,让x自增1 |
| 15 | -extern "C" __global__ __aicore__ void EasyOPf(__gm__ uint32_t* x) | 15 | +extern "C" __global__ __aicore__ void EasyOPf(__gm__ uint32_t* x) |
| 16 | -{ | 16 | +{ |
| 17 | - int32_t idx = block_idx; | 17 | + int32_t idx = block_idx; |
| 18 | - x[idx] += 1; | 18 | + x[idx] += 1; |
| 19 | -#if __NPU_ARCH__ == 3510 | 19 | +#if __NPU_ARCH__ == 3510 |
| 20 | - dcci(reinterpret_cast<__gm__ int64_t*>(x), | 20 | + dcci(reinterpret_cast<__gm__ int64_t*>(x), cache_line_t::ENTIRE_DATA_CACHE, dcci_dst_t::CACHELINE_OUT); |
| 21 | - cache_line_t::ENTIRE_DATA_CACHE, | 21 | +#endif |
| 22 | - dcci_dst_t::CACHELINE_OUT); | ||
| 23 | - | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -void EasyOP(uint32_t blockDim, void *stream, uint32_t* x) | ||
| 27 | -{ | ||
| 28 | - EasyOPf<<<blockDim, nullptr, stream>>>(x); | ||
| 29 | } | 22 | } |
| 23 | + | ||
| 24 | +void EasyOP(uint32_t blockDim, void* stream, uint32_t* x) { EasyOPf<<<blockDim, nullptr, stream>>>(x); } | ||
| @@ -1,23 +1,17 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - | 13 | + |
| 14 | -// 核函数,会报错,用于遇错即停的情况 | 14 | +// 核函数,会报错,用于遇错即停的情况 |
| 15 | -extern "C" __global__ __aicore__ void ErrorOPf() | 15 | +extern "C" __global__ __aicore__ void ErrorOPf() { asm("trap"); } |
| 16 | -{ | 16 | + |
| 17 | - asm("trap"); | 17 | +void ErrorOP(uint32_t blockDim, void* stream) { ErrorOPf<<<blockDim, nullptr, stream>>>(); } |
| 18 | -} | ||
| 19 | - | ||
| 20 | -void ErrorOP(uint32_t blockDim, void *stream) | ||
| 21 | -{ | ||
| 22 | - ErrorOPf<<<blockDim, nullptr, stream>>>(); | ||
| 23 | -} | ||
| @@ -1,114 +1,114 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - | 13 | + |
| 14 | -using namespace AscendC; | 14 | +using namespace AscendC; |
| 15 | - | 15 | + |
| 16 | -namespace { | 16 | +namespace { |
| 17 | - constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue | 17 | +constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue |
| 18 | - | 18 | + |
| 19 | - class KernelAdd { | 19 | +class KernelAdd { |
| 20 | - public: | 20 | +public: |
| 21 | - __aicore__ inline KernelAdd() {} | 21 | + __aicore__ inline KernelAdd() {} |
| 22 | - | 22 | + |
| 23 | - __aicore__ inline void Init(__gm__ float* srcA, __gm__ float* srcB, __gm__ float* dst, uint32_t totalSize) | 23 | + __aicore__ inline void Init(__gm__ float* srcA, __gm__ float* srcB, __gm__ float* dst, uint32_t totalSize) |
| 24 | - { | 24 | + { |
| 25 | - blockIdx = GetBlockIdx(); | 25 | + blockIdx = GetBlockIdx(); |
| 26 | - blockDim = GetBlockNum(); | 26 | + blockDim = GetBlockNum(); |
| 27 | - elementsPerBlock = totalSize / blockDim; | 27 | + elementsPerBlock = totalSize / blockDim; |
| 28 | - tileLength = elementsPerBlock / TILE_NUM / BUFFER_NUM; | 28 | + tileLength = elementsPerBlock / TILE_NUM / BUFFER_NUM; |
| 29 | - startIdx = blockIdx * elementsPerBlock; | 29 | + startIdx = blockIdx * elementsPerBlock; |
| 30 | - | 30 | + |
| 31 | - // Set the global buffer | 31 | + // Set the global buffer |
| 32 | - srcAGlobal.SetGlobalBuffer(srcA + startIdx, elementsPerBlock); | 32 | + srcAGlobal.SetGlobalBuffer(srcA + startIdx, elementsPerBlock); |
| 33 | - srcBGlobal.SetGlobalBuffer(srcB + startIdx, elementsPerBlock); | 33 | + srcBGlobal.SetGlobalBuffer(srcB + startIdx, elementsPerBlock); |
| 34 | - dstGlobal.SetGlobalBuffer(dst + startIdx, elementsPerBlock); | 34 | + dstGlobal.SetGlobalBuffer(dst + startIdx, elementsPerBlock); |
| 35 | - | 35 | + |
| 36 | - // Initialize the pipe buffer | 36 | + // Initialize the pipe buffer |
| 37 | - pipe.InitBuffer(queueInA, BUFFER_NUM, tileLength * sizeof(float)); | 37 | + pipe.InitBuffer(queueInA, BUFFER_NUM, tileLength * sizeof(float)); |
| 38 | - pipe.InitBuffer(queueInB, BUFFER_NUM, tileLength * sizeof(float)); | 38 | + pipe.InitBuffer(queueInB, BUFFER_NUM, tileLength * sizeof(float)); |
| 39 | - pipe.InitBuffer(queueOut, BUFFER_NUM, tileLength * sizeof(float)); | 39 | + pipe.InitBuffer(queueOut, BUFFER_NUM, tileLength * sizeof(float)); |
| 40 | - } | 40 | + } |
| 41 | - | 41 | + |
| 42 | - __aicore__ inline void Process() | 42 | + __aicore__ inline void Process() |
| 43 | - { | 43 | + { |
| 44 | - int32_t loopCount = TILE_NUM * BUFFER_NUM; | 44 | + int32_t loopCount = TILE_NUM * BUFFER_NUM; |
| 45 | - for (int32_t i = 0; i < loopCount; i++) { | 45 | + for (int32_t i = 0; i < loopCount; i++) { |
| 46 | - CopyIn(i); | 46 | + CopyIn(i); |
| 47 | - Compute(i); | 47 | + Compute(i); |
| 48 | - CopyOut(i); | 48 | + CopyOut(i); |
| 49 | - } | 49 | + } |
| 50 | - } | 50 | + } |
| 51 | - | 51 | + |
| 52 | - private: | 52 | +private: |
| 53 | - __aicore__ inline void CopyIn(int32_t progress) | 53 | + __aicore__ inline void CopyIn(int32_t progress) |
| 54 | - { | 54 | + { |
| 55 | - LocalTensor<float> localA = queueInA.AllocTensor<float>(); | 55 | + LocalTensor<float> localA = queueInA.AllocTensor<float>(); |
| 56 | - LocalTensor<float> localB = queueInB.AllocTensor<float>(); | 56 | + LocalTensor<float> localB = queueInB.AllocTensor<float>(); |
| 57 | - | 57 | + |
| 58 | - DataCopy(localA, srcAGlobal[progress * tileLength], tileLength); | 58 | + DataCopy(localA, srcAGlobal[progress * tileLength], tileLength); |
| 59 | - DataCopy(localB, srcBGlobal[progress * tileLength], tileLength); | 59 | + DataCopy(localB, srcBGlobal[progress * tileLength], tileLength); |
| 60 | - | 60 | + |
| 61 | - queueInA.EnQue(localA); | 61 | + queueInA.EnQue(localA); |
| 62 | - queueInB.EnQue(localB); | 62 | + queueInB.EnQue(localB); |
| 63 | - } | 63 | + } |
| 64 | - | 64 | + |
| 65 | - __aicore__ inline void Compute(int32_t progress) | 65 | + __aicore__ inline void Compute(int32_t progress) |
| 66 | - { | 66 | + { |
| 67 | - LocalTensor<float> localA = queueInA.DeQue<float>(); | 67 | + LocalTensor<float> localA = queueInA.DeQue<float>(); |
| 68 | - LocalTensor<float> localB = queueInB.DeQue<float>(); | 68 | + LocalTensor<float> localB = queueInB.DeQue<float>(); |
| 69 | - LocalTensor<float> localOut = queueOut.AllocTensor<float>(); | 69 | + LocalTensor<float> localOut = queueOut.AllocTensor<float>(); |
| 70 | - | 70 | + |
| 71 | - Add(localOut, localA, localB, tileLength); | 71 | + Add(localOut, localA, localB, tileLength); |
| 72 | - | 72 | + |
| 73 | - queueOut.EnQue<float>(localOut); | 73 | + queueOut.EnQue<float>(localOut); |
| 74 | - queueInA.FreeTensor(localA); | 74 | + queueInA.FreeTensor(localA); |
| 75 | - queueInB.FreeTensor(localB); | 75 | + queueInB.FreeTensor(localB); |
| 76 | - } | 76 | + } |
| 77 | - | 77 | + |
| 78 | - __aicore__ inline void CopyOut(int32_t progress) | 78 | + __aicore__ inline void CopyOut(int32_t progress) |
| 79 | - { | 79 | + { |
| 80 | - LocalTensor<float> localOut = queueOut.DeQue<float>(); | 80 | + LocalTensor<float> localOut = queueOut.DeQue<float>(); |
| 81 | - DataCopy(dstGlobal[progress * tileLength], localOut, tileLength); | 81 | + DataCopy(dstGlobal[progress * tileLength], localOut, tileLength); |
| 82 | - queueOut.FreeTensor(localOut); | 82 | + queueOut.FreeTensor(localOut); |
| 83 | - } | 83 | + } |
| 84 | - | 84 | + |
| 85 | - private: | 85 | +private: |
| 86 | - TPipe pipe; | 86 | + TPipe pipe; |
| 87 | - TQue<QuePosition::VECIN, BUFFER_NUM> queueInA; | 87 | + TQue<QuePosition::VECIN, BUFFER_NUM> queueInA; |
| 88 | - TQue<QuePosition::VECIN, BUFFER_NUM> queueInB; | 88 | + TQue<QuePosition::VECIN, BUFFER_NUM> queueInB; |
| 89 | - TQue<QuePosition::VECOUT, BUFFER_NUM> queueOut; | 89 | + TQue<QuePosition::VECOUT, BUFFER_NUM> queueOut; |
| 90 | - GlobalTensor<float> srcAGlobal; | 90 | + GlobalTensor<float> srcAGlobal; |
| 91 | - GlobalTensor<float> srcBGlobal; | 91 | + GlobalTensor<float> srcBGlobal; |
| 92 | - GlobalTensor<float> dstGlobal; | 92 | + GlobalTensor<float> dstGlobal; |
| 93 | - | 93 | + |
| 94 | - uint32_t blockIdx; | 94 | + uint32_t blockIdx; |
| 95 | - uint32_t blockDim; | 95 | + uint32_t blockDim; |
| 96 | - uint32_t elementsPerBlock; | 96 | + uint32_t elementsPerBlock; |
| 97 | - uint32_t startIdx; | 97 | + uint32_t startIdx; |
| 98 | - uint32_t tileLength; | 98 | + uint32_t tileLength; |
| 99 | - static constexpr int32_t TILE_NUM = 8; // split data into 8 tiles for each core | 99 | + static constexpr int32_t TILE_NUM = 8; // split data into 8 tiles for each core |
| 100 | - }; | 100 | +}; |
| 101 | -} // namespace | 101 | +} // namespace |
| 102 | - | 102 | + |
| 103 | -extern "C" __global__ __aicore__ void AddKernel(__gm__ float* srcA, __gm__ float* srcB, __gm__ float* dst, | 103 | +extern "C" __global__ __aicore__ void AddKernel( |
| 104 | - uint32_t totalSize) | 104 | + __gm__ float* srcA, __gm__ float* srcB, __gm__ float* dst, uint32_t totalSize) |
| 105 | -{ | 105 | +{ |
| 106 | - KernelAdd op; | 106 | + KernelAdd op; |
| 107 | - op.Init(srcA, srcB, dst, totalSize); | 107 | + op.Init(srcA, srcB, dst, totalSize); |
| 108 | - op.Process(); | 108 | + op.Process(); |
| 109 | -} | 109 | +} |
| 110 | - | 110 | + |
| 111 | -void AddDo(uint32_t blockDim, void* stream, float* srcA, float* srcB, float* dst, uint32_t totalSize) | 111 | +void AddDo(uint32_t blockDim, void* stream, float* srcA, float* srcB, float* dst, uint32_t totalSize) |
| 112 | -{ | 112 | +{ |
| 113 | - AddKernel<<<blockDim, nullptr, stream>>>(srcA, srcB, dst, totalSize); | 113 | + AddKernel<<<blockDim, nullptr, stream>>>(srcA, srcB, dst, totalSize); |
| 114 | } | 114 | } |
| @@ -8,20 +8,13 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | - /* | 11 | +/* |
| 12 | * Using the Ascend kernel to print the data | 12 | * Using the Ascend kernel to print the data |
| 13 | */ | 13 | */ |
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | -extern "C" __global__ __aicore__ void KernelPrint() | 18 | +extern "C" __global__ __aicore__ void KernelPrint() { AscendC::printf("Hello World\n"); } |
| 19 | -{ | ||
| 20 | - AscendC::printf("Hello World\n"); | ||
| 21 | -} | ||
| 22 | - | ||
| 23 | -void PrintDo(uint32_t blockDim, void *stream) | ||
| 24 | -{ | ||
| 25 | - KernelPrint<<<blockDim, nullptr, stream>>>(); | ||
| 26 | -} | ||
| 27 | 19 | ||
| 20 | +void PrintDo(uint32_t blockDim, void* stream) { KernelPrint<<<blockDim, nullptr, stream>>>(); } | ||
| @@ -1,42 +1,37 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | - | 13 | + |
| 14 | -// 核函数,长时间任务 | 14 | +// 核函数,长时间任务 |
| 15 | -extern "C" __global__ __aicore__ void LongOPf(__gm__ uint32_t* x) | 15 | +extern "C" __global__ __aicore__ void LongOPf(__gm__ uint32_t* x) |
| 16 | -{ | 16 | +{ |
| 17 | - float temp = 0.0f; | 17 | + float temp = 0.0f; |
| 18 | - int idx = block_idx; | 18 | + int idx = block_idx; |
| 19 | - //复杂的浮点运算来模拟长时间任务 | 19 | + // 复杂的浮点运算来模拟长时间任务 |
| 20 | - for (int i = 0; i < 10000; i++) { | 20 | + for (int i = 0; i < 10000; i++) { |
| 21 | - temp += 0.00002f; | 21 | + temp += 0.00002f; |
| 22 | - temp *= 0.02f; | 22 | + temp *= 0.02f; |
| 23 | - for (int j = 0; j < 25000; j++){ | 23 | + for (int j = 0; j < 25000; j++) { |
| 24 | - temp = temp * temp - 0.000002f; | 24 | + temp = temp * temp - 0.000002f; |
| 25 | - temp = temp + (temp * 0.05f); | 25 | + temp = temp + (temp * 0.05f); |
| 26 | - temp = temp * temp + 0.00001f; | 26 | + temp = temp * temp + 0.00001f; |
| 27 | - temp = temp > 1.0f ? 0.0f : temp; | 27 | + temp = temp > 1.0f ? 0.0f : temp; |
| 28 | - } | 28 | + } |
| 29 | - temp -= 0.00003f * temp; | 29 | + temp -= 0.00003f * temp; |
| 30 | - } | 30 | + } |
| 31 | - x[idx] += temp > 0.0f ? 1:0; | 31 | + x[idx] += temp > 0.0f ? 1 : 0; |
| 32 | -#if __NPU_ARCH__ == 3510 | 32 | +#if __NPU_ARCH__ == 3510 |
| 33 | - dcci(reinterpret_cast<__gm__ int64_t*>(x), | 33 | + dcci(reinterpret_cast<__gm__ int64_t*>(x), cache_line_t::ENTIRE_DATA_CACHE, dcci_dst_t::CACHELINE_OUT); |
| 34 | - cache_line_t::ENTIRE_DATA_CACHE, | 34 | +#endif |
| 35 | - dcci_dst_t::CACHELINE_OUT); | ||
| 36 | - | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -void LongOP(uint32_t blockDim, void *stream, uint32_t* x) | ||
| 40 | -{ | ||
| 41 | - LongOPf<<<blockDim, nullptr, stream>>>(x); | ||
| 42 | } | 35 | } |
| 36 | + | ||
| 37 | +void LongOP(uint32_t blockDim, void* stream, uint32_t* x) { LongOPf<<<blockDim, nullptr, stream>>>(x); } | ||
| @@ -1,28 +1,23 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | -// 核函数,自乘2 | 13 | +// 核函数,自乘2 |
| 14 | -extern "C" __global__ __aicore__ void ShortOPf(__gm__ uint32_t* x) | 14 | +extern "C" __global__ __aicore__ void ShortOPf(__gm__ uint32_t* x) |
| 15 | -{ | 15 | +{ |
| 16 | - int idx = block_idx; | 16 | + int idx = block_idx; |
| 17 | - x[idx] *= 2; | 17 | + x[idx] *= 2; |
| 18 | -#if __NPU_ARCH__ == 3510 | 18 | +#if __NPU_ARCH__ == 3510 |
| 19 | - dcci(reinterpret_cast<__gm__ int64_t*>(x), | 19 | + dcci(reinterpret_cast<__gm__ int64_t*>(x), cache_line_t::ENTIRE_DATA_CACHE, dcci_dst_t::CACHELINE_OUT); |
| 20 | - cache_line_t::ENTIRE_DATA_CACHE, | 20 | +#endif |
| 21 | - dcci_dst_t::CACHELINE_OUT); | ||
| 22 | - | ||
| 23 | -} | ||
| 24 | - | ||
| 25 | -void ShortOP(uint32_t blockDim, void *stream, uint32_t* x) | ||
| 26 | -{ | ||
| 27 | - ShortOPf<<<blockDim, nullptr, stream>>>(x); | ||
| 28 | } | 21 | } |
| 22 | + | ||
| 23 | +void ShortOP(uint32_t blockDim, void* stream, uint32_t* x) { ShortOPf<<<blockDim, nullptr, stream>>>(x); } | ||
| @@ -8,7 +8,7 @@ | |||
| 8 | * See LICENSE in the root of the software repository for the full text of the License. | 8 | * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | - /* | 11 | +/* |
| 12 | * Using the Ascend kernel to write the value at devPtrA and read the value at devPtrB | 12 | * Using the Ascend kernel to write the value at devPtrA and read the value at devPtrB |
| 13 | */ | 13 | */ |
| 14 | 14 | ||
| @@ -22,7 +22,7 @@ extern "C" __global__ __aicore__ void DeviceWrite(__gm__ int* devPtr, int value) | |||
| 22 | AscendC::printf("Source data: %d\n", value); | 22 | AscendC::printf("Source data: %d\n", value); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | -void WriteDo(uint32_t blockDim, void *stream, int* devPtr, int value) | 25 | +void WriteDo(uint32_t blockDim, void* stream, int* devPtr, int value) |
| 26 | { | 26 | { |
| 27 | DeviceWrite<<<blockDim, nullptr, stream>>>(devPtr, value); | 27 | DeviceWrite<<<blockDim, nullptr, stream>>>(devPtr, value); |
| 28 | } | 28 | } |
| @@ -34,7 +34,4 @@ extern "C" __global__ __aicore__ void DeviceRead(__gm__ int* devPtr) | |||
| 34 | AscendC::printf("Destination data: %d\n", value); | 34 | AscendC::printf("Destination data: %d\n", value); |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | -void ReadDo(uint32_t blockDim, void *stream, int* devPtr) | 37 | +void ReadDo(uint32_t blockDim, void* stream, int* devPtr) { DeviceRead<<<blockDim, nullptr, stream>>>(devPtr); } |
| 38 | -{ | ||
| 39 | - DeviceRead<<<blockDim, nullptr, stream>>>(devPtr); | ||
| 40 | -} | ||
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | namespace runtime { | 16 | namespace runtime { |
| 17 | -inline aclError InitRuntimeAndCreateStream(int32_t deviceId, aclrtStream *stream, const char *configPath = nullptr) | 17 | +inline aclError InitRuntimeAndCreateStream(int32_t deviceId, aclrtStream* stream, const char* configPath = nullptr) |
| 18 | { | 18 | { |
| 19 | aclError ret = aclInit(configPath); | 19 | aclError ret = aclInit(configPath); |
| 20 | if (ret != ACL_SUCCESS) { | 20 | if (ret != ACL_SUCCESS) { |
| @@ -16,7 +16,7 @@ int main() | |||
| 16 | { | 16 | { |
| 17 | // aclrtGetSocName returns the same SOC_VERSION string required by sample builds. | 17 | // aclrtGetSocName returns the same SOC_VERSION string required by sample builds. |
| 18 | // It does not require aclInit, so this helper stays small and fast for shell use. | 18 | // It does not require aclInit, so this helper stays small and fast for shell use. |
| 19 | - const char *socName = aclrtGetSocName(); | 19 | + const char* socName = aclrtGetSocName(); |
| 20 | if ((socName == nullptr) || (socName[0] == '\0')) { | 20 | if ((socName == nullptr) || (socName[0] == '\0')) { |
| 21 | std::cerr << "[ERROR]: Failed to get SOC_VERSION by aclrtGetSocName." << std::endl; | 21 | std::cerr << "[ERROR]: Failed to get SOC_VERSION by aclrtGetSocName." << std::endl; |
| 22 | return 1; | 22 | return 1; |
| @@ -16,21 +16,21 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | -#define CHECK_ERROR(call) \ | 19 | +#define CHECK_ERROR(call) \ |
| 20 | - do { \ | 20 | + do { \ |
| 21 | - aclError __ret = (call); \ | 21 | + aclError __ret = (call); \ |
| 22 | - if (__ret != ACL_SUCCESS) { \ | 22 | + if (__ret != ACL_SUCCESS) { \ |
| 23 | ERROR_LOG("Operation failed: %s returned error code %d", | 23 | ERROR_LOG("Operation failed: %s returned error code %d", |
| 24 | - return -1; \ | 24 | + return -1; \ |
| 25 | - } \ | 25 | + } \ |
| 26 | } while (0) | 26 | } while (0) |
| 27 | - | 27 | + |
| 28 | -#define CHECK_ERROR_WITHOUT_RETURN(call) \ | 28 | +#define CHECK_ERROR_WITHOUT_RETURN(call) \ |
| 29 | - do { \ | 29 | + do { \ |
| 30 | - aclError __ret = (call); \ | 30 | + aclError __ret = (call); \ |
| 31 | - if (__ret != ACL_SUCCESS) { \ | 31 | + if (__ret != ACL_SUCCESS) { \ |
| 32 | ERROR_LOG("Operation failed: %s returned error code %d", | 32 | ERROR_LOG("Operation failed: %s returned error code %d", |
| 33 | - } \ | 33 | + } \ |
| 34 | } while (0) | 34 | } while (0) |
| 35 | 35 | ||
| 36 | 36 | ||
| @@ -18,4 +18,4 @@ | |||
| 18 | */ | 18 | */ |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | -#endif // AICPU_ENGINE_STRUCT_COMPAT_H | 21 | +#endif // AICPU_ENGINE_STRUCT_COMPAT_H |
| @@ -1,207 +1,200 @@ | |||
| 1 | -/** | 1 | +/** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 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, | 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. | 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. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | - | 10 | + |
| 11 | -#include <cstring> | 11 | +#include <cstring> |
| 12 | -#include <vector> | 12 | +#include <vector> |
| 13 | -#include <string> | 13 | +#include <string> |
| 14 | -#include "acl_stub.h" | 14 | +#include "acl_stub.h" |
| 15 | - | 15 | + |
| 16 | -namespace { | 16 | +namespace { |
| 17 | - std::string STAGES_STR = "[TEST][TEST]"; | 17 | +std::string STAGES_STR = "[TEST][TEST]"; |
| 18 | -} | 18 | +} |
| 19 | - | 19 | + |
| 20 | -uint32_t aclStub::InitializePlatformInfo() | 20 | +uint32_t aclStub::InitializePlatformInfo() { return 0; } |
| 21 | -{ | 21 | + |
| 22 | - return 0; | 22 | +uint32_t aclStub::GetPlatformInfos( |
| 23 | -} | 23 | + const std::string SoCVersion, fe::PlatFormInfos& platformInfo, fe::OptionalInfos& optionalInfo) |
| 24 | - | 24 | +{ |
| 25 | -uint32_t aclStub::GetPlatformInfos( | 25 | + return 0; |
| 26 | - const std::string SoCVersion, fe::PlatFormInfos &platformInfo, fe::OptionalInfos &optionalInfo) | 26 | +} |
| 27 | -{ | 27 | + |
| 28 | - return 0; | 28 | +uint32_t aclStub::InitRuntimePlatformInfos(const std::string& SoCVersion) { return 0; } |
| 29 | -} | 29 | + |
| 30 | - | 30 | +uint32_t aclStub::GetRuntimePlatformInfosByDevice(const uint32_t& device_id, fe::PlatFormInfos& platform_infos) |
| 31 | -uint32_t aclStub::InitRuntimePlatformInfos(const std::string &SoCVersion) | 31 | +{ |
| 32 | -{ | 32 | + return 0; |
| 33 | - return 0; | 33 | +} |
| 34 | -} | 34 | + |
| 35 | - | 35 | +bool aclStub::GetPlatformResWithLock(const std::string& label, std::map<std::string, std::string>& res) { return true; } |
| 36 | -uint32_t aclStub::GetRuntimePlatformInfosByDevice(const uint32_t &device_id, fe::PlatFormInfos &platform_infos) { | 36 | + |
| 37 | - return 0; | 37 | +bool aclStub::GetPlatformResWithLock(const string& label, const string& key, string& val) { return true; } |
| 38 | -} | 38 | + |
| 39 | - | 39 | +uint32_t aclStub::UpdateRuntimePlatformInfosByDevice(const uint32_t& device_id, fe::PlatFormInfos& platform_infos) |
| 40 | -bool aclStub::GetPlatformResWithLock(const std::string &label, std::map<std::string, std::string> &res) { | 40 | +{ |
| 41 | - return true; | 41 | + return 0; |
| 42 | -} | 42 | +} |
| 43 | - | 43 | + |
| 44 | -bool aclStub::GetPlatformResWithLock(const string &label, const string &key, string &val) | 44 | +std::unique_ptr<const char_t[]> aclStub::GetErrMgrErrorMessage() |
| 45 | -{ | 45 | +{ |
| 46 | - return true; | 46 | + const char* str = "default"; |
| 47 | -} | 47 | + std::unique_ptr<const char[]> errMsg(new char[std::strlen(str) + 1]); |
| 48 | - | 48 | + std::strcpy(const_cast<char*>(errMsg.get()), str); |
| 49 | -uint32_t aclStub::UpdateRuntimePlatformInfosByDevice(const uint32_t &device_id, fe::PlatFormInfos &platform_infos) { | 49 | + return errMsg; |
| 50 | - return 0; | 50 | +} |
| 51 | -} | 51 | + |
| 52 | - | 52 | +int aclStub::Init() { return 0; } |
| 53 | -std::unique_ptr<const char_t[]> aclStub::GetErrMgrErrorMessage() | 53 | + |
| 54 | -{ | 54 | +int error_message::ErrMgrInit(error_message::ErrorMessageMode error_mode) |
| 55 | - const char *str = "default"; | 55 | +{ |
| 56 | - std::unique_ptr<const char[]> errMsg(new char[std::strlen(str) + 1]); | 56 | + (void)error_mode; |
| 57 | - std::strcpy(const_cast<char*>(errMsg.get()), str); | 57 | + return MockFunctionTest::aclStubInstance().Init(); |
| 58 | - return errMsg; | 58 | +} |
| 59 | -} | 59 | + |
| 60 | - | 60 | +std::unique_ptr<const char_t[]> error_message::GetErrMgrErrorMessage() |
| 61 | -int aclStub::Init() | 61 | +{ |
| 62 | -{ | 62 | + return MockFunctionTest::aclStubInstance().GetErrMgrErrorMessage(); |
| 63 | - return 0; | 63 | +} |
| 64 | -} | 64 | + |
| 65 | - | 65 | +int32_t error_message::ReportPredefinedErrMsg(const char* error_code) { return 0; } |
| 66 | -int error_message::ErrMgrInit(error_message::ErrorMessageMode error_mode) | 66 | + |
| 67 | -{ | 67 | +int32_t error_message::ReportPredefinedErrMsg( |
| 68 | - (void)error_mode; | 68 | + const char* error_code, const std::vector<const char*>& key, const std::vector<const char*>& value) |
| 69 | - return MockFunctionTest::aclStubInstance().Init(); | 69 | +{ |
| 70 | -} | 70 | + return 0; |
| 71 | - | 71 | +} |
| 72 | -std::unique_ptr<const char_t[]> error_message::GetErrMgrErrorMessage() | 72 | + |
| 73 | -{ | 73 | +int32_t error_message::ReportInnerErrMsg( |
| 74 | - return MockFunctionTest::aclStubInstance().GetErrMgrErrorMessage(); | 74 | + const char* file_name, const char* func, uint32_t line, const char* error_code, const char* format, ...) |
| 75 | -} | 75 | +{ |
| 76 | - | 76 | + return 0; |
| 77 | -int32_t error_message::ReportPredefinedErrMsg(const char *error_code) { | 77 | +} |
| 78 | - return 0; | 78 | + |
| 79 | -} | 79 | +namespace fe { |
| 80 | - | 80 | +PlatformInfoManager::PlatformInfoManager() : init_flag_(false) {} |
| 81 | -int32_t error_message::ReportPredefinedErrMsg(const char *error_code, const std::vector<const char *> &key, | 81 | + |
| 82 | - const std::vector<const char *> &value) | 82 | +PlatformInfoManager::~PlatformInfoManager() {} |
| 83 | -{ | 83 | + |
| 84 | - return 0; | 84 | +PlatformInfoManager& PlatformInfoManager::GeInstance() |
| 85 | -} | 85 | +{ |
| 86 | - | 86 | + static PlatformInfoManager ge_platform_info; |
| 87 | -int32_t error_message::ReportInnerErrMsg(const char *file_name, const char *func, uint32_t line, const char *error_code, | 87 | + return ge_platform_info; |
| 88 | - const char *format, ...) | 88 | +} |
| 89 | -{ | 89 | + |
| 90 | - return 0; | 90 | +uint32_t PlatformInfoManager::InitRuntimePlatformInfos(const std::string& SoCVersion) |
| 91 | -} | 91 | +{ |
| 92 | - | 92 | + return MockFunctionTest::aclStubInstance().InitRuntimePlatformInfos(SoCVersion); |
| 93 | -namespace fe { | 93 | +} |
| 94 | - PlatformInfoManager::PlatformInfoManager() : init_flag_(false) {} | 94 | + |
| 95 | - | 95 | +uint32_t PlatformInfoManager::GetRuntimePlatformInfosByDevice( |
| 96 | - PlatformInfoManager::~PlatformInfoManager() {} | 96 | + const uint32_t& device_id, PlatFormInfos& platform_infos, bool need_deep_copy) |
| 97 | - | 97 | +{ |
| 98 | - PlatformInfoManager &PlatformInfoManager::GeInstance() { | 98 | + (void)need_deep_copy; |
| 99 | - static PlatformInfoManager ge_platform_info; | 99 | + return MockFunctionTest::aclStubInstance().GetRuntimePlatformInfosByDevice(device_id, platform_infos); |
| 100 | - return ge_platform_info; | 100 | +} |
| 101 | - } | 101 | + |
| 102 | - | 102 | +uint32_t PlatformInfoManager::UpdateRuntimePlatformInfosByDevice( |
| 103 | - uint32_t PlatformInfoManager::InitRuntimePlatformInfos(const std::string &SoCVersion) { | 103 | + const uint32_t& device_id, PlatFormInfos& platform_infos) |
| 104 | - return MockFunctionTest::aclStubInstance().InitRuntimePlatformInfos(SoCVersion); | 104 | +{ |
| 105 | - } | 105 | + return MockFunctionTest::aclStubInstance().UpdateRuntimePlatformInfosByDevice(device_id, platform_infos); |
| 106 | - | 106 | +} |
| 107 | - uint32_t PlatformInfoManager::GetRuntimePlatformInfosByDevice(const uint32_t &device_id, | 107 | + |
| 108 | - PlatFormInfos &platform_infos, | 108 | +uint32_t fe::PlatformInfoManager::InitializePlatformInfo() |
| 109 | - bool need_deep_copy) { | 109 | +{ |
| 110 | - (void) need_deep_copy; | 110 | + return MockFunctionTest::aclStubInstance().InitializePlatformInfo(); |
| 111 | - return MockFunctionTest::aclStubInstance().GetRuntimePlatformInfosByDevice(device_id, platform_infos); | 111 | +} |
| 112 | - } | 112 | + |
| 113 | - | 113 | +uint32_t fe::PlatformInfoManager::GetPlatformInfos( |
| 114 | - uint32_t PlatformInfoManager::UpdateRuntimePlatformInfosByDevice(const uint32_t &device_id, | 114 | + const std::string SoCVersion, fe::PlatFormInfos& platformInfo, fe::OptionalInfos& optionalInfo) |
| 115 | - PlatFormInfos &platform_infos) { | 115 | +{ |
| 116 | - return MockFunctionTest::aclStubInstance().UpdateRuntimePlatformInfosByDevice(device_id, platform_infos); | 116 | + return MockFunctionTest::aclStubInstance().GetPlatformInfos(SoCVersion, platformInfo, optionalInfo); |
| 117 | - } | 117 | +} |
| 118 | - | 118 | + |
| 119 | - uint32_t fe::PlatformInfoManager::InitializePlatformInfo() | 119 | +bool PlatFormInfos::GetPlatformResWithLock(const std::string& label, std::map<std::string, std::string>& res) |
| 120 | - { | 120 | +{ |
| 121 | - return MockFunctionTest::aclStubInstance().InitializePlatformInfo(); | 121 | + return MockFunctionTest::aclStubInstance().GetPlatformResWithLock(label, res); |
| 122 | - } | 122 | +} |
| 123 | - | 123 | + |
| 124 | - uint32_t fe::PlatformInfoManager::GetPlatformInfos( | 124 | +bool PlatFormInfos::GetPlatformResWithLock(const string& label, const string& key, string& val) |
| 125 | - const std::string SoCVersion, fe::PlatFormInfos &platformInfo, fe::OptionalInfos &optionalInfo) | 125 | +{ |
| 126 | - { | 126 | + return MockFunctionTest::aclStubInstance().GetPlatformResWithLock(label, key, val); |
| 127 | - return MockFunctionTest::aclStubInstance().GetPlatformInfos(SoCVersion, platformInfo, optionalInfo); | 127 | +} |
| 128 | - } | 128 | + |
| 129 | - | 129 | +void PlatFormInfos::SetPlatformResWithLock(const std::string& label, std::map<std::string, std::string>& res) |
| 130 | - bool PlatFormInfos::GetPlatformResWithLock(const std::string &label, std::map<std::string, std::string> &res) { | 130 | +{ |
| 131 | - return MockFunctionTest::aclStubInstance().GetPlatformResWithLock(label, res); | 131 | + return; |
| 132 | - } | 132 | +} |
| 133 | - | 133 | +} // namespace fe |
| 134 | - bool PlatFormInfos::GetPlatformResWithLock(const string &label, const string &key, string &val) | 134 | + |
| 135 | - { | 135 | +MockFunctionTest::MockFunctionTest() { ResetToDefaultMock(); } |
| 136 | - return MockFunctionTest::aclStubInstance().GetPlatformResWithLock(label, key, val); | 136 | + |
| 137 | - } | 137 | +MockFunctionTest& MockFunctionTest::aclStubInstance() |
| 138 | - | 138 | +{ |
| 139 | - void PlatFormInfos::SetPlatformResWithLock(const std::string &label, std::map<std::string, std::string> &res) { | 139 | + static MockFunctionTest stub; |
| 140 | - return; | 140 | + return stub; |
| 141 | - } | 141 | +}; |
| 142 | -} | 142 | + |
| 143 | - | 143 | +void MockFunctionTest::ResetToDefaultMock() |
| 144 | - | 144 | +{ |
| 145 | -MockFunctionTest::MockFunctionTest() | 145 | + // delegates the default actions of the RTS methods to aclStub |
| 146 | -{ | 146 | + ON_CALL(*this, rtDvppMallocWithFlag) |
| 147 | - ResetToDefaultMock(); | 147 | + .WillByDefault([this](void** devPtr, uint64_t size, uint32_t flag, uint16_t moduleId) { |
| 148 | -} | 148 | + return aclStub::rtDvppMallocWithFlag(devPtr, size, flag, moduleId); |
| 149 | - | 149 | + }); |
| 150 | -MockFunctionTest& MockFunctionTest::aclStubInstance() | 150 | + ON_CALL(*this, rtDvppMalloc).WillByDefault([this](void** devPtr, uint64_t size, uint16_t moduleId) { |
| 151 | -{ | 151 | + return aclStub::rtDvppMalloc(devPtr, size, moduleId); |
| 152 | - static MockFunctionTest stub; | 152 | + }); |
| 153 | - return stub; | 153 | + ON_CALL(*this, rtDvppFree).WillByDefault([this](void* devPtr) { return aclStub::rtDvppFree(devPtr); }); |
| 154 | -}; | 154 | + ON_CALL(*this, rtMalloc).WillByDefault([this](void** devPtr, uint64_t size, rtMemType_t type, uint16_t moduleId) { |
| 155 | - | 155 | + return aclStub::rtMalloc(devPtr, size, type, moduleId); |
| 156 | -void MockFunctionTest::ResetToDefaultMock() { | 156 | + }); |
| 157 | - // delegates the default actions of the RTS methods to aclStub | 157 | + ON_CALL(*this, rtMemAllocManaged) |
| 158 | - ON_CALL(*this, rtDvppMallocWithFlag).WillByDefault([this](void **devPtr, uint64_t size, uint32_t flag, uint16_t moduleId) { | 158 | + .WillByDefault([this](void** ptr, uint64_t size, uint32_t flag, const uint16_t moduleId) { |
| 159 | - return aclStub::rtDvppMallocWithFlag(devPtr, size, flag, moduleId);}); | 159 | + return aclStub::rtMemAllocManaged(ptr, size, flag, moduleId); |
| 160 | - ON_CALL(*this, rtDvppMalloc).WillByDefault([this](void **devPtr, uint64_t size, uint16_t moduleId) { | 160 | + }); |
| 161 | - return aclStub::rtDvppMalloc(devPtr, size, moduleId); | 161 | + ON_CALL(*this, rtMemManagedAdvise) |
| 162 | - }); | 162 | + .WillByDefault([this](const void* const ptr, uint64_t size, uint16_t advise, rtMemManagedLocation location) { |
| 163 | - ON_CALL(*this, rtDvppFree).WillByDefault([this](void *devPtr) { | 163 | + return aclStub::rtMemManagedAdvise(ptr, size, advise, location); |
| 164 | - return aclStub::rtDvppFree(devPtr); | 164 | + }); |
| 165 | - }); | 165 | + ON_CALL(*this, rtMemManagedGetAttr) |
| 166 | - ON_CALL(*this, rtMalloc).WillByDefault([this](void **devPtr, uint64_t size, rtMemType_t type, uint16_t moduleId) { | 166 | + .WillByDefault( |
| 167 | - return aclStub::rtMalloc(devPtr, size, type, moduleId); | 167 | + [this](rtMemManagedRangeAttribute attribute, const void* ptr, size_t size, void* data, size_t dataSize) { |
| 168 | - }); | 168 | + return aclStub::rtMemManagedGetAttr(attribute, ptr, size, data, dataSize); |
| 169 | - ON_CALL(*this, rtMemAllocManaged).WillByDefault([this](void **ptr, uint64_t size, uint32_t flag, const uint16_t moduleId) { | 169 | + }); |
| 170 | - return aclStub::rtMemAllocManaged(ptr, size, flag, moduleId); | 170 | + ON_CALL(*this, rtMemManagedGetAttrs) |
| 171 | - }); | 171 | + .WillByDefault([this]( |
| 172 | - ON_CALL(*this, rtMemManagedAdvise).WillByDefault([this](const void *const ptr, uint64_t size, uint16_t advise, rtMemManagedLocation location) { | 172 | + rtMemManagedRangeAttribute* attributes, size_t numAttributes, const void* ptr, size_t size, |
| 173 | - return aclStub::rtMemManagedAdvise(ptr, size, advise, location); | 173 | + void** data, size_t* dataSizes) { |
| 174 | - }); | 174 | + return aclStub::rtMemManagedGetAttrs(attributes, numAttributes, ptr, size, data, dataSizes); |
| 175 | - ON_CALL(*this, rtMemManagedGetAttr).WillByDefault([this](rtMemManagedRangeAttribute attribute, const void *ptr, size_t size, void *data, size_t dataSize) { | 175 | + }); |
| 176 | - return aclStub::rtMemManagedGetAttr(attribute, ptr, size, data, dataSize); | 176 | + ON_CALL(*this, rtFree).WillByDefault([this](void* devPtr) { return aclStub::rtFree(devPtr); }); |
| 177 | - }); | 177 | + ON_CALL(*this, rtMallocHost).WillByDefault([this](void** hostPtr, uint64_t size, uint16_t moduleId) { |
| 178 | - ON_CALL(*this, rtMemManagedGetAttrs).WillByDefault([this](rtMemManagedRangeAttribute *attributes, size_t numAttributes, const void *ptr, | 178 | + return aclStub::rtMallocHost(hostPtr, size, moduleId); |
| 179 | - size_t size, void **data, size_t *dataSizes) { | 179 | + }); |
| 180 | - return aclStub::rtMemManagedGetAttrs(attributes, numAttributes, ptr, size, data, dataSizes); | 180 | + ON_CALL(*this, rtFreeHost).WillByDefault([this](void* devPtr) { return aclStub::rtFreeHost(devPtr); }); |
| 181 | - }); | 181 | + ON_CALL(*this, rtFreeWithDevSync).WillByDefault([this](void* devPtr) { |
| 182 | - ON_CALL(*this, rtFree).WillByDefault([this](void *devPtr) { | 182 | + return aclStub::rtFreeWithDevSync(devPtr); |
| 183 | - return aclStub::rtFree(devPtr); | 183 | + }); |
| 184 | - }); | 184 | + ON_CALL(*this, rtFreeHostWithDevSync).WillByDefault([this](void* hostPtr) { |
| 185 | - ON_CALL(*this, rtMallocHost).WillByDefault([this](void **hostPtr, uint64_t size, uint16_t moduleId) { | 185 | + return aclStub::rtFreeHostWithDevSync(hostPtr); |
| 186 | - return aclStub::rtMallocHost(hostPtr, size, moduleId); | 186 | + }); |
| 187 | - }); | 187 | + ON_CALL(*this, rtMallocCached) |
| 188 | - ON_CALL(*this, rtFreeHost).WillByDefault([this](void *devPtr) { | 188 | + .WillByDefault([this](void** devPtr, uint64_t size, rtMemType_t type, uint16_t moduleId) { |
| 189 | - return aclStub::rtFreeHost(devPtr); | 189 | + return aclStub::rtMallocCached(devPtr, size, type, moduleId); |
| 190 | - }); | 190 | + }); |
| 191 | - ON_CALL(*this, rtFreeWithDevSync).WillByDefault([this](void *devPtr) { | 191 | + ON_CALL(*this, rtMemcpy) |
| 192 | - return aclStub::rtFreeWithDevSync(devPtr); | 192 | + .WillByDefault([this](void* dst, uint64_t destMax, const void* src, uint64_t count, rtMemcpyKind_t kind) { |
| 193 | - }); | 193 | + return aclStub::rtMemcpy(dst, destMax, src, count, kind); |
| 194 | - ON_CALL(*this, rtFreeHostWithDevSync).WillByDefault([this](void *hostPtr) { | 194 | + }); |
| 195 | - return aclStub::rtFreeHostWithDevSync(hostPtr); | 195 | + ON_CALL(*this, GetErrMgrErrorMessage).WillByDefault([this]() { return aclStub::GetErrMgrErrorMessage(); }); |
| 196 | - }); | 196 | + ON_CALL(*this, rtHostMemMapCapabilities) |
| 197 | - ON_CALL(*this, rtMallocCached).WillByDefault([this](void **devPtr, uint64_t size, rtMemType_t type, uint16_t moduleId) { | 197 | + .WillByDefault([this](uint32_t deviceId, rtHacType hacType, rtHostMemMapCapability* capabilities) { |
| 198 | - return aclStub::rtMallocCached(devPtr, size, type, moduleId);}); | 198 | + return aclStub::rtHostMemMapCapabilities(deviceId, hacType, capabilities); |
| 199 | - ON_CALL(*this, rtMemcpy).WillByDefault([this](void *dst, uint64_t destMax, const void *src, uint64_t count, rtMemcpyKind_t kind) { | 199 | + }); |
| 200 | - return aclStub::rtMemcpy(dst, destMax, src, count, kind);}); | 200 | +} |
| 201 | - ON_CALL(*this, GetErrMgrErrorMessage).WillByDefault([this]() { | ||
| 202 | - return aclStub::GetErrMgrErrorMessage();}); | ||
| 203 | - ON_CALL(*this, rtHostMemMapCapabilities).WillByDefault([this](uint32_t deviceId, rtHacType hacType, rtHostMemMapCapability *capabilities) { | ||
| 204 | - return aclStub::rtHostMemMapCapabilities(deviceId, hacType, capabilities);}); | ||
| 205 | -} | ||
| 206 | - | ||
| 207 | - | ||