Thrust のベクターと変換
GPU 上の STL 形式の並列処理
「Thrust のベクターと変換」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The STL for Your GPU
Thrust is a high-level library that feels like the C++ STL but runs on the GPU. You write almost no kernel code yourself. 🎉
Two Kinds of Vector
Thrust gives you host_vector for CPU memory and device_vector for GPU memory. They look identical but live in different worlds.
thrust::host_vector<int> h(100);
thrust::device_vector<int> d(100);Copy Across the Boundary
Assigning a host_vector to a device_vector triggers a memory transfer automatically. No cudaMemcpy boilerplate to write by hand.
thrust::device_vector<int> d = h; // host to deviceAllocation Is Automatic
A device_vector allocates and frees GPU memory for you using RAII. When it goes out of scope, the memory is released cleanly.
Fill and Sequence
Helpers like thrust::fill and thrust::sequence initialize a whole device_vector in one parallel call instead of a manual loop.
thrust::sequence(d.begin(), d.end()); // 0,1,2,...Transform Maps Element to Element
thrust::transform applies a function to every element in parallel and writes the result to an output range. This is the classic map pattern.
thrust::transform(d.begin(), d.end(),
out.begin(), op);Functors Are the Operation
The operation you pass is a functor: a struct with an operator() marked __host__ __device__ so it can run on the GPU.
struct Square { __host__ __device__
float operator()(float x){ return x*x; } };Built-In Operators
For common math, Thrust ships functors like thrust::plus and thrust::multiplies, so you skip writing a functor for simple jobs.
thrust::transform(a.begin(), a.end(),
b.begin(), c.begin(), thrust::plus<float>());Two Inputs, One Output
The binary form of transform takes two input ranges and combines them pairwise, perfect for element-wise vector addition.
Get the Raw Pointer
Need to drop into a hand-written kernel? thrust::raw_pointer_cast hands you the underlying device pointer to pass along.
float* p = thrust::raw_pointer_cast(d.data());Less Code, Fewer Bugs
Thrust hides allocation, copies, and launch configs. You trade a little control for readable, safe GPU code that just works.
Quick Check
Recall how Thrust applies an operation to a whole vector.
Recap
You used device_vector for automatic memory, transferred data by assignment, and ran transform with functors. STL-style GPU power. 💪
よくある質問
「Thrust のベクターと変換」レッスンは無料ですか?
はい。「Thrust のベクターと変換」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「Thrust のベクターと変換」で何を学びますか?
GPU 上の STL 形式の並列処理 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Thrust のベクターと変換」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。