التكرار عبر مراحل البلاطات
اجمع المجاميع الجزئية عبر البلاطات.
التكرار عبر مراحل البلاطات درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Dot Product Is Split
A full row times a full column is too big for one tile. So you split that long sum into chunks of width TILE, one chunk per phase.
Counting the Phases
If the matrices are N wide and tiles are TILE wide, you need N / TILE phases to cover the whole inner dimension.
int numPhases = (N + TILE - 1) / TILE;The Outer Phase Loop
Wrap your load-sync-compute steps in a loop over phase. Each pass slides the tile window further along the row of A and column of B.
for (int phase = 0; phase < numPhases; ++phase) {
// load, sync, compute, sync
}Accumulate Across Phases
The local sum variable lives outside the loop, so it keeps growing. Each phase adds its slice of the dot product to the running total.
float sum = 0.0f;
for (int phase = 0; phase < numPhases; ++phase) { ... }Tile Offset per Phase
Each phase shifts the column you read from A and the row you read from B by phase * TILE. That is how the window advances.
As[ty][tx] = A[row*N + phase*TILE + tx];
Bs[ty][tx] = B[(phase*TILE + ty)*N + col];Sync After Loading
Just like before, call __syncthreads() after the loads so the tile is complete before anyone computes on it.
__syncthreads();Compute This Phase's Slice
The inner loop adds TILE products into sum, using only the freshly loaded tile. It contributes one chunk of the final dot product.
for (int k = 0; k < TILE; ++k)
sum += As[ty][k] * Bs[k][tx];The Second Barrier
End each phase with another __syncthreads() so no thread overwrites the tile while a slower thread is still reading it. 🚧
__syncthreads(); // before the next phase loadsWhy Two Syncs Matter
One barrier guards reads-after-load, the other guards loads-after-read. Together they keep every thread in lockstep across phases.
Write the Final Sum
After all phases finish, the running sum is the complete dot product. Store it into C once, guarded by a bounds check.
if (row < N && col < N)
C[row*N + col] = sum;Handling Ragged Sizes
When N is not a clean multiple of TILE, load zero for out-of-range elements so the extra products add nothing to the sum.
Quick Check
Think about where the running total lives during the phase loop.
Recap
You looped over phases, shifting tiles, syncing twice, and accumulating partial sums into one total. Now let us measure how much faster it is. 📈
الأسئلة الشائعة
هل درس «التكرار عبر مراحل البلاطات» مجاني؟
نعم — نص درس «التكرار عبر مراحل البلاطات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «التكرار عبر مراحل البلاطات»؟
اجمع المجاميع الجزئية عبر البلاطات. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «التكرار عبر مراحل البلاطات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نواة Matmul الساذجة
- تقسيم الجداء الداخلي إلى بلاطات
- التكرار عبر مراحل البلاطات
- قياس التسارع