Stream Creation and Destruction
Call aclrtCreateStream to create a stream. The obtained aclrtStream object is used as the stream input parameter for subsequent asynchronous memory copy, stream synchronization, kernel execution, and other APIs. An explicitly created stream needs to be explicitly destroyed by calling aclrtDestroyStream. If there are unfinished tasks in the stream to be destroyed, the stream will be destroyed after the tasks are complete.
The following is a code example for creating a stream and dispatching compute tasks to the stream. It 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 | // Explicitly create a stream. aclrtStream stream; aclrtCreateStream(&stream); // Dispatch the host-to-device copy task, MyKernel task, and device-to-host copy task to the stream. aclrtMemcpyAsync(devPtr, devSize, hostPtr, hostSize, ACL_MEMCPY_HOST_TO_DEVICE, stream); myKernel<<<8, nullptr, stream>>>(); aclrtMemcpyAsync(hostPtr, hostSize, devPtr, devSize, ACL_MEMCPY_DEVICE_TO_HOST, stream); // Destroy the stream (after the device-to-host copy task is complete). aclrtDestroyStream(stream); |
Parent topic: Stream Management