0Pricing
CUDA Academy · Lesson

Thrust Reduce, Scan, and Sort

High-level primitives with one call.

Thrust Reduce, Scan, and Sort 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.

Hard Algorithms, One Line

Reductions, scans, and sorts are tricky to write fast by hand. Thrust gives you tuned versions through a single function call. 🎁

Reduce Collapses to One Value

thrust::reduce combines every element into a single result, like summing an array, all in parallel under the hood.

int total = thrust::reduce(d.begin(), d.end());

Custom Reduction Operators

Reduce defaults to addition, but you can pass an init value and a binary op to compute a product, max, or anything associative.

int m = thrust::reduce(d.begin(), d.end(),
  0, thrust::maximum<int>());

Scan Keeps the Running Total

A scan, or prefix sum, outputs the running total at each position. It is the backbone of compaction, sorting, and stream allocation.

Inclusive vs Exclusive

inclusive_scan includes the current element in its sum; exclusive_scan does not. Picking the right one avoids an off-by-one bug.

thrust::inclusive_scan(d.begin(), d.end(),
  out.begin());

Scan Is Not Obvious to Parallelize

A prefix sum looks sequential, yet Thrust runs it in parallel with a clever tree algorithm you never have to write yourself.

Sort in Place

thrust::sort orders a device_vector in place using a fast GPU radix or merge sort, far quicker than a CPU sort on big data.

thrust::sort(d.begin(), d.end());

Sort by Key

sort_by_key sorts one array and reorders a second values array to match, perfect for keeping records aligned with their keys.

thrust::sort_by_key(keys.begin(),
  keys.end(), values.begin());

Compose Primitives

Real pipelines chain these: transform then reduce, or sort then scan. Each step is one tuned call, so you focus on the logic.

Fused transform_reduce

transform_reduce maps and sums in one pass, computing things like a dot product or sum of squares without a temporary array.

float ss = thrust::transform_reduce(
  d.begin(), d.end(), sq, 0.0f, thrust::plus<float>());

Let the Library Win

These primitives are heavily optimized by NVIDIA. Reaching for them first usually beats a custom kernel and saves hours of work.

Quick Check

Recall what a prefix sum produces.

Recap

You collapsed data with reduce, built running totals with scan, ordered arrays with sort, and fused steps with transform_reduce. 🏁

Frequently asked questions

Is the “Thrust Reduce, Scan, and Sort” lesson free?

Yes — the full text of “Thrust Reduce, Scan, and Sort” 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 “Thrust Reduce, Scan, and Sort”?

High-level primitives with one call. 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 “Thrust Reduce, Scan, and Sort” 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. cuBLAS GEMM Done Right
  2. Thrust Vectors and Transforms
  3. Thrust Reduce, Scan, and Sort
  4. cuDNN for Deep Learning
← Back to CUDA Academy