通过计算时钟时间对价为零 - Linux的 [英] Calculating time using clock gives value as zero - linux

查看:166
本文介绍了通过计算时钟时间对价为零 - Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CUDA code这对GPU进行运算。
我使用的时钟();找出时序

I have a cuda code which performs calculation on GPU. I am using clock(); to find out timings

我的code结构

__global__ static void sum(){

// calculates sum 
}

extern "C"
int run_kernel(int array[],int nelements){
 clock_t start, end;
  start = clock();
  //perform operation on gpu - call sum
 end = clock();
 double elapsed_time = ((double) (end - start)) / CLOCKS_PER_SEC;
 printf("time required : %lf", elapsed_time);
}

但时间总是0.0000
我检查印刷开始和结束时间。开始有一定的价值,但结束时间始终为零。

But the time is always 0.0000 I checked printing start and end time. Start has some value but end time is always zero.

任何想法可能是什么原因?任何替代品来测量时间。

Any idea what might be the cause? Any alternatives to measure time.

任何帮助将是AP preciated。

Any help would be appreciated.

感谢

推荐答案

有这里有两个问题:


  1. 时钟()函数有过低的分辨率来衡量你正在尝试一次事件的持续时间

  2. 的CUDA内核启动是一个异步操作,因此它消耗几乎没有时间(通常为10-20微秒在一个健全的平台)。除非你使用同步CUDA API调用来强制主机CPU阻塞,直到内核完成运行,你是不是将要测量的执行时间。

  1. The clock() function has too low resolution to measure the duration of the event you are trying to time
  2. The CUDA kernel launch is an asynchronous operation, so it consumes almost no time (typically 10-20 microseconds on a sane platform). Unless you use a synchronous CUDA API call to force the host CPU to block until the kernel finishes running, you are not going to be measuring the execution time.

CUDA有它自己的高precision定时的API,它是推荐的方式,以其中在GPU上运行时操作。在code使用它会是这个样子:

CUDA has its own high precision timing API, and it is the recommended way to time operations which run on the GPU. The code to use it would look something like this:

int run_kernel(int array[],int nelements){

    cudaEvent_t start,stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    cudaEventRecord(start, 0);

    //
    //perform operation on gpu - call sum
    //

    cudaEventRecord(stop, 0); 
    cudaEventSynchronize(stop); 
    float elapsedTime; 
    cudaEventElapsedTime(&elapsedTime, start, stop); 
    printf("time required : %f", elapsed_time); 

    cudaEventDestroy(start);
    cudaEventDestroy(stop);
}

这篇关于通过计算时钟时间对价为零 - Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