__device__関数と__host__関数
それぞれの関数を実行できる場所を学びます。
「__device__関数と__host__関数」はCoddyKit上の無料CUDA Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはCUDA Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 CUDA Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond the Kernel
Kernels are entry points, but most code lives in helpers. CUDA gives two more qualifiers to say where a plain function may run. Let us meet them.
The __device__ Qualifier
A __device__ function runs on the GPU and is callable only from other GPU code, like a kernel or another device function.
__device__ int square(int x) {
return x * x;
}Helpers for Kernels
Think of __device__ functions as your GPU toolbox. They keep kernels tidy by holding logic the device threads reuse.
The __host__ Qualifier
The __host__ qualifier means ordinary CPU code. It is the default, so plain C++ functions are already host functions.
__host__ void setup() {
// runs on the CPU
}Default Is Host
Leave off all qualifiers and your function is host-only by default. That is why normal C++ still compiles inside a .cu file.
Both at Once
Combine the two and a function compiles for both sides. nvcc builds one version for the CPU and one for the GPU.
__host__ __device__ int dbl(int x) {
return x + x;
}Why Dual-Compile?
Marking a helper __host__ __device__ lets the same math run in CPU reference code and inside kernels, with zero duplication. ✨
Calling Rules
Host code may not call a __device__ function, and device code may not call a host-only one. The compiler enforces the boundary.
No __global__ from Device
A __device__ function is not launchable. Only __global__ kernels accept the triple-angle-bracket launch from the host.
Inlining for Speed
The compiler often inlines small __device__ functions, so wrapping logic in helpers usually costs nothing at runtime.
Pick the Right Tag
Choose by where it runs: __device__ for GPU helpers, __host__ for CPU code, and both when the logic is shared.
Quick Check
Time to test the device and host qualifiers.
Recap
You learned __device__ runs on the GPU, __host__ runs on the CPU, the default is host, and combining both shares one function across worlds. 🎯
よくある質問
「__device__関数と__host__関数」レッスンは無料ですか?
はい。「__device__関数と__host__関数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、CUDA Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 CUDA Academyコースには全4レッスンが含まれています。
「__device__関数と__host__関数」で何を学びますか?
それぞれの関数を実行できる場所を学びます。 ブラウザで直接実行するハンズオンコードでCUDA Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
CUDA Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのCUDA Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「__device__関数と__host__関数」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このCUDA Academyレッスンでコードを書いて実行できますか?
はい。すべてのCUDA Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- __global__関数修飾子
- __device__関数と__host__関数
- アドレス空間を分離する
- CUDAプログラムのライフサイクル