Host Callback Task
CANN provides a flexible way for asynchronous collaboration between the CPU and NPU. You can use aclrtLaunchHostFunc to insert a host callback task at any position in a stream. After all previous tasks in the stream are complete, the host callback task is automatically executed and blocks the execution of subsequent tasks in the stream.
The callback function cannot directly or indirectly call CANN Runtime APIs. Otherwise, errors or deadlocks may occur.
The following is a code sample for inserting a host callback task to a stream, which is for reference only and cannot be directly copied for compilation or running. For the complete sample code, click here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // Host callback task void myHostCallback(void *args) { printf("In MyHostCallback.\n"); // Processing after myKernel1 is complete, blocking the execution of MyKernel2 ...... } ...... // Create a stream. aclrtStream stream; aclrtCreateStream(&stream); // Deliver tasks to the stream. aclrtMemcpyAsync(devPtrIn, size, hostPtr, hostSize, ACL_MEMCPY_HOST_TO_DEVICE, stream); myKernel1<<<8, nullptr, stream>>>(devPtrIn, devPtrOut, size); aclrtLaunchHostFunc(stream, myHostCallback, nullptr); myKernel2<<<8, nullptr, stream>>>(devPtrOut, size); aclrtMemcpyAsync(hostPtr, hostSize, devPtrOut, size, ACL_MEMCPY_DEVICE_TO_HOST, stream); // Block the application running until all tasks in the specified stream are complete. aclrtSynchronizeStream(stream); // Destroy the stream. aclrtDestroyStream(stream); |
Parent topic: Stream Management