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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:29:39+00:00 2026-06-15T18:29:39+00:00

I’m trying very hard to make a code run in parallel (CUDA). The code,

  • 0

I’m trying very hard to make a code run in parallel (CUDA). The code, simplified, is:

float sum = ...         //sum = some number
for (i = 0; i < N; i++){
    f = ...             // f = a function that returns a float and puts it into f
    sum += f;
}

The issue I’m having is sum+=f because it requires sum to be shared between the threads. I tried using the __shared__ argument when declaring sum (__shared__ float sum), but that didn’t work (it didn’t give me the proper result). I’ve also heard about reduction (and know how to use it on OpenMP), but have no idea how to apply it here.

Any help would be greatly appreciated. Thanks!

  • 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-15T18:29:41+00:00Added an answer on June 15, 2026 at 6:29 pm

    Here is the code:

    #include <stdio.h>
    __global__ void para(float* f, int len) {
        int i = threadIdx.x + blockDim.x * blockIdx.x;
        if (i < len ){
            // calculate f[i], f in iteration ith
            f[i] = i;
        }
    }
    
    int main(int argc, char ** argv) {
        int inputLength=1024;
        float * h_f;
        float * d_f;
        int size = inputLength*sizeof(float);
    
        h_f = (float *) malloc(size);
        cudaMalloc((void**)&d_f , size);
        cudaMemcpy(d_f, h_f, size, cudaMemcpyHostToDevice);
    
        dim3 DimGrid((inputLength)/256 +1 , 1 , 1);
        dim3 DimBlock(256 , 1, 1);
    
        para<<<DimGrid , DimBlock>>>(d_f , inputLength);
        cudaThreadSynchronize();
    
        cudaMemcpy(h_f, d_f, size , cudaMemcpyDeviceToHost);
    
        cudaFree(d_f);
    
        // do parallel reduction
        int i;
        float sum=0;
        for(i=0; i<inputLength; i++)
            sum+=h_f[i];
    
        printf("%6.4f\n",sum);
    
        free(h_f);
    
        return 0;
    }
    

    The parallel reduction section can be replaced by a working CUDA parallel sum reduction (for example this one). Soon I will take time to change it.

    EDIT:

    Here is the code for performing parallel reduction with CUDA:

    #include <stdio.h>
    __global__ void para(float* f, int len) {
        int i = threadIdx.x + blockDim.x * blockIdx.x;
        if (i < len ){
            // calculate f[i], f in iteration ith
            f[i] = i;
        }
    }
    __global__ void strideSum(float *f, int len, int strid){
        int i = threadIdx.x + blockDim.x * blockIdx.x;
        if(i+strid<len){
            f[i]=f[i]+f[i+strid];
        }
    }
    
    #define BLOCKSIZE 256
    int main(int argc, char ** argv) {
        int inputLength=4096;
        float * h_f;
        float * d_f;
        int size = inputLength*sizeof(float);
    
        h_f = (float *) malloc(size);
        cudaMalloc((void**)&d_f , size);
        cudaMemcpy(d_f, h_f, size, cudaMemcpyHostToDevice);
    
        dim3 DimGrid((inputLength)/BLOCKSIZE +1 , 1 , 1);
        dim3 DimBlock(BLOCKSIZE , 1, 1);
    
        para<<<DimGrid , DimBlock>>>(d_f , inputLength);
        cudaThreadSynchronize();
    
        int i;
        float sum=0, d_sum=0;
        // serial sum on host. YOU CAN SAFELY COMMENT FOLLOWING COPY AND LOOP. intended for sum validity check.
        cudaMemcpy(h_f, d_f, size , cudaMemcpyDeviceToHost);
        for(i=0; i<inputLength; i++)
            sum+=h_f[i];
    
        // parallel reduction on gpu
        for(i=inputLength; i>1; i=i/2){
            strideSum<<<((i/BLOCKSIZE)+1),BLOCKSIZE>>>(d_f,i,i/2);
            cudaThreadSynchronize();
        }
        cudaMemcpy(&d_sum, d_f, 1*sizeof(float) , cudaMemcpyDeviceToHost);
    
        printf("Host -> %6.4f, Device -> %6.4f\n",sum,d_sum);
    
        cudaFree(d_f);
        free(h_f);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words

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.