Shared memory

Hello everbody, i’m new in CUDA and i developed a little sobel filter and i would like to optimize it.

__global__ void Sobel(int* Gray, int* Sobel, int height, int width)


    int j = blockDim.x * blockIdx.x + threadIdx.x;

    int i = blockDim.y * blockIdx.y + threadIdx.y;

int Gx = 0;

    int Gy = 0;

if ( ( i > 0 && i < height) && ( j > 0 && j < width) )


        Gx = (Gray[((i-1)*width)+(j-1)]*1)+(Gris[((i-1)*width)+(j+1)]*(-1))+(Gris[(i*width)+(j-1)]*2)+(Gris[(i*width)+(j+1)]*(-2))+(Gris[((i+1)*width)+(j-1)]*1)+(Gris[((i+1)*width)+(j+1)]*(-1));

        Gy = (Gray[((i-1)*width)+(j-1)]*1)+(Gris[((i-1)*width)+j]*2)+(Gris[((i-1)*width)+(j+1)]*1)+(Gris[((i+1)*width)+(j-1)]*(-1))+(Gris[((i+1)*width)+j]*(-2))+(Gris[((i+1)*width)+(j+1)]*(-1));

Sobel[(i*width)+j] = sqrt(powf(Gx,2)+powf(Gy,2));



I would like to use shared memory, i tried but i crash the Nvidia driver :rolleyes:

In fact i’m a little bit scared by using shared memory.

I try the solution found in the programming guide but i didnt get it …

Thank you.

I consider it a bad habit to put too many things in the same line. Not going to read that

Seriously, I think this forum should put a sticky on top to tell beginners what to do when things go wrong. For this case, please use cudaGetLastError to find out the error code and get its error string using cudaGetErrorString to see what’s exactly wrong.