已合并
fix: 规范示例代码格式 #4464
zhang-song-rui创建于 19 天前
fix: 规范示例代码格式 #4464
已合并
共 31 个文件变更+987-959
| @@ -334,7 +334,7 @@ int main() { | |||
| 334 | void* workspaceAddr = nullptr; | 334 | void* workspaceAddr = nullptr; |
| 335 | if (workspaceSize > 0) { | 335 | if (workspaceSize > 0) { |
| 336 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 336 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 337 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 337 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 338 | } | 338 | } |
| 339 | // 调用aclnnBitwiseAndTensor第二段接口 | 339 | // 调用aclnnBitwiseAndTensor第二段接口 |
| 340 | ret = aclnnBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); | 340 | ret = aclnnBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -502,7 +502,7 @@ int main() { | |||
| 502 | void* workspaceAddr = nullptr; | 502 | void* workspaceAddr = nullptr; |
| 503 | if (workspaceSize > 0) { | 503 | if (workspaceSize > 0) { |
| 504 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 504 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 505 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 505 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 506 | } | 506 | } |
| 507 | // 调用aclnnBitwiseAndTensorOut第二段接口 | 507 | // 调用aclnnBitwiseAndTensorOut第二段接口 |
| 508 | ret = aclnnBitwiseAndTensorOut(workspaceAddr, workspaceSize, executor, stream); | 508 | ret = aclnnBitwiseAndTensorOut(workspaceAddr, workspaceSize, executor, stream); |
| @@ -306,7 +306,7 @@ int main() { | |||
| 306 | void* workspaceAddr = nullptr; | 306 | void* workspaceAddr = nullptr; |
| 307 | if (workspaceSize > 0) { | 307 | if (workspaceSize > 0) { |
| 308 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 308 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 309 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 309 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 310 | } | 310 | } |
| 311 | // 调用aclnnInplaceBitwiseAndScalar第二段接口 | 311 | // 调用aclnnInplaceBitwiseAndScalar第二段接口 |
| 312 | ret = aclnnInplaceBitwiseAndScalar(workspaceAddr, workspaceSize, executor, stream); | 312 | ret = aclnnInplaceBitwiseAndScalar(workspaceAddr, workspaceSize, executor, stream); |
| @@ -311,7 +311,7 @@ int main() { | |||
| 311 | void* workspaceAddr = nullptr; | 311 | void* workspaceAddr = nullptr; |
| 312 | if (workspaceSize > 0) { | 312 | if (workspaceSize > 0) { |
| 313 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 313 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 314 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 314 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 315 | } | 315 | } |
| 316 | // 调用aclnnInplaceBitwiseAndTensor第二段接口 | 316 | // 调用aclnnInplaceBitwiseAndTensor第二段接口 |
| 317 | ret = aclnnInplaceBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); | 317 | ret = aclnnInplaceBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,133 +14,138 @@ | |||
| 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> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - std::vector<int64_t> outShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* selfDeviceAddr = nullptr; | 86 | + std::vector<int64_t> outShape = {4, 2}; |
| 84 | - void* otherDeviceAddr = nullptr; | 87 | + void* selfDeviceAddr = nullptr; |
| 85 | - void* outDeviceAddr = nullptr; | 88 | + void* otherDeviceAddr = nullptr; |
| 86 | - aclTensor* self = nullptr; | 89 | + void* outDeviceAddr = nullptr; |
| 87 | - aclTensor* other = nullptr; | 90 | + aclTensor* self = nullptr; |
| 88 | - aclTensor* out = nullptr; | 91 | + aclTensor* other = nullptr; |
| 89 | - std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 92 | + aclTensor* out = nullptr; |
| 90 | - std::vector<int64_t> otherHostData = {1, 1, 2, 3, 3, 3, 4, 4}; | 93 | + std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 91 | - std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 94 | + std::vector<int64_t> otherHostData = {1, 1, 2, 3, 3, 3, 4, 4}; |
| 92 | - // 创建self aclTensor | 95 | + std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 93 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); | 96 | + // 创建self aclTensor |
| 94 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 97 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); |
| 95 | - // 创建other aclTensor | 98 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 96 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); | 99 | + // 创建other aclTensor |
| 97 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 100 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); |
| 98 | - // 创建out aclTensor | 101 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 99 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); | 102 | + // 创建out aclTensor |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 103 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); |
| 104 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 101 | 105 | ||
| 102 | - // 3. 调用CANN算子库API,需要修改为具体的API | 106 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 103 | - uint64_t workspaceSize = 0; | 107 | + uint64_t workspaceSize = 0; |
| 104 | - aclOpExecutor* executor; | 108 | + aclOpExecutor* executor; |
| 105 | - // 调用aclnnBitwiseAndTensor第一段接口 | 109 | + // 调用aclnnBitwiseAndTensor第一段接口 |
| 106 | - ret = aclnnBitwiseAndTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); | 110 | + ret = aclnnBitwiseAndTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); |
| 107 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseAndTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 111 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseAndTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 108 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 112 | + return ret); |
| 109 | - void* workspaceAddr = nullptr; | 113 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 110 | - if (workspaceSize > 0) { | 114 | + void* workspaceAddr = nullptr; |
| 111 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 115 | + if (workspaceSize > 0) { |
| 112 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 116 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 113 | - } | 117 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 114 | - // 调用aclnnBitwiseAndTensor第二段接口 | 118 | + } |
| 115 | - ret = aclnnBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); | 119 | + // 调用aclnnBitwiseAndTensor第二段接口 |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseAndTensor failed. ERROR: %d\n", ret); return ret); | 120 | + ret = aclnnBitwiseAndTensor(workspaceAddr, workspaceSize, executor, stream); |
| 117 | - // 4. (固定写法)同步等待任务执行结束 | 121 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseAndTensor failed. ERROR: %d\n", ret); return ret); |
| 118 | - ret = aclrtSynchronizeStream(stream); | 122 | + // 4. (固定写法)同步等待任务执行结束 |
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 123 | + ret = aclrtSynchronizeStream(stream); |
| 120 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 124 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 121 | - auto size = GetShapeSize(outShape); | 125 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 122 | - std::vector<int64_t> resultData(size, 0); | 126 | + auto size = GetShapeSize(outShape); |
| 123 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(int64_t), | 127 | + std::vector<int64_t> resultData(size, 0); |
| 124 | - ACL_MEMCPY_DEVICE_TO_HOST); | 128 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 125 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 129 | + size * sizeof(int64_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 126 | - for (int64_t i = 0; i < size; i++) { | 130 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 127 | - LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); | 131 | + for (int64_t i = 0; i < size; i++) { |
| 128 | - } | 132 | + LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); |
| 133 | + } | ||
| 129 | 134 | ||
| 130 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 135 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 131 | - aclDestroyTensor(self); | 136 | + aclDestroyTensor(self); |
| 132 | - aclDestroyTensor(other); | 137 | + aclDestroyTensor(other); |
| 133 | - aclDestroyTensor(out); | 138 | + aclDestroyTensor(out); |
| 134 | 139 | ||
| 135 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 140 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 136 | - aclrtFree(selfDeviceAddr); | 141 | + aclrtFree(selfDeviceAddr); |
| 137 | - aclrtFree(otherDeviceAddr); | 142 | + aclrtFree(otherDeviceAddr); |
| 138 | - aclrtFree(outDeviceAddr); | 143 | + aclrtFree(outDeviceAddr); |
| 139 | - if (workspaceSize > 0) { | 144 | + if (workspaceSize > 0) { |
| 140 | - aclrtFree(workspaceAddr); | 145 | + aclrtFree(workspaceAddr); |
| 141 | - } | 146 | + } |
| 142 | - aclrtDestroyStream(stream); | 147 | + aclrtDestroyStream(stream); |
| 143 | - aclrtResetDevice(deviceId); | 148 | + aclrtResetDevice(deviceId); |
| 144 | - aclFinalize(); | 149 | + aclFinalize(); |
| 145 | - return 0; | 150 | + return 0; |
| 146 | -} | 151 | +} |
| @@ -512,7 +512,7 @@ int main() { | |||
| 512 | void* workspaceAddr = nullptr; | 512 | void* workspaceAddr = nullptr; |
| 513 | if (workspaceSize > 0) { | 513 | if (workspaceSize > 0) { |
| 514 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 514 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 515 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 515 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 516 | } | 516 | } |
| 517 | // 调用aclnnBitwiseOrTensor第二段接口 | 517 | // 调用aclnnBitwiseOrTensor第二段接口 |
| 518 | ret = aclnnBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); | 518 | ret = aclnnBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -650,7 +650,7 @@ int main() { | |||
| 650 | void* workspaceAddr = nullptr; | 650 | void* workspaceAddr = nullptr; |
| 651 | if (workspaceSize > 0) { | 651 | if (workspaceSize > 0) { |
| 652 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 652 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 653 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 653 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 654 | } | 654 | } |
| 655 | // 调用aclnnInplaceBitwiseOrTensor第二段接口 | 655 | // 调用aclnnInplaceBitwiseOrTensor第二段接口 |
| 656 | ret = aclnnInplaceBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); | 656 | ret = aclnnInplaceBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,133 +14,138 @@ | |||
| 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> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - std::vector<int64_t> outShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* selfDeviceAddr = nullptr; | 86 | + std::vector<int64_t> outShape = {4, 2}; |
| 84 | - void* otherDeviceAddr = nullptr; | 87 | + void* selfDeviceAddr = nullptr; |
| 85 | - void* outDeviceAddr = nullptr; | 88 | + void* otherDeviceAddr = nullptr; |
| 86 | - aclTensor* self = nullptr; | 89 | + void* outDeviceAddr = nullptr; |
| 87 | - aclTensor* other = nullptr; | 90 | + aclTensor* self = nullptr; |
| 88 | - aclTensor* out = nullptr; | 91 | + aclTensor* other = nullptr; |
| 89 | - std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 92 | + aclTensor* out = nullptr; |
| 90 | - std::vector<int64_t> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; | 93 | + std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 91 | - std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 94 | + std::vector<int64_t> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; |
| 92 | - // 创建self aclTensor | 95 | + std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 93 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); | 96 | + // 创建self aclTensor |
| 94 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 97 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); |
| 95 | - // 创建other aclTensor | 98 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 96 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); | 99 | + // 创建other aclTensor |
| 97 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 100 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); |
| 98 | - // 创建out aclTensor | 101 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 99 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); | 102 | + // 创建out aclTensor |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 103 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); |
| 104 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 101 | 105 | ||
| 102 | - // 3. 调用CANN算子库API,需要修改为具体的API | 106 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 103 | - uint64_t workspaceSize = 0; | 107 | + uint64_t workspaceSize = 0; |
| 104 | - aclOpExecutor* executor; | 108 | + aclOpExecutor* executor; |
| 105 | - // 调用aclnnBitwiseOrTensor第一段接口 | 109 | + // 调用aclnnBitwiseOrTensor第一段接口 |
| 106 | - ret = aclnnBitwiseOrTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); | 110 | + ret = aclnnBitwiseOrTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); |
| 107 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseOrTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 111 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseOrTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 108 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 112 | + return ret); |
| 109 | - void* workspaceAddr = nullptr; | 113 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 110 | - if (workspaceSize > 0) { | 114 | + void* workspaceAddr = nullptr; |
| 111 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 115 | + if (workspaceSize > 0) { |
| 112 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 116 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 113 | - } | 117 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 114 | - // 调用aclnnBitwiseOrTensor第二段接口 | 118 | + } |
| 115 | - ret = aclnnBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); | 119 | + // 调用aclnnBitwiseOrTensor第二段接口 |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseOrTensor failed. ERROR: %d\n", ret); return ret); | 120 | + ret = aclnnBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); |
| 117 | - // 4. (固定写法)同步等待任务执行结束 | 121 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseOrTensor failed. ERROR: %d\n", ret); return ret); |
| 118 | - ret = aclrtSynchronizeStream(stream); | 122 | + // 4. (固定写法)同步等待任务执行结束 |
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 123 | + ret = aclrtSynchronizeStream(stream); |
| 120 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 124 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 121 | - auto size = GetShapeSize(outShape); | 125 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 122 | - std::vector<int64_t> resultData(size, 0); | 126 | + auto size = GetShapeSize(outShape); |
| 123 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(int64_t), | 127 | + std::vector<int64_t> resultData(size, 0); |
| 124 | - ACL_MEMCPY_DEVICE_TO_HOST); | 128 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 125 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 129 | + size * sizeof(int64_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 126 | - for (int64_t i = 0; i < size; i++) { | 130 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 127 | - LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); | 131 | + for (int64_t i = 0; i < size; i++) { |
| 128 | - } | 132 | + LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); |
| 133 | + } | ||
| 129 | 134 | ||
| 130 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 135 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 131 | - aclDestroyTensor(self); | 136 | + aclDestroyTensor(self); |
| 132 | - aclDestroyTensor(other); | 137 | + aclDestroyTensor(other); |
| 133 | - aclDestroyTensor(out); | 138 | + aclDestroyTensor(out); |
| 134 | 139 | ||
| 135 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 140 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 136 | - aclrtFree(selfDeviceAddr); | 141 | + aclrtFree(selfDeviceAddr); |
| 137 | - aclrtFree(otherDeviceAddr); | 142 | + aclrtFree(otherDeviceAddr); |
| 138 | - aclrtFree(outDeviceAddr); | 143 | + aclrtFree(outDeviceAddr); |
| 139 | - if (workspaceSize > 0) { | 144 | + if (workspaceSize > 0) { |
| 140 | - aclrtFree(workspaceAddr); | 145 | + aclrtFree(workspaceAddr); |
| 141 | - } | 146 | + } |
| 142 | - aclrtDestroyStream(stream); | 147 | + aclrtDestroyStream(stream); |
| 143 | - aclrtResetDevice(deviceId); | 148 | + aclrtResetDevice(deviceId); |
| 144 | - aclFinalize(); | 149 | + aclFinalize(); |
| 145 | - return 0; | 150 | + return 0; |
| 146 | } | 151 | } |
| @@ -14,127 +14,132 @@ | |||
| 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> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - std::vector<int64_t> outShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* selfDeviceAddr = nullptr; | 86 | + std::vector<int64_t> outShape = {4, 2}; |
| 84 | - void* otherDeviceAddr = nullptr; | 87 | + void* selfDeviceAddr = nullptr; |
| 88 | + void* otherDeviceAddr = nullptr; | ||
| 85 | 89 | ||
| 86 | - aclTensor* self = nullptr; | 90 | + aclTensor* self = nullptr; |
| 87 | - aclTensor* other = nullptr; | 91 | + aclTensor* other = nullptr; |
| 88 | 92 | ||
| 89 | - std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 93 | + std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 90 | - std::vector<int64_t> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; | 94 | + std::vector<int64_t> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; |
| 91 | 95 | ||
| 92 | - // 创建self aclTensor | 96 | + // 创建self aclTensor |
| 93 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); | 97 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); |
| 94 | - CHECK_RET(ret == 0, return ret); | 98 | + CHECK_RET(ret == 0, return ret); |
| 95 | - // 创建other aclTensor | 99 | + // 创建other aclTensor |
| 96 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); | 100 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); |
| 97 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 101 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 98 | 102 | ||
| 99 | - // 3. 调用CANN算子库API,需要修改为具体的API | 103 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 100 | - uint64_t workspaceSize = 0; | 104 | + uint64_t workspaceSize = 0; |
| 101 | - aclOpExecutor* executor; | 105 | + aclOpExecutor* executor; |
| 102 | - // 调用aclnnInplaceBitwiseOrTensor第一段接口 | 106 | + // 调用aclnnInplaceBitwiseOrTensor第一段接口 |
| 103 | - ret = aclnnInplaceBitwiseOrTensorGetWorkspaceSize(self, other,&workspaceSize, &executor); | 107 | + ret = aclnnInplaceBitwiseOrTensorGetWorkspaceSize(self, other, &workspaceSize, &executor); |
| 104 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseOrTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 108 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseOrTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 105 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 109 | + return ret); |
| 106 | - void* workspaceAddr = nullptr; | 110 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 107 | - if (workspaceSize > 0) { | 111 | + void* workspaceAddr = nullptr; |
| 108 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 112 | + if (workspaceSize > 0) { |
| 109 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 113 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 110 | - } | 114 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 111 | - // 调用aclnnInplaceBitwiseOrTensor第二段接口 | 115 | + } |
| 112 | - ret = aclnnInplaceBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); | 116 | + // 调用aclnnInplaceBitwiseOrTensor第二段接口 |
| 113 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseOrTensor failed. ERROR: %d\n", ret); return ret); | 117 | + ret = aclnnInplaceBitwiseOrTensor(workspaceAddr, workspaceSize, executor, stream); |
| 114 | - // 4. (固定写法)同步等待任务执行结束 | 118 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseOrTensor failed. ERROR: %d\n", ret); return ret); |
| 115 | - ret = aclrtSynchronizeStream(stream); | 119 | + // 4. (固定写法)同步等待任务执行结束 |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 120 | + ret = aclrtSynchronizeStream(stream); |
| 117 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 121 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 118 | - auto size = GetShapeSize(selfShape); | 122 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 119 | - std::vector<int64_t> resultData(size, 0); | 123 | + auto size = GetShapeSize(selfShape); |
| 120 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, size * sizeof(int64_t), | 124 | + std::vector<int64_t> resultData(size, 0); |
| 121 | - ACL_MEMCPY_DEVICE_TO_HOST); | 125 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, |
| 122 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 126 | + size * sizeof(int64_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 123 | - for (int64_t i = 0; i < size; i++) { | 127 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 124 | - LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); | 128 | + for (int64_t i = 0; i < size; i++) { |
| 125 | - } | 129 | + LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); |
| 130 | + } | ||
| 126 | 131 | ||
| 127 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 132 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 128 | - aclDestroyTensor(self); | 133 | + aclDestroyTensor(self); |
| 129 | - aclDestroyTensor(other); | 134 | + aclDestroyTensor(other); |
| 130 | - // 7. 释放device 资源,需要根据具体API的接口定义修改 | 135 | + // 7. 释放device 资源,需要根据具体API的接口定义修改 |
| 131 | - aclrtFree(selfDeviceAddr); | 136 | + aclrtFree(selfDeviceAddr); |
| 132 | - aclrtFree(otherDeviceAddr); | 137 | + aclrtFree(otherDeviceAddr); |
| 133 | - if (workspaceSize > 0) { | 138 | + if (workspaceSize > 0) { |
| 134 | - aclrtFree(workspaceAddr); | 139 | + aclrtFree(workspaceAddr); |
| 135 | - } | 140 | + } |
| 136 | - aclrtDestroyStream(stream); | 141 | + aclrtDestroyStream(stream); |
| 137 | - aclrtResetDevice(deviceId); | 142 | + aclrtResetDevice(deviceId); |
| 138 | - aclFinalize(); | 143 | + aclFinalize(); |
| 139 | - return 0; | 144 | + return 0; |
| 140 | } | 145 | } |
| @@ -530,7 +530,7 @@ int main() { | |||
| 530 | void* workspaceAddr = nullptr; | 530 | void* workspaceAddr = nullptr; |
| 531 | if (workspaceSize > 0) { | 531 | if (workspaceSize > 0) { |
| 532 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 532 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 533 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 533 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 534 | } | 534 | } |
| 535 | // 调用aclnnBitwiseXorTensor第二段接口 | 535 | // 调用aclnnBitwiseXorTensor第二段接口 |
| 536 | ret = aclnnBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); | 536 | ret = aclnnBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -666,7 +666,7 @@ int main() { | |||
| 666 | void* workspaceAddr = nullptr; | 666 | void* workspaceAddr = nullptr; |
| 667 | if (workspaceSize > 0) { | 667 | if (workspaceSize > 0) { |
| 668 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 668 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 669 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 669 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 670 | } | 670 | } |
| 671 | // 调用aclnnInplaceBitwiseXorTensor第二段接口 | 671 | // 调用aclnnInplaceBitwiseXorTensor第二段接口 |
| 672 | ret = aclnnInplaceBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); | 672 | ret = aclnnInplaceBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,135 +14,140 @@ | |||
| 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> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - std::vector<int64_t> outShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* selfDeviceAddr = nullptr; | 86 | + std::vector<int64_t> outShape = {4, 2}; |
| 84 | - void* otherDeviceAddr = nullptr; | 87 | + void* selfDeviceAddr = nullptr; |
| 85 | - void* outDeviceAddr = nullptr; | 88 | + void* otherDeviceAddr = nullptr; |
| 86 | - aclTensor* self = nullptr; | 89 | + void* outDeviceAddr = nullptr; |
| 87 | - aclTensor* other = nullptr; | 90 | + aclTensor* self = nullptr; |
| 88 | - aclTensor* out = nullptr; | 91 | + aclTensor* other = nullptr; |
| 89 | - std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 92 | + aclTensor* out = nullptr; |
| 90 | - std::vector<int64_t> otherHostData = {0, 1, 1, 9, 3, 4, 5, 6}; | 93 | + std::vector<int64_t> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 91 | - std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 94 | + std::vector<int64_t> otherHostData = {0, 1, 1, 9, 3, 4, 5, 6}; |
| 92 | - // 创建self aclTensor | 95 | + std::vector<int64_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 93 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); | 96 | + // 创建self aclTensor |
| 94 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 97 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_INT64, &self); |
| 95 | - // 创建other aclTensor | 98 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 96 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); | 99 | + // 创建other aclTensor |
| 97 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 100 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); |
| 98 | - // 创建out aclTensor | 101 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 99 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); | 102 | + // 创建out aclTensor |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 103 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT64, &out); |
| 104 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 101 | 105 | ||
| 102 | - // 3. 调用CANN算子库API,需要修改为具体的API | 106 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 103 | - uint64_t workspaceSize = 0; | 107 | + uint64_t workspaceSize = 0; |
| 104 | - aclOpExecutor* executor; | 108 | + aclOpExecutor* executor; |
| 105 | - // 调用aclnnBitwiseXorTensor第一段接口 | 109 | + // 调用aclnnBitwiseXorTensor第一段接口 |
| 106 | - ret = aclnnBitwiseXorTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); | 110 | + ret = aclnnBitwiseXorTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); |
| 107 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseXorTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 111 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseXorTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 108 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 112 | + return ret); |
| 109 | - void* workspaceAddr = nullptr; | 113 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 110 | - if (workspaceSize > 0) { | 114 | + void* workspaceAddr = nullptr; |
| 111 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 115 | + if (workspaceSize > 0) { |
| 112 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 116 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 113 | - } | 117 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 114 | - // 调用aclnnBitwiseXorTensor第二段接口 | 118 | + } |
| 115 | - ret = aclnnBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); | 119 | + // 调用aclnnBitwiseXorTensor第二段接口 |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseXorTensor failed. ERROR: %d\n", ret); return ret); | 120 | + ret = aclnnBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); |
| 121 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBitwiseXorTensor failed. ERROR: %d\n", ret); return ret); | ||
| 117 | 122 | ||
| 118 | - // 4. (固定写法)同步等待任务执行结束 | 123 | + // 4. (固定写法)同步等待任务执行结束 |
| 119 | - ret = aclrtSynchronizeStream(stream); | 124 | + ret = aclrtSynchronizeStream(stream); |
| 120 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 125 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 121 | 126 | ||
| 122 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 127 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 123 | - auto size = GetShapeSize(outShape); | 128 | + auto size = GetShapeSize(outShape); |
| 124 | - std::vector<int64_t> resultData(size, 0); | 129 | + std::vector<int64_t> resultData(size, 0); |
| 125 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, | 130 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, |
| 126 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 131 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 127 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 132 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 128 | - for (int64_t i = 0; i < size; i++) { | 133 | + for (int64_t i = 0; i < size; i++) { |
| 129 | - LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); | 134 | + LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); |
| 130 | - } | 135 | + } |
| 131 | 136 | ||
| 132 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 137 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 133 | - aclDestroyTensor(self); | 138 | + aclDestroyTensor(self); |
| 134 | - aclDestroyTensor(other); | 139 | + aclDestroyTensor(other); |
| 135 | - aclDestroyTensor(out); | 140 | + aclDestroyTensor(out); |
| 136 | 141 | ||
| 137 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 142 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 138 | - aclrtFree(selfDeviceAddr); | 143 | + aclrtFree(selfDeviceAddr); |
| 139 | - aclrtFree(otherDeviceAddr); | 144 | + aclrtFree(otherDeviceAddr); |
| 140 | - aclrtFree(outDeviceAddr); | 145 | + aclrtFree(outDeviceAddr); |
| 141 | - if (workspaceSize > 0) { | 146 | + if (workspaceSize > 0) { |
| 142 | - aclrtFree(workspaceAddr); | 147 | + aclrtFree(workspaceAddr); |
| 143 | - } | 148 | + } |
| 144 | - aclrtDestroyStream(stream); | 149 | + aclrtDestroyStream(stream); |
| 145 | - aclrtResetDevice(deviceId); | 150 | + aclrtResetDevice(deviceId); |
| 146 | - aclFinalize(); | 151 | + aclFinalize(); |
| 147 | - return 0; | 152 | + return 0; |
| 148 | } | 153 | } |
| @@ -14,126 +14,131 @@ | |||
| 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> selfRefShape = {4, 2}; | 83 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 81 | - std::vector<int64_t> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfRefShape = {4, 2}; |
| 82 | - void* selfRefDeviceAddr = nullptr; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* otherDeviceAddr = nullptr; | 86 | + void* selfRefDeviceAddr = nullptr; |
| 84 | - aclTensor* selfRef = nullptr; | 87 | + void* otherDeviceAddr = nullptr; |
| 85 | - aclTensor* other = nullptr; | 88 | + aclTensor* selfRef = nullptr; |
| 86 | - std::vector<int64_t> selfRefHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 89 | + aclTensor* other = nullptr; |
| 87 | - std::vector<int64_t> otherHostData = {0, 1, 1, 9, 3, 4, 5, 6}; | 90 | + std::vector<int64_t> selfRefHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 88 | - // 创建selfRef aclTensor | 91 | + std::vector<int64_t> otherHostData = {0, 1, 1, 9, 3, 4, 5, 6}; |
| 89 | - ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_INT64, &selfRef); | 92 | + // 创建selfRef aclTensor |
| 90 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 93 | + ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_INT64, &selfRef); |
| 91 | - // 创建other aclTensor | 94 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 92 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); | 95 | + // 创建other aclTensor |
| 93 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 96 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_INT64, &other); |
| 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 | - // 调用aclnnInplaceBitwiseXorTensor第一段接口 | 102 | + // 调用aclnnInplaceBitwiseXorTensor第一段接口 |
| 99 | - ret = aclnnInplaceBitwiseXorTensorGetWorkspaceSize(selfRef, other, &workspaceSize, &executor); | 103 | + ret = aclnnInplaceBitwiseXorTensorGetWorkspaceSize(selfRef, other, &workspaceSize, &executor); |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseXorTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 104 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseXorTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 101 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 105 | + return ret); |
| 102 | - void* workspaceAddr = nullptr; | 106 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 103 | - if (workspaceSize > 0) { | 107 | + void* workspaceAddr = nullptr; |
| 104 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 108 | + if (workspaceSize > 0) { |
| 105 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 109 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 106 | - } | 110 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 107 | - // 调用aclnnInplaceBitwiseXorTensor第二段接口 | 111 | + } |
| 108 | - ret = aclnnInplaceBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); | 112 | + // 调用aclnnInplaceBitwiseXorTensor第二段接口 |
| 109 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseXorTensor failed. ERROR: %d\n", ret); return ret); | 113 | + ret = aclnnInplaceBitwiseXorTensor(workspaceAddr, workspaceSize, executor, stream); |
| 114 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceBitwiseXorTensor failed. ERROR: %d\n", ret); return ret); | ||
| 110 | 115 | ||
| 111 | - // 4. (固定写法)同步等待任务执行结束 | 116 | + // 4. (固定写法)同步等待任务执行结束 |
| 112 | - ret = aclrtSynchronizeStream(stream); | 117 | + ret = aclrtSynchronizeStream(stream); |
| 113 | - 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); |
| 114 | 119 | ||
| 115 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 120 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 116 | - auto size = GetShapeSize(selfRefShape); | 121 | + auto size = GetShapeSize(selfRefShape); |
| 117 | - std::vector<int64_t> resultData(size, 0); | 122 | + std::vector<int64_t> resultData(size, 0); |
| 118 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr, | 123 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr, |
| 119 | - size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); | 124 | + 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); | 125 | + 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++) { | 126 | + for (int64_t i = 0; i < size; i++) { |
| 122 | - LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); | 127 | + LOG_PRINT("result[%ld] is: %ld\n", i, resultData[i]); |
| 123 | - } | 128 | + } |
| 124 | 129 | ||
| 125 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 130 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 126 | - aclDestroyTensor(selfRef); | 131 | + aclDestroyTensor(selfRef); |
| 127 | - aclDestroyTensor(other); | 132 | + aclDestroyTensor(other); |
| 128 | 133 | ||
| 129 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | 134 | + // 7. 释放device资源,需要根据具体API的接口定义修改 |
| 130 | - aclrtFree(selfRefDeviceAddr); | 135 | + aclrtFree(selfRefDeviceAddr); |
| 131 | - aclrtFree(otherDeviceAddr); | 136 | + aclrtFree(otherDeviceAddr); |
| 132 | - if (workspaceSize > 0) { | 137 | + if (workspaceSize > 0) { |
| 133 | - aclrtFree(workspaceAddr); | 138 | + aclrtFree(workspaceAddr); |
| 134 | - } | 139 | + } |
| 135 | - aclrtDestroyStream(stream); | 140 | + aclrtDestroyStream(stream); |
| 136 | - aclrtResetDevice(deviceId); | 141 | + aclrtResetDevice(deviceId); |
| 137 | - aclFinalize(); | 142 | + aclFinalize(); |
| 138 | - return 0; | 143 | + return 0; |
| 139 | } | 144 | } |
| @@ -236,12 +236,12 @@ aclnnStatus aclnnGcd( | |||
| 236 | if (!(cond)) { \ | 236 | if (!(cond)) { \ |
| 237 | return_expr; \ | 237 | return_expr; \ |
| 238 | } \ | 238 | } \ |
| 239 | - } while(0) | 239 | + } while (0) |
| 240 | 240 | ||
| 241 | #define LOG_PRINT(message, ...) \ | 241 | #define LOG_PRINT(message, ...) \ |
| 242 | do { \ | 242 | do { \ |
| 243 | printf(message, ##__VA_ARGS__); \ | 243 | printf(message, ##__VA_ARGS__); \ |
| 244 | - } while(0) | 244 | + } while (0) |
| 245 | 245 | ||
| 246 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 246 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { |
| 247 | int64_t shape_size = 1; | 247 | int64_t shape_size = 1; |
| @@ -505,7 +505,7 @@ int PrepareAndExecuteGtTensor(aclrtStream stream, GtTensorData& data, void*& wor | |||
| 505 | workspaceAddr = nullptr; | 505 | workspaceAddr = nullptr; |
| 506 | if (workspaceSize > 0) { | 506 | if (workspaceSize > 0) { |
| 507 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 507 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 508 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 508 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 509 | } | 509 | } |
| 510 | // 调用aclnnGtTensor第二段接口 | 510 | // 调用aclnnGtTensor第二段接口 |
| 511 | ret = aclnnGtTensor(workspaceAddr, workspaceSize, executor, stream); | 511 | ret = aclnnGtTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -678,7 +678,7 @@ int main() { | |||
| 678 | void* workspaceAddr = nullptr; | 678 | void* workspaceAddr = nullptr; |
| 679 | if (workspaceSize > 0) { | 679 | if (workspaceSize > 0) { |
| 680 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 680 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 681 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 681 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 682 | } | 682 | } |
| 683 | // 调用aclnnInplaceGtTensor第二段接口 | 683 | // 调用aclnnInplaceGtTensor第二段接口 |
| 684 | ret = aclnnInplaceGtTensor(workspaceAddr, workspaceSize, executor, stream); | 684 | ret = aclnnInplaceGtTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,133 +14,137 @@ | |||
| 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> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 82 | - std::vector<int64_t> outShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 83 | - void* selfDeviceAddr = nullptr; | 86 | + std::vector<int64_t> outShape = {4, 2}; |
| 84 | - void* otherDeviceAddr = nullptr; | 87 | + void* selfDeviceAddr = nullptr; |
| 85 | - void* outDeviceAddr = nullptr; | 88 | + void* otherDeviceAddr = nullptr; |
| 86 | - aclTensor* self = nullptr; | 89 | + void* outDeviceAddr = nullptr; |
| 87 | - aclTensor* other = nullptr; | 90 | + aclTensor* self = nullptr; |
| 88 | - aclTensor* out = nullptr; | 91 | + aclTensor* other = nullptr; |
| 89 | - std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 92 | + aclTensor* out = nullptr; |
| 90 | - std::vector<float> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; | 93 | + std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 91 | - std::vector<char> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; | 94 | + std::vector<float> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; |
| 92 | - // 创建self aclTensor | 95 | + std::vector<char> outHostData = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 93 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 96 | + // 创建self aclTensor |
| 94 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 97 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 95 | - // 创建other aclTensor | 98 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 96 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); | 99 | + // 创建other aclTensor |
| 97 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 100 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); |
| 98 | - // 创建out aclTensor | 101 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 99 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_BOOL, &out); | 102 | + // 创建out aclTensor |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 103 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_BOOL, &out); |
| 104 | + CHECK_RET(ret == ACL_SUCCESS, return ret); | ||
| 101 | 105 | ||
| 102 | - // 3. 调用CANN算子库API,需要修改为具体的API | 106 | + // 3. 调用CANN算子库API,需要修改为具体的API |
| 103 | - uint64_t workspaceSize = 0; | 107 | + uint64_t workspaceSize = 0; |
| 104 | - aclOpExecutor* executor; | 108 | + aclOpExecutor* executor; |
| 105 | - // 调用aclnnGtTensor第一段接口 | 109 | + // 调用aclnnGtTensor第一段接口 |
| 106 | - ret = aclnnGtTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); | 110 | + ret = aclnnGtTensorGetWorkspaceSize(self, other, out, &workspaceSize, &executor); |
| 107 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGtTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 111 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGtTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 108 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 112 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 109 | - void* workspaceAddr = nullptr; | 113 | + void* workspaceAddr = nullptr; |
| 110 | - if (workspaceSize > 0) { | 114 | + if (workspaceSize > 0) { |
| 111 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 115 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 112 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 116 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 113 | - } | 117 | + } |
| 114 | - // 调用aclnnGtTensor第二段接口 | 118 | + // 调用aclnnGtTensor第二段接口 |
| 115 | - ret = aclnnGtTensor(workspaceAddr, workspaceSize, executor, stream); | 119 | + ret = aclnnGtTensor(workspaceAddr, workspaceSize, executor, stream); |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGtTensor failed. ERROR: %d\n", ret); return ret); | 120 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGtTensor failed. ERROR: %d\n", ret); return ret); |
| 117 | - // 4. (固定写法)同步等待任务执行结束 | 121 | + // 4. (固定写法)同步等待任务执行结束 |
| 118 | - ret = aclrtSynchronizeStream(stream); | 122 | + ret = aclrtSynchronizeStream(stream); |
| 119 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 123 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 120 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 124 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 121 | - auto size = GetShapeSize(outShape); | 125 | + auto size = GetShapeSize(outShape); |
| 122 | - std::vector<char> resultData(size, 0); | 126 | + std::vector<char> resultData(size, 0); |
| 123 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(char), | 127 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(char), |
| 124 | - ACL_MEMCPY_DEVICE_TO_HOST); | 128 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 125 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 129 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 126 | - for (int64_t i = 0; i < size; i++) { | 130 | + for (int64_t i = 0; i < size; i++) { |
| 127 | - LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); | 131 | + LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]); |
| 128 | - } | 132 | + } |
| 129 | 133 | ||
| 130 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 134 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 131 | - aclDestroyTensor(self); | 135 | + aclDestroyTensor(self); |
| 132 | - aclDestroyTensor(other); | 136 | + aclDestroyTensor(other); |
| 133 | - aclDestroyTensor(out); | 137 | + aclDestroyTensor(out); |
| 134 | 138 | ||
| 135 | - // 7. 释放device资源 | 139 | + // 7. 释放device资源 |
| 136 | - aclrtFree(selfDeviceAddr); | 140 | + aclrtFree(selfDeviceAddr); |
| 137 | - aclrtFree(otherDeviceAddr); | 141 | + aclrtFree(otherDeviceAddr); |
| 138 | - aclrtFree(outDeviceAddr); | 142 | + aclrtFree(outDeviceAddr); |
| 139 | - if (workspaceSize > 0) { | 143 | + if (workspaceSize > 0) { |
| 140 | - aclrtFree(workspaceAddr); | 144 | + aclrtFree(workspaceAddr); |
| 141 | - } | 145 | + } |
| 142 | - aclrtDestroyStream(stream); | 146 | + aclrtDestroyStream(stream); |
| 143 | - aclrtResetDevice(deviceId); | 147 | + aclrtResetDevice(deviceId); |
| 144 | - aclFinalize(); | 148 | + aclFinalize(); |
| 145 | - return 0; | 149 | + return 0; |
| 146 | -} | 150 | +} |
| @@ -13,125 +13,130 @@ | |||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | 15 | ||
| 16 | - do { \ | 16 | + do { \ |
| 17 | - if (!(cond)) { \ | 17 | + if (!(cond)) { \ |
| 18 | - return_expr; \ | 18 | + return_expr; \ |
| 19 | - } \ | 19 | + } \ |
| 20 | - } while (0) | 20 | + } while (0) |
| 21 | 21 | ||
| 22 | -#define LOG_PRINT(message, ...) \ | 22 | +#define LOG_PRINT(message, ...) \ |
| 23 | - do { \ | 23 | + do { \ |
| 24 | - printf(message, ##__VA_ARGS__); \ | 24 | + printf(message, ##__VA_ARGS__); \ |
| 25 | - } while (0) | 25 | + } while (0) |
| 26 | 26 | ||
| 27 | -int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 27 | +int64_t GetShapeSize(const std::vector<int64_t>& shape) |
| 28 | - int64_t shape_size = 1; | 28 | +{ |
| 29 | - for (auto i : shape) { | 29 | + int64_t shape_size = 1; |
| 30 | - shape_size *= i; | 30 | + for (auto i : shape) { |
| 31 | - } | 31 | + shape_size *= i; |
| 32 | - return shape_size; | 32 | + } |
| 33 | + return shape_size; | ||
| 33 | } | 34 | } |
| 34 | 35 | ||
| 35 | -int Init(int32_t deviceId, aclrtStream* stream) { | 36 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 36 | - // 固定写法,资源初始化 | 37 | +{ |
| 37 | - auto ret = aclInit(nullptr); | 38 | + // 固定写法,资源初始化 |
| 38 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); | 39 | + auto ret = aclInit(nullptr); |
| 39 | - ret = aclrtSetDevice(deviceId); | 40 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret); |
| 40 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 41 | + ret = aclrtSetDevice(deviceId); |
| 41 | - ret = aclrtCreateStream(stream); | 42 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 42 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 43 | + ret = aclrtCreateStream(stream); |
| 43 | - return 0; | 44 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 45 | + return 0; | ||
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | template <typename T> | 48 | template <typename T> |
| 47 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, | 49 | int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 48 | - aclDataType dataType, aclTensor** tensor) { | 50 | + aclDataType dataType, aclTensor** tensor) |
| 49 | - auto size = GetShapeSize(shape) * sizeof(T); | 51 | +{ |
| 50 | - // 调用aclrtMalloc申请device侧内存 | 52 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 51 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 53 | + // 调用aclrtMalloc申请device侧内存 |
| 52 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 54 | + auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 55 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | ||
| 53 | 56 | ||
| 54 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 57 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 55 | - ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); | 58 | + ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); |
| 56 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret); | 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根据自己的需要处理 | 79 | + auto ret = Init(deviceId, &stream); |
| 77 | - CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 80 | + // check根据自己的需要处理 |
| 78 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 81 | + CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 79 | - std::vector<int64_t> selfShape = {4, 2}; | 82 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 80 | - std::vector<int64_t> otherShape = {4, 2}; | 83 | + std::vector<int64_t> selfShape = {4, 2}; |
| 81 | - void* selfDeviceAddr = nullptr; | 84 | + std::vector<int64_t> otherShape = {4, 2}; |
| 82 | - void* otherDeviceAddr = nullptr; | 85 | + void* selfDeviceAddr = nullptr; |
| 83 | - aclTensor* self = nullptr; | 86 | + void* otherDeviceAddr = nullptr; |
| 84 | - aclTensor* other = nullptr; | 87 | + aclTensor* self = nullptr; |
| 85 | - std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 88 | + aclTensor* other = nullptr; |
| 86 | - std::vector<float> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; | 89 | + std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 90 | + std::vector<float> otherHostData = {0, 1, 1, 2, 3, 4, 5, 6}; | ||
| 87 | 91 | ||
| 88 | - // 创建self aclTensor | 92 | + // 创建self aclTensor |
| 89 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 93 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 90 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 94 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 91 | - // 创建other aclTensor | 95 | + // 创建other aclTensor |
| 92 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); | 96 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); |
| 93 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 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 | - // 调用aclnnInplaceGtTensor第一段接口 | 102 | + // 调用aclnnInplaceGtTensor第一段接口 |
| 99 | - ret = aclnnInplaceGtTensorGetWorkspaceSize(self, other, &workspaceSize, &executor); | 103 | + ret = aclnnInplaceGtTensorGetWorkspaceSize(self, other, &workspaceSize, &executor); |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceGtTensorGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 104 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceGtTensorGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 101 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 105 | + return ret); |
| 102 | - void* workspaceAddr = nullptr; | 106 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 103 | - if (workspaceSize > 0) { | 107 | + void* workspaceAddr = nullptr; |
| 104 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 108 | + if (workspaceSize > 0) { |
| 105 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 109 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 106 | - } | 110 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 107 | - // 调用aclnnInplaceGtTensor第二段接口 | 111 | + } |
| 108 | - ret = aclnnInplaceGtTensor(workspaceAddr, workspaceSize, executor, stream); | 112 | + // 调用aclnnInplaceGtTensor第二段接口 |
| 109 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceGtTensor failed. ERROR: %d\n", ret); return ret); | 113 | + ret = aclnnInplaceGtTensor(workspaceAddr, workspaceSize, executor, stream); |
| 110 | - // 4. (固定写法)同步等待任务执行结束 | 114 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceGtTensor failed. ERROR: %d\n", ret); return ret); |
| 111 | - ret = aclrtSynchronizeStream(stream); | 115 | + // 4. (固定写法)同步等待任务执行结束 |
| 112 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); | 116 | + ret = aclrtSynchronizeStream(stream); |
| 113 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 117 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret); |
| 114 | - auto size = GetShapeSize(selfShape); | 118 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 115 | - std::vector<float> resultData(size, 0); | 119 | + auto size = GetShapeSize(selfShape); |
| 116 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, size * sizeof(resultData[0]), | 120 | + std::vector<float> resultData(size, 0); |
| 117 | - ACL_MEMCPY_DEVICE_TO_HOST); | 121 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfDeviceAddr, |
| 118 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 122 | + size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST); |
| 119 | - for (int64_t i = 0; i < size; i++) { | 123 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 120 | - LOG_PRINT("result[%ld] is: %lf\n", i, resultData[i]); | 124 | + for (int64_t i = 0; i < size; i++) { |
| 121 | - } | 125 | + LOG_PRINT("result[%ld] is: %lf\n", i, resultData[i]); |
| 126 | + } | ||
| 122 | 127 | ||
| 123 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 128 | + // 6. 释放aclTensor,需要根据具体API的接口定义修改 |
| 124 | - aclDestroyTensor(self); | 129 | + aclDestroyTensor(self); |
| 125 | - aclDestroyTensor(other); | 130 | + aclDestroyTensor(other); |
| 126 | 131 | ||
| 127 | - // 7. 释放device资源 | 132 | + // 7. 释放device资源 |
| 128 | - aclrtFree(selfDeviceAddr); | 133 | + aclrtFree(selfDeviceAddr); |
| 129 | - aclrtFree(otherDeviceAddr); | 134 | + aclrtFree(otherDeviceAddr); |
| 130 | - if (workspaceSize > 0) { | 135 | + if (workspaceSize > 0) { |
| 131 | - aclrtFree(workspaceAddr); | 136 | + aclrtFree(workspaceAddr); |
| 132 | - } | 137 | + } |
| 133 | - aclrtDestroyStream(stream); | 138 | + aclrtDestroyStream(stream); |
| 134 | - aclrtResetDevice(deviceId); | 139 | + aclrtResetDevice(deviceId); |
| 135 | - aclFinalize(); | 140 | + aclFinalize(); |
| 136 | - return 0; | 141 | + return 0; |
| 137 | -} | 142 | +} |
| @@ -645,7 +645,7 @@ int main() { | |||
| 645 | void* workspaceAddr = nullptr; | 645 | void* workspaceAddr = nullptr; |
| 646 | if (workspaceSize > 0) { | 646 | if (workspaceSize > 0) { |
| 647 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 647 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 648 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 648 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 649 | } | 649 | } |
| 650 | // 调用aclnnInplaceGeTensor第二段接口 | 650 | // 调用aclnnInplaceGeTensor第二段接口 |
| 651 | ret = aclnnInplaceGeTensor(workspaceAddr, workspaceSize, executor, stream); | 651 | ret = aclnnInplaceGeTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -572,12 +572,12 @@ int main() { | |||
| 572 | if (!(cond)) { \ | 572 | if (!(cond)) { \ |
| 573 | return_expr; \ | 573 | return_expr; \ |
| 574 | } \ | 574 | } \ |
| 575 | - } while(0) | 575 | + } while (0) |
| 576 | 576 | ||
| 577 | #define LOG_PRINT(message, ...) \ | 577 | #define LOG_PRINT(message, ...) \ |
| 578 | do { \ | 578 | do { \ |
| 579 | printf(message, ##__VA_ARGS__); \ | 579 | printf(message, ##__VA_ARGS__); \ |
| 580 | - } while(0) | 580 | + } while (0) |
| 581 | 581 | ||
| 582 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 582 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { |
| 583 | int64_t shape_size = 1; | 583 | int64_t shape_size = 1; |
| @@ -638,12 +638,12 @@ int main() { | |||
| 638 | if (!(cond)) { \ | 638 | if (!(cond)) { \ |
| 639 | return_expr; \ | 639 | return_expr; \ |
| 640 | } \ | 640 | } \ |
| 641 | - } while(0) | 641 | + } while (0) |
| 642 | 642 | ||
| 643 | #define LOG_PRINT(message, ...) \ | 643 | #define LOG_PRINT(message, ...) \ |
| 644 | do { \ | 644 | do { \ |
| 645 | printf(message, ##__VA_ARGS__); \ | 645 | printf(message, ##__VA_ARGS__); \ |
| 646 | - } while(0) | 646 | + } while (0) |
| 647 | 647 | ||
| 648 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 648 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { |
| 649 | int64_t shape_size = 1; | 649 | int64_t shape_size = 1; |
| @@ -571,12 +571,12 @@ int main() { | |||
| 571 | if (!(cond)) { \ | 571 | if (!(cond)) { \ |
| 572 | return_expr; \ | 572 | return_expr; \ |
| 573 | } \ | 573 | } \ |
| 574 | - } while(0) | 574 | + } while (0) |
| 575 | 575 | ||
| 576 | #define LOG_PRINT(message, ...) \ | 576 | #define LOG_PRINT(message, ...) \ |
| 577 | do { \ | 577 | do { \ |
| 578 | printf(message, ##__VA_ARGS__); \ | 578 | printf(message, ##__VA_ARGS__); \ |
| 579 | - } while(0) | 579 | + } while (0) |
| 580 | 580 | ||
| 581 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 581 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { |
| 582 | int64_t shape_size = 1; | 582 | int64_t shape_size = 1; |
| @@ -585,12 +585,12 @@ int main() { | |||
| 585 | if (!(cond)) { \ | 585 | if (!(cond)) { \ |
| 586 | return_expr; \ | 586 | return_expr; \ |
| 587 | } \ | 587 | } \ |
| 588 | - } while(0) | 588 | + } while (0) |
| 589 | 589 | ||
| 590 | #define LOG_PRINT(message, ...) \ | 590 | #define LOG_PRINT(message, ...) \ |
| 591 | do { \ | 591 | do { \ |
| 592 | printf(message, ##__VA_ARGS__); \ | 592 | printf(message, ##__VA_ARGS__); \ |
| 593 | - } while(0) | 593 | + } while (0) |
| 594 | 594 | ||
| 595 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { | 595 | int64_t GetShapeSize(const std::vector<int64_t>& shape) { |
| 596 | int64_t shape_size = 1; | 596 | int64_t shape_size = 1; |
| @@ -508,7 +508,7 @@ int main() | |||
| 508 | void* workspaceAddr = nullptr; | 508 | void* workspaceAddr = nullptr; |
| 509 | if (workspaceSize > 0) { | 509 | if (workspaceSize > 0) { |
| 510 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 510 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 511 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 511 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 512 | } | 512 | } |
| 513 | // 调用aclnnFmodScalar第二段接口 | 513 | // 调用aclnnFmodScalar第二段接口 |
| 514 | ret = aclnnFmodScalar(workspaceAddr, workspaceSize, executor, stream); | 514 | ret = aclnnFmodScalar(workspaceAddr, workspaceSize, executor, stream); |
| @@ -641,7 +641,7 @@ int main() { | |||
| 641 | void* workspaceAddr = nullptr; | 641 | void* workspaceAddr = nullptr; |
| 642 | if (workspaceSize > 0) { | 642 | if (workspaceSize > 0) { |
| 643 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 643 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 644 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 644 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 645 | } | 645 | } |
| 646 | // 调用aclnnInplaceFmodScalar第二段接口 | 646 | // 调用aclnnInplaceFmodScalar第二段接口 |
| 647 | ret = aclnnInplaceFmodScalar(workspaceAddr, workspaceSize, executor, stream); | 647 | ret = aclnnInplaceFmodScalar(workspaceAddr, workspaceSize, executor, stream); |
| @@ -515,7 +515,7 @@ int main() | |||
| 515 | void* workspaceAddr = nullptr; | 515 | void* workspaceAddr = nullptr; |
| 516 | if (workspaceSize > 0) { | 516 | if (workspaceSize > 0) { |
| 517 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 517 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 518 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 518 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 519 | } | 519 | } |
| 520 | // 调用aclnnFmodTensor第二段接口 | 520 | // 调用aclnnFmodTensor第二段接口 |
| 521 | ret = aclnnFmodTensor(workspaceAddr, workspaceSize, executor, stream); | 521 | ret = aclnnFmodTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -650,7 +650,7 @@ int main() { | |||
| 650 | void* workspaceAddr = nullptr; | 650 | void* workspaceAddr = nullptr; |
| 651 | if (workspaceSize > 0) { | 651 | if (workspaceSize > 0) { |
| 652 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 652 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 653 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 653 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 654 | } | 654 | } |
| 655 | // 调用aclnnInplaceFmodTensor第二段接口 | 655 | // 调用aclnnInplaceFmodTensor第二段接口 |
| 656 | ret = aclnnInplaceFmodTensor(workspaceAddr, workspaceSize, executor, stream); | 656 | ret = aclnnInplaceFmodTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -47,9 +47,8 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | template <typename T> | 49 | template <typename T> |
| 50 | -int CreateAclTensor( | 50 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 51 | - const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | 51 | + aclDataType dataType, aclTensor** tensor) |
| 52 | - aclTensor** tensor) | ||
| 53 | { | 52 | { |
| 54 | auto size = GetShapeSize(shape) * sizeof(T); | 53 | auto size = GetShapeSize(shape) * sizeof(T); |
| 55 | // 调用aclrtMalloc申请device侧内存 | 54 | // 调用aclrtMalloc申请device侧内存 |
| @@ -67,9 +66,8 @@ int CreateAclTensor( | |||
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | // 调用aclCreateTensor接口创建aclTensor | 68 | // 调用aclCreateTensor接口创建aclTensor |
| 70 | - *tensor = aclCreateTensor( | 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 71 | - shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 72 | - *deviceAddr); | ||
| 73 | return 0; | 71 | return 0; |
| 74 | } | 72 | } |
| 75 | 73 | ||
| @@ -113,7 +111,7 @@ int main() | |||
| 113 | void* workspaceAddr = nullptr; | 111 | void* workspaceAddr = nullptr; |
| 114 | if (workspaceSize > 0) { | 112 | if (workspaceSize > 0) { |
| 115 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 113 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 114 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 117 | } | 115 | } |
| 118 | // 调用aclnnFmodScalar第二段接口 | 116 | // 调用aclnnFmodScalar第二段接口 |
| 119 | ret = aclnnFmodScalar(workspaceAddr, workspaceSize, executor, stream); | 117 | ret = aclnnFmodScalar(workspaceAddr, workspaceSize, executor, stream); |
| @@ -125,9 +123,8 @@ int main() | |||
| 125 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 123 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 126 | auto size = GetShapeSize(outShape); | 124 | auto size = GetShapeSize(outShape); |
| 127 | std::vector<float> resultData(size, 0); | 125 | std::vector<float> resultData(size, 0); |
| 128 | - ret = aclrtMemcpy( | 126 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 129 | - resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 127 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 130 | - ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 131 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 128 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 132 | for (int64_t i = 0; i < size; i++) { | 129 | for (int64_t i = 0; i < size; i++) { |
| 133 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 130 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| @@ -148,4 +145,4 @@ int main() | |||
| 148 | aclrtResetDevice(deviceId); | 145 | aclrtResetDevice(deviceId); |
| 149 | aclFinalize(); | 146 | aclFinalize(); |
| 150 | return 0; | 147 | return 0; |
| 151 | -} | 148 | +} |
| @@ -47,9 +47,8 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | template <typename T> | 49 | template <typename T> |
| 50 | -int CreateAclTensor( | 50 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 51 | - const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | 51 | + aclDataType dataType, aclTensor** tensor) |
| 52 | - aclTensor** tensor) | ||
| 53 | { | 52 | { |
| 54 | auto size = GetShapeSize(shape) * sizeof(T); | 53 | auto size = GetShapeSize(shape) * sizeof(T); |
| 55 | // 调用aclrtMalloc申请device侧内存 | 54 | // 调用aclrtMalloc申请device侧内存 |
| @@ -67,9 +66,8 @@ int CreateAclTensor( | |||
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | // 调用aclCreateTensor接口创建aclTensor | 68 | // 调用aclCreateTensor接口创建aclTensor |
| 70 | - *tensor = aclCreateTensor( | 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 71 | - shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 72 | - *deviceAddr); | ||
| 73 | return 0; | 71 | return 0; |
| 74 | } | 72 | } |
| 75 | 73 | ||
| @@ -115,7 +113,7 @@ int main() | |||
| 115 | void* workspaceAddr = nullptr; | 113 | void* workspaceAddr = nullptr; |
| 116 | if (workspaceSize > 0) { | 114 | if (workspaceSize > 0) { |
| 117 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 115 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 118 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 116 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 119 | } | 117 | } |
| 120 | // 调用aclnnFmodTensor第二段接口 | 118 | // 调用aclnnFmodTensor第二段接口 |
| 121 | ret = aclnnFmodTensor(workspaceAddr, workspaceSize, executor, stream); | 119 | ret = aclnnFmodTensor(workspaceAddr, workspaceSize, executor, stream); |
| @@ -126,9 +124,8 @@ int main() | |||
| 126 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 124 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 127 | auto size = GetShapeSize(outShape); | 125 | auto size = GetShapeSize(outShape); |
| 128 | std::vector<float> resultData(size, 0); | 126 | std::vector<float> resultData(size, 0); |
| 129 | - ret = aclrtMemcpy( | 127 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 130 | - resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 128 | + ACL_MEMCPY_DEVICE_TO_HOST); |
| 131 | - ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 132 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); | 129 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret); |
| 133 | for (int64_t i = 0; i < size; i++) { | 130 | for (int64_t i = 0; i < size; i++) { |
| 134 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 131 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| @@ -150,4 +147,4 @@ int main() | |||
| 150 | aclrtResetDevice(deviceId); | 147 | aclrtResetDevice(deviceId); |
| 151 | aclFinalize(); | 148 | aclFinalize(); |
| 152 | return 0; | 149 | return 0; |
| 153 | -} | 150 | +} |
| @@ -29,11 +29,11 @@ | |||
| 29 | - 计算公式: | 29 | - 计算公式: |
| 30 | 30 | ||
| 31 | $$ | 31 | $$ |
| 32 | -out_i=(self_i \ne other)?[1]:[0] | 32 | +out_i=(self_i \ne other)?[1]: [0] |
| 33 | $$ | 33 | $$ |
| 34 | 34 | ||
| 35 | $$ | 35 | $$ |
| 36 | -selfRef_i=(selfRef_i \ne other)?[1]:[0] | 36 | +selfRef_i=(selfRef_i \ne other)?[1]: [0] |
| 37 | $$ | 37 | $$ |
| 38 | 38 | ||
| 39 | ## 函数原型 | 39 | ## 函数原型 |
| @@ -29,11 +29,11 @@ | |||
| 29 | - 计算公式: | 29 | - 计算公式: |
| 30 | 30 | ||
| 31 | $$ | 31 | $$ |
| 32 | -out_i=(self_i \ne other_i)?[1]:[0] | 32 | +out_i=(self_i \ne other_i)?[1]: [0] |
| 33 | $$ | 33 | $$ |
| 34 | 34 | ||
| 35 | $$ | 35 | $$ |
| 36 | -selfRef_i=(selfRef_i\ \ne other_i)\ ?\ [1]:[0] | 36 | +selfRef_i=(selfRef_i\ \ne other_i)\ ?\ [1]: [0] |
| 37 | $$ | 37 | $$ |
| 38 | 38 | ||
| 39 | ## 函数原型 | 39 | ## 函数原型 |
| @@ -277,7 +277,7 @@ int main() { | |||
| 277 | void* workspaceAddr = nullptr; | 277 | void* workspaceAddr = nullptr; |
| 278 | if (workspaceSize > 0) { | 278 | if (workspaceSize > 0) { |
| 279 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 279 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 280 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 280 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 281 | } | 281 | } |
| 282 | // 调用aclnnSWhere第二段接口 | 282 | // 调用aclnnSWhere第二段接口 |
| 283 | ret = aclnnSWhere(workspaceAddr, workspaceSize, executor, stream); | 283 | ret = aclnnSWhere(workspaceAddr, workspaceSize, executor, stream); |
| @@ -14,144 +14,147 @@ | |||
| 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 | - | 37 | +int Init(int32_t deviceId, aclrtStream* stream) |
| 37 | -int Init(int32_t deviceId, aclrtStream* stream) { | 38 | +{ |
| 38 | - // 固定写法,资源初始化 | 39 | + // 固定写法,资源初始化 |
| 39 | - auto ret = aclInit(nullptr); | 40 | + auto ret = aclInit(nullptr); |
| 40 | - 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); |
| 41 | - ret = aclrtSetDevice(deviceId); | 42 | + ret = aclrtSetDevice(deviceId); |
| 42 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); | 43 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret); |
| 43 | - ret = aclrtCreateStream(stream); | 44 | + ret = aclrtCreateStream(stream); |
| 44 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); | 45 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret); |
| 45 | - return 0; | 46 | + return 0; |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 48 | template <typename T> | 49 | template <typename T> |
| 49 | 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, |
| 50 | - aclDataType dataType, aclTensor** tensor) { | 51 | + aclDataType dataType, aclTensor** tensor) |
| 51 | - auto size = GetShapeSize(shape) * sizeof(T); | 52 | +{ |
| 52 | - // 调用aclrtMalloc申请device侧内存 | 53 | + auto size = GetShapeSize(shape) * sizeof(T); |
| 53 | - auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 54 | + // 调用aclrtMalloc申请device侧内存 |
| 54 | - 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); | ||
| 55 | 57 | ||
| 56 | - // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 | 58 | + // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上 |
| 57 | - 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); |
| 58 | - 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); |
| 59 | 61 | ||
| 60 | - // 计算连续tensor的strides | 62 | + // 计算连续tensor的strides |
| 61 | - std::vector<int64_t> strides(shape.size(), 1); | 63 | + std::vector<int64_t> strides(shape.size(), 1); |
| 62 | - for (int64_t i = shape.size() - 2; i >= 0; i--) { | 64 | + for (int64_t i = shape.size() - 2; i >= 0; i--) { |
| 63 | - strides[i] = shape[i + 1] * strides[i + 1]; | 65 | + strides[i] = shape[i + 1] * strides[i + 1]; |
| 64 | - } | 66 | + } |
| 65 | 67 | ||
| 66 | - // 调用aclCreateTensor接口创建aclTensor | 68 | + // 调用aclCreateTensor接口创建aclTensor |
| 67 | - *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, |
| 68 | - shape.data(), shape.size(), *deviceAddr); | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 69 | - return 0; | 71 | + return 0; |
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | -int main() { | 74 | +int main() |
| 73 | - // 1. (固定写法)device/stream初始化, 参考acl API手册 | 75 | +{ |
| 74 | - // 根据自己的实际device填写deviceId | 76 | + // 1. (固定写法)device/stream初始化, 参考acl API手册 |
| 75 | - int32_t deviceId = 0; | 77 | + // 根据自己的实际device填写deviceId |
| 76 | - aclrtStream stream; | 78 | + int32_t deviceId = 0; |
| 77 | - auto ret = Init(deviceId, &stream); | 79 | + aclrtStream stream; |
| 78 | - // check根据自己的需要处理 | 80 | + auto ret = Init(deviceId, &stream); |
| 79 | - CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 81 | + // check根据自己的需要处理 |
| 80 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 82 | + CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 81 | - std::vector<int64_t> selfShape = {4, 2}; | 83 | + // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 82 | - std::vector<int64_t> otherShape = {4, 2}; | 84 | + std::vector<int64_t> selfShape = {4, 2}; |
| 83 | - std::vector<int64_t> conditionShape = {4, 2}; | 85 | + std::vector<int64_t> otherShape = {4, 2}; |
| 84 | - std::vector<int64_t> outShape = {4, 2}; | 86 | + std::vector<int64_t> conditionShape = {4, 2}; |
| 85 | - void* selfDeviceAddr = nullptr; | 87 | + std::vector<int64_t> outShape = {4, 2}; |
| 86 | - void* otherDeviceAddr = nullptr; | 88 | + void* selfDeviceAddr = nullptr; |
| 87 | - void* conditionDeviceAddr = nullptr; | 89 | + void* otherDeviceAddr = nullptr; |
| 88 | - void* outDeviceAddr = nullptr; | 90 | + void* conditionDeviceAddr = nullptr; |
| 89 | - aclTensor* self = nullptr; | 91 | + void* outDeviceAddr = nullptr; |
| 90 | - aclTensor* other = nullptr; | 92 | + aclTensor* self = nullptr; |
| 91 | - aclTensor* condition = nullptr; | 93 | + aclTensor* other = nullptr; |
| 92 | - aclTensor* out = nullptr; | 94 | + aclTensor* condition = nullptr; |
| 93 | - std::vector<float> selfHostData = {0, 0, 0, 0, 0, 0, 0, 7}; | 95 | + aclTensor* out = nullptr; |
| 94 | - std::vector<float> otherHostData = {0, 1, 2, 3, 4, 5, 6, 7}; | 96 | + std::vector<float> selfHostData = {0, 0, 0, 0, 0, 0, 0, 7}; |
| 95 | - std::vector<int8_t> conditionHostData = {false,false,false,false,true,true,true,true}; | 97 | + std::vector<float> otherHostData = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 96 | - std::vector<float> outHostData = {10, 10, 10, 10, 10, 10, 10, 10}; | 98 | + std::vector<int8_t> conditionHostData = {false, false, false, false, true, true, true, true}; |
| 99 | + std::vector<float> outHostData = {10, 10, 10, 10, 10, 10, 10, 10}; | ||
| 97 | 100 | ||
| 98 | - // 创建self aclTensor | 101 | + // 创建self aclTensor |
| 99 | - ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); | 102 | + ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self); |
| 100 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 103 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 101 | - // 创建self aclTensor | 104 | + // 创建self aclTensor |
| 102 | - ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); | 105 | + ret = CreateAclTensor(otherHostData, otherShape, &otherDeviceAddr, aclDataType::ACL_FLOAT, &other); |
| 103 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 106 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 104 | - // 创建condition aclTensor | 107 | + // 创建condition aclTensor |
| 105 | - ret = CreateAclTensor(conditionHostData, conditionShape, &conditionDeviceAddr, aclDataType::ACL_BOOL, &condition); | 108 | + ret = CreateAclTensor(conditionHostData, conditionShape, &conditionDeviceAddr, aclDataType::ACL_BOOL, &condition); |
| 106 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 109 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 107 | - // 创建out aclTensor | 110 | + // 创建out aclTensor |
| 108 | - ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); | 111 | + ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out); |
| 109 | - CHECK_RET(ret == ACL_SUCCESS, return ret); | 112 | + CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 110 | 113 | ||
| 111 | - // 3. 调用CANN算子库API | 114 | + // 3. 调用CANN算子库API |
| 112 | - uint64_t workspaceSize = 0; | 115 | + uint64_t workspaceSize = 0; |
| 113 | - aclOpExecutor* executor; | 116 | + aclOpExecutor* executor; |
| 114 | - // 调用aclnnSWhere第一段接口 | 117 | + // 调用aclnnSWhere第一段接口 |
| 115 | - ret = aclnnSWhereGetWorkspaceSize(condition, self, other, out, &workspaceSize, &executor); | 118 | + ret = aclnnSWhereGetWorkspaceSize(condition, self, other, out, &workspaceSize, &executor); |
| 116 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSWhereGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 119 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSWhereGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 117 | - // 根据第一段接口计算出的workspaceSize申请device内存 | 120 | + // 根据第一段接口计算出的workspaceSize申请device内存 |
| 118 | - void* workspaceAddr = nullptr; | 121 | + void* workspaceAddr = nullptr; |
| 119 | - if (workspaceSize > 0) { | 122 | + if (workspaceSize > 0) { |
| 120 | - ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 123 | + ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 121 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 124 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 122 | - } | 125 | + } |
| 123 | - // 调用aclnnSWhere第二段接口 | 126 | + // 调用aclnnSWhere第二段接口 |
| 124 | - ret = aclnnSWhere(workspaceAddr, workspaceSize, executor, stream); | 127 | + ret = aclnnSWhere(workspaceAddr, workspaceSize, executor, stream); |
| 125 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSWhere failed. ERROR: %d\n", ret); return ret); | 128 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSWhere failed. ERROR: %d\n", ret); return ret); |
| 126 | - // 4. (固定写法)同步等待任务执行结束 | 129 | + // 4. (固定写法)同步等待任务执行结束 |
| 127 | - ret = aclrtSynchronizeStream(stream); | 130 | + ret = aclrtSynchronizeStream(stream); |
| 128 | - 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); |
| 129 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 132 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 130 | - auto size = GetShapeSize(outShape); | 133 | + auto size = GetShapeSize(outShape); |
| 131 | - std::vector<float> resultData(size, 0); | 134 | + std::vector<float> resultData(size, 0); |
| 132 | - ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), | 135 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float), |
| 133 | - ACL_MEMCPY_DEVICE_TO_HOST); | 136 | + 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); | 137 | + 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++) { | 138 | + for (int64_t i = 0; i < size; i++) { |
| 136 | - LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 139 | + LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| 137 | - } | 140 | + } |
| 138 | 141 | ||
| 139 | - // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 | 142 | + // 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改 |
| 140 | - aclDestroyTensor(self); | 143 | + aclDestroyTensor(self); |
| 141 | - aclDestroyTensor(other); | 144 | + aclDestroyTensor(other); |
| 142 | - aclDestroyTensor(condition); | 145 | + aclDestroyTensor(condition); |
| 143 | - aclDestroyTensor(out); | 146 | + aclDestroyTensor(out); |
| 144 | 147 | ||
| 145 | - // 7.释放device资源,需要根据具体API的接口定义修改 | 148 | + // 7.释放device资源,需要根据具体API的接口定义修改 |
| 146 | - aclrtFree(selfDeviceAddr); | 149 | + aclrtFree(selfDeviceAddr); |
| 147 | - aclrtFree(otherDeviceAddr); | 150 | + aclrtFree(otherDeviceAddr); |
| 148 | - aclrtFree(conditionDeviceAddr); | 151 | + aclrtFree(conditionDeviceAddr); |
| 149 | - aclrtFree(outDeviceAddr); | 152 | + aclrtFree(outDeviceAddr); |
| 150 | - if (workspaceSize > 0) { | 153 | + if (workspaceSize > 0) { |
| 151 | - aclrtFree(workspaceAddr); | 154 | + aclrtFree(workspaceAddr); |
| 152 | - } | 155 | + } |
| 153 | - aclrtDestroyStream(stream); | 156 | + aclrtDestroyStream(stream); |
| 154 | - aclrtResetDevice(deviceId); | 157 | + aclrtResetDevice(deviceId); |
| 155 | - aclFinalize(); | 158 | + aclFinalize(); |
| 156 | - return 0; | 159 | + return 0; |
| 157 | -} | 160 | +} |
| @@ -355,7 +355,7 @@ int main() { | |||
| 355 | void* workspaceAddr = nullptr; | 355 | void* workspaceAddr = nullptr; |
| 356 | if (workspaceSize > 0) { | 356 | if (workspaceSize > 0) { |
| 357 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 357 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 358 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 358 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 359 | } | 359 | } |
| 360 | // 调用aclnnRsub第二段接口 | 360 | // 调用aclnnRsub第二段接口 |
| 361 | ret = aclnnRsub(workspaceAddr, workspaceSize, executor, stream); | 361 | ret = aclnnRsub(workspaceAddr, workspaceSize, executor, stream); |
| @@ -329,7 +329,7 @@ int main() { | |||
| 329 | void* workspaceAddr = nullptr; | 329 | void* workspaceAddr = nullptr; |
| 330 | if (workspaceSize > 0) { | 330 | if (workspaceSize > 0) { |
| 331 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 331 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 332 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;); | 332 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 333 | } | 333 | } |
| 334 | // 调用aclnnTanhBackward第二段接口 | 334 | // 调用aclnnTanhBackward第二段接口 |
| 335 | ret = aclnnTanhBackward(workspaceAddr, workspaceSize, executor, stream); | 335 | ret = aclnnTanhBackward(workspaceAddr, workspaceSize, executor, stream); |
| @@ -47,9 +47,8 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | template <typename T> | 49 | template <typename T> |
| 50 | -int CreateAclTensor( | 50 | +int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, |
| 51 | - const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr, aclDataType dataType, | 51 | + aclDataType dataType, aclTensor** tensor) |
| 52 | - aclTensor** tensor) | ||
| 53 | { | 52 | { |
| 54 | auto size = GetShapeSize(shape) * sizeof(T); | 53 | auto size = GetShapeSize(shape) * sizeof(T); |
| 55 | // 调用aclrtMalloc申请device侧内存 | 54 | // 调用aclrtMalloc申请device侧内存 |
| @@ -67,9 +66,8 @@ int CreateAclTensor( | |||
| 67 | } | 66 | } |
| 68 | 67 | ||
| 69 | // 调用aclCreateTensor接口创建aclTensor | 68 | // 调用aclCreateTensor接口创建aclTensor |
| 70 | - *tensor = aclCreateTensor( | 69 | + *tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, |
| 71 | - shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), | 70 | + shape.data(), shape.size(), *deviceAddr); |
| 72 | - *deviceAddr); | ||
| 73 | return 0; | 71 | return 0; |
| 74 | } | 72 | } |
| 75 | 73 | ||
| @@ -96,8 +94,8 @@ int main() | |||
| 96 | std::vector<float> outputHostData = {1, 1, 1, 2, 2, 2, 3, 3.0}; | 94 | std::vector<float> outputHostData = {1, 1, 1, 2, 2, 2, 3, 3.0}; |
| 97 | std::vector<float> gradInputHostData = {0, 0, 0, 0, 0, 0, 0, 0.0}; | 95 | std::vector<float> gradInputHostData = {0, 0, 0, 0, 0, 0, 0, 0.0}; |
| 98 | // 创建gradOutput aclTensor | 96 | // 创建gradOutput aclTensor |
| 99 | - ret = CreateAclTensor( | 97 | + ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, aclDataType::ACL_FLOAT, |
| 100 | - gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, aclDataType::ACL_FLOAT, &gradOutput); | 98 | + &gradOutput); |
| 101 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 99 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 102 | // 创建output aclTensor | 100 | // 创建output aclTensor |
| 103 | ret = CreateAclTensor(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_FLOAT, &output); | 101 | ret = CreateAclTensor(outputHostData, outputShape, &outputDeviceAddr, aclDataType::ACL_FLOAT, &output); |
| @@ -116,7 +114,7 @@ int main() | |||
| 116 | void* workspaceAddr = nullptr; | 114 | void* workspaceAddr = nullptr; |
| 117 | if (workspaceSize > 0) { | 115 | if (workspaceSize > 0) { |
| 118 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 116 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 119 | - 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); |
| 120 | } | 118 | } |
| 121 | // 调用aclnnTanhBackward第二段接口 | 119 | // 调用aclnnTanhBackward第二段接口 |
| 122 | ret = aclnnTanhBackward(workspaceAddr, workspaceSize, executor, stream); | 120 | ret = aclnnTanhBackward(workspaceAddr, workspaceSize, executor, stream); |
| @@ -127,9 +125,8 @@ int main() | |||
| 127 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 125 | // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 |
| 128 | auto size = GetShapeSize(gradInputShape); | 126 | auto size = GetShapeSize(gradInputShape); |
| 129 | std::vector<float> resultData(size, 0); | 127 | std::vector<float> resultData(size, 0); |
| 130 | - ret = aclrtMemcpy( | 128 | + ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradInputDeviceAddr, |
| 131 | - resultData.data(), resultData.size() * sizeof(resultData[0]), gradInputDeviceAddr, size * sizeof(float), | 129 | + size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); |
| 132 | - ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 133 | 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); |
| 134 | for (int64_t i = 0; i < size; i++) { | 131 | for (int64_t i = 0; i < size; i++) { |
| 135 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); | 132 | LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]); |
| @@ -151,4 +148,4 @@ int main() | |||
| 151 | aclrtResetDevice(deviceId); | 148 | aclrtResetDevice(deviceId); |
| 152 | aclFinalize(); | 149 | aclFinalize(); |
| 153 | return 0; | 150 | return 0; |
| 154 | -} | 151 | +} |