# clock() doesn't  work properly

**URL:** <https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540>\
**Category:** CUDA Programming and Performance\
**Created:** [July 1, 2009, 12:48pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540 "2009-07-01T12:48:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaiK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@KaiK](https://forums.developer.nvidia.com/u/KaiK)\
**Post date:** [July 1, 2009, 12:48pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/1 "2009-07-01T12:48:40Z")

</div>

Hi,

I’m trying to know how many time my kernel takes, using the following code:

```
            //START CLOCK COUNTER
	clock_t timer_start = clock();

	//ENTER KERNEL
	my_kernel <<< dimGrid, dimBlock >>> (args);
	checkCUDAError("kernel");
	//EXIT KERNEL    
	
	//STOP AND PRINT THE TIMER
	cudaThreadSynchronize();
	timer_end = clock();
	printf("start cicles : %ld\n", timer_start);
	printf("end cicles : %ld\n", timer_end);
	timer_diff = static_cast<long double>( (timer_end - timer_start + 0.0) / CLOCKS_PER_SEC); 
            printf("my Kernel, iteration %d: %.12Lf seconds\n",i, timer_diff);

```

but the result I get is almost always the same number of cycles for timer\_start and timer\_end, so the last line is usually 0.000…0

In some more strange cases, there’s a difference of 1000 cycles, between start and stop, so the last line is 0.010…0

I’m sure there’s something wrong, because it is impossible to not change at least 1 clock between the kernel execution.

The usual response I get is this:

start cicles : 120000  
end cicles : 120000  
Kernel, iteration 0: 0.000000000000 seconds

start cicles : 130000  
end cicles : 130000  
Kernel, iteration 1: 0.000000000000 seconds

start cicles : 130000  
end cicles : 130000  
Kernel, iteration 2: 0.000000000000 seconds

start cicles : 130000  
end cicles : 130000  
Kernel, iteration 3: 0.000000000000 seconds

start cicles : 130000  
end cicles : 130000  
Kernel, iteration 4: 0.000000000000 seconds

Can anybody help me?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![wanderine](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wanderine](https://forums.developer.nvidia.com/u/wanderine)\
**Post date:** [July 1, 2009, 1:16pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/2 "2009-07-01T13:16:53Z")

</div>

> [@](#):
>
> Hi,
> 
> I’m trying to know how many time my kernel takes, using the following code:
> 
> //START CLOCK COUNTER
> 
> ```
> clock_t timer_start = clock();
> 
> //ENTER KERNEL
> 
> my_kernel <<< dimGrid, dimBlock >>> (args);
> 
> checkCUDAError("kernel");
> 
> //EXIT KERNEL    
> 
> 	
> 
> //STOP AND PRINT THE TIMER
> 
> cudaThreadSynchronize();
> 
> timer_end = clock();
> 
> printf("start cicles : %ld\n", timer_start);
> 
> printf("end cicles : %ld\n", timer_end);
> 
> timer_diff = static_cast<long double>( (timer_end - timer_start + 0.0) / CLOCKS_PER_SEC); 
> 
> printf("my Kernel, iteration %d: %.12Lf seconds\n",i, timer_diff);
> 
> ```
> 
> but the result I get is almost always the same number of cycles for timer\_start and timer\_end, so the last line is usually 0.000…0
> 
> In some more strange cases, there’s a difference of 1000 cycles, between start and stop, so the last line is 0.010…0
> 
> I’m sure there’s something wrong, because it is impossible to not change at least 1 clock between the kernel execution.
> 
> The usual response I get is this:
> 
> start cicles : 120000
> 
> end cicles : 120000
> 
> Kernel, iteration 0: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 1: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 2: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 3: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 4: 0.000000000000 seconds
> 
> Can anybody help me?
> 
> Thanks in advance

I don’t think you can use clock, since the control is given back to the CPU directly after the kernel launch (not when the kernel has finished) and thereby there is no change in the number of clock cycles for the CPU.

I normally use

```auto
unsigned int hTimer;

cutilSafeCall( cudaThreadSynchronize() );	  

cutilCheckError( cutResetTimer(hTimer) );

cutilCheckError( cutStartTimer(hTimer) );

my_kernel <<< dimGrid, dimBlock >>> (args);

cutilSafeCall( cudaThreadSynchronize() );

cutilCheckError( cutStopTimer(hTimer) );

double time = cutGetTimerValue(hTimer);

```

---

<div class="post-metadata">

**Author:** ![KaiK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@KaiK](https://forums.developer.nvidia.com/u/KaiK)\
**Post date:** [July 1, 2009, 1:35pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/3 "2009-07-01T13:35:32Z")

</div>

I know the control is returned to the CPU after launching the kernel, but the timer\_stop clock I use is after a “cudaThreadSynchronize()” call, so this shoudn’t be the problem…should it?

---

<div class="post-metadata">

**Author:** ![awthomp](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@awthomp](https://forums.developer.nvidia.com/u/awthomp)\
**Post date:** [July 1, 2009, 2:09pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/4 "2009-07-01T14:09:53Z")

</div>

> [@](#):
>
> Hi,
> 
> I’m trying to know how many time my kernel takes, using the following code:
> 
> //START CLOCK COUNTER
> 
> ```
> clock_t timer_start = clock();
> 
> //ENTER KERNEL
> 
> my_kernel <<< dimGrid, dimBlock >>> (args);
> 
> checkCUDAError("kernel");
> 
> //EXIT KERNEL    
> 
> 	
> 
> //STOP AND PRINT THE TIMER
> 
> cudaThreadSynchronize();
> 
> timer_end = clock();
> 
> printf("start cicles : %ld\n", timer_start);
> 
> printf("end cicles : %ld\n", timer_end);
> 
> timer_diff = static_cast<long double>( (timer_end - timer_start + 0.0) / CLOCKS_PER_SEC); 
> 
> printf("my Kernel, iteration %d: %.12Lf seconds\n",i, timer_diff);
> 
> ```
> 
> but the result I get is almost always the same number of cycles for timer\_start and timer\_end, so the last line is usually 0.000…0
> 
> In some more strange cases, there’s a difference of 1000 cycles, between start and stop, so the last line is 0.010…0
> 
> I’m sure there’s something wrong, because it is impossible to not change at least 1 clock between the kernel execution.
> 
> The usual response I get is this:
> 
> start cicles : 120000
> 
> end cicles : 120000
> 
> Kernel, iteration 0: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 1: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 2: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 3: 0.000000000000 seconds
> 
> start cicles : 130000
> 
> end cicles : 130000
> 
> Kernel, iteration 4: 0.000000000000 seconds
> 
> Can anybody help me?
> 
> Thanks in advance

I normally set up the clock variables as integers.

So…

int startClock, endClock;

startClock = clock();

> > run kernel \<\<

stopClock = clock();

int executionTime = stopClock-endClock;

Hopefully, this is similar to what you’re trying to do. Be sure to #include \<time.h\>.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Collange](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Sylvain\_Collange](https://forums.developer.nvidia.com/u/Sylvain_Collange)\
**Post date:** [July 1, 2009, 2:59pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/5 "2009-07-01T14:59:42Z")

</div>

The clock() function has a very bad resolution (typically several milliseconds, apparently 10ms in your case), and cannot accurately measure periods shorter than a few seconds.  
This has nothing to do with CUDA. CUDA also has a device function named clock() on the GPU which is cycle-accurate, but you obviously cannot call it from host code.

Use rather gettimeofday on Unix and QueryPerformanceCounter on Windows, or the RDTSC X86 instruction, which are much more accurate. Or even better, use CUDA events.

---

<div class="post-metadata">

**Author:** ![KaiK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@KaiK](https://forums.developer.nvidia.com/u/KaiK)\
**Post date:** [July 1, 2009, 3:54pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/6 "2009-07-01T15:54:05Z")

</div>

> [@](#):
>
> The clock() function has a **very** bad resolution (typically several milliseconds, apparently 10ms in your case), and cannot accurately measure periods shorter than a few seconds.
> 
> This has nothing to do with CUDA. CUDA also has a device function named clock() on the GPU which is cycle-accurate, but you obviously cannot call it from host code.
> 
> Use rather gettimeofday on Unix and QueryPerformanceCounter on Windows, or the RDTSC X86 instruction, which are much more accurate. Or even better, use CUDA events.

Well, I found another way of doing it, also from the host, but it need a cuda library and I don’t want to read any cuda library from my code.

Did the cuda events need any library? How should I use them?

Thanks!

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [July 2, 2009, 7:16am UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/7 "2009-07-02T07:16:26Z")

</div>

Any CU file will get linked against some kind of CUDA library – atleast to launch the kernels… no?

What is so wrong in linking against cuda.lib or cudart.lib? One of these would implement the event APIs, I guess…

---

<div class="post-metadata">

**Author:** ![KaiK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@KaiK](https://forums.developer.nvidia.com/u/KaiK)\
**Post date:** [July 2, 2009, 7:35am UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/8 "2009-07-02T07:35:14Z")

</div>

> [@](#):
>
> Any CU file will get linked against some kind of CUDA library – atleast to launch the kernels… no?
> 
> What is so wrong in linking against cuda.lib or cudart.lib? One of these would implement the event APIs, I guess…

The problem is that my project will be involved into a hug library or resources of a department, and they want to use only their own libraries, and also the necessary ones (in my case, cuda.h is really needed because if not, kernel won’t launch as you said before, but cudart.lib is not so needed).

Any other way? In the case I’ll have to use one of this libraries, what should be the more accurated method?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Nico](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Nico](https://forums.developer.nvidia.com/u/Nico)\
**Post date:** [July 2, 2009, 7:46am UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/9 "2009-07-02T07:46:52Z")

</div>

> [@](#):
>
> The problem is that my project will be involved into a hug library or resources of a department, and they want to use only their own libraries, and also the necessary ones (in my case, cuda.h is really needed because if not, kernel won’t launch as you said before, but cudart.lib is not so needed).
> 
> Any other way? In the case I’ll have to use one of this libraries, what should be the more accurated method?
> 
> Thanks in advance!

I believe cudart is the runtime library, but events are also implemented in the driver API so I suggest you use events, they’re very accurate.

N.

---

<div class="post-metadata">

**Author:** ![KaiK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@KaiK](https://forums.developer.nvidia.com/u/KaiK)\
**Post date:** [July 2, 2009, 1:58pm UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/10 "2009-07-02T13:58:58Z")

</div>

> [@](#):
>
> I believe cudart is the runtime library, but events are also implemented in the driver API so I suggest you use events, they’re very accurate.
> 
> N.

Thanks a lot!

You’re right, events are implemented in both API’s, and there’s no need to include any other library.

As we can see in the reference manual, events have a resolution of 0.5 microseconds, so it works fine!

Thanks!

-KaiK-

---

<div class="post-metadata">

**Author:** ![Philipp82](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Philipp82](https://forums.developer.nvidia.com/u/Philipp82)\
**Post date:** [July 3, 2009, 8:47am UTC](https://forums.developer.nvidia.com/t/clock-doesnt-work-properly/10540/11 "2009-07-03T08:47:48Z")

</div>

Hi,

Use:

[font=“Lucida Console”]  
//create and start timer  
unsigned int timerGPU = 0;  
cudaThreadSynchronize();  
cutCreateTimer(&timerGPU);  
cutStartTimer(timerGPU);

… your kernel execution …

//stop timer and show result:  
cudaThreadSynchronize();  
cutStopTimer(timerGPU);  
printf(“Processing time: %f (ms) \n”, cutGetTimerValue(timerGPU));  
[/font]

That works for me.  
Sometimes cuda has to be initialized, before that code works, so if you have problems, write a minimal dummy-kernel and start that kernel at the beginning of your programm.
