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

  • Home
  • SEARCH
  • 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 8050575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:07:02+00:00 2026-06-05T07:07:02+00:00

How can I write a statement in my CUDA kernel that is executed by

  • 0

How can I write a statement in my CUDA kernel that is executed by a single thread. For example if I have the following kernel:

__global__ void Kernel(bool *d_over, bool *d_update_flag_threads, int no_nodes)
{
   int tid = blockIdx.x*blockDim.x + threadIdx.x;
   if( tid<no_nodes && d_update_flag_threads[tid])
   {
     ...
     *d_over=true; // writing a single memory location, only 1 thread should do?
     ...
   }
}

In above kernel, “d_over” is a single boolean flag while “d_update_flag_threads” is a boolean array.

What I normally did before is using the first thread in the thread block e.g.:

if(threadIdx.x==0)

but It could not work in this case as I have a flag array here and only threads with assosiated flag “true” will execute the if statement. That flag array is set by another CUDA kernel called before and I don’t have any knowledge about it in advance.

In short, I need something similar to “Single” construct in OpenMP.

  • 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-05T07:07:03+00:00Added an answer on June 5, 2026 at 7:07 am

    A possible approach is use atomic operations. If you need only one thread per block to do the update, you could do the atomic operation in shared memory (for compute capability >= 1.2) which is generally much faster than perform it in global memory.

    Said that, the idea is as follow:

    int tid = blockIdx.x*blockDim.x + threadIdx.x;
    
    __shared__ int sFlag;
    // initialize flag
    if (threadIdx.x == 0) sFlag = 0;
    __syncthreads();
    
    if( tid<no_nodes && d_update_flag_threads[tid])
    {
      // safely update the flag
      int singleFlag = atomicAdd(&sFlag, 1);
      // custom single operation
      if ( singleFlag == 0) 
          *d_over=true; // writing a single memory location, only 1 thread will do it
           ...
    }
    

    It is just an idea. I’ve not tested it but is close to an operation performed by a single thread, not being the first thread of the block.

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

Sidebar

Related Questions

I'd like to write a single JDBC statement that can handle the equivalent of
How can you write the following statement in the given languages? a(0) = 1
We can write the same statement in two different ways as follows. I have
How can I write an sql statement that returns a list of how many
In XCode 4.2, I find that I can write a statement like this: [obj
i have a if statement, and i can write it in 2 ways: 1.
I'm curious if a single SQL statement can do something like this? I have
How can write a print statement in python that will print exactly 2 digits
I know that I can write a Ruby case statement to check a match
How can I write an insert statement which includes the & character? For example,

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.