0Pricing
CUDA Academy · レッスン

基本のインデックス計算式

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

「基本のインデックス計算式」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「基本のインデックス計算式」レッスンは無料ですか?

はい。「基本のインデックス計算式」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。

「基本のインデックス計算式」で何を学びますか?

blockIdx.x * blockDim.x + threadIdx.x。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

CUDA Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「基本のインデックス計算式」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このCUDA Academyレッスンでコードを書いて実行できますか?

はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 基本のインデックス計算式
  2. 範囲外アクセスを防ぐ
  3. ブロック数を切り上げる
  4. グリッドストライドループ
← CUDA Academyに戻る