Event Timestamp Recording

The event created in Event Creation and Destruction can be used to collect statistics on the time consumed by compute tasks in a stream. A code example is as follows. It is used only to describe how to use events 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
uint64_t time = 0;
float useTime = 0;

// Create a stream.
aclrtStream stream;
aclrtCreateStream(&stream);

aclrtEvent startEvent;
aclrtEvent endEvent;
// Create an event. Pass the ACL_EVENT_TIME_LINE parameter to the API, indicating that the created event is used for recording.
aclrtCreateEventExWithFlag(&startEvent, ACL_EVENT_TIME_LINE);
aclrtCreateEventExWithFlag(&endEvent, ACL_EVENT_TIME_LINE);

// Insert startEvent.
aclrtRecordEvent(startEvent, stream);
// Dispatch compute tasks to the stream.
kernel<<< grid, block, 0, stream>>>(...);
// Insert endEvent.
aclrtRecordEvent(endEvent, stream);
aclrtSynchronizeStream(stream);

// Obtain the timestamp and calculate the time consumed.
aclrtEventElapsedTime(&useTime, startEvent, endEvent);