0Pricing
CUDA Academy · Lesson

The Classic Index Formula

blockIdx.x * blockDim.x + threadIdx.x.

The Classic Index Formula is a free CUDA Academy lesson on CoddyKit — lesson 1 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.

One Thread, One Element

The whole point of a CUDA kernel is that every thread handles one piece of data. To do that, each thread needs a unique global index.

Why Local IDs Are Not Enough

Inside a block, threadIdx.x only counts 0 up to blockDim minus one. Many blocks reuse those same small numbers, so it cannot be your final index.

Blocks Sit Side by Side

Picture the grid as blocks laid end to end. Each block owns a contiguous slice of the array, and blockIdx.x tells you which slice you are in.

How Wide Is a Block?

blockDim.x is the number of threads per block. It is the width of each slice, so it scales your block offset to the right spot.

The Classic Formula

Combine the three: skip past earlier blocks, then add your position inside this block. That single line gives every thread a unique index.

int i = blockIdx.x * blockDim.x + threadIdx.x;

Walking Through It

With 4 threads per block, block 0 covers 0 to 3, block 1 covers 4 to 7, block 2 covers 8 to 11. The offsets never overlap.

A Concrete Example

Thread 2 in block 3 with blockDim 256 lands at 3 times 256 plus 2, which is 770. That is its global position in the data.

// blockIdx.x=3, blockDim.x=256, threadIdx.x=2
int i = 3 * 256 + 2; // i == 770

Using the Index

Once you have i, you treat it as an array subscript. Each thread reads and writes only its own element, with no overlap.

out[i] = a[i] + b[i];

It Maps One to One

Launch n threads and the formula produces every value from 0 to n minus 1 exactly once. That is a perfect one-to-one cover of the array.

The Order Matters

Always multiply before you add. blockIdx.x * blockDim.x is the start of your slice, and threadIdx.x is the step inside it.

Beyond One Dimension

The same idea extends to 2D and 3D using the .y and .z members, but for flat arrays the .x formula is all you need. 🚀

Quick Check

Compute one thread's global index.

Recap

You learned the formula every kernel uses: blockIdx.x * blockDim.x + threadIdx.x. It hands each thread one unique slot in your array. 🎉

Frequently asked questions

Is the “The Classic Index Formula” lesson free?

Yes — the full text of “The Classic Index Formula” 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 “The Classic Index Formula”?

blockIdx.x * blockDim.x + threadIdx.x. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Classic Index Formula” 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 Classic Index Formula
  2. Guarding Against Out-of-Range
  3. Rounding Up the Block Count
  4. Grid-Stride Loops
← Back to CUDA Academy