ใช้ cudaGetLastError หลังการเรียกใช้
ตรวจจับการตั้งค่าการเรียกใช้ที่ไม่ถูกต้อง
ใช้ cudaGetLastError หลังการเรียกใช้ เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 countIt 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 หลังการเรียกใช้” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ใช้ cudaGetLastError หลังการเรียกใช้”
ตรวจจับการตั้งค่าการเรียกใช้ที่ไม่ถูกต้อง คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ใช้ cudaGetLastError หลังการเรียกใช้” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม
ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รหัสส่งกลับเทียบกับข้อผิดพลาดแบบอะซิงโครนัส
- ใช้ cudaGetLastError หลังการเรียกใช้
- มาโคร CUDA_CHECK ที่นำกลับมาใช้ซ้ำได้
- ถอดรหัส cudaGetErrorString