cudaEvent_t记录程序运行时间
create two events
这里声明两个事件对象start 和stop
cudaEvent_t start, stop;
这里创建两个事件对象start 和stop
cudaEventCreate(&start);
cudaEventCreate(&stop);
record start event on the default stream
记录对象strat的时间,当前时刻,即kernel运行前的时刻
cudaEventRecord(start);
execute kernel
kernel<<<grid, block>>>(arguments);
record stop event on the default stream
记录对象stop的时间,当前时刻,即kernel运行后的时刻
cudaEventRecord(stop);
wait until the stop event completes
告诉CPU在stop事件上同步
cudaEventSynchronize(stop);
calculate the elapsed time between two events
float time;
start与stop两个时刻之间的时间差即为kernel的运行时间。工具函数,用来计算两个事件之间经历的时间,第一个参数为某个浮点变量的地址,在这个参数中将包含两次事件之间经历的时间,单位是毫秒
cudaEventElapsedTime(&time, start, stop);
clean up the two events
cudaEventDestroy(start);
cudaEventDestroy(stop);
Stream Synchronization