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 8551205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:08:45+00:00 2026-06-11T14:08:45+00:00

Recently I’ve been doing string comparing jobs on CUDA, and i wonder how can

  • 0

Recently I’ve been doing string comparing jobs on CUDA, and i wonder how can a __global__ function return a value when it finds the exact string that I’m looking for.

I mean, i need the __global__ function which contains a great amount of threads to find a certain string among a big big string-pool simultaneously, and i hope that once the exact string is caught, the __global__ function can stop all the threads and return back to the main function, and tells me “he did it”!

I’m using CUDA C. How can I possibly achieve this?

  • 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-11T14:08:47+00:00Added an answer on June 11, 2026 at 2:08 pm

    There is no way in CUDA (or on NVIDIA GPUs) for one thread to interrupt execution of all running threads. You can’t have immediate exit of the kernel as soon as a result is found, it’s just not possible today.

    But you can have all threads exit as soon as possible after one thread finds a result. Here’s a model of how you would do that.

    __global___ void kernel(volatile bool *found, ...) 
    {
        while (!(*found) && workLeftToDo()) {
    
           bool iFoundIt = do_some_work(...); // see notes below
    
           if (iFoundIt) *found = true;
        }
    }
    

    Some notes on this.

    1. Note the use of volatile. This is important.
    2. Make sure you initialize found—which must be a device pointer—to false before launching the kernel!
    3. Threads will not exit instantly when another thread updates found. They will exit only the next time they return to the top of the while loop.
    4. How you implement do_some_work matters. If it is too much work (or too variable), then the delay to exit after a result is found will be long (or variable). If it is too little work, then your threads will be spending most of their time checking found rather than doing useful work.
    5. do_some_work is also responsible for allocating tasks (i.e. computing/incrementing indices), and how you do that is problem specific.
    6. If the number of blocks you launch is much larger than the maximum occupancy of the kernel on the present GPU, and a match is not found in the first running “wave” of thread blocks, then this kernel (and the one below) can deadlock. If a match is found in the first wave, then later blocks will only run after found == true, which means they will launch, then exit immediately. The solution is to launch only as many blocks as can be resident simultaneously (aka “maximal launch”), and update your task allocation accordingly.
    7. If the number of tasks is relatively small, you can replace the while with an if and run just enough threads to cover the number of tasks. Then there is no chance for deadlock (but the first part of the previous point applies).
    8. workLeftToDo() is problem-specific, but it would return false when there is no work left to do, so that we don’t deadlock in the case that no match is found.

    Now, the above may result in excessive partition camping (all threads banging on the same memory), especially on older architectures without L1 cache. So you might want to write a slightly more complicated version, using a shared status per block.

    __global___ void kernel(volatile bool *found, ...) 
    {
        volatile __shared__ bool someoneFoundIt;
    
        // initialize shared status
        if (threadIdx.x == 0) someoneFoundIt = *found;
        __syncthreads();
    
        while(!someoneFoundIt && workLeftToDo()) {
    
           bool iFoundIt = do_some_work(...); 
    
           // if I found it, tell everyone they can exit
           if (iFoundIt) { someoneFoundIt = true; *found = true; }
    
           // if someone in another block found it, tell 
           // everyone in my block they can exit
           if (threadIdx.x == 0 && *found) someoneFoundIt = true;
    
           __syncthreads();
        }
    }
    

    This way, one thread per block polls the global variable, and only threads that find a match ever write to it, so global memory traffic is minimized.

    Aside: __global__ functions are void because it’s difficult to define how to return values from 1000s of threads into a single CPU thread. It is trivial for the user to contrive a return array in device or zero-copy memory which suits his purpose, but difficult to make a generic mechanism.

    Disclaimer: Code written in browser, untested, unverified.

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

Sidebar

Related Questions

Recently I've been doing quite the project mostly working with the DateTime class. Now,..
Recently I've been doing things like this: import Tkinter class C(object): def __init__(self): self.root
Recently the PHP manual started showing the following warning on every mysql function page:
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently I've been dealing with texts with mixed languages, including Chinese, English, and even
Recently I've been thinking about how to transform a complex polygon into a non-complex
Recently I've been experimenting with the use of the Func<T> class, and so far
Recently I have been working with the xml coding,and the problem happened. Given the
Recently I have been told that static class/methods are evil. Take for example my

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.