Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8574099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:23:57+00:00 2026-06-11T19:23:57+00:00

A follow up to: CUDA: Stop all other threads I’m looking for a way

  • 0

A follow up to: CUDA: Stop all other threads

I’m looking for a way to exit a kernel if a “bad condition” occurs.
The prog manual say NVCC does not support exception handling. I’m wondering if there is a user defined cuda-error-code. In other words if “bad” happens, then terminate with this user error code. I doubt there is one, so my other idea would be to cause one.

Something like, if “bad” happens, divide by zero. But I’m unsure if one thread does a divide-by-zero, is that enough to crash the whole kernel, or just that thread?

Is there a better approach to terminating a kernel?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:23:58+00:00Added an answer on June 11, 2026 at 7:23 pm

    You should first read this question and the answers by harrism and tera (asked/answered yesterday).

    You may be tempted to use something like

    if (there_is_an_error) {
      *status = MY_ERROR_CODE; // store to device pointer
      __threadfence();         // ensure store issued before trap
      asm("trap;");            // kill kernel with error
    }
    

    This does not exactly satisfy your condition of “graceful”, in my opinion. Trap causes the kernel to exit and the runtime to report cudaErrorUnknown. But since kernel execution is asynchronous, you will need to synchronize your stream / device in order to catch this error, which means synchronizing after every kernel call, unless you are OK with having imprecise errors (i.e. you may not catch the error code until after calls to subsequent CUDA API calls).

    But this is just the way kernel error handling is in CUDA, and well-written codes should be synchronizing in debug builds to check kernel errors, and settling for imprecise error messages in release builds. Unfortunately, I don’t think there is a more graceful way than that.

    edit: on Compute capability 2.0 and later you can use assert() to exit with an error in debug builds. It was unclear if this is what you want though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A follow up Q from: CUDA: Calling a __device__ function from a kernel I'm
Follow up question from Multi-core usage, threads, thread-pools . Are threads moved from one
Follow up to This I have also read up on some other questions but
Follow along in my test source. What is a good way to get a
I have a CUDA kernel where every thread traverse a tree. Because of this
Follow Up Question: I noticed with the binaryformatter that all I had to do
Follow-to this question: The Dual Nature of svn:ignore Is there any way to accomplish
I have a CUDA kernel which I'm compiling to a cubin file without any
follow up: while I had already been using swfobject with static publishing, dynamic publishing
Follow up to this question . I have the following code: string[] names =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.