CUDA-cudaEvent_t记录程序运行时间

  • Post author:
  • Post category:其他


// create two events

cudaEvent_t start, stop; //这里声明两个事件对象start 和stop。

cudaEventCreate(&start);

cudaEventCreate(&stop);//这里创建两个事件对象start 和stop

// record start event on the default stream

cudaEventRecord(start);  //记录对象strat的时间,当前时刻,即kernel运行前的时刻

// execute kernel

kernel<<<grid, block>>>(arguments);

// record stop event on the default stream

cudaEventRecord(stop);   //记录对象stop的时间,当前时刻,即kernel运行后的时刻

// wait until the stop event completes

cudaEventSynchronize(stop); //告诉CPU在stop事件上同步

// calculate the elapsed time between two events

float time;

cudaEventElapsedTime(&time, start, stop);  //start与stop两个时刻之间的时间差即为kernel的运行时间。工具函数,用来计算两个事件之间经历的时间,第一个参数为某个浮点变量的地址,在这个参数中将包含两次事件之间经历的时间,单位是毫秒

// clean up the two events

cudaEventDestroy(start);

cudaEventDestroy(stop);

Stream Synchronization



版权声明:本文为weixin_55035144原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。