0Pricing
CUDA Academy · Lektion

Ein wiederverwendbares CUDA_CHECK-Makro

Umschließen Sie jeden Aufruf für mehr Sicherheit.

Ein wiederverwendbares CUDA_CHECK-Makro ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.

Checking Every Call Is Tedious

Writing an if-statement after every CUDA call clutters your code fast. A reusable CUDA_CHECK macro fixes that with one clean line. 🧹

The Goal of the Macro

You want to wrap any call, grab its return code, and bail out loudly if it failed. One macro can do this everywhere.

CUDA_CHECK(cudaMalloc(&d, n));

Capturing the Result

Inside, the macro stores the call result in a local cudaError_t so it can be inspected once without calling twice.

cudaError_t e = (call);

Comparing to Success

It then tests the code against cudaSuccess. If they match, nothing happens and your program flows on as normal.

if (e != cudaSuccess) { /* report */ }

Printing Where It Failed

On failure it prints a readable message using __FILE__ and __LINE__, so you jump straight to the offending line.

printf("CUDA error %s at %s:%d\n", msg, __FILE__, __LINE__);

The do-while(0) Trick

The body is wrapped in a do-while(0). This makes the macro a single statement that works safely even inside an unbraced if.

#define CUDA_CHECK(c) do { /* ... */ } while(0)

A Full Macro Sketch

Put it together and you get a compact, reusable safety net you can paste into any CUDA project header.

#define CUDA_CHECK(c) do { cudaError_t e=(c); if(e) exit(1); } while(0)

Wrapping API Calls

Use it on any call that returns a code: cudaMalloc, cudaMemcpy, cudaFree. One wrapper guards them all uniformly.

CUDA_CHECK(cudaMemcpy(d, h, n, cudaMemcpyHostToDevice));

Also Guard the Launch

Kernels return nothing, so wrap cudaGetLastError and a sync after the launch instead of wrapping the launch line itself.

kernel<<<g, b>>>();
CUDA_CHECK(cudaGetLastError());

Keep It in a Header

Drop the macro in a shared header so every file uses the same check. Consistency here saves hours of confusing debugging.

Fail Fast, Fail Clearly

The big win is failing fast: the instant a call breaks, you get a precise file, line, and message instead of silent garbage.

Quick Check

Test your grasp of the CUDA_CHECK macro.

Recap: One Macro to Guard Them All

A CUDA_CHECK macro captures the code, compares to success, and prints file and line on failure. Define it once, use it everywhere. 🎉

Häufig gestellte Fragen

Ist die Lektion „Ein wiederverwendbares CUDA_CHECK-Makro“ kostenlos?

Ja — der vollständige Text von „Ein wiederverwendbares CUDA_CHECK-Makro“ 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 „Ein wiederverwendbares CUDA_CHECK-Makro“?

Umschließen Sie jeden Aufruf für mehr Sicherheit. 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 3 von 4.

Wie lange dauert die Lektion „Ein wiederverwendbares CUDA_CHECK-Makro“?

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. Rückgabecodes vs. asynchrone Fehler
  2. cudaGetLastError nach dem Start
  3. Ein wiederverwendbares CUDA_CHECK-Makro
  4. cudaGetErrorString entschlüsseln
← Zurück zu CUDA Academy