Stream Priority Configuration

During running, the scheduler on the device determines the task execution sequence based on the priority of each stream. Tasks to be executed in a high-priority stream are scheduled before those in a low-priority stream, but do not preempt running low-priority tasks. During execution, the device does not dynamically re-evaluate the task queue. Therefore, increasing the priority of a stream does not interrupt the tasks that are being executed.

The priority of a stream is mainly used to affect the task scheduling sequence, rather than strictly specifying the execution sequence. You can adjust the priority of a stream to guide the task execution sequence, but this does not guarantee the absolute sequence of tasks.

When creating a stream by calling aclrtCreateStreamWithConfig, you can specify the priority of the stream. You can obtain the minimum and maximum priorities through the aclrtDeviceGetStreamPriorityRange API, thus determining the priority range.

The following is the sample code, which is for reference only and cannot be directly copied for compilation and running:

1
2
3
4
5
6
7
8
// Query the minimum and maximum stream priorities supported by the current device.
aclrtDeviceGetStreamPriorityRange(&leastPriority, &greatestPriority);

// Create streams with the highest and lowest priorities.
aclrtStream stream_high;
aclrtStream stream_low;
aclrtCreateStreamWithConfig(&stream_high, greatestPriority, ACL_STREAM_FAST_LAUNCH);
aclrtCreateStreamWithConfig(&stream_low, leastPriority, ACL_STREAM_FAST_LAUNCH);

The stream priority takes effect within the device scope, not within the context scope.