0Pricing
CUDA Academy · レッスン

ベクトル加算カーネル

1つのスレッドで1組の要素を加算します。

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

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

The Big Idea

Vector addition is the perfect first kernel: each output is just C[i] = A[i] + B[i]. Every element is independent, so they can all run at once. 🚀

One Thread, One Element

The whole trick is simple: you assign one thread to one element. Instead of looping over the array, thousands of threads each do a single add in parallel.

Marking It as a Kernel

A function that runs on the GPU is a kernel, marked with the __global__ qualifier. That word tells nvcc this code launches on the device.

__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
    // body comes next
}

Kernels Return void

A kernel always has a void return type. There is no return value to hand back to the CPU, so results must be written into device memory instead.

Finding This Thread's Index

Each thread computes its own global index so it knows which element to handle. The classic formula combines the block and thread coordinates.

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

The Single Line of Work

Once a thread knows its index i, the real work is one line. No loop, no branching, just one add per thread.

C[i] = A[i] + B[i];

Why a Bounds Check Matters

You usually launch more threads than elements, so add an if (i < n) guard. Without it, extra threads read past the array and crash. 🛡️

if (i < n) {
    C[i] = A[i] + B[i];
}

The Full Kernel

Put it together and the entire vecAdd kernel is just a few lines. Tiny code, but it runs across thousands of threads at once.

__global__ void vecAdd(const float* A, const float* B, float* C, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) C[i] = A[i] + B[i];
}

Pointers Live on the Device

The pointers A, B, and C must point to device memory. Hand a kernel a plain host pointer and it will read garbage or fault.

Mark Inputs as const

A and B are only read, so mark them const float*. This documents intent and lets the compiler optimize the read-only inputs more freely.

No Shared State Needed

Because every thread touches a different element, there are no races and no locks. This independence is exactly what makes the GPU shine here.

Quick Check

Why does the vector add kernel need an if (i < n) guard?

Recap

You wrote your first kernel: __global__ void vecAdd, one thread per element, a global index, and a bounds check. Simple code, massive parallelism. 🎉

よくある質問

「ベクトル加算カーネル」レッスンは無料ですか?

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

「ベクトル加算カーネル」で何を学びますか?

1つのスレッドで1組の要素を加算します。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「ベクトル加算カーネル」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. ベクトル加算カーネル
  2. ホスト側をつなぎ込む
  3. CPUで結果を検証する
  4. 初めての高速化を計測する
← CUDA Academyに戻る