0Pricing
CUDA Academy · レッスン

範囲外アクセスを防ぐ

if (i < n)による境界チェックを学びます。

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

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

You Often Launch Too Many

Thread counts come in fixed block sizes, so you almost always launch a few extra threads beyond your array length. Those spares need handling.

What Goes Wrong

An extra thread computes an index past the end of the array. If it writes there, it touches memory it does not own, causing a silent out-of-bounds bug.

The GPU Will Not Warn You

Unlike a clean crash, an out-of-range write may corrupt nearby data or read garbage. The kernel keeps running, so the failure is invisible until results look wrong.

The Fix Is One Line

Before any thread uses its index, check that it falls inside the array. This tiny bounds check is the most important safety habit in CUDA.

if (i < n) {
    out[i] = a[i] + b[i];
}

Why Less Than, Not Less Or Equal

Valid indices run 0 to n minus 1. The strict i < n lets the last real element through and stops the first invalid one.

Idle Threads Just Exit

A thread whose index is out of range simply skips the work and returns. Doing nothing is perfectly safe and costs almost no time.

Pass n to the Kernel

The kernel cannot guess the array length, so you give it n as a parameter. Then the guard always knows where the data ends.

__global__ void add(float* a, float* b, float* out, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) out[i] = a[i] + b[i];
}

Guard Reads Too

It is not only writes. Reading a[i] past the end loads garbage or faults, so the same i < n check protects every access.

A Common Off-by-One

Writing i <= n by mistake lets one thread touch element n, which does not exist. Always keep the comparison strict.

Cheap Insurance

The branch costs almost nothing because extra threads are rare and exit fast. The safety it buys is worth far more than that tiny cost. ✅

Make It a Reflex

Treat the bounds check as part of the index formula itself. Compute i, then immediately guard it before doing anything else.

Quick Check

Pick the correct guard.

Recap

You learned to add if (i < n) after computing the index. This one line stops out-of-range reads and writes that the GPU would never warn you about. 🛡️

よくある質問

「範囲外アクセスを防ぐ」レッスンは無料ですか?

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

「範囲外アクセスを防ぐ」で何を学びますか?

if (i < n)による境界チェックを学びます。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「範囲外アクセスを防ぐ」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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