Pointer auf GPU-Speicher
Device-Pointer werden in Host-Variablen gespeichert.
Pointer auf GPU-Speicher ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
A Pointer That Lives on the CPU
cudaMalloc hands you a normal C++ pointer variable. It sits in host memory, but the address inside it points into GPU memory. 🔗
The Address Means the Device
A device pointer holds a number that is only valid in the GPU's address space. To the CPU it is just an integer, not real RAM.
float* d_data; // host variable, device addressThe d_ Naming Habit
Programmers prefix device pointers with d_ and host pointers with h_. The name alone reminds you which side each one belongs to.
float* h_data;
float* d_data;Do Not Dereference on the Host
Writing *d_data in CPU code is a trap. The host cannot touch GPU memory directly, so this crashes or reads nonsense.
Kernels Dereference It Freely
Inside a kernel the same device pointer is perfectly valid. There, d_data[i] reads and writes real GPU memory as you expect.
__global__ void k(float* d) {
d[0] = 1.0f;
}Pass It Into the Kernel
You hand the device pointer to a kernel as an argument. The GPU then uses that address to find your data.
myKernel<<<grid, block>>>(d_data);Two Pointers, Two Spaces
Keep host and device pointers strictly apart. A host pointer is meaningless on the GPU, and a device pointer is meaningless on the CPU.
cudaMemcpy Bridges the Gap
To get data between the two, you copy it. cudaMemcpy takes both pointers and moves bytes across the divide for you.
Pointer Arithmetic Still Works
You may offset a device pointer on the host, like d_data + n, to point at a slice. You just must not read through it there.
float* d_tail = d_data + 100;Mixing Them Up Is Subtle
The compiler will not stop you from confusing the two. Many CUDA bugs are simply a host pointer handed where a device pointer belonged.
Discipline Beats Debugging
Clear naming and a fixed convention save hours. Decide your d_ and h_ scheme early and never break it. 🧹
Quick Check
Let us test where a device pointer can safely be dereferenced.
Recap
You learned a device pointer lives in a host variable but addresses GPU memory: usable inside kernels, off-limits to direct host access. 🎉
Häufig gestellte Fragen
Ist die Lektion „Pointer auf GPU-Speicher“ kostenlos?
Ja — der vollständige Text von „Pointer auf GPU-Speicher“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Pointer auf GPU-Speicher“?
Device-Pointer werden in Host-Variablen gespeichert. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um CUDA Academy zu starten?
Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Pointer auf GPU-Speicher“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?
Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- cudaMalloc und cudaFree
- Pointer auf GPU-Speicher
- cudaMemset zur Initialisierung
- Leaks und Double-Frees vermeiden