# Host CPU busy while waiting ?

**URL:** <https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292>\
**Category:** CUDA Programming and Performance\
**Created:** [May 5, 2009, 3:32pm UTC](https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292 "2009-05-05T15:32:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NMGlykos](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NMGlykos](https://forums.developer.nvidia.com/u/NMGlykos)\
**Post date:** [May 5, 2009, 3:32pm UTC](https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292/1 "2009-05-05T15:32:11Z")

</div>

Dear All,

A quick question. I have noticed that after launching a kernel and while waiting for it to finish,  
the host CPU appears to be running at 100% (while, I assume, the only thing that does is polling  
the GPU). The solution of inserting a sleep() in the host code does the trick, but does not appear to  
be very elegant (mainly because you have to do a pre-timing of the kernel, so that you know for how  
long you can safely sleep()).

What have I missed ?  
Apologies if it turns-out that I haven’t RTFM for long enough,

Regards,  
Nicholas

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [May 5, 2009, 4:13pm UTC](https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292/2 "2009-05-05T16:13:55Z")

</div>

Basically you have to choices - either let the host thread do something constructive while you wait for the kernel to finish, or sit in a spinlock at a cudaThreadSynchronize() (either your own or one contained within one a copy request). Either way the thread will eat cpu cycles. That is how it works. Artificially adding sleeps() or whatever isn’t a good idea.

---

<div class="post-metadata">

**Author:** ![MisterAnderson42](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MisterAnderson42](https://forums.developer.nvidia.com/u/MisterAnderson42)\
**Post date:** [May 5, 2009, 4:20pm UTC](https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292/3 "2009-05-05T16:20:12Z")

</div>

CUDA 2.2 adds an context creation flag that will prevent the process from spin-waiting and it will actually sleep (presumably woken on an interrupt). Using it will increase latency, though.

---

<div class="post-metadata">

**Author:** ![NMGlykos](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@NMGlykos](https://forums.developer.nvidia.com/u/NMGlykos)\
**Post date:** [May 5, 2009, 4:46pm UTC](https://forums.developer.nvidia.com/t/host-cpu-busy-while-waiting/9292/4 "2009-05-05T16:46:32Z")

</div>

Thank you both for your replies. The CPU is doing something useful after launching the kernel  
(which is to prepare or analyze data), but that takes only, say, 5 sec of CPU time, whereas each  
kernel runs for tens of seconds. I found it rather wasteful to run a core at 100% while waiting for  
the GPU to finish the actual calculation.

Thanks again,  
Nicholas
