Iamax【免费下载链接】sip本项目是CANN提供的一款高效、可靠的高性能信号处理算子加速库基于华为Ascend AI处理器专门为信号处理领域而设计。项目地址: https://gitcode.com/cann/sip产品支持情况产品是否支持Atlas 200I/500 A2 推理产品×Atlas 推理系列产品×Atlas 训练系列产品×Atlas A3 训练系列产品/Atlas A3 推理系列产品√Atlas A2 训练系列产品/Atlas A2 推理系列产品√Ascend 950PR/Ascend 950DT×功能说明接口功能asdBlasMakeIamaxPlan初始化该句柄对应的Iamax算子配置。asdBlasIsamax找到实数向量中绝对值最大的元素并返回其索引。如果有多个元素相等则返回其中的最小索引。asdBlasIcamax找到复数向量中虚部、实部绝对值之和最大的元素并返回其索引。如果有多个元素的绝对值之和相等则返回最先找到的那个元素的索引。计算公式asdBlasIsamax的公式$$ i_{max}argmax_{i1}^n(|(x_{i})| ) $$示例 输入“x”为 [3, 4, 4,3] 调用asdBlasIsamax算子后输出“result”为 2asdBlasIcamax的公式$$ i_{max}argmax_{i1}^n(|Im(x_{i}) ||Re(x_{i} )|) $$ 其中Re(∙)表示复数的实部Im(∙)表示复数的虚部。 示例 输入“x”为 [34i,4-3i,42i,3-1i] 调用asdBlasIcamax算子后输出“result”为 1函数原型AspbStatus asdBlasMakeIamaxPlan( asdBlasHandle handle)AspbStatus asdBlasIcamax( asdBlasHandle handle, const int64_t n, aclTensor * x, const int64_t incx, aclTensor * result)AspbStatus asdBlasIsamax( asdBlasHandle handle, const int64_t n, aclTensor * x, const int64_t incx, aclTensor * result)asdBlasMakeIamaxPlan参数说明参数名输入/输出描述handleasdBlasHandle输入算子的句柄返回值返回状态码具体参见SiP返回码。asdBlasIsamax asdBlasIcamax参数说明参数名输入/输出描述handleasdBlasHandle输入算子的句柄。nint64_t输入总的元素个数。xaclTensor *输入对应公式中的x。asdBlasIsamax支持的数据类型支持FLOAT32。asdBlasIcamax支持的数据类型支持COMPLEX64。数据格式支持ND。shape为[n]。incxint64_t输入x相邻元素间的内存地址偏移量当前约束为1。resultaclTensor *输出表示输出的结果对应公式中的result。数据类型支持INT32只包含一个元素。数据格式支持ND。shape为[1]。返回值返回状态码具体参见SiP返回码。约束说明输入的元素个数n当前支持的范围为[16.71e06]。算子输入shape为[n]输出shape为[1]。算子实际计算时不支持ND高维度运算不支持维度≥3的运算。调用示例示例代码如下该样例旨在提供快速上手、开发和调试算子的最小化实现其核心目标是使用最精简的代码展示算子的核心功能而非提供生产级的安全保障。不推荐用户直接将示例代码作为业务代码若用户将示例代码应用在自身的真实业务场景中且发生了安全问题则需用户自行承担。asdBlasIsamax#include iostream #include vector #include asdsip.h #include acl/acl.h #include acl_meta.h using namespace AsdSip; #define ASD_STATUS_CHECK(err) \ do { \ AsdSip::AspbStatus err_ (err); \ if (err_ ! AsdSip::ErrorType::ACL_SUCCESS) { \ std::cout Execute failed. std::endl; \ exit(-1); \ } \ } while (0) #define CHECK_RET(cond, return_expr) \ do { \ if (!(cond)) { \ return_expr; \ } \ } while (0) #define LOG_PRINT(message, ...) \ do { \ printf(message, ##__VA_ARGS__); \ } while (0) int64_t GetShapeSize(const std::vectorint64_t shape) { int64_t shapeSize 1; for (auto i : shape) { shapeSize * i; } return shapeSize; } int Init(int32_t deviceId, aclrtStream *stream) { // 固定写法acl初始化 auto ret aclInit(nullptr); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclInit failed. ERROR: %d\n, ret); return ret); ret aclrtSetDevice(deviceId); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtSetDevice failed. ERROR: %d\n, ret); return ret); ret aclrtCreateStream(stream); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtCreateStream failed. ERROR: %d\n, ret); return ret); return 0; } template typename T int CreateAclTensor(const std::vectorT hostData, const std::vectorint64_t shape, void **deviceAddr, aclDataType dataType, aclTensor **tensor) { auto size GetShapeSize(shape) * sizeof(T); // 调用aclrtMalloc申请device侧内存 auto ret aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtMalloc failed. ERROR: %d\n, ret); return ret); // 调用aclrtMemcpy将host侧数据复制到device侧内存上 ret aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtMemcpy failed. ERROR: %d\n, ret); return ret); // 计算连续tensor的strides std::vectorint64_t strides(shape.size(), 1); for (int64_t i shape.size() - 2; i 0; i--) { strides[i] shape[i 1] * strides[i 1]; } // 调用aclCreateTensor接口创建aclTensor *tensor aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); return 0; } int main(int argc, char **argv) { int deviceId 0; aclrtStream stream; auto ret Init(deviceId, stream); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(Init acl failed. ERROR: %d\n, ret); return ret); int64_t n 6; int64_t incx 1; int64_t xSize n; int64_t ySize 1; std::vectorfloat tensorInXData; tensorInXData.reserve(xSize); for (int64_t i 0; i xSize; i) { if (i xSize / 2) { tensorInXData[i] 1.0 * i; } else { tensorInXData[i] xSize - 1.0 * i; } } std::vectorint32_t tensorOutYData; tensorOutYData.reserve(ySize); std::cout ------- input X ------- std::endl; for (int64_t i 0; i xSize; i) { std::cout tensorInXData[i] ; } std::cout std::endl; std::vectorint64_t xShape {xSize}; std::vectorint64_t yShape {ySize}; aclTensor *inputX nullptr; aclTensor *outputY nullptr; void *inputXDeviceAddr nullptr; void *outputYDeviceAddr nullptr; ret CreateAclTensor(tensorInXData, xShape, inputXDeviceAddr, aclDataType::ACL_FLOAT, inputX); CHECK_RET(ret ::ACL_SUCCESS, return ret); ret CreateAclTensor(tensorOutYData, yShape, outputYDeviceAddr, aclDataType::ACL_INT32, outputY); CHECK_RET(ret ::ACL_SUCCESS, return ret); asdBlasHandle handle; asdBlasCreate(handle); size_t lwork 0; void *buffer nullptr; asdBlasMakeIamaxPlan(handle); asdBlasGetWorkspaceSize(handle, lwork); std::cout lwork lwork std::endl; if (lwork 0) { ret aclrtMalloc(buffer, static_castint64_t(lwork), ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(allocate workspace failed. ERROR: %d\n, ret); return ret); } asdBlasSetWorkspace(handle, buffer); asdBlasSetStream(handle, stream); ASD_STATUS_CHECK(asdBlasIsamax(handle, n, inputX, incx, outputY)); asdBlasSynchronize(handle); asdBlasDestroy(handle); ret aclrtMemcpy(tensorOutYData.data(), ySize * sizeof(int32_t), outputYDeviceAddr, ySize * sizeof(int32_t), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(copy result from device to host failed. ERROR: %d\n, ret); return ret); std::cout ------- result ------- std::endl; std::cout tensorOutYData[0] std::endl; std::cout Execute successfully. std::endl; aclDestroyTensor(inputX); aclDestroyTensor(outputY); aclrtFree(inputXDeviceAddr); aclrtFree(outputYDeviceAddr); aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }asdBlasIcamax#include iostream #include vector #include asdsip.h #include acl/acl.h #include acl_meta.h using namespace AsdSip; #define ASD_STATUS_CHECK(err) \ do { \ AsdSip::AspbStatus err_ (err); \ if (err_ ! AsdSip::ErrorType::ACL_SUCCESS) { \ std::cout Execute failed. std::endl; \ exit(-1); \ } \ } while (0) #define CHECK_RET(cond, return_expr) \ do { \ if (!(cond)) { \ return_expr; \ } \ } while (0) #define LOG_PRINT(message, ...) \ do { \ printf(message, ##__VA_ARGS__); \ } while (0) int64_t GetShapeSize(const std::vectorint64_t shape) { int64_t shapeSize 1; for (auto i : shape) { shapeSize * i; } return shapeSize; } int Init(int32_t deviceId, aclrtStream *stream) { // 固定写法acl初始化 auto ret aclInit(nullptr); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclInit failed. ERROR: %d\n, ret); return ret); ret aclrtSetDevice(deviceId); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtSetDevice failed. ERROR: %d\n, ret); return ret); ret aclrtCreateStream(stream); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtCreateStream failed. ERROR: %d\n, ret); return ret); return 0; } template typename T int CreateAclTensor(const std::vectorT hostData, const std::vectorint64_t shape, void **deviceAddr, aclDataType dataType, aclTensor **tensor) { auto size GetShapeSize(shape) * sizeof(T); // 调用aclrtMalloc申请device侧内存 auto ret aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtMalloc failed. ERROR: %d\n, ret); return ret); // 调用aclrtMemcpy将host侧数据复制到device侧内存上 ret aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(aclrtMemcpy failed. ERROR: %d\n, ret); return ret); // 计算连续tensor的strides std::vectorint64_t strides(shape.size(), 1); for (int64_t i shape.size() - 2; i 0; i--) { strides[i] shape[i 1] * strides[i 1]; } // 调用aclCreateTensor接口创建aclTensor *tensor aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND, shape.data(), shape.size(), *deviceAddr); return 0; } int main(int argc, char **argv) { int deviceId 0; aclrtStream stream; auto ret Init(deviceId, stream); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(Init acl failed. ERROR: %d\n, ret); return ret); int64_t n 6; int64_t incx 1; int64_t xSize 6; int64_t ySize 1; std::vectorstd::complexfloat tensorInXData; tensorInXData.reserve(xSize); for (int64_t i 0; i xSize; i) { if (i xSize / 2) { tensorInXData[i] {(float)(1.0 * i), (float)(1.0 * i)}; } else { tensorInXData[i] {(float)(xSize - 1.0 * i), (float)(xSize - 1.0 * i)}; } } std::vectorint32_t tensorOutYData; tensorOutYData.reserve(ySize); std::cout n: n std::endl; std::cout ------- input x ------- std::endl; for (int64_t i 0; i xSize; i) { std::cout tensorInXData[i] ; } std::cout std::endl; std::vectorint64_t xShape {xSize}; std::vectorint64_t yShape {ySize}; aclTensor *inputX nullptr; aclTensor *outputY nullptr; void *inputXDeviceAddr nullptr; void *outputYDeviceAddr nullptr; ret CreateAclTensor(tensorInXData, xShape, inputXDeviceAddr, aclDataType::ACL_COMPLEX64, inputX); CHECK_RET(ret ::ACL_SUCCESS, return ret); ret CreateAclTensor(tensorOutYData, yShape, outputYDeviceAddr, aclDataType::ACL_INT32, outputY); CHECK_RET(ret ::ACL_SUCCESS, return ret); asdBlasHandle handle; asdBlasCreate(handle); size_t lwork 0; void *buffer nullptr; asdBlasMakeIamaxPlan(handle); asdBlasGetWorkspaceSize(handle, lwork); std::cout lwork lwork std::endl; if (lwork 0) { ret aclrtMalloc(buffer, static_castint64_t(lwork), ACL_MEM_MALLOC_HUGE_FIRST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(allocate workspace failed. ERROR: %d\n, ret); return ret); } asdBlasSetWorkspace(handle, buffer); asdBlasSetStream(handle, stream); ASD_STATUS_CHECK(asdBlasIcamax(handle, n, inputX, incx, outputY)); asdBlasSynchronize(handle); asdBlasDestroy(handle); ret aclrtMemcpy(tensorOutYData.data(), ySize * sizeof(int32_t), outputYDeviceAddr, ySize * sizeof(int32_t), ACL_MEMCPY_DEVICE_TO_HOST); CHECK_RET(ret ::ACL_SUCCESS, LOG_PRINT(copy result from device to host failed. ERROR: %d\n, ret); return ret); std::cout ------- result ------- std::endl; std::cout tensorOutYData[0] std::endl; std::cout Execute successfully. std::endl; aclDestroyTensor(inputX); aclDestroyTensor(outputY); aclrtFree(inputXDeviceAddr); aclrtFree(outputYDeviceAddr); aclrtDestroyStream(stream); aclrtResetDevice(deviceId); aclFinalize(); return 0; }【免费下载链接】sip本项目是CANN提供的一款高效、可靠的高性能信号处理算子加速库基于华为Ascend AI处理器专门为信号处理领域而设计。项目地址: https://gitcode.com/cann/sip创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考