0Pricing
CUDA Academy · Lektion

__device__- und __host__-Funktionen

Wo die jeweilige Art von Funktion ausgeführt werden kann.

__device__- und __host__-Funktionen ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „__device__- und __host__-Funktionen“ kostenlos?

Ja — der vollständige Text von „__device__- und __host__-Funktionen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „__device__- und __host__-Funktionen“?

Wo die jeweilige Art von Funktion ausgeführt werden kann. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um CUDA Academy zu starten?

Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „__device__- und __host__-Funktionen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?

Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Der Funktionsqualifizierer __global__
  2. __device__- und __host__-Funktionen
  3. Getrennte Adressräume
  4. Der Lebenszyklus eines CUDA-Programms
← Zurück zu CUDA Academy