Movendo dados de e para o dispositivo
Gerencie transferências de memória entre hospedeiro e dispositivo.
Movendo dados de e para o dispositivo é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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. 🔁
Perguntas Frequentes
A aula “Movendo dados de e para o dispositivo” é grátis?
Sim — o texto completo de “Movendo dados de e para o dispositivo” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.
O que vou aprender em “Movendo dados de e para o dispositivo”?
Gerencie transferências de memória entre hospedeiro e dispositivo. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Mojo Academy?
Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Movendo dados de e para o dispositivo”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Mojo Academy?
Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Por que usar GPUs em cargas de trabalho de IA
- Threads, blocos e grades
- Escrevendo uma função de kernel para GPU
- Movendo dados de e para o dispositivo