Thrust の Reduce、Scan、Sort
1 回の呼び出しで使える高水準プリミティブ
「Thrust の Reduce、Scan、Sort」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🏁
よくある質問
「Thrust の Reduce、Scan、Sort」レッスンは無料ですか?
はい。「Thrust の Reduce、Scan、Sort」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「Thrust の Reduce、Scan、Sort」で何を学びますか?
1 回の呼び出しで使える高水準プリミティブ ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「Thrust の Reduce、Scan、Sort」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- cuBLAS GEMM を正しく使う
- Thrust のベクターと変換
- Thrust の Reduce、Scan、Sort
- ディープラーニングのための cuDNN