aclnnPolar
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Supported
Atlas training products : Not supported
Function
Description: Converts the polar coordinates corresponding to the absolute value input and angle into Cartesian coordinates, producing a complex tensor out.
Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnPolarGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor that contains the operator computation process, and then call the "aclnnPolar" API to perform the computation.
aclnnStatus aclnnPolarGetWorkspaceSize(const aclTensor* input, const aclTensor* angle, aclTensor* out,uint64_t* workspaceSize, aclOpExecutor** executor)aclnnStatus aclnnPolar(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
aclnnPolarGetWorkspaceSize
Parameters:
input(aclTensor*, calculation input): Input tensor, corresponding to
absin the formula. The supported data type is FLOAT. Non-contiguous tensors are supported, and the data format supports ND. The number of dimensions cannot exceed 8.angle(aclTensor*, calculation input): Input tensor, corresponding to
anglein the formula. The data type must be the same as that of input. Non-contiguous tensors are supported, and the data format supports ND. The number of dimensions must be the same as that of input.out(aclTensor*, calculation output): Output tensor, corresponding to
outin the formula. The supported data type is COMPLEX64. Non-contiguous tensors are supported, and the data format supports ND. The number of dimensions must be the same as that of input.workspaceSize (uint64_t*, output parameter): Returns the workspace size to be allocated on the Device side.
executor (aclOpExecutor**, output parameter): Returns the op executor, which includes computation.
Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
The first-phase API checks input parameters and reports errors in the following use cases:
Returns 161001 (ACLNN_ERR_PARAM_NULLPTR): 1. The input, angle, or out is a null pointer.
Returns 161002 (ACLNN_ERR_PARAM_INVALID): 1. The data types of input and angle are not within the supported range.
2. The data types of input and angle are inconsistent.
3. The data type of out is not COMPLEX64.
4. The dimensions of input, angle, or out exceed 8 or are inconsistent.aclnnPolar
Parameters:
workspace (void*, input parameter): Device-side allocated workspace memory address.
workspaceSize (uint64_t, input parameter): Device-side allocated workspace size, obtained from the first-phase API aclnnPolarGetWorkspaceSize.
executor (aclOpExecutor*, input parameter): Op executor, which includes the computation process of the operator.
stream (aclrtStream, input parameter): Stream on which the task is executed.
Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnPolar defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about the compilation and execution procedure, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_polar.h"
#include <complex>
#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::vector<int64_t>& shape) {
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate for resource initialization.
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::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate memory on the 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);
// Call aclrtMemcpy to copy data from the host to the device memory.
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);
// Compute the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t 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);
// 2. Construct the input and output as required by the API.
std::vector<int64_t> inputShape = {1, 2};
std::vector<int64_t> angleShape = {1, 2};
std::vector<int64_t> outShape = {1, 2};
void* inputDeviceAddr = nullptr;
void* angleDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* input = nullptr;
aclTensor* angle = nullptr;
aclTensor* out = nullptr;
std::vector<float> inputHostData = {1, 2};
std::vector<float> angleHostData = {1, 2};
std::vector<std::complex<float>> outHostData = {1, 2};
// Create the input aclTensor.
ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the angle aclTensor.
ret = CreateAclTensor(angleHostData, angleShape, &angleDeviceAddr, aclDataType::ACL_FLOAT, &angle);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_COMPLEX64, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnPolar.
ret = aclnnPolarGetWorkspaceSize(input, angle, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnPolarGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnPolar.
ret = aclnnPolar(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnPolar failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait synchronously for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value, copy the result on the device to the host. Modify the code based on the specific API definition
auto size = GetShapeSize(outShape);
std::vector<std::complex<float>> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
size * sizeof(resultData[0]), 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);
for (int64_t i = 0; i < size; i++) {
// The first number in the output is the real part, and the second is the imaginary part
std::cout << "result[" << i << "]: " <<resultData[i] << std::endl;
}
// 6. Release aclTensor and aclIntArray. Modify the code based on the specific API definition
aclDestroyTensor(input);
aclDestroyTensor(angle);
aclDestroyTensor(out);
// 7. Release device resources
aclrtFree(inputDeviceAddr);
aclrtFree(angleDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}