cuda中时间用法

转载:http://blog.csdn.net/jdhanhua/article/details/4843653

在CUDA中统计运算时间,大致有三种方法:

<1>使用cutil.h中的函数
unsigned int timer=0;
//创建计时器
cutCreateTimer(&timer);
//开始计时
cutStartTimer(timer);
{

//************
  …………

//*************

}
//停止计时
cutStopTimer(timer);
//获得从开始计时到停止之间的时间
cutGetTimerValue( timer);
//删除timer值
cutDeleteTimer( timer);

 在装了cuda toolkit 6.0,库链接正确的机器上运行找不到头文件,未测试。

 

<2>time.h中的clock函数
clock_t start, finish;
float costtime;
start = clock(); 
//************
  …………

//*************
finish = clock();
//得到两次记录之间的时间差
costtime = (float)(finish - start) / CLOCKS_PER_SEC; 
时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒

clock()百度百科:http://baike.baidu.com/view/1516611.htm?fr=aladdin

clock()计算占用cpu的时间,因此如果用GPU计算, 不知结果是否有问题。

 

<3>事件event
cudaEvent_t start,stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecend(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。如果记录其中任何一个事件使用了非零流,则结果不确定。 

 **************************************以上为转载*****************************************

因测试的实验程序是在GPU和CPU上一半一半执行的,因此,试过前两种,结果有问题,还是使用了最原始的time_t。

<4>#include <time.h>

time_t begin,end;

time(&begin);

//************
  需要统计时间代码部分

//************ 

time(&end);

int use=difftime(end,begin);

int min=(use/60)%60;
int sec=use%60;
int hour=use/3600;
fprintf(fw,"the %d recon use time:%02d:%02d:%02d\n",n+1,hour,min,sec);

精确到秒。

转载于:https://www.cnblogs.com/Anita-z/p/3920417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值