0Pricing
CUDA Academy · Lección

Vectores y transformaciones de Thrust

Paralelismo al estilo de STL en la GPU

Vectores y transformaciones de Thrust es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de CUDA Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de CUDA Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Vectores y transformaciones de Thrust» es gratis?

Sí — el texto completo de «Vectores y transformaciones de Thrust» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de CUDA Academy, actualiza a CoddyKit PRO. El curso de CUDA Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Vectores y transformaciones de Thrust»?

Paralelismo al estilo de STL en la GPU Practicas CUDA Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar CUDA Academy?

No se requiere experiencia previa. CUDA Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «Vectores y transformaciones de Thrust»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de CUDA Academy?

Sí. Cada lección de CUDA Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. cuBLAS GEMM bien hecho
  2. Vectores y transformaciones de Thrust
  3. Reduce, Scan y Sort de Thrust
  4. cuDNN para deep learning
← Volver a CUDA Academy