مؤشرات إلى ذاكرة GPU
توجد مؤشرات الجهاز في متغيرات المضيف.
مؤشرات إلى ذاكرة GPU درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. 🎉
الأسئلة الشائعة
هل درس «مؤشرات إلى ذاكرة GPU» مجاني؟
نعم — نص درس «مؤشرات إلى ذاكرة GPU» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «مؤشرات إلى ذاكرة GPU»؟
توجد مؤشرات الجهاز في متغيرات المضيف. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «مؤشرات إلى ذاكرة GPU»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- cudaMalloc وcudaFree
- مؤشرات إلى ذاكرة GPU
- تهيئة الذاكرة باستخدام cudaMemset
- تجنّب تسريبات الذاكرة والتحرير المزدوج