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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:21:16+00:00 2026-05-19T12:21:16+00:00

I have some code that I want to make into a cuda kernel. Behold:

  • 0

I have some code that I want to make into a cuda kernel. Behold:

    for (r = Y; r < Y + H; r+=2)
    {
        ch1RowSum = ch2RowSum = ch3RowSum = 0;
        for (c = X; c < X + W; c+=2)
        {
            chan1Value = //some calc'd value
                            chan3Value = //some calc'd value
            chan2Value = //some calc'd value
            ch2RowSum  += chan2Value;
            ch3RowSum  += chan3Value;
            ch1RowSum  += chan1Value;
        }
        ch1Mean += ch1RowSum / W;
        ch2Mean += ch2RowSum / W;
        ch3Mean += ch3RowSum / W;
    }

Should this be split up into two kernels, one to calculate the RowSums and one to calculate the Means, and how should I handle the fact that my loop indices dont start at zero and end at N?

  • 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-19T12:21:17+00:00Added an answer on May 19, 2026 at 12:21 pm

    Let’s suppose you have a kernel that computes the three values. Each thread in your configuration will compute the three values for each (r,c) pair.

    __global__ value_kernel(Y, H, X, W)
    {
        r = blockIdx.x + Y;
        c = threadIdx.x + W;
    
        chan1value = ...
        chan2value = ...
        chan3value = ...
    }
    

    I don’t believe you can calculate the sum (completely in parallel, at least) in the above kernel. You won’t be able to use += like you have above. You could put it all in one kernel if you have only one thread in each block (row) do the sum and mean, like this…

    __global__ both_kernel(Y, H, X, W)
    {
        r = blockIdx.x + Y;
        c = threadIdx.x + W;
    
        chan1value = ...
        chan2value = ...
        chan3value = ...
    
        if(threadIdx.x == 0)
        {
            ch1RowSum = 0;
            ch2RowSum = 0;
            ch3RowSum = 0;
    
            for(i=0; i<blockDim.x; i++)
            {
                ch1RowSum += chan1value;
                ch2RowSum += chan2value;
                ch3RowSum += chan3value;
            }
    
            ch1Mean = ch1RowSum / blockDim.x;
            ch2Mean = ch2RowSum / blockDim.x;
            ch3Mean = ch3RowSum / blockDim.x;
        }
    }
    

    but it’s probably better to use the first value kernel and then a second kernel for both sums and means… It’s possible to further parallelize the kernel below, and if it’s separate you can focus on that when you’re ready.

    __global__ sum_kernel(Y,W)
    {
        r = blockIdx.x + Y;
    
        ch1RowSum = 0;
        ch2RowSum = 0;
        ch3RowSum = 0;
    
        for(i=0; i<W; i++)
        {
            ch1RowSum += chan1value;
            ch2RowSum += chan2value;
            ch3RowSum += chan3value;
        }
    
        ch1Mean = ch1RowSum / W;
        ch2Mean = ch2RowSum / W;
        ch3Mean = ch3RowSum / W;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery/JavaScript code that I want to run only when there is
I have a standalone Java app that has some licensing code that I want
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general
I have some code that effectively does this : File file = new File(C:\\Program
I have some code that I am putting in the code-behind of a master
I have some code that uses the Oracle function add_months to increment a Date

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.