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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:01:38+00:00 2026-06-08T13:01:38+00:00

I recently asked a question about synchronization issues among the threads of a block

  • 0

I recently asked a question about synchronization issues among the threads of a block in CUDA, here: Does early exiting a thread disrupt synchronization among CUDA threads in a block? One of the comments to my question gave a link to a similar thread, which quoted the following about the CUDA barrier (__syncthreads()) instruction from the PTX guide:

Barriers are executed on a per-warp basis as if all the threads in a warp are active. Thus, if any thread in a warp executes a bar instruction, it is as if all the threads in the warp have executed the bar instruction. All threads in the warp are stalled until the barrier completes, and the arrival count for the barrier is incremented by the warp size (not the number of active threads in the warp). In conditionally executed code, a bar instruction should only be used if it is known that all threads evaluate the condition identically (the warp does not diverge). Since barriers are executed on a per-warp basis, the optional thread count must be a multiple of the warp size.

I am still a bit confused about the mechanism explained in this quote. It says that if we use barriers in a conditional code and if some of the threads fail to reach the barrier instruction by taking a different path at the conditional code, then it could cause undefined behavior and even deadlocks. The thing is, I don’t understand how this mechanism can cause deadlocks. (Even thread numbers, which are not a multiple of the warp size, are dangerous.) The document says that if even a single thread executes a bar instruction it is treated as if all the threads in the warp executed the warp instruction and the arrival counter is updated by the number of the threads in the warp. Probably the CUDA architecture determines whether all the threads have been sycnhronized by checking this arrival counter; by comparing it to the actual number of threads in the block. If it was updated as a per-thread basis, then this could cause a deadlock since the counter will never reach to the max. num. of threads since some of them took conditional paths which don’t contain the bar instruction. But here, the number is updated with the number of threads in the warp. So, I don’t exactly understand the underlying mechanism here.

My other question is about conditional statements overall. I know that all threads in a warp executes the same instruction at a given time and in the case of an if clause, the threads which take if and else branches wait for each other by staying idle and synchronizing again at the end of the conditional. So there is an implicit synchronization mechanism for such conditional codes. Now, how will this work in a code like the following:

int foundCount=0;
for(int i=0;i<array1_length;i++)
{
    for(j=0;j<array0_length;j++)
    {
        if(i == array0[j])
        {
            array1[i] = array1[i] + 1;
            foundCount++;
            break;
        }
    }

     if(foundCount == foundLimit)
        break;
}

This is a piece of code from my current task; for each member of the array1 I need to check whether the current array1 index is contained by the array0. If it is so, I increment the element for the current index in the array1 and since it is already contained by the array0, I exit from the inner loop with a break statement. If the total contained number of indices for the array1 reaches a limit, we don’t need to continue the outer loop and we can exit from it as well. This is straightforward for a CPU code but I want to know how the CUDA’s warp mechanism handles such a nested conditional case. Imagine a warp of 32 threads are processing this code, some of them can process the inner loop while some of them has already exited it and some of them could even have exited from the outer loop. How does the architecture organize the working of the threads in this case, does it hold a list of current “waiting points” of the threads? How it ensures in such complex situations that the threads in the same warp do process the same line of code?

  • 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-08T13:01:41+00:00Added an answer on June 8, 2026 at 1:01 pm

    Conditional branching is implemented by having all threads in the warp execute all branches. Those threads which don’t follow the branch execute the equivalent of a null op. This is usually referred to as masked execution, and it is also how partial warps can be accomodated: partial warps contain permenantly masked threads. There is also direct conditional execution instructions available for implementing things like ternary operators without branching.

    These mechanisms do not apply to the standard bar PTX instruction. As you note, that is implemented using a simple counter decrement scheme, and if all threads in the block don’t decrement the counter to zero, a deadlock will result.

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

Sidebar

Related Questions

I recently asked a question here on Stack Overflow about how to cast my
I recently asked a question about formatting JavaScript code in Vim. And I've also
I've recently asked a question about clipping an image via path at view's drawRect
Recently I have asked a question about what I should use to create self-contained
Note: I originally asked this question about an hour ago but only recently realized
So recently, I asked a question about a class I created that extended SQLiteOpenHelper
I recently asked a question about some problems I was having getting MIT Kerberos
I recently asked a question about creating elements with jquery. Specifically I needed input
I recently asked a question about one of the Dijkstra’s algorithms ( shunting-yard ).
Recently I asked a question about how to use ajax calls to authenticate user

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.