user2933632
user2933632

Reputation: 35

opencl : atomic function slow the execution time

I am trying to detect a circle in binary image using hough transform.

the kernel code is very slow when execute. the wait time in atomic function

kernel void hough_circle(read_only image2d_t imageIn, global int* in,const int w_hough,__global        int * circle)
 {
 sampler_t sampler=CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
 int gid0 = get_global_id(0);
 int gid1 = get_global_id(1);
 uint4 pixel;
 int x0=0,y0=0,r;
 int maxval=0;
 pixel=read_imageui(imageIn,sampler,(int2)(gid0,gid1));
 if(pixel.x==255)
 {
 for(int r=20;r<150;r+=2)
 {
// int r=100;

          for(int theta=0; theta<360;theta+=2)
          {

                          x0=(int) round(gid0-r*cos( (float) radians( (float) theta) ));
                        y0=(int) round(gid1-r*sin( (float) radians( (float) theta) ));
                       if((x0>0) && (x0<get_global_size(0)) && (y0>0)&&(y0<get_global_size(1)))
                        atom_inc(&in[w_hough*y0+x0]);
          }
          if(maxval<in[w_hough*y0+x0])
          {
          maxval=in[w_hough*y0+x0];
            circle[0]=gid0;
            circle[1]=gid1;
            circle[2]=r;
          }

          }

 }

}

can any one help me what change i do to avoid that the code main.cpp and kernel.cl compress in rar file http://www.files.com/set/527152684017e use opencv lib for read and display img

Upvotes: 0

Views: 565

Answers (1)

Dithermaster
Dithermaster

Reputation: 6343

Atomic operations by their nature are very slow compared to non-atomic equivalents. You should not use them in an inner-loop.

Try to do as much as you can with local memory, and only use atomics to store final results.

Search for "parallel reduction" because this transform has a lot in common with a reduction (the output of each pixel is a sum of weights).

Upvotes: 3

Related Questions