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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:12:47+00:00 2026-06-13T23:12:47+00:00

I have two arrays, a and b, and I would like to compute the

  • 0

I have two arrays, a and b, and I would like to compute the “min convolution” to produce result c. Simple pseudo code looks like the following:

for i = 0 to size(a)+size(b)
    c[i] = inf
    for j = 0 to size(a)
        if (i - j >= 0) and (i - j < size(b))
            c[i] = min(c[i], a[j] + b[i-j])

(edit: changed loops to start at 0 instead of 1)

If the min were instead a sum, we could use a Fast Fourier Transform (FFT), but in the min case, there is no such analog. Instead, I’d like to make this simple algorithm as fast as possible by using a GPU (CUDA). I’d be happy to find existing code that does this (or code that implements the sum case without FFTs, so that I could adapt it for my purposes), but my search so far hasn’t turned up any good results. My use case will involve a’s and b’s that are of size between 1,000 and 100,000.

Questions:

  • Does code to do this efficiently already exist?

  • If I am going to implement this myself, structurally, how should the CUDA kernel look so as to maximize efficiency? I’ve tried a simple solution where each c[i] is computed by a separate thread, but this doesn’t seem like the best way. Any tips in terms of how to set up thread block structure and memory access patterns?

  • 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-13T23:12:47+00:00Added an answer on June 13, 2026 at 11:12 pm

    An alternative which might be useful for large a and b would be to use a block per output entry in c. Using a block allows for memory coalescing, which will be important in what is a memory bandwidth limited operation, and a fairly efficient shared memory reduction can be used to combine per thread partial results into a final per block result. Probably the best strategy is to launch as many blocks per MP as will run concurrently and have each block emit multiple output points. This eliminates some of the scheduling overheads associated with launching and retiring many blocks with relatively low total instruction counts.

    An example of how this might be done:

    #include <math.h>
    
    template<int bsz>
    __global__ __launch_bounds__(512)
    void minconv(const float *a, int sizea, const float *b, int sizeb, float *c)
    {
        __shared__ volatile float buff[bsz];
        for(int i = blockIdx.x; i<(sizea + sizeb); i+=(gridDim.x*blockDim.x)) {
            float cval = INFINITY;
            for(int j=threadIdx.x; j<sizea; j+= blockDim.x) {
                int t = i - j;
                if ((t>=0) && (t<sizeb))
                    cval = min(cval, a[j] + b[t]);
            }
            buff[threadIdx.x] = cval; __syncthreads();
            if (bsz > 256) {
                if (threadIdx.x < 256) 
                    buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+256]);
                __syncthreads();
            }
            if (bsz > 128) {
                if (threadIdx.x < 128) 
                    buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+128]); 
                __syncthreads();
            }
            if (bsz > 64) {
                if (threadIdx.x < 64) 
                    buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+64]);
                __syncthreads();
            }
            if (threadIdx.x < 32) {
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+32]);
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+16]);
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+8]);
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+4]);
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+2]);
                buff[threadIdx.x] = min(buff[threadIdx.x], buff[threadIdx.x+1]);
                if (threadIdx.x == 0) c[i] = buff[0];
            }
        }
    }
    
    // Instances for all valid block sizes.
    template __global__ void minconv<64>(const float *, int, const float *, int, float *);
    template __global__ void minconv<128>(const float *, int, const float *, int, float *);
    template __global__ void minconv<256>(const float *, int, const float *, int, float *);
    template __global__ void minconv<512>(const float *, int, const float *, int, float *);
    

    [disclaimer: not tested or benchmarked, use at own risk]

    This is single precision floating point, but the same idea should work for double precision floating point. For integer, you would need to replace the C99 INFINITY macro with something like INT_MAX or LONG_MAX, but the principle remains the same otherwise.

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

Sidebar

Related Questions

I have two arrays that I would like to compare and ultimately wind up
I have two arrays and I would like to append one to the end
I have two arrays of x - y coordinates, and I would like to
I have two arrays of structs. array_of_structs1 array_of_structs2 The struct class looks like this,
I have two arrays of values that I would like to combine - but
I have two arrays of objects. I would like iterate over each of these
I have two arrays of strings that I would like to compare for equality:
I have two Arrays of strings, and I would like to find the set
I have two arrays of data that I would like to display in an
I have two arrays @input0 and @input1 . I would like a for loop

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.