How to set a variable as an event for cudastream wait event?

3.2.8.8. Events
The runtime also provides a way to closely monitor the device’s progress, as well as perform accurate timing, by letting the application asynchronously record events at any point in the program, and query when these events are completed. An event has completed when all tasks - or optionally, all commands in a given stream - preceding the event have completed. Events in stream zero are completed after all preceding tasks and commands in all streams are completed.
3.2.8.8.1. Creation and Destruction
The following code sample creates two events:
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
They are destroyed this way:
cudaEventDestroy(start);
cudaEventDestroy(stop);
3.2.8.8.2. Elapsed Time
The events created in Creation and Destruction can be used to time the code sample of Creation and Destruction the following way:
cudaEventRecord(start, 0);
for (int i = 0; i < 2; ++i) {
cudaMemcpyAsync(inputDev + i * size, inputHost + i * size,
size, cudaMemcpyHostToDevice, stream[i]);
MyKernel<<<100, 512, 0, stream[i]>>>
(outputDev + i * size, inputDev + i * size, size);
cudaMemcpyAsync(outputHost + i * size, outputDev + i * size,
size, cudaMemcpyDeviceToHost, stream[i]);
}
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
float elapsedTime;
cudaEventElapsedTime(&elapsedTime, start, stop);

https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html?highlight=stream#events

I am trying to create an event, when a flag variable arrive at 2, the event happens, and later I will use this event to trigger a stream. Is this possible? I learned the event description, and I … am not sure how to do it. Thanks!

Let’s assume the flag variable is stored in device memory.

You could issue a kernel into a stream that waits until that variable is 2:

__global__ void k(volatile int *d) { while (*d != 2){};}

Then record the event after that kernel:

k<<<1,1, 0 , stream>>>(d);
cudaEventRecord(...);

In another stream, wait for that event.

1 Like

Oh, you are right! Good point!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.