title: Error: an illegal memory access was encountered
body: <p>I had asked doubt <a href="https://stackoverflow.com/questions/41440428/error-calling-a-host-function-from-a-global-function-is-not-allowed/41440734#41440734">error: calling a __host__ function from a __global__ function is not allowed</a> and i got the ans . accordingly i have modified my code bt i am unable to access d_point[i]. how can i access that.</p>
  
  <pre><code>  __global__ void densefun(int *d_counters,float2 *d_points,int d_x_max,int d_y_max,int width,int height, int min_distance,int size)
  {
    int i = blockDim.x * blockIdx.x + threadIdx.x;
  
     if(i &lt;= size)
     {
          float2 point = (d_points)[i];
          int x = floorf(point.x);
          int y = floorf(point.y);
          printf(" ( %d  %d )",x,y);
          if(x &lt; d_x_max &amp;&amp; y &lt; d_y_max)
          {  
              x /= min_distance;
              y /= min_distance;
              (d_counters)[y*width+x]++;
              __syncthreads();
          }
      }
  }
  
  
  void DenseSample(const Mat&amp; grey, std::vector&lt;Point2f&gt;&amp; points, const double quality, const int min_distance)
  {
      int width = grey.cols/min_distance;
      int height = grey.rows/min_distance;
      Mat eig;
      cornerMinEigenVal(grey, eig, 3, 3);
      double maxVal = 0;
      minMaxLoc(eig, 0, &amp;maxVal);
      const double threshold = maxVal*quality;
      std::vector&lt;int&gt; counters(width*height);
      int x_max = min_distance*width;
      int y_max = min_distance*height; 
      printf("in descriptor size:%ld ",points.size());   
      int *d_counters;
      float2 *d_points;
      cudaMalloc(&amp;d_counters,counters.size()*width*height*sizeof(int));
      printf("in cuda point size:%d       ",points.size());
      cudaMalloc(&amp;d_points,points.size()*sizeof(float2));
      cout&lt;&lt;"points.size() : "&lt;&lt;points.size()&lt;&lt;endl;
      cudaMemcpy(d_points, &amp;points, points.size()*sizeof(float2), cudaMemcpyHostToDevice);
      int blk=cvFloor(points.size()/1024)+1;
      cout&lt;&lt;"blk : "&lt;&lt;blk&lt;&lt;endl;
      if(points.size()&gt;0)
      {
          densefun&lt;&lt;&lt;blk,1024&gt;&gt;&gt;(d_counters,d_points,x_max,y_max,width,height,min_distance, points.size());
          cudaError_t err = cudaGetLastError();
          if (err != cudaSuccess) 
          printf("Error: %s\n", cudaGetErrorString(err));
          cudaMemcpy(&amp;counters, d_counters, counters.size()* width*height*sizeof(int), cudaMemcpyDeviceToHost);
      }
      cudaFree(d_counters);
      cudaFree(d_points);
      points.clear();
      int index = 0;
      int offset = min_distance/2;
      for(int i = 0; i &lt; height; i++)
      for(int j = 0; j &lt; width; j++, index++) 
      {
          if(counters[index] &lt;= 0)
          {
              int x = j*min_distance+offset;
              int y = i*min_distance+offset;
              if(eig.at&lt;float&gt;(y, x) &gt; threshold)
              points.push_back(Point2f(float(x), float(y)));
          }
      }
  }
  </code></pre>
  
  <p>output is:
  in descriptor size:1605       in cuda point size:1605       points.size() : 1605</p>
  
  <p>blk : 2
  Error: an illegal memory access was encountered
  in descriptor size:918       in cuda point size:918       points.size() : 918</p>
  
  <p>blk : 1
  Error: an illegal memory access was encountered</p>