0Pricing
CUDA Academy · درس

المزامنة باستخدام __syncthreads

حواجز تُبقي الخيوط متزامنة.

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

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

Threads Run Out of Step

Threads in a block do not march in lockstep. One may finish writing while another is still loading, so you cannot assume the data is ready yet.

Meet the Barrier

A barrier is a line in the code where every thread must wait until all of them arrive. Only then does the block move on together.

The __syncthreads Call

CUDA gives you __syncthreads() as the block-wide barrier. Call it and every thread in the block pauses until the last one shows up. 🛑

__syncthreads();

The Load-Sync-Use Order

The golden pattern is simple: every thread loads its piece into shared memory, you sync, then everyone safely reads what others wrote.

tile[threadIdx.x] = input[i];
__syncthreads();
float left = tile[threadIdx.x - 1];

Skip It and You Get Garbage

Forget the barrier and a thread may read a slot before its neighbor wrote it. That is a race condition, and the result is silent, wrong data.

It Is a Memory Fence Too

The barrier also makes shared writes visible to all threads after it. So once you pass __syncthreads, everyone sees the freshly written values.

All or None Must Reach It

The strict rule: every thread in the block must hit the same __syncthreads. If some skip it, the block can hang forever.

The Divergent Branch Trap

Never put a barrier inside an if that only some threads enter. Threads taking the other path never arrive, and the block deadlocks. ⚠️

if (threadIdx.x < 64) {
    __syncthreads();
}

Block Scope, Not Grid Scope

One important limit: __syncthreads only synchronizes one block. It cannot coordinate threads across different blocks of the grid.

Syncing Inside Loops

In tiled algorithms you often sync twice per phase: once after loading a tile and once after computing, before loading the next.

Cheap but Not Free

A barrier costs a little time while threads wait. Use it where correctness needs it, but avoid extra calls that just stall fast threads.

Quick Check

Let us test your grasp of the barrier.

Recap

You learned that __syncthreads() is a block-wide barrier that keeps threads in step, and that placing it inside divergent branches can deadlock. Next: bank conflicts. 🎯

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

هل درس «المزامنة باستخدام __syncthreads» مجاني؟

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

ماذا ستتعلم في «المزامنة باستخدام __syncthreads»؟

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

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

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

كم من الوقت يستغرق درس «المزامنة باستخدام __syncthreads»؟

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

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

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

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

  1. تعريف مصفوفات __shared__
  2. المزامنة باستخدام __syncthreads
  3. تجنّب تعارضات البنوك
  4. الذاكرة المشتركة الديناميكية
← العودة إلى CUDA Academy