Thrust-Vektoren und -Transformationen
Parallelismus nach STL-Art auf der GPU
Thrust-Vektoren und -Transformationen ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 💪
Häufig gestellte Fragen
Ist die Lektion „Thrust-Vektoren und -Transformationen“ kostenlos?
Ja — der vollständige Text von „Thrust-Vektoren und -Transformationen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Thrust-Vektoren und -Transformationen“?
Parallelismus nach STL-Art auf der GPU Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um CUDA Academy zu starten?
Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Thrust-Vektoren und -Transformationen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?
Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- cuBLAS-GEMM richtig einsetzen
- Thrust-Vektoren und -Transformationen
- Thrust: Reduce, Scan und Sort
- cuDNN für Deep Learning