zoukankan      html  css  js  c++  java
  • CUDA中记录执行时间-GPU端

    事件event
    cudaEvent_t start,stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord(start,0);
    {
    //统计的代码段
    …………
    }
    cudaEventRecord(stop,0);
    float costtime;
    cudaEventElapsedTime(&costtime,start,stop);

    cudaError_t cudaEventCreate( cudaEvent_t* event )---创建事件对象;
    cudaError_t cudaEventRecord( cudaEvent_t event,CUstream stream )--- 记录事件;
    cudaError_t cudaEventElapsedTime( float* time,cudaEvent_t start,cudaEvent_t end )---计算两次事件之间相差的时间;
    cudaError_t cudaEventDestroy( cudaEvent_t event )---销毁事件对象。
    计算两次事件之间相差的时间(以毫秒为单位,精度为0.5微秒)。如果尚未记录其中任何一个事件,此函数将返回cudaErrorInvalidValue。如果记录其中任何一个事件使用了非零流,则结果不确定。

    该例子是CUDA_C_Best_Practices_Guide中的例子:
    cudaEvent_t start, stop;
    float time;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
    cudaEventRecord( start, 0 );
    kernel<<>> ( d_odata, d_idata, size_x, size_y, NUM_REPS);
    cudaEventRecord( stop, 0 );
    cudaEventSynchronize( stop );
    cudaEventElapsedTime( &time, start, stop );
    cudaEventDestroy( start );
    cudaEventDestroy( stop );

    需要注意的是函数cudaEventSynchronize() 不可或缺,因为CUDA的kernel函数是以异步方式执行的,调用后立刻返回,这会导致计时不准确。cudaEventSynchronize(stop)会使得直到GPU执行完cudaEventRecord(stop, 0)之前的所有语句时,事件stop才会被记录下来,即起到同步的作用。

  • 相关阅读:
    一些博客
    【iOS】得到当前年、月、周的第一天和最后一天
    iOS学习路线
    UI开发--响应者链条
    通知中心 NSNotificationCenter
    UITableView的编辑(插入、删除、移动)
    FMDatabaseQueue 如何保证线程安全
    理解 dispatch_get_specific
    instancetype
    【转】手把手教你ARC——iOS/Mac开发ARC入门和使用
  • 原文地址:https://www.cnblogs.com/liangliangdetianxia/p/3986777.html
Copyright © 2011-2022 走看看