| @@ -470,7 +470,7 @@ int main() { | |||
| 470 | void* workspaceAddr = nullptr; | 470 | void* workspaceAddr = nullptr; |
| 471 | if (workspaceSize > 0) { | 471 | if (workspaceSize > 0) { |
| 472 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 472 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 473 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 473 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 474 | } | 474 | } |
| 475 | std::cout << "aclnnErf 1ok" << std::endl; | 475 | std::cout << "aclnnErf 1ok" << std::endl; |
| 476 | // 调用aclnnErf第二段接口 | 476 | // 调用aclnnErf第二段接口 |
| @@ -499,7 +499,7 @@ int main() { | |||
| 499 | void* inplaceWorkspaceAddr = nullptr; | 499 | void* inplaceWorkspaceAddr = nullptr; |
| 500 | if (inplaceWorkspaceSize > 0) { | 500 | if (inplaceWorkspaceSize > 0) { |
| 501 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 501 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 502 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 502 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 503 | } | 503 | } |
| 504 | // 调用aclnnInplaceErf第二段接口 | 504 | // 调用aclnnInplaceErf第二段接口 |
| 505 | ret = aclnnInplaceErf(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 505 | ret = aclnnInplaceErf(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| @@ -14,155 +14,159 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_erfr; \ | 19 | + return_erfr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | - int64_t shape_size = 1; | 29 | +{ |
| 30 | - for (auto i : shape) { | 30 | + int64_t shape_size = 1; |
| 31 | - shape_size *= i; | 31 | + for (auto i : shape) { |
| 32 | - } | 32 | + shape_size *= i; |
| 33 | - return shape_size; | 33 | + } |
| 34 | + return shape_size; | ||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | - // 固定写法,AscendCL初始化 | 38 | +{ |
| 38 | - auto ret = aclInit(nullptr); | 39 | + // 固定写法,AscendCL初始化 |
| 39 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - ret = aclrtSetDevice(deviceId); | 41 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 41 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - ret = aclrtCreateStream(stream); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - return 0; | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 46 | + return 0; | ||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 50 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 51 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 52 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 53 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 55 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 56 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | ||
| 54 | 57 | ||
| 55 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 58 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 56 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 59 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 57 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 60 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); |
| 58 | 61 | ||
| 59 | - // 计算连续tensor的strides | 62 | + // 计算连续tensor的strides |
| 60 | - std::vector<int64_t> strides(shape.size(), 1); | 63 | + std::vector<int64_t> strides(shape.size(), 1); |
| 61 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 64 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 62 | - strides[i] = shape[i + 1] * strides[i + 1]; | 65 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 63 | - } | 66 | + } |
| 64 | 67 | ||
| 65 | - // 调用aclCreateTensor接口创建aclTensor | 68 | + // 调用aclCreateTensor接口创建aclTensor |
| 66 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 67 | - shape.data(), shape.size(), *deviceAddr); | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 68 | - return 0; | 71 | + return 0; |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -int main() { | 74 | +int main() |
| 72 | - // 1. (固定写法)device/stream初始化, 参考AscendCL对外接口列表 | 75 | +{ |
| 73 | - // 根据自己的实际device填写deviceId | 76 | + // 1. (固定写法)device/stream初始化, 参考AscendCL对外接口列表 |
| 74 | - int32_t deviceId = 0; | 77 | + // 根据自己的实际device填写deviceId |
| 75 | - aclrtStream stream; | 78 | + int32_t deviceId = 0; |
| 76 | - auto ret = Init(deviceId, &stream); | 79 | + aclrtStream stream; |
| 77 | - // check根据自己的需要处理 | 80 | + auto ret = Init(deviceId, &stream); |
| 78 | - CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 81 | + // check根据自己的需要处理 |
| 79 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 80 | - std::vector<int64_t> selfShape = {2, 2}; | 83 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 81 | - std::vector<int64_t> outShape = {2, 2}; | 84 | + std::vector<int64_t> selfShape = {2, 2}; |
| 82 | - void* selfDeviceAddr = nullptr; | 85 | + std::vector<int64_t> outShape = {2, 2}; |
| 83 | - void* outDeviceAddr = nullptr; | 86 | + void* selfDeviceAddr = nullptr; |
| 84 | - aclTensor* self = nullptr; | 87 | + void* outDeviceAddr = nullptr; |
| 85 | - aclTensor* out = nullptr; | 88 | + aclTensor* self = nullptr; |
| 86 | - std::vector<float> selfHostData = {0, 1, 2, 3}; | 89 | + aclTensor* out = nullptr; |
| 87 | - std::vector<float> outHostData = {0, 0, 0, 0}; | 90 | + std::vector<float> selfHostData = {0, 1, 2, 3}; |
| 88 | - // 创建self aclTensor | 91 | + std::vector<float> outHostData = {0, 0, 0, 0}; |
| 89 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 92 | + // 创建self aclTensor |
| 90 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 93 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 91 | - // 创建out aclTensor | 94 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 92 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 95 | + // 创建out aclTensor |
| 93 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 96 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 97 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 94 | 98 | ||
| 95 | - // 3. 调用CANN算子库API,需要修改为具体的API | 99 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 96 | - uint64_t workspaceSize = 0; | 100 | + uint64_t workspaceSize = 0; |
| 97 | - aclOpExecutor* executor; | 101 | + aclOpExecutor* executor; |
| 98 | - // 调用aclnnErf第一段接口 | 102 | + // 调用aclnnErf第一段接口 |
| 99 | - ret = aclnnErfGetWorkspaceSize(self, out, &workspaceSize, &executor); | 103 | + ret = aclnnErfGetWorkspaceSize(self, out, &workspaceSize, &executor); |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnErfGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 104 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnErfGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 101 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 105 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 102 | - void* workspaceAddr = nullptr; | 106 | + void* workspaceAddr = nullptr; |
| 103 | - if (workspaceSize > 0) { | 107 | + if (workspaceSize > 0) { |
| 104 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 108 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 105 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 109 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 106 | - } | 110 | + } |
| 107 | - std::cout << "aclnnErf 1ok" << std::endl; | 111 | + std::cout << "aclnnErf 1ok" << std::endl; |
| 108 | - // 调用aclnnErf第二段接口 | 112 | + // 调用aclnnErf第二段接口 |
| 109 | - ret = aclnnErf(workspaceAddr, workspaceSize, executor, stream); | 113 | + ret = aclnnErf(workspaceAddr, workspaceSize, executor, stream); |
| 110 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnErf failed. ERROR: %d\n", ret); return ret); | 114 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnErf failed. ERROR: %d\n", ret); return ret); |
| 111 | 115 | ||
| 112 | - // 4. (固定写法)同步等待任务执行结束 | 116 | + // 4. (固定写法)同步等待任务执行结束 |
| 113 | - ret = aclrtSynchronizeStream(stream); | 117 | + ret = aclrtSynchronizeStream(stream); |
| 114 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 115 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 119 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 116 | - auto size = GetShapeSize(outShape); | 120 | + auto size = GetShapeSize(outShape); |
| 117 | - std::vector<float> resultData(size, 0); | 121 | + std::vector<float> resultData(size, 0); |
| 118 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 122 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 119 | - ACL_MEMCPY_DEVICE_TO_HOST); | 123 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 120 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 124 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 121 | - for (int64_t i = 0; i < size; i++) { | 125 | + for (int64_t i = 0; i < size; i++) { |
| 122 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 126 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 123 | - } | 127 | + } |
| 124 | 128 | ||
| 125 | - uint64_t inplaceWorkspaceSize = 0; | 129 | + uint64_t inplaceWorkspaceSize = 0; |
| 126 | - aclOpExecutor* inplaceExecutor; | 130 | + aclOpExecutor* inplaceExecutor; |
| 127 | - // 调用aclnnInplaceErf第一段接口 | 131 | + // 调用aclnnInplaceErf第一段接口 |
| 128 | - ret = aclnnInplaceErfGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); | 132 | + ret = aclnnInplaceErfGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); |
| 129 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceErfGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 133 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceErfGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 130 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 134 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 131 | - void* inplaceWorkspaceAddr = nullptr; | 135 | + void* inplaceWorkspaceAddr = nullptr; |
| 132 | - if (inplaceWorkspaceSize > 0) { | 136 | + if (inplaceWorkspaceSize > 0) { |
| 133 | - ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 137 | + ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 134 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 138 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 135 | - } | 139 | + } |
| 136 | - // 调用aclnnInplaceErf第二段接口 | 140 | + // 调用aclnnInplaceErf第二段接口 |
| 137 | - ret = aclnnInplaceErf(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 141 | + ret = aclnnInplaceErf(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| 138 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceErf failed. ERROR: %d\n", ret); return ret); | 142 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceErf failed. ERROR: %d\n", ret); return ret); |
| 139 | 143 | ||
| 140 | - // (固定写法)同步等待任务执行结束 | 144 | + // (固定写法)同步等待任务执行结束 |
| 141 | - ret = aclrtSynchronizeStream(stream); | 145 | + ret = aclrtSynchronizeStream(stream); |
| 142 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 146 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 143 | 147 | ||
| 144 | - auto inplaceSize = GetShapeSize(selfShape); | 148 | + auto inplaceSize = GetShapeSize(selfShape); |
| 145 | - std::vector<float> inplaceResultData(inplaceSize, 0); | 149 | + std::vector<float> inplaceResultData(inplaceSize, 0); |
| 146 | - ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, | 150 | + ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, |
| 147 | - inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | 151 | + inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); |
| 148 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 152 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 149 | - for (int64_t i = 0; i < inplaceSize; i++) { | 153 | + for (int64_t i = 0; i < inplaceSize; i++) { |
| 150 | - LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); | 154 | + LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); |
| 151 | - } | 155 | + } |
| 152 | 156 | ||
| 153 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 157 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 154 | - aclDestroyTensor(self); | 158 | + aclDestroyTensor(self); |
| 155 | - aclDestroyTensor(out); | 159 | + aclDestroyTensor(out); |
| 156 | 160 | ||
| 157 | - // 7.释放device资源,需要根据具体API的接口定义修改 | 161 | + // 7.释放device资源,需要根据具体API的接口定义修改 |
| 158 | - aclrtFree(selfDeviceAddr); | 162 | + aclrtFree(selfDeviceAddr); |
| 159 | - aclrtFree(outDeviceAddr); | 163 | + aclrtFree(outDeviceAddr); |
| 160 | - if (workspaceSize > 0) { | 164 | + if (workspaceSize > 0) { |
| 161 | - aclrtFree(workspaceAddr); | 165 | + aclrtFree(workspaceAddr); |
| 162 | - } | 166 | + } |
| 163 | - aclrtDestroyStream(stream); | 167 | + aclrtDestroyStream(stream); |
| 164 | - aclrtResetDevice(deviceId); | 168 | + aclrtResetDevice(deviceId); |
| 165 | - aclFinalize(); | 169 | + aclFinalize(); |
| 166 | 170 | ||
| 167 | - return 0; | 171 | + return 0; |
| 168 | -} | 172 | +} |
| @@ -475,7 +475,7 @@ int main() { | |||
| 475 | void* inplaceWorkspaceAddr = nullptr; | 475 | void* inplaceWorkspaceAddr = nullptr; |
| 476 | if (inplaceWorkspaceSize > 0) { | 476 | if (inplaceWorkspaceSize > 0) { |
| 477 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 477 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 478 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 478 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 479 | } | 479 | } |
| 480 | // 调用aclnnInplaceExp第二段接口 | 480 | // 调用aclnnInplaceExp第二段接口 |
| 481 | ret = aclnnInplaceExp(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 481 | ret = aclnnInplaceExp(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| @@ -14,149 +14,153 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | - int64_t shapeSize = 1; | 29 | +{ |
| 30 | - for (auto i : shape) { | 30 | + int64_t shapeSize = 1; |
| 31 | - shapeSize *= i; | 31 | + for (auto i : shape) { |
| 32 | - } | 32 | + shapeSize *= i; |
| 33 | - return shapeSize; | 33 | + } |
| 34 | + return shapeSize; | ||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | - // 固定写法,AscendCL初始化 | 38 | +{ |
| 38 | - auto ret = aclInit(nullptr); | 39 | + // 固定写法,AscendCL初始化 |
| 39 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - ret = aclrtSetDevice(deviceId); | 41 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 41 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - ret = aclrtCreateStream(stream); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - return 0; | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 46 | + return 0; | ||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 50 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 51 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 52 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 53 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 55 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 54 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 56 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); |
| 55 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 57 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 56 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 58 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 59 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | ||
| 57 | 60 | ||
| 58 | - // 计算连续tensor的strides | 61 | + // 计算连续tensor的strides |
| 59 | - std::vector<int64_t> strides(shape.size(), 1); | 62 | + std::vector<int64_t> strides(shape.size(), 1); |
| 60 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 63 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 61 | - strides[i] = shape[i + 1] * strides[i + 1]; | 64 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 62 | - } | 65 | + } |
| 63 | 66 | ||
| 64 | - // 调用aclCreateTensor接口创建aclTensor | 67 | + // 调用aclCreateTensor接口创建aclTensor |
| 65 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 68 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 66 | - shape.data(), shape.size(), *deviceAddr); | 69 | + shape.data(), shape.size(), *deviceAddr); |
| 67 | - return 0; | 70 | + return 0; |
| 68 | } | 71 | } |
| 69 | 72 | ||
| 70 | -int main() { | 73 | +int main() |
| 71 | - // 1. (固定写法)device/stream初始化,参考AscendCL对外接口列表 | 74 | +{ |
| 72 | - // 根据自己的实际device填写deviceId | 75 | + // 1. (固定写法)device/stream初始化,参考AscendCL对外接口列表 |
| 73 | - int32_t deviceId = 0; | 76 | + // 根据自己的实际device填写deviceId |
| 74 | - aclrtStream stream; | 77 | + int32_t deviceId = 0; |
| 75 | - auto ret = Init(deviceId, &stream); | 78 | + aclrtStream stream; |
| 76 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 79 | + auto ret = Init(deviceId, &stream); |
| 80 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | ||
| 77 | 81 | ||
| 78 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 79 | - std::vector<int64_t> selfShape = {2, 2}; | 83 | + std::vector<int64_t> selfShape = {2, 2}; |
| 80 | - std::vector<int64_t> outShape = {2, 2}; | 84 | + std::vector<int64_t> outShape = {2, 2}; |
| 81 | - void* selfDeviceAddr = nullptr; | 85 | + void* selfDeviceAddr = nullptr; |
| 82 | - void* outDeviceAddr = nullptr; | 86 | + void* outDeviceAddr = nullptr; |
| 83 | - aclTensor* self = nullptr; | 87 | + aclTensor* self = nullptr; |
| 84 | - aclTensor* out = nullptr; | 88 | + aclTensor* out = nullptr; |
| 85 | - std::vector<float> selfHostData = {0, 1, 2, 3}; | 89 | + std::vector<float> selfHostData = {0, 1, 2, 3}; |
| 86 | - std::vector<float> outHostData = {0, 0, 0, 0}; | 90 | + std::vector<float> outHostData = {0, 0, 0, 0}; |
| 87 | - // 创建self aclTensor | 91 | + // 创建self aclTensor |
| 88 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 92 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 89 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 93 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 90 | - // 创建out aclTensor | 94 | + // 创建out aclTensor |
| 91 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 95 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 92 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 96 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 93 | 97 | ||
| 94 | - // 3. 调用CANN算子库API,需要修改为具体的Api名称 | 98 | + // 3. 调用CANN算子库API,需要修改为具体的Api名称 |
| 95 | - uint64_t workspaceSize = 0; | 99 | + uint64_t workspaceSize = 0; |
| 96 | - aclOpExecutor* executor; | 100 | + aclOpExecutor* executor; |
| 97 | - // 调用aclnnExp第一段接口 | 101 | + // 调用aclnnExp第一段接口 |
| 98 | - ret = aclnnExpGetWorkspaceSize(self, out, &workspaceSize, &executor); | 102 | + ret = aclnnExpGetWorkspaceSize(self, out, &workspaceSize, &executor); |
| 99 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnExpGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 103 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnExpGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 100 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 104 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 101 | - void* workspaceAddr = nullptr; | 105 | + void* workspaceAddr = nullptr; |
| 102 | - if (workspaceSize > 0) { | 106 | + if (workspaceSize > 0) { |
| 103 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 107 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 104 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | 108 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 105 | - } | 109 | + } |
| 106 | - // 调用aclnnExp第二段接口 | 110 | + // 调用aclnnExp第二段接口 |
| 107 | - ret = aclnnExp(workspaceAddr, workspaceSize, executor, stream); | 111 | + ret = aclnnExp(workspaceAddr, workspaceSize, executor, stream); |
| 108 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnExp failed. ERROR: %d\n", ret); return ret); | 112 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnExp failed. ERROR: %d\n", ret); return ret); |
| 109 | - | ||
| 110 | - uint64_t inplaceWorkspaceSize = 0; | ||
| 111 | - aclOpExecutor* inplaceExecutor; | ||
| 112 | - // 调用aclnnInplaceExp第一段接口 | ||
| 113 | - ret = aclnnInplaceExpGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); | ||
| 114 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceExpGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | ||
| 115 | - // 根据第一段接口计算出的workspaceSize申请device内存 | ||
| 116 | - void* inplaceWorkspaceAddr = nullptr; | ||
| 117 | - if (inplaceWorkspaceSize > 0) { | ||
| 118 | - ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | ||
| 120 | - } | ||
| 121 | - // 调用aclnnInplaceExp第二段接口 | ||
| 122 | - ret = aclnnInplaceExp(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | ||
| 123 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceExp failed. ERROR: %d\n", ret); return ret); | ||
| 124 | 113 | ||
| 125 | - // 4. (固定写法)同步等待任务执行结束 | 114 | + uint64_t inplaceWorkspaceSize = 0; |
| 126 | - ret = aclrtSynchronizeStream(stream); | 115 | + aclOpExecutor* inplaceExecutor; |
| 127 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 116 | + // 调用aclnnInplaceExp第一段接口 |
| 117 | + ret = aclnnInplaceExpGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); | ||
| 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceExpGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | ||
| 119 | + // 根据第一段接口计算出的workspaceSize申请device内存 | ||
🔴 Critical 变更行: 受影响的代码合约: 该文件第 16–21 行定义了 失效模式: 移除分号后, 本文件与同批受影响的 .cpp 文件(均存在相同问题,各文件内 CHECK_RET 均定义为
修复方向: 恢复所有被移除的 建议:恢复 CHECK_RET 宏调用末尾的必需分号。 ![]() ![]() | |||
| 120 | + void* inplaceWorkspaceAddr = nullptr; | ||
| 121 | + if (inplaceWorkspaceSize > 0) { | ||
| 122 | + ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 123 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | ||
| 124 | + } | ||
| 125 | + // 调用aclnnInplaceExp第二段接口 | ||
| 126 | + ret = aclnnInplaceExp(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | ||
| 127 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceExp failed. ERROR: %d\n", ret); return ret); | ||
| 128 | 128 | ||
| 129 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 129 | + // 4. (固定写法)同步等待任务执行结束 |
| 130 | - auto size = GetShapeSize(outShape); | 130 | + ret = aclrtSynchronizeStream(stream); |
| 131 | - std::vector<float> resultData(size, 0); | 131 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 132 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | ||
| 133 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 134 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | ||
| 135 | - for (int64_t i = 0; i < size; i++) { | ||
| 136 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | ||
| 137 | - } | ||
| 138 | 132 | ||
| 139 | - auto inplaceSize = GetShapeSize(selfShape); | 133 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 140 | - std::vector<float> inplaceResultData(inplaceSize, 0); | 134 | + auto size = GetShapeSize(outShape); |
| 141 | - ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, | 135 | + std::vector<float> resultData(size, 0); |
| 142 | - inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | 136 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 143 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 137 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 144 | - for (int64_t i = 0; i < inplaceSize; i++) { | 138 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 145 | - LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); | 139 | + for (int64_t i = 0; i < size; i++) { |
| 146 | - } | 140 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 141 | + } | ||
| 147 | 142 | ||
| 148 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 143 | + auto inplaceSize = GetShapeSize(selfShape); |
| 149 | - aclDestroyTensor(self); | 144 | + std::vector<float> inplaceResultData(inplaceSize, 0); |
| 150 | - aclDestroyTensor(out); | 145 | + ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, |
| 146 | + inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 147 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | ||
| 148 | + for (int64_t i = 0; i < inplaceSize; i++) { | ||
| 149 | + LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); | ||
| 150 | + } | ||
| 151 | 151 | ||
| 152 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 152 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 153 | - aclrtFree(selfDeviceAddr); | 153 | + aclDestroyTensor(self); |
| 154 | - aclrtFree(outDeviceAddr); | 154 | + aclDestroyTensor(out); |
| 155 | - if (workspaceSize > 0) { | 155 | + |
| 156 | - aclrtFree(workspaceAddr); | 156 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 157 | - } | 157 | + aclrtFree(selfDeviceAddr); |
| 158 | - aclrtDestroyStream(stream); | 158 | + aclrtFree(outDeviceAddr); |
| 159 | - aclrtResetDevice(deviceId); | 159 | + if (workspaceSize > 0) { |
| 160 | - aclFinalize(); | 160 | + aclrtFree(workspaceAddr); |
| 161 | - return 0; | 161 | + } |
| 162 | -} | 162 | + aclrtDestroyStream(stream); |
| 163 | + aclrtResetDevice(deviceId); | ||
| 164 | + aclFinalize(); | ||
| 165 | + return 0; | ||
| 166 | +} | ||
| @@ -469,7 +469,7 @@ int main() { | |||
| 469 | void* workspaceAddr = nullptr; | 469 | void* workspaceAddr = nullptr; |
| 470 | if (workspaceSize > 0) { | 470 | if (workspaceSize > 0) { |
| 471 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 471 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 472 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 472 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 473 | } | 473 | } |
| 474 | // 调用aclnnFloor第二段接口 | 474 | // 调用aclnnFloor第二段接口 |
| 475 | ret = aclnnFloor(workspaceAddr, workspaceSize, executor, stream); | 475 | ret = aclnnFloor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -46,9 +46,8 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | template <typename T> | 48 | template <typename T> |
| 49 | -int CreateAclTensor( | 49 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 50 | - const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | 50 | + aclDataType dataType, aclTensor** tensor) |
| 51 | - aclTensor** tensor) | ||
| 52 | { | 51 | { |
| 53 | auto size = GetShapeSize(shape) * sizeof(T); | 52 | auto size = GetShapeSize(shape) * sizeof(T); |
| 54 | // 调用aclrtMalloc申请device侧内存 | 53 | // 调用aclrtMalloc申请device侧内存 |
| @@ -66,9 +65,8 @@ int CreateAclTensor( | |||
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | // 调用aclCreateTensor接口创建aclTensor | 67 | // 调用aclCreateTensor接口创建aclTensor |
| 69 | - *tensor = aclCreateTensor( | 68 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 70 | - shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), | 69 | + shape.data(), shape.size(), *deviceAddr); |
| 71 | - *deviceAddr); | ||
| 72 | return 0; | 70 | return 0; |
| 73 | } | 71 | } |
| 74 | 72 | ||
| @@ -107,7 +105,7 @@ int main() | |||
| 107 | void* workspaceAddr = nullptr; | 105 | void* workspaceAddr = nullptr; |
| 108 | if (workspaceSize > 0) { | 106 | if (workspaceSize > 0) { |
| 109 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 107 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 110 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 108 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 111 | } | 109 | } |
| 112 | // 调用aclnnFloor第二段接口 | 110 | // 调用aclnnFloor第二段接口 |
| 113 | ret = aclnnFloor(workspaceAddr, workspaceSize, executor, stream); | 111 | ret = aclnnFloor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -118,9 +116,8 @@ int main() | |||
| 118 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 116 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 119 | auto size = GetShapeSize(outShape); | 117 | auto size = GetShapeSize(outShape); |
| 120 | std::vector<float> resultData(size, 0); | 118 | std::vector<float> resultData(size, 0); |
| 121 | - ret = aclrtMemcpy( | 119 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 122 | - resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 120 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 123 | - ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 124 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 121 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 125 | for (int64_t i = 0; i < size; i++) { | 122 | for (int64_t i = 0; i < size; i++) { |
| 126 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 123 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| @@ -141,4 +138,4 @@ int main() | |||
| 141 | aclFinalize(); | 138 | aclFinalize(); |
| 142 | 139 | ||
| 143 | return 0; | 140 | return 0; |
| 144 | -} | 141 | +} |
| @@ -377,7 +377,7 @@ int main() { | |||
| 377 | void* workspaceAddr = nullptr; | 377 | void* workspaceAddr = nullptr; |
| 378 | if (workspaceSize > 0) { | 378 | if (workspaceSize > 0) { |
| 379 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 379 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 380 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 380 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 381 | } | 381 | } |
| 382 | // 调用aclnnIsClose第二段接口 | 382 | // 调用aclnnIsClose第二段接口 |
| 383 | ret = aclnnIsClose(workspaceAddr, workspaceSize, executor, stream); | 383 | ret = aclnnIsClose(workspaceAddr, workspaceSize, executor, stream); |
| @@ -46,9 +46,8 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | template <typename T> | 48 | template <typename T> |
| 49 | -int CreateAclTensor( | 49 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 50 | - const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | 50 | + aclDataType dataType, aclTensor** tensor) |
| 51 | - aclTensor** tensor) | ||
| 52 | { | 51 | { |
| 53 | auto size = GetShapeSize(shape) * sizeof(T); | 52 | auto size = GetShapeSize(shape) * sizeof(T); |
| 54 | // 调用aclrtMalloc申请device侧内存 | 53 | // 调用aclrtMalloc申请device侧内存 |
| @@ -66,9 +65,8 @@ int CreateAclTensor( | |||
| 66 | } | 65 | } |
| 67 | 66 | ||
| 68 | // 调用aclCreateTensor接口创建aclTensor | 67 | // 调用aclCreateTensor接口创建aclTensor |
| 69 | - *tensor = aclCreateTensor( | 68 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 70 | - shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), | 69 | + shape.data(), shape.size(), *deviceAddr); |
| 71 | - *deviceAddr); | ||
| 72 | return 0; | 70 | return 0; |
| 73 | } | 71 | } |
| 74 | 72 | ||
| @@ -117,7 +115,7 @@ int main() | |||
| 117 | void* workspaceAddr = nullptr; | 115 | void* workspaceAddr = nullptr; |
| 118 | if (workspaceSize > 0) { | 116 | if (workspaceSize > 0) { |
| 119 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 117 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 120 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 121 | } | 119 | } |
| 122 | // 调用aclnnIsClose第二段接口 | 120 | // 调用aclnnIsClose第二段接口 |
| 123 | ret = aclnnIsClose(workspaceAddr, workspaceSize, executor, stream); | 121 | ret = aclnnIsClose(workspaceAddr, workspaceSize, executor, stream); |
| @@ -128,9 +126,8 @@ int main() | |||
| 128 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 126 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 129 | auto size = GetShapeSize(outShape); | 127 | auto size = GetShapeSize(outShape); |
| 130 | std::vector<float> resultData(size, 0); | 128 | std::vector<float> resultData(size, 0); |
| 131 | - ret = aclrtMemcpy( | 129 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 132 | - resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 130 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 133 | - ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 134 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 131 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 135 | for (int64_t i = 0; i < size; i++) { | 132 | for (int64_t i = 0; i < size; i++) { |
| 136 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 133 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| @@ -152,4 +149,4 @@ int main() | |||
| 152 | aclrtResetDevice(deviceId); | 149 | aclrtResetDevice(deviceId); |
| 153 | aclFinalize(); | 150 | aclFinalize(); |
| 154 | return 0; | 151 | return 0; |
| 155 | -} | 152 | +} |
| @@ -470,7 +470,7 @@ int main() { | |||
| 470 | void* workspaceAddr = nullptr; | 470 | void* workspaceAddr = nullptr; |
| 471 | if (workspaceSize > 0) { | 471 | if (workspaceSize > 0) { |
| 472 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 472 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 473 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 473 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 474 | } | 474 | } |
| 475 | // 调用aclnnLog10第二段接口 | 475 | // 调用aclnnLog10第二段接口 |
| 476 | ret = aclnnLog10(workspaceAddr, workspaceSize, executor, stream); | 476 | ret = aclnnLog10(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,126 +14,130 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | - int64_t shape_size = 1; | 29 | +{ |
| 30 | - for (auto i : shape) { | 30 | + int64_t shape_size = 1; |
| 31 | - shape_size *= i; | 31 | + for (auto i : shape) { |
| 32 | - } | 32 | + shape_size *= i; |
| 33 | - return shape_size; | 33 | + } |
| 34 | + return shape_size; | ||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | - // 固定写法,资源初始化 | 38 | +{ |
| 38 | - auto ret = aclInit(nullptr); | 39 | + // 固定写法,资源初始化 |
| 39 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - ret = aclrtSetDevice(deviceId); | 41 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 41 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - ret = aclrtCreateStream(stream); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - return 0; | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 46 | + return 0; | ||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 50 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 51 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 52 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 53 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 55 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 56 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | ||
| 54 | 57 | ||
| 55 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 58 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 56 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 59 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 57 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 60 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); |
| 58 | 61 | ||
| 59 | - // 计算连续tensor的strides | 62 | + // 计算连续tensor的strides |
| 60 | - std::vector<int64_t> strides(shape.size(), 1); | 63 | + std::vector<int64_t> strides(shape.size(), 1); |
| 61 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 64 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 62 | - strides[i] = shape[i + 1] * strides[i + 1]; | 65 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 63 | - } | 66 | + } |
| 64 | 67 | ||
| 65 | - // 调用aclCreateTensor接口创建aclTensor | 68 | + // 调用aclCreateTensor接口创建aclTensor |
| 66 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 67 | - shape.data(), shape.size(), *deviceAddr); | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 68 | - return 0; | 71 | + return 0; |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -int main() { | 74 | +int main() |
| 72 | - // 1. (固定写法)device/stream初始化, 参考acl API手册 | 75 | +{ |
| 73 | - // 根据自己的实际device填写deviceId | 76 | + // 1. (固定写法)device/stream初始化, 参考acl API手册 |
| 74 | - int32_t deviceId = 0; | 77 | + // 根据自己的实际device填写deviceId |
| 75 | - aclrtStream stream; | 78 | + int32_t deviceId = 0; |
| 76 | - auto ret = Init(deviceId, &stream); | 79 | + aclrtStream stream; |
| 77 | - // check根据自己的需要处理 | 80 | + auto ret = Init(deviceId, &stream); |
| 78 | - CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 81 | + // check根据自己的需要处理 |
| 79 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 80 | - std::vector<int64_t> selfShape = {4, 2}; | 83 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 81 | - std::vector<int64_t> outShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - void* selfDeviceAddr = nullptr; | 85 | + std::vector<int64_t> outShape = {4, 2}; |
| 83 | - void* outDeviceAddr = nullptr; | 86 | + void* selfDeviceAddr = nullptr; |
| 84 | - aclTensor* self = nullptr; | 87 | + void* outDeviceAddr = nullptr; |
| 85 | - aclTensor* out = nullptr; | 88 | + aclTensor* self = nullptr; |
| 86 | - std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 89 | + aclTensor* out = nullptr; |
| 87 | - std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 90 | + std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 91 | + std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 88 | 92 | ||
| 89 | - // 创建self aclTensor | 93 | + // 创建self aclTensor |
| 90 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 94 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 91 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 95 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 92 | - // 创建out aclTensor | 96 | + // 创建out aclTensor |
| 93 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 97 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 94 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 98 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 95 | 99 | ||
| 96 | - // aclnnLog10接口调用示例 | 100 | + // aclnnLog10接口调用示例 |
| 97 | - // 3. 调用CANN算子库API | 101 | + // 3. 调用CANN算子库API |
| 98 | - uint64_t workspaceSize = 0; | 102 | + uint64_t workspaceSize = 0; |
| 99 | - aclOpExecutor* executor; | 103 | + aclOpExecutor* executor; |
| 100 | - // 调用aclnnLog10第一段接口 | 104 | + // 调用aclnnLog10第一段接口 |
| 101 | - ret = aclnnLog10GetWorkspaceSize(self, out, &workspaceSize, &executor); | 105 | + ret = aclnnLog10GetWorkspaceSize(self, out, &workspaceSize, &executor); |
| 102 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLog10GetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 106 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLog10GetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 103 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 107 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 104 | - void* workspaceAddr = nullptr; | 108 | + void* workspaceAddr = nullptr; |
| 105 | - if (workspaceSize > 0) { | 109 | + if (workspaceSize > 0) { |
| 106 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 110 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 107 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 111 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 108 | - } | 112 | + } |
| 109 | - // 调用aclnnLog10第二段接口 | 113 | + // 调用aclnnLog10第二段接口 |
| 110 | - ret = aclnnLog10(workspaceAddr, workspaceSize, executor, stream); | 114 | + ret = aclnnLog10(workspaceAddr, workspaceSize, executor, stream); |
| 111 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLog10 failed. ERROR: %d\n", ret); return ret); | 115 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLog10 failed. ERROR: %d\n", ret); return ret); |
| 112 | - // 4. (固定写法)同步等待任务执行结束 | 116 | + // 4. (固定写法)同步等待任务执行结束 |
| 113 | - ret = aclrtSynchronizeStream(stream); | 117 | + ret = aclrtSynchronizeStream(stream); |
| 114 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 115 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 119 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 116 | - auto size = GetShapeSize(outShape); | 120 | + auto size = GetShapeSize(outShape); |
| 117 | - std::vector<float> resultData(size, 0); | 121 | + std::vector<float> resultData(size, 0); |
| 118 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 122 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 119 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 123 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 120 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 124 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 121 | - for (int64_t i = 0; i < size; i++) { | 125 | + for (int64_t i = 0; i < size; i++) { |
| 122 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 126 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 123 | - } | 127 | + } |
| 124 | 128 | ||
| 125 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 129 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 126 | - aclDestroyTensor(self); | 130 | + aclDestroyTensor(self); |
| 127 | - aclDestroyTensor(out); | 131 | + aclDestroyTensor(out); |
| 128 | 132 | ||
| 129 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 133 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 130 | - aclrtFree(selfDeviceAddr); | 134 | + aclrtFree(selfDeviceAddr); |
| 131 | - aclrtFree(outDeviceAddr); | 135 | + aclrtFree(outDeviceAddr); |
| 132 | - if (workspaceSize > 0) { | 136 | + if (workspaceSize > 0) { |
| 133 | - aclrtFree(workspaceAddr); | 137 | + aclrtFree(workspaceAddr); |
| 134 | - } | 138 | + } |
| 135 | - aclrtDestroyStream(stream); | 139 | + aclrtDestroyStream(stream); |
| 136 | - aclrtResetDevice(deviceId); | 140 | + aclrtResetDevice(deviceId); |
| 137 | - aclFinalize(); | 141 | + aclFinalize(); |
| 138 | - return 0; | 142 | + return 0; |
| 139 | -} | 143 | +} |
| @@ -467,7 +467,7 @@ int main() { | |||
| 467 | void* workspaceAddr = nullptr; | 467 | void* workspaceAddr = nullptr; |
| 468 | if (workspaceSize > 0) { | 468 | if (workspaceSize > 0) { |
| 469 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 469 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 470 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 470 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 471 | } | 471 | } |
| 472 | // 调用aclnnLogicalNot第二段接口 | 472 | // 调用aclnnLogicalNot第二段接口 |
| 473 | ret = aclnnLogicalNot(workspaceAddr, workspaceSize, executor, stream); | 473 | ret = aclnnLogicalNot(workspaceAddr, workspaceSize, executor, stream); |
| @@ -493,7 +493,7 @@ int main() { | |||
| 493 | // 根据第一段接口计算出的workspaceSize申请device内存 | 493 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 494 | if (workspaceSize > 0) { | 494 | if (workspaceSize > 0) { |
| 495 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 495 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 496 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 496 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 497 | } | 497 | } |
| 498 | // 调用aclnnInplaceLogicalNot第二段接口 | 498 | // 调用aclnnInplaceLogicalNot第二段接口 |
| 499 | ret = aclnnInplaceLogicalNot(workspaceAddr, workspaceSize, executor, stream); | 499 | ret = aclnnInplaceLogicalNot(workspaceAddr, workspaceSize, executor, stream); |
| @@ -18,18 +18,19 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | - do { \ | 21 | + do { \ |
| 22 | - if (!(cond)) { \ | 22 | + if (!(cond)) { \ |
| 23 | - return_expr; \ | 23 | + return_expr; \ |
| 24 | - } \ | 24 | + } \ |
| 25 | - } while (0) | 25 | + } while (0) |
| 26 | 26 | ||
| 27 | -#define LOG_PRINT(message, ...) \ | 27 | +#define LOG_PRINT(message, ...) \ |
| 28 | - do { \ | 28 | + do { \ |
| 29 | - printf(message, ##__VA_ARGS__); \ | 29 | + printf(message, ##__VA_ARGS__); \ |
| 30 | - } while (0) | 30 | + } while (0) |
| 31 | 31 | ||
| 32 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 32 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 33 | +{ | ||
| 33 | int64_t shape_size = 1; | 34 | int64_t shape_size = 1; |
| 34 | for (auto i : shape) { | 35 | for (auto i : shape) { |
| 35 | shape_size *= i; | 36 | shape_size *= i; |
| @@ -37,7 +38,8 @@ int64_t GetShapeSize(const std::vector<int64_t>& shape) { | |||
| 37 | return shape_size; | 38 | return shape_size; |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 40 | -int Init(int32_t deviceId, aclrtStream* stream) { | 41 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 42 | +{ | ||
| 41 | // 固定写法,资源初始化 | 43 | // 固定写法,资源初始化 |
| 42 | auto ret = aclInit(nullptr); | 44 | auto ret = aclInit(nullptr); |
| 43 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 45 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| @@ -50,7 +52,8 @@ int Init(int32_t deviceId, aclrtStream* stream) { | |||
| 50 | 52 | ||
| 51 | template <typename T> | 53 | template <typename T> |
| 52 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 54 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 53 | - aclDataType dataType, aclTensor** tensor) { | 55 | + aclDataType dataType, aclTensor** tensor) |
| 56 | +{ | ||
| 54 | auto size = GetShapeSize(shape) * sizeof(T); | 57 | auto size = GetShapeSize(shape) * sizeof(T); |
| 55 | // 调用aclrtMalloc申请device侧内存 | 58 | // 调用aclrtMalloc申请device侧内存 |
| 56 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 59 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| @@ -59,20 +62,21 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& | |||
| 59 | // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 62 | // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 60 | ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 63 | ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 61 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 64 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); |
| 62 | - | 65 | + |
| 63 | // 计算连续tensor的strides | 66 | // 计算连续tensor的strides |
| 64 | std::vector<int64_t> strides(shape.size(), 1); | 67 | std::vector<int64_t> strides(shape.size(), 1); |
| 65 | for (int64_t i = shape.size() - 2; i >= 0; i--) { | 68 | for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 66 | strides[i] = shape[i + 1] * strides[i + 1]; | 69 | strides[i] = shape[i + 1] * strides[i + 1]; |
| 67 | } | 70 | } |
| 68 | - | 71 | + |
| 69 | // 调用aclCreateTensor接口创建aclTensor | 72 | // 调用aclCreateTensor接口创建aclTensor |
| 70 | *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 73 | *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 71 | shape.data(), shape.size(), *deviceAddr); | 74 | shape.data(), shape.size(), *deviceAddr); |
| 72 | return 0; | 75 | return 0; |
| 73 | } | 76 | } |
| 74 | 77 | ||
| 75 | -int main() { | 78 | +int main() |
| 79 | +{ | ||
| 76 | // 1. 固定写法,device/stream初始化, 参考acl API手册 | 80 | // 1. 固定写法,device/stream初始化, 参考acl API手册 |
| 77 | // 根据自己的实际device填写deviceId | 81 | // 根据自己的实际device填写deviceId |
| 78 | int32_t deviceId = 0; | 82 | int32_t deviceId = 0; |
| @@ -88,20 +92,20 @@ int main() { | |||
| 88 | void* outDeviceAddr = nullptr; | 92 | void* outDeviceAddr = nullptr; |
| 89 | aclTensor* self = nullptr; | 93 | aclTensor* self = nullptr; |
| 90 | aclTensor* out = nullptr; | 94 | aclTensor* out = nullptr; |
| 91 | - | 95 | + |
| 92 | std::vector<int> selfHostData = {0, 1, 0, 2, 0, 3, 4, 0, 0}; | 96 | std::vector<int> selfHostData = {0, 1, 0, 2, 0, 3, 4, 0, 0}; |
| 93 | - std::vector<int> outHostData ={0, 0, 0, 0, 0, 0, 0, 0, 0}; | 97 | + std::vector<int> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 94 | - | 98 | + |
| 95 | // 创建self aclTensor | 99 | // 创建self aclTensor |
| 96 | ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT32, &self); | 100 | ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT32, &self); |
| 97 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 101 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 98 | // 创建out aclTensor | 102 | // 创建out aclTensor |
| 99 | ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT32, &out); | 103 | ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT32, &out); |
| 100 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 104 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 101 | - | 105 | + |
| 102 | uint64_t workspaceSize = 0; | 106 | uint64_t workspaceSize = 0; |
| 103 | aclOpExecutor* executor; | 107 | aclOpExecutor* executor; |
| 104 | - | 108 | + |
| 105 | // aclnnLogicalNot接口调用示例 | 109 | // aclnnLogicalNot接口调用示例 |
| 106 | // 3. 调用aclnnLogicalNot第一段接口 | 110 | // 3. 调用aclnnLogicalNot第一段接口 |
| 107 | ret = aclnnLogicalNotGetWorkspaceSize(self, out, &workspaceSize, &executor); | 111 | ret = aclnnLogicalNotGetWorkspaceSize(self, out, &workspaceSize, &executor); |
| @@ -110,7 +114,7 @@ int main() { | |||
| 110 | void* workspaceAddr = nullptr; | 114 | void* workspaceAddr = nullptr; |
| 111 | if (workspaceSize > 0) { | 115 | if (workspaceSize > 0) { |
| 112 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 116 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 113 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 117 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 114 | } | 118 | } |
| 115 | // 调用aclnnLogicalNot第二段接口 | 119 | // 调用aclnnLogicalNot第二段接口 |
| 116 | ret = aclnnLogicalNot(workspaceAddr, workspaceSize, executor, stream); | 120 | ret = aclnnLogicalNot(workspaceAddr, workspaceSize, executor, stream); |
| @@ -121,22 +125,23 @@ int main() { | |||
| 121 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 125 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 122 | auto size = GetShapeSize(outShape); | 126 | auto size = GetShapeSize(outShape); |
| 123 | std::vector<int> resultData(size, 0); | 127 | std::vector<int> resultData(size, 0); |
| 124 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 128 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 125 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 129 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 126 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 130 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 127 | - | 131 | + |
| 128 | for (int64_t i = 0; i < size; i++) { | 132 | for (int64_t i = 0; i < size; i++) { |
| 129 | LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); | 133 | LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); |
| 130 | } | 134 | } |
| 131 | - | 135 | + |
| 132 | // aclnnInplaceLogicalNot接口调用示例 | 136 | // aclnnInplaceLogicalNot接口调用示例 |
| 133 | // step3. 调用aclnnInplaceLogicalNot第一段接口 | 137 | // step3. 调用aclnnInplaceLogicalNot第一段接口 |
| 134 | ret = aclnnInplaceLogicalNotGetWorkspaceSize(self, &workspaceSize, &executor); | 138 | ret = aclnnInplaceLogicalNotGetWorkspaceSize(self, &workspaceSize, &executor); |
| 135 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceLogicalNotGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 139 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceLogicalNotGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 140 | + return ret); | ||
| 136 | // 根据第一段接口计算出的workspaceSize申请device内存 | 141 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 137 | if (workspaceSize > 0) { | 142 | if (workspaceSize > 0) { |
| 138 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 143 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 139 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 144 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 140 | } | 145 | } |
| 141 | // 调用aclnnInplaceLogicalNot第二段接口 | 146 | // 调用aclnnInplaceLogicalNot第二段接口 |
| 142 | ret = aclnnInplaceLogicalNot(workspaceAddr, workspaceSize, executor, stream); | 147 | ret = aclnnInplaceLogicalNot(workspaceAddr, workspaceSize, executor, stream); |
| @@ -145,27 +150,27 @@ int main() { | |||
| 145 | ret = aclrtSynchronizeStream(stream); | 150 | ret = aclrtSynchronizeStream(stream); |
| 146 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 151 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 147 | // step5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 152 | // step5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 148 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, | 153 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, |
| 149 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 154 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 150 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 155 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 151 | - | 156 | + |
| 152 | for (int64_t i = 0; i < size; i++) { | 157 | for (int64_t i = 0; i < size; i++) { |
| 153 | LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); | 158 | LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); |
| 154 | } | 159 | } |
| 155 | - | 160 | + |
| 156 | // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 161 | // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 157 | aclDestroyTensor(self); | 162 | aclDestroyTensor(self); |
| 158 | aclDestroyTensor(out); | 163 | aclDestroyTensor(out); |
| 159 | - | 164 | + |
| 160 | // 7. 释放device 资源 | 165 | // 7. 释放device 资源 |
| 161 | aclrtFree(selfDeviceAddr); | 166 | aclrtFree(selfDeviceAddr); |
| 162 | aclrtFree(outDeviceAddr); | 167 | aclrtFree(outDeviceAddr); |
| 163 | if (workspaceSize > 0) { | 168 | if (workspaceSize > 0) { |
| 164 | - aclrtFree(workspaceAddr); | 169 | + aclrtFree(workspaceAddr); |
| 165 | } | 170 | } |
| 166 | aclrtDestroyStream(stream); | 171 | aclrtDestroyStream(stream); |
| 167 | aclrtResetDevice(deviceId); | 172 | aclrtResetDevice(deviceId); |
| 168 | aclFinalize(); | 173 | aclFinalize(); |
| 169 | - | 174 | + |
| 170 | return 0; | 175 | return 0; |
| 171 | -} | 176 | +} |
| @@ -464,7 +464,7 @@ int main() { | |||
| 464 | void* workspaceAddr = nullptr; | 464 | void* workspaceAddr = nullptr; |
| 465 | if (workspaceSize > 0) { | 465 | if (workspaceSize > 0) { |
| 466 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 466 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 467 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 467 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 468 | } | 468 | } |
| 469 | // 调用aclnnRound第二段接口 | 469 | // 调用aclnnRound第二段接口 |
| 470 | ret = aclnnRound(workspaceAddr, workspaceSize, executor, stream); | 470 | ret = aclnnRound(workspaceAddr, workspaceSize, executor, stream); |
| @@ -491,7 +491,7 @@ int main() { | |||
| 491 | // 根据第一段接口计算出的workspaceSize申请device内存 | 491 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 492 | if (workspaceSize > 0) { | 492 | if (workspaceSize > 0) { |
| 493 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 493 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 494 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 494 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 495 | } | 495 | } |
| 496 | // 调用aclnnInplaceRound第二段接口 | 496 | // 调用aclnnInplaceRound第二段接口 |
| 497 | ret = aclnnInplaceRound(workspaceAddr, workspaceSize, executor, stream); | 497 | ret = aclnnInplaceRound(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,18 +14,19 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | +{ | ||
| 29 | int64_t shape_size = 1; | 30 | int64_t shape_size = 1; |
| 30 | for (auto i : shape) { | 31 | for (auto i : shape) { |
| 31 | shape_size *= i; | 32 | shape_size *= i; |
| @@ -33,7 +34,8 @@ int64_t GetShapeSize(const std::vector<int64_t>& shape) { | |||
| 33 | return shape_size; | 34 | return shape_size; |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 38 | +{ | ||
| 37 | // 固定写法,资源初始化 | 39 | // 固定写法,资源初始化 |
| 38 | auto ret = aclInit(nullptr); | 40 | auto ret = aclInit(nullptr); |
| 39 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 41 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| @@ -46,7 +48,8 @@ int Init(int32_t deviceId, aclrtStream* stream) { | |||
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 52 | +{ | ||
| 50 | auto size = GetShapeSize(shape) * sizeof(T); | 53 | auto size = GetShapeSize(shape) * sizeof(T); |
| 51 | // 调用aclrtMalloc申请device侧内存 | 54 | // 调用aclrtMalloc申请device侧内存 |
| 52 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 55 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| @@ -68,7 +71,8 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& | |||
| 68 | return 0; | 71 | return 0; |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -int main() { | 74 | +int main() |
| 75 | +{ | ||
| 72 | // 1. 固定写法,device/stream初始化, 参考acl API手册 | 76 | // 1. 固定写法,device/stream初始化, 参考acl API手册 |
| 73 | // 根据自己的实际device填写deviceId | 77 | // 根据自己的实际device填写deviceId |
| 74 | int32_t deviceId = 0; | 78 | int32_t deviceId = 0; |
| @@ -105,7 +109,7 @@ int main() { | |||
| 105 | // 根据第一段接口计算出的workspaceSize申请device内存 | 109 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 106 | if (workspaceSize > 0) { | 110 | if (workspaceSize > 0) { |
| 107 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 111 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 108 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 112 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 109 | } | 113 | } |
| 110 | // 调用aclnnInplaceRound第二段接口 | 114 | // 调用aclnnInplaceRound第二段接口 |
| 111 | ret = aclnnInplaceRound(workspaceAddr, workspaceSize, executor, stream); | 115 | ret = aclnnInplaceRound(workspaceAddr, workspaceSize, executor, stream); |
| @@ -116,7 +120,7 @@ int main() { | |||
| 116 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 120 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 117 | auto size = GetShapeSize(outShape); | 121 | auto size = GetShapeSize(outShape); |
| 118 | std::vector<float> resultData(size, 0); | 122 | std::vector<float> resultData(size, 0); |
| 119 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, | 123 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, |
| 120 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 124 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 121 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 125 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 122 | 126 | ||
| @@ -132,11 +136,11 @@ int main() { | |||
| 132 | aclrtFree(selfDeviceAddr); | 136 | aclrtFree(selfDeviceAddr); |
| 133 | aclrtFree(outDeviceAddr); | 137 | aclrtFree(outDeviceAddr); |
| 134 | if (workspaceSize > 0) { | 138 | if (workspaceSize > 0) { |
| 135 | - aclrtFree(workspaceAddr); | 139 | + aclrtFree(workspaceAddr); |
| 136 | } | 140 | } |
| 137 | aclrtDestroyStream(stream); | 141 | aclrtDestroyStream(stream); |
| 138 | aclrtResetDevice(deviceId); | 142 | aclrtResetDevice(deviceId); |
| 139 | aclFinalize(); | 143 | aclFinalize(); |
| 140 | 144 | ||
| 141 | return 0; | 145 | return 0; |
| 142 | -} | 146 | +} |
| @@ -14,18 +14,19 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | +{ | ||
| 29 | int64_t shape_size = 1; | 30 | int64_t shape_size = 1; |
| 30 | for (auto i : shape) { | 31 | for (auto i : shape) { |
| 31 | shape_size *= i; | 32 | shape_size *= i; |
| @@ -33,7 +34,8 @@ int64_t GetShapeSize(const std::vector<int64_t>& shape) { | |||
| 33 | return shape_size; | 34 | return shape_size; |
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 38 | +{ | ||
| 37 | // 固定写法,资源初始化 | 39 | // 固定写法,资源初始化 |
| 38 | auto ret = aclInit(nullptr); | 40 | auto ret = aclInit(nullptr); |
| 39 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 41 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| @@ -46,7 +48,8 @@ int Init(int32_t deviceId, aclrtStream* stream) { | |||
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 52 | +{ | ||
| 50 | auto size = GetShapeSize(shape) * sizeof(T); | 53 | auto size = GetShapeSize(shape) * sizeof(T); |
| 51 | // 调用aclrtMalloc申请device侧内存 | 54 | // 调用aclrtMalloc申请device侧内存 |
| 52 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 55 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| @@ -68,7 +71,8 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& | |||
| 68 | return 0; | 71 | return 0; |
| 69 | } | 72 | } |
| 70 | 73 | ||
| 71 | -int main() { | 74 | +int main() |
| 75 | +{ | ||
| 72 | // 1. 固定写法,device/stream初始化, 参考acl API手册 | 76 | // 1. 固定写法,device/stream初始化, 参考acl API手册 |
| 73 | // 根据自己的实际device填写deviceId | 77 | // 根据自己的实际device填写deviceId |
| 74 | int32_t deviceId = 0; | 78 | int32_t deviceId = 0; |
| @@ -105,7 +109,7 @@ int main() { | |||
| 105 | void* workspaceAddr = nullptr; | 109 | void* workspaceAddr = nullptr; |
| 106 | if (workspaceSize > 0) { | 110 | if (workspaceSize > 0) { |
| 107 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 111 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 108 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 112 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 109 | } | 113 | } |
| 110 | // 调用aclnnRound第二段接口 | 114 | // 调用aclnnRound第二段接口 |
| 111 | ret = aclnnRound(workspaceAddr, workspaceSize, executor, stream); | 115 | ret = aclnnRound(workspaceAddr, workspaceSize, executor, stream); |
| @@ -116,7 +120,7 @@ int main() { | |||
| 116 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 120 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 117 | auto size = GetShapeSize(outShape); | 121 | auto size = GetShapeSize(outShape); |
| 118 | std::vector<float> resultData(size, 0); | 122 | std::vector<float> resultData(size, 0); |
| 119 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 123 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 120 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 124 | size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 121 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 125 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 122 | 126 | ||
| @@ -132,11 +136,11 @@ int main() { | |||
| 132 | aclrtFree(selfDeviceAddr); | 136 | aclrtFree(selfDeviceAddr); |
| 133 | aclrtFree(outDeviceAddr); | 137 | aclrtFree(outDeviceAddr); |
| 134 | if (workspaceSize > 0) { | 138 | if (workspaceSize > 0) { |
| 135 | - aclrtFree(workspaceAddr); | 139 | + aclrtFree(workspaceAddr); |
| 136 | } | 140 | } |
| 137 | aclrtDestroyStream(stream); | 141 | aclrtDestroyStream(stream); |
| 138 | aclrtResetDevice(deviceId); | 142 | aclrtResetDevice(deviceId); |
| 139 | aclFinalize(); | 143 | aclFinalize(); |
| 140 | 144 | ||
| 141 | return 0; | 145 | return 0; |
| 142 | -} | 146 | +} |
| @@ -489,7 +489,7 @@ int main() { | |||
| 489 | void* inplaceWorkspaceAddr = nullptr; | 489 | void* inplaceWorkspaceAddr = nullptr; |
| 490 | if (inplaceWorkspaceSize > 0) { | 490 | if (inplaceWorkspaceSize > 0) { |
| 491 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 491 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 492 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 492 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 493 | } | 493 | } |
| 494 | // 调用aclnnInplaceSin第二段接口 | 494 | // 调用aclnnInplaceSin第二段接口 |
| 495 | ret = aclnnInplaceSin(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 495 | ret = aclnnInplaceSin(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| @@ -14,149 +14,153 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | - int64_t shapeSize = 1; | 29 | +{ |
| 30 | - for (auto i : shape) { | 30 | + int64_t shapeSize = 1; |
| 31 | - shapeSize *= i; | 31 | + for (auto i : shape) { |
| 32 | - } | 32 | + shapeSize *= i; |
| 33 | - return shapeSize; | 33 | + } |
| 34 | + return shapeSize; | ||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | - // 固定写法,资源初始化 | 38 | +{ |
| 38 | - auto ret = aclInit(nullptr); | 39 | + // 固定写法,资源初始化 |
| 39 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - ret = aclrtSetDevice(deviceId); | 41 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 41 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - ret = aclrtCreateStream(stream); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - return 0; | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 46 | + return 0; | ||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 50 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 51 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 52 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 53 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 55 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 54 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 56 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); |
| 55 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 57 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 56 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 58 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 59 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | ||
| 57 | 60 | ||
| 58 | - // 计算连续tensor的strides | 61 | + // 计算连续tensor的strides |
| 59 | - std::vector<int64_t> strides(shape.size(), 1); | 62 | + std::vector<int64_t> strides(shape.size(), 1); |
| 60 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 63 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 61 | - strides[i] = shape[i + 1] * strides[i + 1]; | 64 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 62 | - } | 65 | + } |
| 63 | 66 | ||
| 64 | - // 调用aclCreateTensor接口创建aclTensor | 67 | + // 调用aclCreateTensor接口创建aclTensor |
| 65 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 68 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 66 | - shape.data(), shape.size(), *deviceAddr); | 69 | + shape.data(), shape.size(), *deviceAddr); |
| 67 | - return 0; | 70 | + return 0; |
| 68 | } | 71 | } |
| 69 | 72 | ||
| 70 | -int main() { | 73 | +int main() |
| 71 | - // 1. (固定写法)device/stream初始化,参考acl API手册 | 74 | +{ |
| 72 | - // 根据自己的实际device填写deviceId | 75 | + // 1. (固定写法)device/stream初始化,参考acl API手册 |
| 73 | - int32_t deviceId = 0; | 76 | + // 根据自己的实际device填写deviceId |
| 74 | - aclrtStream stream; | 77 | + int32_t deviceId = 0; |
| 75 | - auto ret = Init(deviceId, &stream); | 78 | + aclrtStream stream; |
| 76 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 79 | + auto ret = Init(deviceId, &stream); |
| 80 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | ||
| 77 | 81 | ||
| 78 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 79 | - std::vector<int64_t> selfShape = {4, 2}; | 83 | + std::vector<int64_t> selfShape = {4, 2}; |
| 80 | - std::vector<int64_t> outShape = {4, 2}; | 84 | + std::vector<int64_t> outShape = {4, 2}; |
| 81 | - void* selfDeviceAddr = nullptr; | 85 | + void* selfDeviceAddr = nullptr; |
| 82 | - void* outDeviceAddr = nullptr; | 86 | + void* outDeviceAddr = nullptr; |
| 83 | - aclTensor* self = nullptr; | 87 | + aclTensor* self = nullptr; |
| 84 | - aclTensor* out = nullptr; | 88 | + aclTensor* out = nullptr; |
| 85 | - std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 89 | + std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 86 | - std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 90 | + std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 87 | - // 创建self aclTensor | 91 | + // 创建self aclTensor |
| 88 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 92 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 89 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 93 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 90 | - // 创建out aclTensor | 94 | + // 创建out aclTensor |
| 91 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 95 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 92 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 96 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 93 | 97 | ||
| 94 | - // 3. 调用CANN算子库API,需要修改为具体的Api名称 | 98 | + // 3. 调用CANN算子库API,需要修改为具体的Api名称 |
| 95 | - uint64_t workspaceSize = 0; | 99 | + uint64_t workspaceSize = 0; |
| 96 | - aclOpExecutor* executor; | 100 | + aclOpExecutor* executor; |
| 97 | - // 调用aclnnSin第一段接口 | 101 | + // 调用aclnnSin第一段接口 |
| 98 | - ret = aclnnSinGetWorkspaceSize(self, out, &workspaceSize, &executor); | 102 | + ret = aclnnSinGetWorkspaceSize(self, out, &workspaceSize, &executor); |
| 99 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSinGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 103 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSinGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 100 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 104 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 101 | - void* workspaceAddr = nullptr; | 105 | + void* workspaceAddr = nullptr; |
| 102 | - if (workspaceSize > 0) { | 106 | + if (workspaceSize > 0) { |
| 103 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 107 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 104 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | 108 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 105 | - } | 109 | + } |
| 106 | - // 调用aclnnSin第二段接口 | 110 | + // 调用aclnnSin第二段接口 |
| 107 | - ret = aclnnSin(workspaceAddr, workspaceSize, executor, stream); | 111 | + ret = aclnnSin(workspaceAddr, workspaceSize, executor, stream); |
| 108 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSin failed. ERROR: %d\n", ret); return ret); | 112 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSin failed. ERROR: %d\n", ret); return ret); |
| 109 | 113 | ||
| 110 | - uint64_t inplaceWorkspaceSize = 0; | 114 | + uint64_t inplaceWorkspaceSize = 0; |
| 111 | - aclOpExecutor* inplaceExecutor; | 115 | + aclOpExecutor* inplaceExecutor; |
| 112 | - // 调用aclnnInplaceSin第一段接口 | 116 | + // 调用aclnnInplaceSin第一段接口 |
| 113 | - ret = aclnnInplaceSinGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); | 117 | + ret = aclnnInplaceSinGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); |
| 114 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceSinGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceSinGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 115 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 119 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 116 | - void* inplaceWorkspaceAddr = nullptr; | 120 | + void* inplaceWorkspaceAddr = nullptr; |
| 117 | - if (inplaceWorkspaceSize > 0) { | 121 | + if (inplaceWorkspaceSize > 0) { |
| 118 | - ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 122 | + ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 123 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 120 | - } | 124 | + } |
| 121 | - // 调用aclnnInplaceSin第二段接口 | 125 | + // 调用aclnnInplaceSin第二段接口 |
| 122 | - ret = aclnnInplaceSin(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 126 | + ret = aclnnInplaceSin(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| 123 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceSin failed. ERROR: %d\n", ret); return ret); | 127 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceSin failed. ERROR: %d\n", ret); return ret); |
| 124 | 128 | ||
| 125 | - // 4. (固定写法)同步等待任务执行结束 | 129 | + // 4. (固定写法)同步等待任务执行结束 |
| 126 | - ret = aclrtSynchronizeStream(stream); | 130 | + ret = aclrtSynchronizeStream(stream); |
| 127 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 131 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 128 | 132 | ||
| 129 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 133 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 130 | - auto size = GetShapeSize(outShape); | 134 | + auto size = GetShapeSize(outShape); |
| 131 | - std::vector<float> resultData(size, 0); | 135 | + std::vector<float> resultData(size, 0); |
| 132 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 136 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 133 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 137 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 134 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 138 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 135 | - for (int64_t i = 0; i < size; i++) { | 139 | + for (int64_t i = 0; i < size; i++) { |
| 136 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 140 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 137 | - } | 141 | + } |
| 138 | 142 | ||
| 139 | - auto inplaceSize = GetShapeSize(selfShape); | 143 | + auto inplaceSize = GetShapeSize(selfShape); |
| 140 | - std::vector<float> inplaceResultData(inplaceSize, 0); | 144 | + std::vector<float> inplaceResultData(inplaceSize, 0); |
| 141 | - ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, | 145 | + ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, |
| 142 | - inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | 146 | + inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); |
| 143 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 147 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 144 | - for (int64_t i = 0; i < inplaceSize; i++) { | 148 | + for (int64_t i = 0; i < inplaceSize; i++) { |
| 145 | - LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); | 149 | + LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); |
| 146 | - } | 150 | + } |
| 147 | 151 | ||
| 148 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 152 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 149 | - aclDestroyTensor(self); | 153 | + aclDestroyTensor(self); |
| 150 | - aclDestroyTensor(out); | 154 | + aclDestroyTensor(out); |
| 151 | 155 | ||
| 152 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 156 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 153 | - aclrtFree(selfDeviceAddr); | 157 | + aclrtFree(selfDeviceAddr); |
| 154 | - aclrtFree(outDeviceAddr); | 158 | + aclrtFree(outDeviceAddr); |
| 155 | - if (workspaceSize > 0) { | 159 | + if (workspaceSize > 0) { |
| 156 | - aclrtFree(workspaceAddr); | 160 | + aclrtFree(workspaceAddr); |
| 157 | - } | 161 | + } |
| 158 | - aclrtDestroyStream(stream); | 162 | + aclrtDestroyStream(stream); |
| 159 | - aclrtResetDevice(deviceId); | 163 | + aclrtResetDevice(deviceId); |
| 160 | - aclFinalize(); | 164 | + aclFinalize(); |
| 161 | - return 0; | 165 | + return 0; |
| 162 | -} | 166 | +} |
| @@ -481,7 +481,7 @@ int main() { | |||
| 481 | void* inplaceWorkspaceAddr = nullptr; | 481 | void* inplaceWorkspaceAddr = nullptr; |
| 482 | if (inplaceWorkspaceSize > 0) { | 482 | if (inplaceWorkspaceSize > 0) { |
| 483 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 483 | ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 484 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 484 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 485 | } | 485 | } |
| 486 | // 调用aclnnInplaceTrunc第二段接口 | 486 | // 调用aclnnInplaceTrunc第二段接口 |
| 487 | ret = aclnnInplaceTrunc(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 487 | ret = aclnnInplaceTrunc(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| @@ -14,149 +14,153 @@ | |||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | - do { \ | 17 | + do { \ |
| 18 | - if (!(cond)) { \ | 18 | + if (!(cond)) { \ |
| 19 | - return_expr; \ | 19 | + return_expr; \ |
| 20 | - } \ | 20 | + } \ |
| 21 | - } while (0) | 21 | + } while (0) |
| 22 | 22 | ||
| 23 | -#define LOG_PRINT(message, ...) \ | 23 | +#define LOG_PRINT(message, ...) \ |
| 24 | - do { \ | 24 | + do { \ |
| 25 | - printf(message, ##__VA_ARGS__); \ | 25 | + printf(message, ##__VA_ARGS__); \ |
| 26 | - } while (0) | 26 | + } while (0) |
| 27 | 27 | ||
| 28 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 28 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 29 | - int64_t shapeSize = 1; | 29 | +{ |
| 30 | - for (auto i : shape) { | 30 | + int64_t shapeSize = 1; |
| 31 | - shapeSize *= i; | 31 | + for (auto i : shape) { |
| 32 | - } | 32 | + shapeSize *= i; |
| 33 | - return shapeSize; | 33 | + } |
| 34 | + return shapeSize; | ||
| 34 | } | 35 | } |
| 35 | 36 | ||
| 36 | -int Init(int32_t deviceId, aclrtStream* stream) { | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | - // 固定写法,资源初始化 | 38 | +{ |
| 38 | - auto ret = aclInit(nullptr); | 39 | + // 固定写法,资源初始化 |
| 39 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - ret = aclrtSetDevice(deviceId); | 41 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 41 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - ret = aclrtCreateStream(stream); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - return 0; | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 46 | + return 0; | ||
| 45 | } | 47 | } |
| 46 | 48 | ||
| 47 | template <typename T> | 49 | template <typename T> |
| 48 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 50 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 49 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 50 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 51 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 52 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 53 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 55 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 54 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 56 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); |
| 55 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 57 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 56 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 58 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 59 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | ||
| 57 | 60 | ||
| 58 | - // 计算连续tensor的strides | 61 | + // 计算连续tensor的strides |
| 59 | - std::vector<int64_t> strides(shape.size(), 1); | 62 | + std::vector<int64_t> strides(shape.size(), 1); |
| 60 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 63 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 61 | - strides[i] = shape[i + 1] * strides[i + 1]; | 64 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 62 | - } | 65 | + } |
| 63 | 66 | ||
| 64 | - // 调用aclCreateTensor接口创建aclTensor | 67 | + // 调用aclCreateTensor接口创建aclTensor |
| 65 | - *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, | 68 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 66 | - shape.data(), shape.size(), *deviceAddr); | 69 | + shape.data(), shape.size(), *deviceAddr); |
| 67 | - return 0; | 70 | + return 0; |
| 68 | } | 71 | } |
| 69 | 72 | ||
| 70 | -int main() { | 73 | +int main() |
| 71 | - // 1. (固定写法)device/stream初始化,参考acl API手册 | 74 | +{ |
| 72 | - // 根据自己的实际device填写deviceId | 75 | + // 1. (固定写法)device/stream初始化,参考acl API手册 |
| 73 | - int32_t deviceId = 0; | 76 | + // 根据自己的实际device填写deviceId |
| 74 | - aclrtStream stream; | 77 | + int32_t deviceId = 0; |
| 75 | - auto ret = Init(deviceId, &stream); | 78 | + aclrtStream stream; |
| 76 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 79 | + auto ret = Init(deviceId, &stream); |
| 80 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | ||
| 77 | 81 | ||
| 78 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 79 | - std::vector<int64_t> selfShape = {4, 2}; | 83 | + std::vector<int64_t> selfShape = {4, 2}; |
| 80 | - std::vector<int64_t> outShape = {4, 2}; | 84 | + std::vector<int64_t> outShape = {4, 2}; |
| 81 | - void* selfDeviceAddr = nullptr; | 85 | + void* selfDeviceAddr = nullptr; |
| 82 | - void* outDeviceAddr = nullptr; | 86 | + void* outDeviceAddr = nullptr; |
| 83 | - aclTensor* self = nullptr; | 87 | + aclTensor* self = nullptr; |
| 84 | - aclTensor* out = nullptr; | 88 | + aclTensor* out = nullptr; |
| 85 | - std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 89 | + std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 86 | - std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 90 | + std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 87 | - // 创建self aclTensor | 91 | + // 创建self aclTensor |
| 88 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 92 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 89 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 93 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 90 | - // 创建out aclTensor | 94 | + // 创建out aclTensor |
| 91 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 95 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 92 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 96 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 93 | 97 | ||
| 94 | - // 3. 调用CANN算子库API,需要修改为具体的Api名称 | 98 | + // 3. 调用CANN算子库API,需要修改为具体的Api名称 |
| 95 | - uint64_t workspaceSize = 0; | 99 | + uint64_t workspaceSize = 0; |
| 96 | - aclOpExecutor* executor; | 100 | + aclOpExecutor* executor; |
| 97 | - // 调用aclnnTrunc第一段接口 | 101 | + // 调用aclnnTrunc第一段接口 |
| 98 | - ret = aclnnTruncGetWorkspaceSize(self, out, &workspaceSize, &executor); | 102 | + ret = aclnnTruncGetWorkspaceSize(self, out, &workspaceSize, &executor); |
| 99 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTruncGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 103 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTruncGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 100 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 104 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 101 | - void* workspaceAddr = nullptr; | 105 | + void* workspaceAddr = nullptr; |
| 102 | - if (workspaceSize > 0) { | 106 | + if (workspaceSize > 0) { |
| 103 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 107 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 104 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | 108 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 105 | - } | 109 | + } |
| 106 | - // 调用aclnnTrunc第二段接口 | 110 | + // 调用aclnnTrunc第二段接口 |
| 107 | - ret = aclnnTrunc(workspaceAddr, workspaceSize, executor, stream); | 111 | + ret = aclnnTrunc(workspaceAddr, workspaceSize, executor, stream); |
| 108 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTrunc failed. ERROR: %d\n", ret); return ret); | 112 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnTrunc failed. ERROR: %d\n", ret); return ret); |
| 109 | 113 | ||
| 110 | - uint64_t inplaceWorkspaceSize = 0; | 114 | + uint64_t inplaceWorkspaceSize = 0; |
| 111 | - aclOpExecutor* inplaceExecutor; | 115 | + aclOpExecutor* inplaceExecutor; |
| 112 | - // 调用aclnnInplaceTrunc第一段接口 | 116 | + // 调用aclnnInplaceTrunc第一段接口 |
| 113 | - ret = aclnnInplaceTruncGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); | 117 | + ret = aclnnInplaceTruncGetWorkspaceSize(self, &inplaceWorkspaceSize, &inplaceExecutor); |
| 114 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTruncGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTruncGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 115 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 119 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 116 | - void* inplaceWorkspaceAddr = nullptr; | 120 | + void* inplaceWorkspaceAddr = nullptr; |
| 117 | - if (inplaceWorkspaceSize > 0) { | 121 | + if (inplaceWorkspaceSize > 0) { |
| 118 | - ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 122 | + ret = aclrtMalloc(&inplaceWorkspaceAddr, inplaceWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 123 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 120 | - } | 124 | + } |
| 121 | - // 调用aclnnInplaceTrunc第二段接口 | 125 | + // 调用aclnnInplaceTrunc第二段接口 |
| 122 | - ret = aclnnInplaceTrunc(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); | 126 | + ret = aclnnInplaceTrunc(inplaceWorkspaceAddr, inplaceWorkspaceSize, inplaceExecutor, stream); |
| 123 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTrunc failed. ERROR: %d\n", ret); return ret); | 127 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceTrunc failed. ERROR: %d\n", ret); return ret); |
| 124 | 128 | ||
| 125 | - // 4. (固定写法)同步等待任务执行结束 | 129 | + // 4. (固定写法)同步等待任务执行结束 |
| 126 | - ret = aclrtSynchronizeStream(stream); | 130 | + ret = aclrtSynchronizeStream(stream); |
| 127 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 131 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 128 | 132 | ||
| 129 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 133 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 130 | - auto size = GetShapeSize(outShape); | 134 | + auto size = GetShapeSize(outShape); |
| 131 | - std::vector<float> resultData(size, 0); | 135 | + std::vector<float> resultData(size, 0); |
| 132 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 136 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 133 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 137 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 134 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 138 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 135 | - for (int64_t i = 0; i < size; i++) { | 139 | + for (int64_t i = 0; i < size; i++) { |
| 136 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 140 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 137 | - } | 141 | + } |
| 138 | 142 | ||
| 139 | - auto inplaceSize = GetShapeSize(selfShape); | 143 | + auto inplaceSize = GetShapeSize(selfShape); |
| 140 | - std::vector<float> inplaceResultData(inplaceSize, 0); | 144 | + std::vector<float> inplaceResultData(inplaceSize, 0); |
| 141 | - ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, | 145 | + ret = aclrtMemcpy(inplaceResultData.data(), inplaceResultData.size() * sizeof(inplaceResultData[0]), selfDeviceAddr, |
| 142 | - inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | 146 | + inplaceSize * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); |
| 143 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 147 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 144 | - for (int64_t i = 0; i < inplaceSize; i++) { | 148 | + for (int64_t i = 0; i < inplaceSize; i++) { |
| 145 | - LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); | 149 | + LOG_PRINT("inplaceResult[%ld] is: %f\n", i, inplaceResultData[i]); |
| 146 | - } | 150 | + } |
| 147 | 151 | ||
| 148 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 152 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 149 | - aclDestroyTensor(self); | 153 | + aclDestroyTensor(self); |
| 150 | - aclDestroyTensor(out); | 154 | + aclDestroyTensor(out); |
| 151 | 155 | ||
| 152 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 156 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 153 | - aclrtFree(selfDeviceAddr); | 157 | + aclrtFree(selfDeviceAddr); |
| 154 | - aclrtFree(outDeviceAddr); | 158 | + aclrtFree(outDeviceAddr); |
| 155 | - if (workspaceSize > 0) { | 159 | + if (workspaceSize > 0) { |
| 156 | - aclrtFree(workspaceAddr); | 160 | + aclrtFree(workspaceAddr); |
| 157 | - } | 161 | + } |
| 158 | - aclrtDestroyStream(stream); | 162 | + aclrtDestroyStream(stream); |
| 159 | - aclrtResetDevice(deviceId); | 163 | + aclrtResetDevice(deviceId); |
| 160 | - aclFinalize(); | 164 | + aclFinalize(); |
| 161 | - return 0; | 165 | + return 0; |
| 162 | -} | 166 | +} |


