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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:24:44+00:00 2026-05-23T13:24:44+00:00

here is my code trying to do reduction to find maximum of a 50

  • 0

here is my code trying to do reduction to find maximum of a 50 value array in a block. I have padded the array to 64.

For threads 1-31 I have correct maxVal printing out but for threads 32-49 it’s a completely random number. I dont know what I am doing wrong.

btw. I thought I dont need to _sync every line in unrolling but apparently I have to. any suggestion about that?

Thanks in advance for any help.

//block size = 50


__syncthreads();

if (tid<32){

    cptmp[tid]=(cptmp[tid]< cptmp[tid+32]) ? cptmp[tid+32] : cptmp[tid];__syncthreads();    
    cptmp[tid]=(cptmp[tid]< cptmp[tid+16]) ? cptmp[tid+16] : cptmp[tid];__syncthreads();
    cptmp[tid]=(cptmp[tid]< cptmp[tid+8]) ? cptmp[tid+8] : cptmp[tid];  __syncthreads();    
    cptmp[tid]=(cptmp[tid]< cptmp[tid+4]) ? cptmp[tid+4] : cptmp[tid];  __syncthreads();
    cptmp[tid]=(cptmp[tid]< cptmp[tid+2]) ? cptmp[tid+2] : cptmp[tid];  __syncthreads();    
    cptmp[tid]=(cptmp[tid]< cptmp[tid+1]) ? cptmp[tid+1] : cptmp[tid];  __syncthreads();

}

__syncthreads();

//if (tid==0) {
    maxVal=cptmp[0];
    if(bix==0 && biy==0) cuPrintf(" max:%f x:%d y:%d\n", maxVal, blockIdx.x, blockIdx.y);
//}
  • 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-05-23T13:24:45+00:00Added an answer on May 23, 2026 at 1:24 pm

    Here is a more efficient (at least on Fermi GPUs) and correct code using volatile. Replace T with your type (or use a template):

    if (tid<32) {
        volatile T *c = cptmp;
        T t = c[tid];
        c[tid] = t = (t < c[tid+32]) ? c[tid+32] : t;
        c[tid] = t = (t < c[tid+16]) ? c[tid+16] : t;
        c[tid] = t = (t < c[tid+ 8]) ? c[tid+ 8] : t;
        c[tid] = t = (t < c[tid+ 4]) ? c[tid+ 4] : t;
        c[tid] = t = (t < c[tid+ 2]) ? c[tid+ 2] : t;
        c[tid] = t = (t < c[tid+ 1]) ? c[tid+ 1] : t;
    }
    

    Why is this more efficient? Well, for correctness in the absence of __syncthreads() we must use a volatile pointer to shared memory. But that forces the compiler to “honor” all reads from and writes to shared memory — it can’t optimize and keep anything in registers. So by explicitly always keeping c[tid] in the temporary t, we save one shared memory load per line of code. And since Fermi is a load/store architecture which can only use registers as instruction operands, that means we save an instruction per line, or 6 instructions total (about 25% overall, I expect).

    On the old T10/GT200 architecture and earlier, your code (with volatile and no __syncthreads()) would be equally efficient because that architecture could source one operand per instruction directly from shared memory.

    This code should be equivalent if you prefer if over ?::

    if (tid<32) {
        volatile T *c = cptmp;
        T t = c[tid];
        if (t < c[tid+32]) c[tid] = t = c[tid+32];
        if (t < c[tid+16]) c[tid] = t = c[tid+16];
        if (t < c[tid+ 8]) c[tid] = t = c[tid+ 8];
        if (t < c[tid+ 4]) c[tid] = t = c[tid+ 4];
        if (t < c[tid+ 2]) c[tid] = t = c[tid+ 2];
        if (t < c[tid+ 1]) c[tid] = t = c[tid+ 1];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to reduce some code here. I will explain how I have
I'm trying to match this block here /code\ int foo(string $bar, int $bleh); Simple
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I am trying to reuse Apple's Speak Here sample code in my own iPhone
I am trying to install MySQLdb package. I found the source code here .
I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
I'm trying to update an element in the XML document below: Here's the code:
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
I am trying to subclass NSOutlineView. Here is my code: OutlineViewSublcass.h: #import <Cocoa/Cocoa.h> @interface

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.