0Pricing
CUDA Academy · Lesson

threadIdx, blockIdx, blockDim

The built-in variables that locate a thread.

threadIdx, blockIdx, blockDim is a free CUDA Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. ✅

Frequently asked questions

Is the “threadIdx, blockIdx, blockDim” lesson free?

Yes — the full text of “threadIdx, blockIdx, blockDim” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.

What will I learn in “threadIdx, blockIdx, blockDim”?

The built-in variables that locate a thread. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CUDA Academy?

No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “threadIdx, blockIdx, blockDim” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CUDA Academy lesson?

Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Thread Hierarchy
  2. threadIdx, blockIdx, blockDim
  3. Why Blocks Exist
  4. Choosing Threads per Block
← Back to CUDA Academy