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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:40+00:00 2026-06-12T22:07:40+00:00

I wrote a simple CUDA kernel for Box Filtering of an image. texture<unsigned char,2>

  • 0

I wrote a simple CUDA kernel for Box Filtering of an image.

texture<unsigned char,2> tex8u;

#define FILTER_SIZE 7
#define FILTER_OFFSET (FILTER_SIZE/2)

__global__ void box_filter_8u_c1(unsigned char* out, int width, int height, int pitch)
{
   unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
   unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;

   if(x>=width || y>=height)    return;

   float val = 0.0f;

   for(int i = -FILTER_OFFSET; i<= FILTER_OFFSET; i++)
     for(int j= -FILTER_OFFSET; j<= FILTER_OFFSET; j++)
        val += tex2D(tex8u,x + i, y + j);

   out[y * pitch + x] = static_cast<unsigned char>(val/(FILTER_SIZE  * FILTER_SIZE));   

}

The problem with the above code was that the top and left borders of the image were filtered incorrectly. They contained values from the bottom and right borders respectively. Width of the incorrect borders was equal to FILTER_OFFSET.

But when I changed the x and y indices to intinstead of unsigned int, the output was perfect.

Question: Why is it so?

P.S: The texture addressing mode is set to cudaAddressModeClamp for both x and y directions.

  • 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-12T22:07:41+00:00Added an answer on June 12, 2026 at 10:07 pm

    The root cause of this has nothing to do with CUDA, it is basic C type conversion rules which are causing the result you see. The C99 standard says the following about how the conversion is performed:

    6.3.1.8 Usual arithmetic conversions

    1. If both operands have the same type, then no further conversion is needed.
    2. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer
      conversion rank is converted to the type of the operand with greater
      rank.
    3. Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then
      the operand with signed integer type is converted to the type of the
      operand with unsigned integer type.
    4. Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned
      integer type, then the operand with unsigned integer type is converted
      to the type of the operand with signed integer type.
    5. Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

    The third point implies that the signed integer (so i and j in this case) is first converted to an unsigned integer and the added to the unsigned integer (x and y). The result of converting a negative signed integer to an unsigned integer is implementation specific, but here, a straightforward two’s complement representation will turn a small negative integer into a very large unsigned integer. The read mode of your texture clamps this out-of-range coordinate to the maximum allowable in the texture, and your kernel winds up reading from the wrong side of the texture.

    If you use signed integers, no conversion occurs, and this whole problem disappears. The moral of this story is probably “know thy programming language”.

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

Sidebar

Related Questions

I wrote simple kernel to test functionality of CUDA __syncthreads . In kernel I've
I wrote simple raytracer in C++ and I want to add CUDA to the
I have wrote simple macro to define interactive function and bind it to key
Wrote a simple program that calls ls, then passes each line through regexp filtering
i wrote simple 3 functions to scrape titles , description and keywords of simple
I wrote simple class that on the start it just increase the value of
I wrote simple load testing tool for testing performance of Java modules. One problem
I wrote a simple XML file and a DTD file including an entity, but
I wrote a simple function, that makes emacs add matching quotes (so when I
I wrote a simple plugin which sets some css code using wp_options. It all

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.