0Pricing
CUDA Academy · レッスン

グリッドストライドループ

グリッドより大きな配列を処理します。

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

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

When the Array Is Huge

Sometimes the data is far bigger than the threads you launch. One thread per element no longer fits, so you need each thread to handle several elements.

The Grid Has a Size

The total number of threads is the grid size: blocks times threads per block. This stride is how far apart each thread's elements sit.

int stride = blockDim.x * gridDim.x;

Start, Then Stride

Each thread begins at its usual global index, then jumps forward by the grid size again and again until it runs off the array.

The Grid-Stride Loop

This loop is the whole pattern: start at i, step by stride, stop at n. Any array size is covered no matter how many threads you launch.

for (int i = blockIdx.x * blockDim.x + threadIdx.x;
     i < n;
     i += stride) {
    out[i] = a[i] + b[i];
}

The Built-in Guard

Notice the loop condition i < n is itself the bounds check. Threads that start past the end simply never enter the loop.

How the Work Splits

With a stride of 4 threads, thread 0 does elements 0, 4, 8, while thread 1 does 1, 5, 9. The work is interleaved, not chunked.

Interleaving Helps Coalescing

Because neighboring threads still touch neighboring addresses each step, the access stays coalesced and memory bandwidth stays high.

Decouple Threads From Data

Now your launch size no longer depends on n. You can pick a thread count that fits the GPU and let the loop absorb any workload.

Tune for the Hardware

A common choice is enough blocks to fill every multiprocessor, then let each thread loop. This keeps the GPU busy without overlaunching.

It Also Works When Tiny

If n is smaller than the grid, each thread runs the loop body at most once. The pattern degrades gracefully to the simple case.

A Robust Default

Many CUDA pros write every elementwise kernel as a grid-stride loop. It is flexible, safe, and rarely the wrong choice. 🚀

Quick Check

Identify the stride.

Recap

You learned the grid-stride loop: start at the global index and step by blockDim.x * gridDim.x until i reaches n. One kernel now handles any array size. 🎉

よくある質問

「グリッドストライドループ」レッスンは無料ですか?

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

「グリッドストライドループ」で何を学びますか?

グリッドより大きな配列を処理します。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「グリッドストライドループ」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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