0Pricing
CUDA Academy · レッスン

ホストからデバイスへの転送

入力データをGPUにアップロードします。

「ホストからデバイスへの転送」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「ホストからデバイスへの転送」レッスンは無料ですか?

はい。「ホストからデバイスへの転送」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。

「ホストからデバイスへの転送」で何を学びますか?

入力データをGPUにアップロードします。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

CUDA Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「ホストからデバイスへの転送」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このCUDA Academyレッスンでコードを書いて実行できますか?

はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ホストからデバイスへの転送
  2. デバイスからホストへの転送
  3. コピー方向の列挙型
  4. PCIe転送のボトルネック
← CUDA Academyに戻る