توصيل جانب المضيف
احجز، وانسخ، وشغّل، وانسخ مجددًا، ثم حرّر.
توصيل جانب المضيف درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Host Has a Job Too
The kernel does the math, but the host CPU sets everything up. Its job is allocate, copy in, launch, copy back, and free. 🧩
Two Sets of Pointers
You keep host pointers for CPU arrays and separate device pointers for GPU buffers. A common habit is naming them h_A and d_A.
float *h_A, *h_B, *h_C;
float *d_A, *d_B, *d_C;Fill the Input on the CPU
First prepare your data in normal host memory. Here you just initialize h_A and h_B with values the GPU will add later.
for (int i = 0; i < n; i++) {
h_A[i] = i; h_B[i] = 2 * i;
}Allocate on the Device
The GPU needs its own buffers, so call cudaMalloc for each array. Note the size is in bytes, computed as count times sizeof(float).
size_t bytes = n * sizeof(float);
cudaMalloc(&d_A, bytes);Copy Inputs Up
Move your input arrays to the GPU with cudaMemcpy and the HostToDevice direction. The kernel can only see data that lives on the device.
cudaMemcpy(d_A, h_A, bytes, cudaMemcpyHostToDevice);
cudaMemcpy(d_B, h_B, bytes, cudaMemcpyHostToDevice);Decide the Launch Shape
Pick a thread count per block, then compute how many blocks cover all elements. Rounding up guarantees every element gets a thread.
int threads = 256;
int blocks = (n + threads - 1) / threads;Launch the Kernel
Now fire the kernel with the triple-angle-bracket syntax, passing your device pointers. This call returns immediately while the GPU works.
vecAdd<<<blocks, threads>>>(d_A, d_B, d_C, n);Copy the Result Down
When the kernel finishes, bring C back with cudaMemcpyDeviceToHost. This copy also waits for the launch to complete first.
cudaMemcpy(h_C, d_C, bytes, cudaMemcpyDeviceToHost);Free Device Memory
GPU memory is not garbage collected, so release every buffer with cudaFree. Skipping this leaks memory that lasts until the process exits.
cudaFree(d_A); cudaFree(d_B); cudaFree(d_C);The Order Is Sacred
Always follow the same sequence: allocate, copy in, launch, copy back, free. Swap steps and the kernel reads stale or invalid data.
Host Code Is Plain C++
Notice the host side is ordinary C++ plus a handful of cuda calls. There is no special compiler magic beyond the kernel launch.
Quick Check
What must happen before you can launch the vecAdd kernel?
Recap
You wired the host side end to end: two pointer sets, cudaMalloc, copy up, launch, copy back, and cudaFree. The boilerplate that powers every kernel. ✅
الأسئلة الشائعة
هل درس «توصيل جانب المضيف» مجاني؟
نعم — نص درس «توصيل جانب المضيف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «توصيل جانب المضيف»؟
احجز، وانسخ، وشغّل، وانسخ مجددًا، ثم حرّر. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «توصيل جانب المضيف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نواة جمع المتجهات
- توصيل جانب المضيف
- التحقق من النتيجة على CPU
- قياس أول تسارع لك