Note that there are some explanatory texts on larger screens.

plurals
  1. POCUDA combining thread independent(??) variables during execution
    text
    copied!<p>Guys I apologize if the title is confusing. I though long and hard and couldn't come up with proper way to phrase the question in a single line. So here's more detail. I am doing a basic image subtraction where the second image has been modified and I need to find the ratio of how much change was done to the image. for this I used the following code. Both images are 128x1024. </p> <pre><code>for(int i = 0; i &lt; 128; i++) { for(int j = 0; j &lt; 1024; j++) { den++; diff[i * 1024 + j] = orig[i * 1024 + j] - modified[i * 1024 + j]; if(diff[i * 1024 + j] &lt; error) { num++; } } } ratio = num/den; </code></pre> <p>The above code works fine on the CPU but I want to try to do this on CUDA. For this I can setup CUDA to do the basic subtraction of the images (code below) but I can't figure out how to do the conditional if statement to get my ratio out.</p> <pre><code>__global__ void calcRatio(float *orig, float *modified, int size, float *result) { int index = threadIdx.x + blockIdx.x * blockDim.x; if(index &lt; size) result[index] = orig[index] - modified[index]; } </code></pre> <p>So, up to this point it works but I cannot figure out how to parrallelize the num and den counters in each thread to calculate the ratio at the end of all the thread executions. To me it feels like the num and den counders are independent of the threads as every time I have tried to use them it seems they get incremented only once.</p> <p>Any help will be appreciated as I am just starting out in CUDA and every example I see online never seems to apply to what I need to do.</p> <p><strong>EDIT: Fixed my naive code. Forgot to type one of the main condition in the code. It was a long long day.</strong> </p> <pre><code>for(int i = 0; i &lt; 128; i++) { for(int j = 0; j &lt; 1024; j++) { if(modified[i * 1024 + j] &lt; 400.0) //400.0 threshold value to ignore noise { den++; diff[i * 1024 + j] = orig[i * 1024 + j] - modified[i * 1024 + j]; if(diff[i * 1024 + j] &lt; error) { num++; } } } } ratio = num/den; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload