Event Query
The aclrtQueryEventStatus API can be called to check whether a specified event is complete. This API is non-blocking. The event status can be ACL_EVENT_RECORDED_STATUS_COMPLETE (all tasks are complete) or ACL_EVENT_RECORDED_STATUS_NOT_READY (some tasks are not complete).
The following is the sample code for implementing multi-thread memory pool reuse management using events, which is for reference only and cannot be directly copied for compilation or running.
- Create a memory pool in thread A. The memory used by the operator comes from the memory pool. Insert an Event Record task after the operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Allocate the memory pool. ...... // Create a stream. aclrtStream stream; aclrtCreateStream(&stream); // Create an event. aclrtEvent event; aclrtCreateEventExWithFlag(&event, ACL_EVENT_CAPTURE_STREAM_PROGRESS); // Deliver compute tasks to the stream. ...... // Insert an event into the stream where the operator is located. aclrtRecordEvent(event, stream);
- Call the query API in thread B. If the queried event is complete, the memory occupied by the operator before Event Record can be safely reused.
1 2 3 4 5 6 7 8
aclrtEventRecordedStatus status; // Check whether the event of thread A is complete. aclrtQueryEventStatus(event, &status); if (status == ACL_EVENT_RECORDED_STATUS_COMPLETE) { // The event is complete, and the memory occupied by the operator can be reused. } else { // The operator execution is not complete, and the memory occupied by the operator cannot be reused. }
Parent topic: Event-based synchronization