0Pricing
CUDA Academy · درس

الحماية من الخروج عن النطاق

فحص الحدود if (i < n).

الحماية من الخروج عن النطاق درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

You Often Launch Too Many

Thread counts come in fixed block sizes, so you almost always launch a few extra threads beyond your array length. Those spares need handling.

What Goes Wrong

An extra thread computes an index past the end of the array. If it writes there, it touches memory it does not own, causing a silent out-of-bounds bug.

The GPU Will Not Warn You

Unlike a clean crash, an out-of-range write may corrupt nearby data or read garbage. The kernel keeps running, so the failure is invisible until results look wrong.

The Fix Is One Line

Before any thread uses its index, check that it falls inside the array. This tiny bounds check is the most important safety habit in CUDA.

if (i < n) {
    out[i] = a[i] + b[i];
}

Why Less Than, Not Less Or Equal

Valid indices run 0 to n minus 1. The strict i < n lets the last real element through and stops the first invalid one.

Idle Threads Just Exit

A thread whose index is out of range simply skips the work and returns. Doing nothing is perfectly safe and costs almost no time.

Pass n to the Kernel

The kernel cannot guess the array length, so you give it n as a parameter. Then the guard always knows where the data ends.

__global__ void add(float* a, float* b, float* out, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) out[i] = a[i] + b[i];
}

Guard Reads Too

It is not only writes. Reading a[i] past the end loads garbage or faults, so the same i < n check protects every access.

A Common Off-by-One

Writing i <= n by mistake lets one thread touch element n, which does not exist. Always keep the comparison strict.

Cheap Insurance

The branch costs almost nothing because extra threads are rare and exit fast. The safety it buys is worth far more than that tiny cost. ✅

Make It a Reflex

Treat the bounds check as part of the index formula itself. Compute i, then immediately guard it before doing anything else.

Quick Check

Pick the correct guard.

Recap

You learned to add if (i < n) after computing the index. This one line stops out-of-range reads and writes that the GPU would never warn you about. 🛡️

الأسئلة الشائعة

هل درس «الحماية من الخروج عن النطاق» مجاني؟

نعم — نص درس «الحماية من الخروج عن النطاق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «الحماية من الخروج عن النطاق»؟

فحص الحدود if (i < n). تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «الحماية من الخروج عن النطاق»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. صيغة الفهرس الكلاسيكية
  2. الحماية من الخروج عن النطاق
  3. التقريب إلى أعلى لعدد الكتل
  4. حلقات Grid-Stride
← العودة إلى CUDA Academy