0Pricing
CUDA Academy · Lesson

Verifying the Result on the CPU

Comparing against a reference answer.

Verifying the Result on the CPU is a free CUDA Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Trust, but Verify

A kernel that runs without crashing is not proof it is correct. You must check the numbers, because silent logic bugs are easy to miss. 🔍

Compute a Reference

The simplest check is a plain CPU loop that does the same math. This reference result is your trusted source of truth.

for (int i = 0; i < n; i++)
    ref[i] = h_A[i] + h_B[i];

Compare Element by Element

Walk both arrays and compare each GPU result against the reference. One loop over all n elements catches any wrong value.

for (int i = 0; i < n; i++)
    if (h_C[i] != ref[i]) { /* mismatch */ }

Floats Are Not Exact

For floating point, never test with plain equality. Tiny rounding differences mean you should compare with a small tolerance instead.

if (fabs(h_C[i] - ref[i]) > 1e-5f) { /* fail */ }

Why Tolerance, Not Equality

The GPU and CPU may round in different orders, so results can differ by a hair. A small epsilon like 1e-5 absorbs that harmless noise.

Count the Errors

Instead of bailing on the first mismatch, track an error count. Seeing how many elements fail hints at the kind of bug you have.

int errors = 0;
if (fabs(h_C[i] - ref[i]) > 1e-5f) errors++;

Print a Clear Verdict

End with a single, honest message. A clear PASS or FAIL line saves you from squinting at raw arrays.

printf(errors == 0 ? "PASS\n" : "FAIL: %d\n", errors);

Spot Check the Edges

Boundary bugs love the first and last elements. Printing C[0] and C[n-1] is a fast sanity glance before the full loop.

An Off-by-One Tell

If exactly the last element is wrong, suspect your bounds math. A bad block count or guard often drops the final thread.

Verify Before You Optimize

Always confirm correctness before you tune for speed. A fast kernel that returns wrong answers is worse than a slow correct one.

Keep the Check Around

Leave the verification step in during development. It is your regression test, catching mistakes the moment you change the kernel.

Quick Check

How should you compare floating-point GPU results to a CPU reference?

Recap

You verified the result: a CPU reference, a tolerance compare, an error count, and a clear PASS or FAIL. Correctness first, always. ✅

Frequently asked questions

Is the “Verifying the Result on the CPU” lesson free?

Yes — the full text of “Verifying the Result on the CPU” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “Verifying the Result on the CPU”?

Comparing against a reference answer. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Verifying the Result on the CPU” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Vector Add Kernel
  2. Wiring Up the Host Side
  3. Verifying the Result on the CPU
  4. Timing Your First Speedup
← Back to CUDA Academy