The Timer Said the Function Took No Time, Because It Had Not Run Yet
Last timeTwo Workers, One Cell
A launch hands the work to the device and returns at once. A clock read straight afterwards measures the hand-over, so a timing must wait for the device on both sides.
Two machines, one queue
Everything so far happened on the device. This last lesson stands on the other side, in the ordinary program that makes the launch.
That program runs on the processor, and the device is a separate machine beside it. The launching call does not run the function. It places a request in a queue for the device and returns. NVIDIA's programming guide, in its 2026 edition, says the launch returns immediately, and that the program continues before the function has completed and possibly even before it has started. The vendor's word for this is asynchronous.
This is deliberate. The program can prepare the next batch while the device works on this one. But it sets a trap for anyone holding a stopwatch.
auto start = std::chrono::steady_clock::now();
vecAdd<<<blocks, threads>>>(devA, devB, devC, vectorLength);
auto stop = std::chrono::steady_clock::now();
cudaMemcpy(C, devC, vectorLength*sizeof(float), cudaMemcpyDefault);Put illustrative numbers on it. Suppose the hand-over takes 0.02 milliseconds, the function takes 5 milliseconds on the device, and the copy takes 1.
| step | time in ms | the program | the device | what happened |
|---|---|---|---|---|
| 1 | 0.00 | reads the clock, launches | idle | The launch places the request in the device's queue. |
| 2 | 0.02 | reads the clock again | starting the function | The program sees 0.02 ms and records it as the time of the function. |
| 3 | 0.02 | calls the copy | running the function | The copy is queued behind the function. The program is now stuck inside the copy call. |
| 4 | 5.02 | still in the copy | function done, copying | Only now does the copy itself begin. |
| 5 | 6.02 | copy returns | idle | The copy call lasted 6 ms, of which 5 were spent waiting for somebody else's work. |
The measured 0.02 against the true 5.02 makes the function look 251 times faster than it is. There is an easy test for this mistake. Double the array and the number does not move, because queueing a request costs the same whatever its size.
The second error is quieter. The time did not vanish. The guide describes the device's queue as worked through in order, and the plain copy as a call that does not return until the copy is complete. So the copy absorbs the wait, and a profile of the program blames the copy for 6 milliseconds. People then spend a week speeding up a copy that takes 1.
The lesson stops here
4 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents