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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:26:38+00:00 2026-06-18T07:26:38+00:00

If multiple threads are simultaneously writing a single memory location.,there will be a race

  • 0

If multiple threads are simultaneously writing a single memory location.,there will be a race condition,right??
In my case same is happening..

Consider a module from ‘reduce.cl’

int i = get_global_id(0);
int n,j;

n = keyMobj[i];                       // this n is the key..It can be either 0 or 1.
for(j=0; j<2; j++)
      sumMobj[n*2+j] += dataMobj[i].dattr[j];        //summing operation.

Here, The memory locations
sumMobj===> […0…, ….1…] is accessed 4 threads simultaneously &
sumMobj===> [….3…, ….4…] is accessed 6 threads simultaneously..

Is there any way to still make it parallely,like using locking or semaphore? As this summing is a very big part in my algorithm…

  • 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-18T07:26:39+00:00Added an answer on June 18, 2026 at 7:26 am

    I can give you some hint as I was also facing similar problem.

    I can think of three different methods for achieving similar goal:

    Consider a simple kernel, assuming you launched 4 (0-3) threads

    _kernel void addition (int *p)
    {
    int i = get_local_id(0);
         p[4]+= p[i];
    }
    

    You want to add values p[0], p[1], p[2], p[3], p[4], and store the final sum in p[4]. right? i.e:

    p[4]= p[0] + p[1] + p[2] + p[3] + p[4] 
    

    Method -1 (no parallelism)

    Assign this job to only 1 thread (no parallelism):

    int i = get_local_id(0);
    if (i==0)
    
    {
    
    p[4]+= p[i];
    
    } 
    

    Method-2 (with parallelism)

    Express your problem as follows:

    p[4]= p[0] + p[1] + p[2] + p[3] + p[4] + 0  
    

    This is a reduction problem

    So launch 3 threads: i=0 to i=2. In first iteration

     i=0 finds p[0] + p[1]
     i=1 finds p[2] + p[3]  
     i=2 finds p[4] + 0
    

    Now you have three numbers, you apply the same logic as above and add these numbers (with suitable padding of 0 to make it in power of two)

    Method -3 Atomic operations

    If you still need to implement this atomically, you can use atomic_add():

      int fsfunc atomic_add (   volatile __global int *p ,int val)
    

    Description

    Read the 32-bit value (referred to as old) stored at location pointed
    by p. Compute (old + val) and store result at location pointed by p.
    The function returns old.

    This is assuming the data is int type. Otherwise you can see the link as suggested above.

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

Sidebar

Related Questions

If I have multiple Java threads writing to the same Socket instance simultaneously, will
Is registering components on castle windsor thread-safe? I.e., can multiple threads simultaneously register components
I need help on which random number generator to use simultaneously from multiple threads
I have multiple threads who all need to write to the same Dictionary. I
As we all know in case of multiple threads each thread maintains it's separate
I've just written a method that is called by multiple threads simultaneously and I
Is it valid (defined behavior) to access a string literal simultaneously with multiple threads?
My Perl script needs to run multiple threads simultaneously... use threads ('yield', 'exit' =>
Would it be thread-safe if I solely call Enqueue(T) from multiple threads simultaneously and
It is possible to execute the following code from multiple threads simultaneously. this._sequencer.Completed +=

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.