0Pricing
Mojo Academy · Lección

Mover datos al dispositivo y desde él

Gestione las transferencias de memoria entre host y dispositivo.

Mover datos al dispositivo y desde él es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 4 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 Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

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

Two Separate Memories

The CPU and GPU each have their own memory. The GPU cannot read your host arrays until the data lives on the device.

Host and Device

We call the CPU side the host and the GPU side the device. Moving data between them is a core part of GPU work.

Allocating on the Device

First you reserve space on the GPU for your data. This device buffer is where inputs and outputs will live.

var d_a = ctx.enqueue_create_buffer[DType.float32](n)

Copying Inputs Over

Next you copy your host arrays into those device buffers. This upload gets the GPU the numbers it needs.

ctx.enqueue_copy(d_a, h_a)

Running the Kernel

With data on the device, you launch the kernel. It reads and writes the device buffers, never the host ones.

ctx.enqueue_function[kernel](grid_dim=blocks, block_dim=256)

Copying Results Back

The output sits on the GPU until you fetch it. A download copies the result buffer back into host memory.

ctx.enqueue_copy(h_out, d_out)

Work Is Queued, Not Instant

These calls are enqueued on the GPU, not finished at once. You must synchronize before trusting the results on the host.

ctx.synchronize()

The Round Trip

The full cycle is upload, compute, download. Picture data making a round trip from host to device and back.

Transfers Cost Time

Copying over the bus is slow compared to compute. Each transfer can erase your speedup if you do it too often.

Keep Data on the Device

Chain several kernels on the same buffers before copying back. Minimizing round trips keeps the GPU win intact.

Free What You Allocate

Device memory is limited, so release buffers you no longer need. Mojo's ownership rules help free them at the right time.

Quick Check

You want a GPU program to actually run faster end to end.

Recap

Upload inputs to the device, run the kernel, then download results and synchronize, while keeping costly transfers to a minimum. 🔁

Preguntas frecuentes

¿La lección «Mover datos al dispositivo y desde él» es gratis?

Sí — el texto completo de «Mover datos al dispositivo y desde él» 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 Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Mover datos al dispositivo y desde él»?

Gestione las transferencias de memoria entre host y dispositivo. Practicas Mojo 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 Mojo Academy?

No se requiere experiencia previa. Mojo 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 4 de 4.

¿Cuánto tiempo toma la lección «Mover datos al dispositivo y desde él»?

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 Mojo Academy?

Sí. Cada lección de Mojo 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. Por qué usar GPU para cargas de trabajo de IA
  2. Hilos, bloques y grids
  3. Escribir una función kernel para GPU
  4. Mover datos al dispositivo y desde él
← Volver a Mojo Academy