Basis Lab

How a Program Runs on a GPU

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.

FIG 1A timing that measures the hand-over
cpp
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);
The clock is read immediately after the launch returns. At that moment the request is in the queue and the function may not have begun. The last line copies the result back to the program, and it cannot do that until the function has finished.

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.

FIG 2What the program and the device are each doing
steptime in msthe programthe devicewhat happened
10.00reads the clock, launchesidleThe launch places the request in the device's queue.
20.02reads the clock againstarting the functionThe program sees 0.02 ms and records it as the time of the function.
30.02calls the copyrunning the functionThe copy is queued behind the function. The program is now stuck inside the copy call.
45.02still in the copyfunction done, copyingOnly now does the copy itself begin.
56.02copy returnsidleThe copy call lasted 6 ms, of which 5 were spent waiting for somebody else's work.
5 steps
The whole job took 6.02 ms whichever way it is measured. The mistake is in the split: 0.02 ms charged to the function and 6 ms to the copy, where the truth is 5 and 1.

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 contents

This 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

The rest of this course

  1. 01The Loop Had an Order and the Sum Never Needed It
  2. 02Nobody Hands a Worker Its Index, So It Works It Out from Where It Sitsopening only
  3. 03One Worker Disagrees and Thirty-One Others Stand and Waitopening only
  4. 04A Group That Is Waiting Does Not Need the Arithmetic Units, So Another Takes Themopening only
  5. 05The Function Asked for a Little More Storage and Half Its Groups Had to Leaveopening only
  6. 06Adding 256 Numbers in Eight Stages, with Everyone Waiting at the End of Eachopening only
  7. 07Two Workers Added One to the Same Cell and It Went Up by Oneopening only
  8. 08The Timer Said the Function Took No Time, Because It Had Not Run Yetyou are here

Read alongside