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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:40:40+00:00 2026-05-26T20:40:40+00:00

According to NVIDIA, this is the fastest sum reduction kernel: template <unsigned int blockSize>

  • 0

According to NVIDIA, this is the fastest sum reduction kernel:

template <unsigned int blockSize>
__device__ void warpReduce(volatile int *sdata, unsigned int tid) {
if (blockSize >=  64) sdata[tid] += sdata[tid + 32];
if (blockSize >=  32) sdata[tid] += sdata[tid + 16];
if (blockSize >=  16) sdata[tid] += sdata[tid +  8];
if (blockSize >=    8) sdata[tid] += sdata[tid +  4];
if (blockSize >=    4) sdata[tid] += sdata[tid +  2];
if (blockSize >=    2) sdata[tid] += sdata[tid +  1];
}
template <unsigned int blockSize>
__global__ void reduce6(int *g_idata, int *g_odata, unsigned int n) {
extern __shared__ int sdata[];
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*(blockSize*2) + tid;
unsigned int gridSize = blockSize*2*gridDim.x;
sdata[tid] = 0;
while (i < n) { sdata[tid] += g_idata[i] + g_idata[i+blockSize];  i += gridSize;  }
__syncthreads();
if (blockSize >= 512) { if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockSize >= 128) { if (tid <   64) { sdata[tid] += sdata[tid +   64]; } __syncthreads(); }
if (tid < 32) warpReduce(sdata, tid);
if (tid == 0) g_odata[blockIdx.x] = sdata[0];
}

However, I don’t understand the “n” parameter. Any clues? I don’t think it’s the size of the array to reduce, since in the while loop there would be a buffer overflow.

  • 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-26T20:40:40+00:00Added an answer on May 26, 2026 at 8:40 pm

    I believe you’ve discovered a typo in the slides (it should probably be something like while(i + blockDim.x < n)).

    If you take a look at the source code in the CUDA SDK sample “reduction”, the body of the most recent reduce6 looks like this:

    template <class T, unsigned int blockSize, bool nIsPow2>
    __global__ void
    reduce6(T *g_idata, T *g_odata, unsigned int n)
    {
        T *sdata = SharedMemory<T>();
    
        // perform first level of reduction,
        // reading from global memory, writing to shared memory
        ...
    
        T mySum = 0;
    
        // we reduce multiple elements per thread.  The number is determined by the 
        // number of active thread blocks (via gridDim).  More blocks will result
        // in a larger gridSize and therefore fewer elements per thread
        while (i < n)
        {         
            mySum += g_idata[i];
            // ensure we don't read out of bounds -- this is optimized away for powerOf2 sized arrays
            if (nIsPow2 || i + blockSize < n) 
                mySum += g_idata[i+blockSize];  
            i += gridSize;
        } 
    

    Note the explicit check within the while which prevents out of bounds access to g_idata. Your initial suspicion is correct; n is simply the size of the g_idata array.

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

Sidebar

Related Questions

According to this discussion , the iphone agreement says that it doesn't allow loading
According to the answers to this question, I cannot embed a file version in
According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when
According to my class notes, you can allocate an array in C++ like int
According to this article and a number of other documents, JDBC resources are deployed
According to this: Get current date/time in seconds var seconds = new Date().getTime() /
According to this great article about HTTP uploads by Scott Hanselman, the browser typically
According to the C++0x spec , the following is legal class A { A(int
According to this post a typical firefox user has 2-3 tabs open. How can
According to the documentation i would like to overwrite predefined formats using this settings:

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.