0Pricing
CUDA Academy · Lección

Transferencias del host al dispositivo

Cargue los datos de entrada en la GPU.

Transferencias del host al dispositivo es una lección gratuita de CUDA Academy en CoddyKit. Esta es la lección 1 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.

Two Memories, One Goal

Your data starts in host RAM, but the GPU can only crunch numbers that live in its own device memory. First you must move it across. 🚚

Meet cudaMemcpy

The cudaMemcpy function is your delivery truck: it copies a block of bytes from one address to another, host or device.

cudaMemcpy(dst, src, bytes, kind);

The Upload Direction

To send input data up to the GPU you use cudaMemcpyHostToDevice. Source is host RAM, destination is device memory.

cudaMemcpy(d_a, h_a, n*sizeof(float), cudaMemcpyHostToDevice);

Argument Order Matters

Like memcpy, the destination comes first, then the source. Swap them by accident and your GPU buffer stays empty. 😬

cudaMemcpy(d_dst, h_src, bytes, cudaMemcpyHostToDevice);

Count Bytes, Not Elements

The size argument is measured in bytes, so multiply your element count by the type size with sizeof.

size_t bytes = n * sizeof(float);

Allocate Before You Copy

The device pointer must already point at real GPU memory. Always cudaMalloc the destination before uploading into it.

cudaMalloc(&d_a, bytes);
cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);

A Synchronous Wait

Plain cudaMemcpy is blocking: your CPU thread pauses until every byte has safely landed on the GPU.

Then Launch Your Kernel

Upload first, compute second. Once the input is on the device, your kernel can read it and start the real work.

cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);
myKernel<<<blocks, threads>>>(d_a, n);

Sizes Must Match

Make sure the byte count you upload fits the buffer you allocated. Copying more than you reserved is a classic overflow bug.

Check the Return Code

cudaMemcpy returns a cudaError_t. Inspect it so a bad pointer or size fails loudly instead of corrupting your run.

cudaError_t err = cudaMemcpy(d_a, h_a, bytes, cudaMemcpyHostToDevice);

Copies Are Not Free

Every upload travels across the slow PCIe bus, so copy only the data you truly need on the device.

Quick Check

You want to send an input array from CPU RAM to the GPU. Which call is correct?

Recap

You learned to upload data with cudaMemcpyHostToDevice: allocate the device buffer, put destination first, size it in bytes, and copy before launching. 🎉

Preguntas frecuentes

¿La lección «Transferencias del host al dispositivo» es gratis?

Sí — el texto completo de «Transferencias del host al dispositivo» 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 «Transferencias del host al dispositivo»?

Cargue los datos de entrada 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 1 de 4.

¿Cuánto tiempo toma la lección «Transferencias del host al dispositivo»?

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. Transferencias del host al dispositivo
  2. Transferencias del dispositivo al host
  3. El enum de dirección de copia
  4. El cuello de botella de transferencia PCIe
← Volver a CUDA Academy