Sample Code

Principles

Sample Code

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);
if (runMode == ACL_HOST) { 
    //Allocate the host buffer inputHostBuff and read the input image to this address. inDevBufferSize is the size of the read image.
    void* inputHostBuff = nullptr;
    inputHostBuff = malloc(inDevBufferSize);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, inputHostBuff, inDevBufferSize);
    //Allocate the device buffer inDevBuffer_.
    aclRet = acldvppMalloc(&inDevBuffer_, inDevBufferSize);
    //Transfer the input image data to the device by using the aclrtMemcpy call.
    aclRet = aclrtMemcpy(inDevBuffer_, inDevBufferSize, inputHostBuff, inDevBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);

} else {
    //Allocate input buffer inDevBuffer_ on the device.
    ret = acldvppMalloc(&inDevBuffer_, inDevBufferSize);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, inDevBuffer_, inDevBufferSize);
}

//6. Allocate decoding output buffer decodeOutDevBuffer_.
//Calculate the buffer size required for the PNGD processing result.
uint32_t decodeOutBufferSize = 0;
ret = acldvppPngPredictDecSize(inputHostBuff, inDevBufferSize, PIXEL_FORMAT_RGB_888, &decodeOutBufferSize)
ret = acldvppMalloc(&decodeOutDevBuffer_, decodeOutBufferSize)
//Free the buffer in a timely manner.
free(inputHostBuff);

//7. Create the description of the decoded image and set the attribute values.
//decodeOutputDesc is of type acldvppPicDesc.
decodeOutputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(decodeOutputDesc_, decodeOutDevBuffer_);
acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_RGB_888); 
acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize);

//8. Perform asynchronous decoding and call aclrtSynchronizeStream to wait for the stream tasks to complete.
ret = acldvppPngDecodeAsync(dvppChannelDesc_, inDevBuffer_, inDevBufferSize, decodeOutputDesc_, stream_);
ret = aclrtSynchronizeStream(stream_);

//9. Destroy the allocations, including the description of the output image, output buffer, channel description, and channel.
acldvppDestroyPicDesc(decodeOutputDesc_);

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 OutHostBuffer.
    void* OutHostBuffer = nullptr;
    OutHostBuffer = malloc(decodeOutBufferSize);
    //Transfer the processing result of the device to the host by using the aclrtMemcpy call.
    aclRet = aclrtMemcpy(OutHostBuffer, decodeOutBufferSize, decodeOutDevBuffer_, decodeOutBufferSize, ACL_MEMCPY_DEVICE_TO_HOST);
    //Free the input and output buffer on the device.
    (void)acldvppFree(inDevBuffer_);
    (void)acldvppFree(decodeOutDevBuffer_);
    //Free the buffer after the data is used.
    free(OutHostBuffer);
} 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(decodeOutDevBuffer_);
}
acldvppDestroyChannel(dvppChannelDesc_);
(void)acldvppDestroyChannelDesc(dvppChannelDesc_);
dvppChannelDesc_ = nullptr;

//10. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);

//11. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....