Reduce, Scan e Sort com Thrust
Primitivas de alto nível com uma única chamada.
Reduce, Scan e Sort com Thrust é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 🏁
Perguntas Frequentes
A aula “Reduce, Scan e Sort com Thrust” é grátis?
Sim — o texto completo de “Reduce, Scan e Sort com Thrust” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.
O que vou aprender em “Reduce, Scan e Sort com Thrust”?
Primitivas de alto nível com uma única chamada. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar CUDA Academy?
Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Reduce, Scan e Sort com Thrust”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de CUDA Academy?
Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- GEMM com cuBLAS do jeito certo
- Vetores e transformações com Thrust
- Reduce, Scan e Sort com Thrust
- cuDNN para aprendizado profundo