threadIdx, blockIdx, blockDim
ตัวแปรในตัวที่ใช้ระบุตำแหน่งของเธรด
threadIdx, blockIdx, blockDim เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Where Am I?
Every thread runs the same code, so each one needs to ask: which piece of data is mine? CUDA answers with built-in variables. 📍
Meet threadIdx
Inside a kernel, threadIdx tells a thread its position within its own block. The first thread in a block has threadIdx.x equal to 0.
int t = threadIdx.x;Meet blockIdx
The variable blockIdx tells a thread which block it belongs to within the grid. Block numbering also starts at 0.
int b = blockIdx.x;Meet blockDim
You also get blockDim, the number of threads per block. If you launched 256 threads, then blockDim.x is 256 for every thread.
int width = blockDim.x;They Are Automatic
You never set these variables yourself. CUDA fills them in for each thread the moment the kernel launches.
The .x, .y, .z Fields
Each of these has x, y, and z fields for 1D, 2D, or 3D launches. For simple arrays you usually only touch the x field.
Combining Them
On their own, threadIdx repeats in every block. To get a unique spot you must combine blockIdx and blockDim with threadIdx.
A Tiny Walkthrough
With blockDim 4, thread 2 in block 0 is element 2, but thread 2 in block 1 is element 6. The block shifts the start point.
Read-Only Helpers
These built-ins are read-only inside the kernel. Treat them as trusted coordinates describing exactly where your thread sits.
Also gridDim
There is one more friend: gridDim holds the total number of blocks in the grid. It is handy when you loop over more data than threads.
int blocks = gridDim.x;Why It Matters
These three variables are the raw materials for computing a global index, which is how every thread claims its own element.
Quick Check
Pick the variable that does the stated job.
Recap: Locating a Thread
You met the built-ins: threadIdx for position in a block, blockIdx for the block, and blockDim for block size. Together they pinpoint a thread. ✅
คำถามที่พบบ่อย
บทเรียน “threadIdx, blockIdx, blockDim” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “threadIdx, blockIdx, blockDim” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “threadIdx, blockIdx, blockDim”
ตัวแปรในตัวที่ใช้ระบุตำแหน่งของเธรด คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “threadIdx, blockIdx, blockDim” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ลำดับชั้นของเธรด
- threadIdx, blockIdx, blockDim
- ทำไมจึงมีบล็อก
- เลือกจำนวนเธรดต่อบล็อก