0Pricing
CUDA Academy · درس

استخدام cudaGetLastError بعد التشغيل

اكتشف إعدادات التشغيل غير الصالحة.

استخدام cudaGetLastError بعد التشغيل درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Catching the Silent Launch

Since a kernel launch returns nothing, you need a helper to ask the GPU what just happened. That helper is cudaGetLastError. 🔎

What It Returns

cudaGetLastError hands back the most recent error code recorded by the runtime, or cudaSuccess if everything is fine so far.

myKernel<<<g, b>>>(d);
cudaError_t e = cudaGetLastError();

Call It Right After Launch

Place the check on the very next line after the launch. That captures configuration mistakes before any other call overwrites the state.

kernel<<<g, b>>>();
if (cudaGetLastError() != cudaSuccess) abort();

It Catches Launch Config Errors

This catches the configuration stage: too many threads per block, zero blocks, or a grid that the device simply cannot accept.

kernel<<<g, 99999>>>(); // invalid thread count

It Clears the Error

Reading the error also resets it to cudaSuccess. The runtime keeps only the last error, so checking it wipes the slate clean.

Peek Without Clearing

Want to look without resetting? Use cudaPeekAtLastError. It returns the same code but leaves the error state untouched.

cudaError_t e = cudaPeekAtLastError();

It Does Not Catch Runtime Faults

By itself, this only sees the launch, not the execution. An out-of-bounds access inside the kernel still slips past it.

Pair It With a Sync

To catch runtime faults, follow up with cudaDeviceSynchronize. The launch check plus a sync covers both stages of failure.

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

Why Two Checks Help

The launch check tells you the config was valid; the sync tells you the kernel actually finished without crashing. You learn which stage broke.

Wrap It in a Macro

Typing this after every launch gets old. Most projects wrap the launch check in a tidy macro so it is one short line.

kernel<<<g, b>>>();
CHECK_LAUNCH();

A Debugging Lifesaver

When a kernel mysteriously does nothing, a quick cudaGetLastError often reveals an invalid launch you would never have spotted otherwise.

Quick Check

Test your grasp of cudaGetLastError.

Recap: Check the Launch

Call cudaGetLastError right after a launch to catch config errors, then sync for runtime faults. Two checks reveal where things broke. 🎉

الأسئلة الشائعة

هل درس «استخدام cudaGetLastError بعد التشغيل» مجاني؟

نعم — نص درس «استخدام cudaGetLastError بعد التشغيل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «استخدام cudaGetLastError بعد التشغيل»؟

اكتشف إعدادات التشغيل غير الصالحة. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «استخدام cudaGetLastError بعد التشغيل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. رموز الإرجاع مقابل الأخطاء غير المتزامنة
  2. استخدام cudaGetLastError بعد التشغيل
  3. وحدة ماكرو CUDA_CHECK قابلة لإعادة الاستخدام
  4. فك ترميز cudaGetErrorString
← العودة إلى CUDA Academy