zoukankan      html  css  js  c++  java
  • CUDA计时

    from:http://blog.sina.com.cn/s/blog_45209f340101341e.html

    <1>使用cutil.h中的函数

    unsigned int timer=0;

    //创建计时器

    cutCreateTimer(&timer);

    //开始计时

    cutStartTimer(timer);

    {

      //统计的代码段

      …………

    }

    //停止计时

    cutStopTimer(timer);

    //获得从开始计时到停止之间的时间

    cutGetTimerValue( timer);

    //删除timer值

    cutDeleteTimer( timer);

    不知道在这种情况下,统计精度。

    <2>time.h中的clock函数

    clock_t start, finish;

    float costtime;

    start = clock();

    {

      //统计的代码段

      …………

    }

    finish = clock();

    //得到两次记录之间的时间差

    costtime = (float)(finish - start) / CLOCKS_PER_SEC;

    时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒。

    <3>事件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才会被记录下来,即起到同步的作用。

  • 相关阅读:
    magent + memcached 集群测试
    SQL Server 2000/2005检测存储过程名是否存在,存在删除
    ASP.NET在线用户列表精确版——解决用户意外退出在线列表无法及时更新问题
    使用asp.net/c# ajax 乱码的解决办法
    清空删除mssql数据库日志原文网址:http://admin.88443.net/article.
    net2.0下的简繁转换
    创建和注册自定义 httpModules 模块
    监控用户是否关闭浏览器
    Asp.net(Ajax)表单验证 函数包
    IE自带的网页过渡特效
  • 原文地址:https://www.cnblogs.com/zhaoyang10/p/4468552.html
Copyright © 2011-2022 走看看