0Pricing
CUDA Academy · درس

حالات التسابق على GPU

لماذا تفسد عمليات الكتابة المتزامنة البيانات.

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

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

Many Hands, One Counter

On the GPU, thousands of threads can touch the same memory at once. When several write to one spot together, you get a race condition. 🏁

What a Race Really Is

A race condition happens when the final result depends on the unpredictable order in which threads run. Same code, different answer each launch.

Read-Modify-Write

Incrementing a counter is really three steps: read the value, add one, write it back. This read-modify-write sequence is where things break.

counter = counter + 1;

How the Steps Interleave

Two threads both read 5, both compute 6, both write 6. You expected 7 but got 6. One increment silently vanished.

The Lost Update

That vanished increment is called a lost update. With thousands of threads racing, dozens or hundreds of updates can disappear at once.

A Kernel That Looks Fine

This kernel looks correct, but every thread races on the same address. The final count will be wrong and will vary between runs.

__global__ void count(int* total) {
    *total = *total + 1;
}

Why It Is Nondeterministic

The hardware never promises a thread order. So a racy kernel is nondeterministic: it may even pass on small inputs and fail on big ones.

Reads Alone Are Safe

Many threads reading the same value is perfectly fine. Trouble starts only when at least one thread writes while others read or write.

Disjoint Writes Are Safe Too

If each thread writes its own unique slot, like out[i], there is no conflict. A race needs threads aiming at the same location.

out[i] = a[i] + b[i];

The Fix Preview

The cure is to make read-modify-write happen as one indivisible step. That is an atomic operation, coming up in the next lesson. ⚛️

Spotting Races in Review

When reviewing a kernel, ask: do two threads write the same address without protection? If yes, you almost certainly have a data race.

Quick Check

Let's make sure the race idea clicked.

Recap: Races on the GPU

You learned that unguarded read-modify-write on shared data causes race conditions and lost updates. Atomics, up next, make those steps indivisible. ✅

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

هل درس «حالات التسابق على GPU» مجاني؟

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

ماذا ستتعلم في «حالات التسابق على GPU»؟

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

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

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

كم من الوقت يستغرق درس «حالات التسابق على GPU»؟

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

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

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

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

  1. حالات التسابق على GPU
  2. atomicAdd وما شابهها
  3. بناء Histogram
  4. عمليات ذرية مخصصة باستخدام atomicCAS
← العودة إلى CUDA Academy