0Pricing
CUDA Academy · Lesson

Thrust Vectors and Transforms

STL-style parallelism on the GPU.

Thrust Vectors and Transforms is a free CUDA Academy lesson on CoddyKit — lesson 2 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.

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 device

Allocation 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. 💪

Frequently asked questions

Is the “Thrust Vectors and Transforms” lesson free?

Yes — the full text of “Thrust Vectors and Transforms” 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 Vectors and Transforms”?

STL-style parallelism on the GPU. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Thrust Vectors and Transforms” 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