0Pricing
Mojo Academy · Lesson

Moving Data to and from Device

Manage host-device memory transfers.

Moving Data to and from Device is a free Mojo Academy lesson on CoddyKit — lesson 4 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 Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Moving Data to and from Device” lesson free?

Yes — the full text of “Moving Data to and from Device” is free to read here on the web, and the Mojo 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 Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Moving Data to and from Device”?

Manage host-device memory transfers. You practise Mojo 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 Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Moving Data to and from Device” 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 Mojo Academy lesson?

Yes. Every Mojo 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. Why GPUs for AI Workloads
  2. Threads, Blocks, and Grids
  3. Writing a GPU Kernel Function
  4. Moving Data to and from Device
← Back to Mojo Academy