วนซ้ำตามช่วงของไทล์
สะสมผลรวมบางส่วนจากไทล์ต่าง ๆ
วนซ้ำตามช่วงของไทล์ เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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. 📈
คำถามที่พบบ่อย
บทเรียน “วนซ้ำตามช่วงของไทล์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “วนซ้ำตามช่วงของไทล์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “วนซ้ำตามช่วงของไทล์”
สะสมผลรวมบางส่วนจากไทล์ต่าง ๆ คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “วนซ้ำตามช่วงของไทล์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เคอร์เนลคูณเมทริกซ์แบบพื้นฐาน
- แบ่งผลคูณภายในเป็นไทล์
- วนซ้ำตามช่วงของไทล์
- วัดการเพิ่มความเร็ว