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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:53:31+00:00 2026-06-14T09:53:31+00:00

I am trying to compute the variance of a 2D gpu_array. A reduction kernel

  • 0

I am trying to compute the variance of a 2D gpu_array. A reduction kernel sounds like a good idea:

http://documen.tician.de/pycuda/array.html

However, this documentation implies that reduction kernels just reduce 2 arrays into 1 array. How do I reduce a single 2D array into a single value?

  • 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-14T09:53:32+00:00Added an answer on June 14, 2026 at 9:53 am

    I guess the first step is to define variance for this case. In matlab, the variance function on a 2D array returns a vector (1D-array) of values. But it sounds like you want a single-valued variance, so as others have already suggested, probably the first thing to do is to treat the 2D-array as 1D. In C we won’t require any special steps to accomplish this. If you have a pointer to the array you can index into it as if it were a 1D array. I’m assuming you don’t need help on how to handle a 2D array with a 1D index.

    Now if it’s the 1D variance you’re after, I’m assuming a function like variance(x)=sum((x[i]-mean(x))^2) where the sum is over all i, is what you’re after (based on my read of the wikipedia article ). We can break this down into 3 steps:

    1. compute the mean (this is a classical reduction – one value is produced for the data set – sum all elements then divide by the number of elements)
    2. compute the value (x[i]-mean)^2 for all i – this is an element by element operation producing an output data set equal in size (number of elements) to the input data set
    3. compute the sum of the elements produced in step 2 – this is another classical reduction, as one value is produced for the entire data set.

    Both steps 1 and 3 are classical reductions which are summing all elements of an array. Rather than cover that ground here, I’ll point you to Mark Harris’ excellent treatment of the topic as well as some CUDA sample code. For step 2, I’ll bet you could figure out the kernel code on your own, but it would look something like this:

    #include <math.h>
        __global__ void var(float *input, float *output, unsigned N, float mean){
    
          unsigned idx=threadIdx.x+(blockDim.x*blockIdx.x);
          if (idx < N) output[idx] = __powf(input[idx]-mean, 2);
        }
    

    Note that you will probably want to combine the reductions and the above code into a single kernel.

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

Sidebar

Related Questions

I am trying to compute the hash of a byte array in Java. To
I'm new to NumPy and I'm trying to compute some simple statistics like the
Im trying to compute a 24 hour rms (root mean squared) from a dataset
I'm trying to compute confidence intervals for groups with a common model using lmList
I'm trying to compute item-to-item similarity along the lines of Amazon's Customers who viewed/purchased
I'm trying to compute the average of a field over various subsets of a
I am trying to compute (360 / 24) / 60 I keep getting the
I am trying to compute and count the number of rows inside a C#
I have been trying to compute the total sum of all these ord s
I'm working on a directed network problem and trying to compute all valid paths

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.