The Ascend 310 AI Processor does not support this API.The Ascend 910 AI Processor does not support this API.
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code sample of key steps for your reference only.
//1. Initialize AscendCL.
aclRet = aclInit(nullptr);
//2. Allocate runtime resources including the device, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);
//3. Create description of the data processing channel. dvppChannelDesc_ is of type acldvppChannelDesc.
dvppChannelDesc_ = acldvppCreateChannelDesc();
//4. Create a data processing channel.
aclError ret = acldvppCreateChannel(dvppChannelDesc_);
//5. Allocate input buffer (run mode specific).
//Call aclrtGetRunMode to obtain the run mode of the software stack. If ACL_DEVICE is returned, allocate and use the device buffer. If ACL_HOST is returned, transfer the input image data to the device by using the aclrtMemcpy call. After the data transfer is complete, the buffer needs to be freed in a timely manner.
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
//inputPicWidth and inputPicHeight respectively indicate the width and height of an image after alignment. The following uses a YUV420SP image as an example.
uint32_t inBufferSize = inputPicWidth * inputPicHeight * 3 / 2;
if (runMode == ACL_HOST) {
//Allocate host buffer vpcInHostBuffer.
void* vpcInHostBuffer = nullptr;
vpcInHostBuffer = malloc(inBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, vpcInHostBuffer, inBufferSize);
//Allocate the device buffer inDevBuffer_.
aclRet = acldvppMalloc(&inDevBuffer_, inBufferSize);
//Transfer the input image data to the device by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(inDevBuffer_, inBufferSize, vpcInHostBuffer, inBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);
//Free the buffer in a timely manner after data transfer is complete.
free(vpcInHostBuffer);
} else {
//Allocate input buffer inDevBuffer_ on the device.
ret = acldvppMalloc(&inDevBuffer_, inBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, inDevBuffer_, inBufferSize);
}
//6. Allocate the CSC output buffer outBufferDev_. The buffer size outBufferSize_ is obtained based on the calculation formula.
//outputPicWidth and outputPicHeight respectively indicate the width and height after image alignment. The following uses a YUV420SP image as an example.
uint32_t outBufferSize_ = outputPicWidth * outputPicHeight * 3 / 2
ret = acldvppMalloc(&outBufferDev_, outBufferSize_)
//7. Create the description of the input image for CSC and set the attribute values.
//inputDesc_ is of type acldvppPicDesc.
inputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(inputDesc_, inDevBuffer_);
acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(inputDesc_, inputWidth_);
acldvppSetPicDescHeight(inputDesc_, inputHeight_);
acldvppSetPicDescWidthStride(inputDesc_, inputWidthStride);
acldvppSetPicDescHeightStride(inputDesc_, inputHeightStride);
acldvppSetPicDescSize(inputDesc_, inBufferSize);
//8. Create the description of the output image for CSC and set the attribute values. The output height and width must be the same as those of the input.
//If the CSC output image is used as the input for model inference, the output width and height must meet the requirements of the model.
//outputDesc_ is of type acldvppPicDesc.
outputDesc_= acldvppCreatePicDesc();
acldvppSetPicDescData(outputDesc_, outBufferDev_);
acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_YUV_400);
acldvppSetPicDescWidth(outputDesc_, outputWidth_);
acldvppSetPicDescHeight(outputDesc_, outputHeight_);
acldvppSetPicDescWidthStride(outputDesc_, outputWidthStride);
acldvppSetPicDescHeightStride(outputDesc_, outputHeightStride);
acldvppSetPicDescSize(outputDesc_, outBufferSize_);
//9. Perform asynchronous CSC and call aclrtSynchronizeStream to wait for the stream tasks to complete.
ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_);
ret = aclrtSynchronizeStream(stream_);
//10. After CSC is complete, destroy the allocations, including the description of the input and output image, as well as the input and output buffer.
acldvppDestroyPicDesc(inputDesc_);
acldvppDestroyPicDesc(outputDesc_);
if (runMode == ACL_HOST) {
//In this mode, the processing result is on the device. Therefore, you need to call the memory copy API to transfer the result data, and then free the device buffer.
//Allocate host buffer vpcOutHostBuffer.
void* vpcOutHostBuffer = nullptr;
vpcOutHostBuffer = malloc(outBufferSize_);
//Transfer the processing result of the device to the host by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(vpcOutHostBuffer, outBufferSize_, outBufferDev_, outBufferSize_, ACL_MEMCPY_DEVICE_TO_HOST);
//Free the input and output buffer on the device.
(void)acldvppFree(inDevBuffer_);
(void)acldvppFree(outBufferDev_);
//Free the buffer after the data is used.
free(vpcOutHostBuffer);
} else {
//The process is running on the device, and the processing result is also on the device. Free the device buffer when the data is no longer needed.
(void)acldvppFree(inDevBuffer_);
(void)acldvppFree(outBufferDev_);
}
//11. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);
//12. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....