デバイスからホストへの転送
結果をCPUにダウンロードします。
「デバイスからホストへの転送」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Bringing Results Home
Your kernel just filled a device buffer with answers, but the CPU cannot read GPU memory directly. You must download the results back. 📥
The Download Direction
To pull data back you use cudaMemcpyDeviceToHost. Now the source is device memory and the destination is host RAM.
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Destination Still First
The rule never changes: destination argument first, source second. For a download, the host pointer leads.
cudaMemcpy(h_dst, d_src, bytes, cudaMemcpyDeviceToHost);Host Buffer Must Exist
The destination needs real CPU memory waiting for it. Allocate host storage with malloc or new before you copy back.
float* h_c = (float*)malloc(bytes);Wait for the Kernel
A blocking cudaMemcpy waits for prior work in the default stream, so the kernel finishes before any results are read.
myKernel<<<blocks, threads>>>(d_c, n);
cudaMemcpy(h_c, d_c, bytes, cudaMemcpyDeviceToHost);Same Byte Count
Download exactly as many bytes as you uploaded and computed. Mismatched sizes give you truncated or garbage results.
size_t bytes = n * sizeof(float);Now You Can Read It
Once the copy returns, the values live in normal CPU memory. You can print, check, or save them like any host array.
printf("%f\n", h_c[0]);The Round Trip
A full GPU job is a round trip: upload inputs, launch the kernel, then download outputs. Each leg uses cudaMemcpy.
Verify Before You Trust
After downloading, compare the GPU result against a quick CPU reference. Verification catches bugs before they spread. ✅
Free What You Allocated
When results are home, release both sides: cudaFree the device buffer and free the host buffer to avoid leaks.
cudaFree(d_c);
free(h_c);Downloads Cost Time Too
The return trip crosses the same slow bus, so only copy back the results you actually need on the host.
Quick Check
Your kernel wrote results into device buffer d_c. How do you read them on the CPU?
Recap
You learned the return trip with cudaMemcpyDeviceToHost: allocate host storage, host pointer first, wait for the kernel, then verify and free. 🎉
よくある質問
「デバイスからホストへの転送」レッスンは無料ですか?
はい。「デバイスからホストへの転送」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「デバイスからホストへの転送」で何を学びますか?
結果をCPUにダウンロードします。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「デバイスからホストへの転送」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ホストからデバイスへの転送
- デバイスからホストへの転送
- コピー方向の列挙型
- PCIe転送のボトルネック