🟡 Medium Priority
变更行:
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);→CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret)(移除了末尾分号)受影响的代码合约: 该 .md 文档的代码示例中,
CHECK_RET宏在第 375–380 行定义为: #define CHECK_RET(cond, return_expr)do {
if (!(cond)) {
return_expr;
}
} while (0) 这是标准的
do-while(0)惯用法,宏体末尾while(0)之后没有分号,调用方必须添加分号。文档中的代码示例若被用户复制使用,缺少分号将导致编译错误。失效模式: 用户从文档复制示例代码后,
CHECK_RET(...)无尾分号展开为do { ... } while(0),C++ 编译器报 syntax error,用户无法编译通过。本文件与同批受影响的 .md 文件(均存在相同问题):
math/exp/docs/aclnnExp&aclnnInplaceExp.md第 478 行math/floor/docs/aclnnFloor&aclnnInplaceFloor.md第 472 行math/is_close/docs/aclnnIsClose.md第 380 行math/log/docs/aclnnLog10&aclnnInplaceLog10.md第 473 行math/logical_not/docs/aclnnLogicalNot&aclnnInplaceLogicalNot.md第 470、496 行math/round/docs/aclnnRound&aclnnInplaceRound.md第 467、494 行math/sin/docs/aclnnSin&aclnnInplaceSin.md第 492 行math/trunc/docs/aclnnTrunc&aclnnInplaceTrunc.md第 484 行修复方向: 恢复所有文档代码示例中
CHECK_RET(...)末尾的分号。建议:恢复文档代码示例中 CHECK_RET 宏调用末尾的必需分号,避免用户复制示例代码后编译失败。