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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:55:32+00:00 2026-05-28T13:55:32+00:00

I am trying to write a parallel prefix scan on cuda by following this

  • 0

I am trying to write a parallel prefix scan on cuda by following this tutorial –

I am trying the work-inefficient ‘double buffered one’ as explained in the tutorial.

This is what I have:

// double buffered naive.

// d = number of iterations, N - size, and input.
__global__ void prefixsum(int* in, int d, int N)
{

        //get the block index
        int idx = blockIdx.x*blockDim.x + threadIdx.x;

        // allocate shared memory
        extern __shared__ int temp_in[], temp_out[];

        // copy data to it.
        temp_in[idx] = in[idx];
        temp_out[idx] = 0;

        // block until all threads copy

        __syncthreads();

        int i = 1;
        for (i; i<=d; i++)
        {
                if (idx < N+1 && idx >= (int)pow(2.0f,(float)i-1))
                {
                        // copy new result to temp_out
                        temp_out[idx] += temp_in[idx - (int)pow(2.0f,(float)i-1)] + temp_in[idx];
                }
                else
                {
                        // if the element is to remain unchanged, copy the same thing
                        temp_out[idx] = temp_in[idx];
                }
                // block until all theads do this
                __syncthreads();
                // copy the result to temp_in for next iteration
                temp_in[idx] = temp_out[idx];
                // wait for all threads to do so
                __syncthreads();
        }

        //finally copy everything back to global memory
        in[idx] = temp_in[idx];
}

Can you point out what’s wrong with this? I have written comments for what I think should happen.

This is the kernel invocation –

prefixsum<<<dimGrid,dimBlock>>>(d_arr, log(SIZE)/log(2), N);

This is the grid and block allocations:

dim3 dimGrid(numBlocks);
dim3 dimBlock(numThreadsPerBlock);

The problem is that I don’t get the correct output for any input that’s more than 8 elements long.

  • 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-28T13:55:33+00:00Added an answer on May 28, 2026 at 1:55 pm

    I see two problems in your code

    Problem 1: extern shared memory

    Agh…. I hate extern __shared__ memory. The problem is, that the compiler does not know how big are the arrays. As a result, they both point to the same piece of memory!
    So, in your case: temp_in[5] and temp_out[5] refer to the same word in shared memory.

    If you really want the extern __shared__ memory, you can manually offset the second array, for example something like this:

    size_t size = .... //the size of your array
    extern __shared__ int memory[];
    int* temp_in=memory;
    int* temp_out=memory+size;
    

    Problem 2: Shared array index

    Shared memory is private for each block. That is, temp[0] in one block can be different than temp[0] in another block. However, you index it by blockIdx.x*blockDim.x + threadIdx.x as if the temp arrays were shared between the blocks.

    Instead, you should most likely index your temp arrays just by threadIdx.x.

    Of course, the idx array is global and you index that one correctly.

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

Sidebar

Related Questions

I'm trying to write parallel tests in soapui and need to transfer properties between
I'm trying to write byte 0xff to the parallel port at 0x378 . It
I'm trying to write dynamic method that calls Parallel.ForEach. I have checked a IL
Does PHP has access to the parallel ? I'm trying to write data to
I am trying to write a C++ program that takes the following inputs from
I trying to write a drupal module. I'm following book Learning Drupal 6 Module
I referred to this article http://www.codeproject.com/KB/security/DotNetCrypto.aspx and I am trying write an encrypted string
I am trying to read and write to the parallel port, I implemented the
I'm trying to write a simple generic parallel code for minimizing a function in
I'm trying write a query to find records which don't have a matching record

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.