Eventos para cronometragem e sincronização
Use cudaEvent para medir e ordenar o trabalho.
Eventos para cronometragem e sincronização é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
What Is an Event?
A CUDA event is a marker you drop into a stream. The GPU records when it reaches that point, letting you time and order work. ⏱️
The Event Handle
Events use a cudaEvent_t handle, just like streams. You create it, record it, query it, and destroy it when finished.
cudaEvent_t start, stop;Creating Events
Call cudaEventCreate for each event you need. Timing usually wants two: one before the work and one after.
cudaEventCreate(&start);
cudaEventCreate(&stop);Recording an Event
cudaEventRecord places the event into a stream. The GPU stamps it the moment it finishes everything queued before it.
cudaEventRecord(start, stream);Timing a Kernel
Record start, launch your kernel, then record stop, all in the same stream. The gap between them is the real GPU execution time.
cudaEventRecord(start, s);
kernel<<<g, b, 0, s>>>(d);
cudaEventRecord(stop, s);Waiting for an Event
Events are async, so the host must wait. cudaEventSynchronize blocks until the given event has actually been recorded on the GPU.
cudaEventSynchronize(stop);Measuring Elapsed Time
cudaEventElapsedTime returns the milliseconds between two recorded events. This is far more accurate than a host-side CPU clock.
float ms;
cudaEventElapsedTime(&ms, start, stop);Why Not the CPU Clock?
Host timers include launch overhead and miss true GPU timing. Events sit inside the stream, so they capture exactly what the device did.
Cross-Stream Ordering
Events also synchronize streams. cudaStreamWaitEvent makes one stream pause until an event recorded in another has happened.
cudaStreamWaitEvent(streamB, eventA, 0);Building Dependencies
With stream-wait-event you express a dependency: streamB starts its work only after streamA reaches its event, without blocking the host.
Cleaning Up Events
Free events you no longer need with cudaEventDestroy. Like streams, they are cheap but should not be leaked in long-running apps.
cudaEventDestroy(start);
cudaEventDestroy(stop);Quick Check
Which function gives you the time between two events?
Recap
Events let you time GPU work precisely and order one stream after another. They are your tool for measuring and coordinating overlap. 🎯
Perguntas Frequentes
A aula “Eventos para cronometragem e sincronização” é grátis?
Sim — o texto completo de “Eventos para cronometragem e sincronização” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.
O que vou aprender em “Eventos para cronometragem e sincronização”?
Use cudaEvent para medir e ordenar o trabalho. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar CUDA Academy?
Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Eventos para cronometragem e sincronização”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de CUDA Academy?
Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- A armadilha do fluxo padrão
- Crie e use fluxos
- Eventos para cronometragem e sincronização
- Sobreponha cópia e cálculo