在我们写CUDA程序中,我们需要知道程序的运行时间,可以通过以下代码知道
cudaEvent_t start, stop; //事件对象
cudaEventCreate(&start); //创建事件
cudaEventCreate(&stop); //创建事件
cudaEventRecord(start, stream); //记录开始
myKernel<<<dimg,dimb,size_smem,stream>>>(parameter list);//执行核函数
cudaEventRecord(stop,stream); //记录结束事件
cudaEventSynchronize(stop); //事件同步,等待结束事件之前的设备操作均已完成
float elapsedTime;
cudaEventElapsedTime(&elapsedTime,start,stop);//计算两个事件之间时长(单位为ms)