0Pricing
CUDA Academy · Lektion

atomicAdd und weitere Funktionen

Hardwareoperationen zum Lesen, Ändern und Schreiben.

atomicAdd und weitere 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.

One Indivisible Step

An atomic operation reads, modifies, and writes a value as a single step that no other thread can interrupt. That kills the race. ⚛️

Meet atomicAdd

atomicAdd takes a pointer and a value, adds safely, and returns the old value. Many threads can hit the same address with no lost updates.

atomicAdd(total, 1);

Fixing the Racy Counter

Swap the plain increment for atomicAdd and the count becomes correct every single time, no matter how many threads run.

__global__ void count(int* total) {
    atomicAdd(total, 1);
}

The Return Value Is Useful

atomicAdd returns the value held before your add. That old value is a unique slot you can use as an index or a ticket number.

int slot = atomicAdd(counter, 1);

A Whole Family

atomicAdd has many friends: atomicSub, atomicMax, atomicMin, atomicExch, atomicAnd, atomicOr, and atomicXor all work the same atomic way.

Swapping a Value

atomicExch stores a new value and returns the old one in one step. It is handy for claiming flags or grabbing a previous state safely.

int prev = atomicExch(flag, 1);

Atomic Max and Min

atomicMax updates the target only if your value is larger. It is perfect for finding a global maximum across all threads without races.

atomicMax(best, myValue);

Supported Types

Atomics cover int, unsigned, and on newer hardware float and double too. Always check that your type and arch support the call you want.

Atomics Have a Cost

Atomics serialize threads that target the same address. Heavy contention on one spot becomes a bottleneck, so use them only where needed.

Spread the Load

Reduce contention by having threads update different addresses, then combine results. Fewer collisions means atomics stay fast.

Atomics vs Reductions

For summing huge arrays, a tree reduction often beats a flood of atomics. Reach for atomics when updates are sparse or irregular.

Quick Check

Test your grip on atomicAdd's behavior.

Recap: atomicAdd and Friends

You met the atomic family, fixed the racy counter, and learned atomics return the old value but cost time under contention. ✅

Häufig gestellte Fragen

Ist die Lektion „atomicAdd und weitere Funktionen“ kostenlos?

Ja — der vollständige Text von „atomicAdd und weitere 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 „atomicAdd und weitere Funktionen“?

Hardwareoperationen zum Lesen, Ändern und Schreiben. 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 „atomicAdd und weitere 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. Race Conditions auf der GPU
  2. atomicAdd und weitere Funktionen
  3. Ein Histogramm erstellen
  4. Benutzerdefinierte Atomics mit atomicCAS
← Zurück zu CUDA Academy