2

I am using THRUST to support STL in device code. I declared device vectors and when I am using functions like push_back() in device code it is showing an error.

CODE:

#include <thrust/device_vector.h>
using namespace thrust;

__global__ void kernel_fillholes_g(uchar *M, bool * visited, int x, int y)
{
   int i = blockIdx.x * blockDim.x + threadIdx.x;
   int j = blockIdx.y * blockDim.y + threadIdx.y;    

   if (M[i*x + j] && !visited[i*x + j] && (i==0 || j==0 || i==x-1 || j==y-1)) 
   {
       M[i*x + j] = 0;              
       s1.push_back(i);
       s2.push_back(j);
       //BFS_g(M, visited, s1, s2, x, y);
   }        
}

void func(uchar *M, bool * visited,)
{
   //
   copy M and bool to device memory
   //
   dim3 block1(BLOCK_SIZE_X, BLOCK_SIZE_Y);
   dim3 grid1(size_x/BLOCK_SIZE_X, size_y/BLOCK_SIZE_Y);

   device_vector <int> s1;
   device_vector <int> s2;
   kernel_fillholes_g<<<grid1, block1>>>(M, visited, s1, s2);
}

Error:

error : calling a __host__ function("thrust::detail::vector_base<int,thrust::device_malloc_allocator<int> > ::push_back") 
from a __global__ function("kernel_fillholes_g") is not allowed

I am new to THRUST library. Please tell me how to use functions similar to push_back() in device code. I searched for examples but couldn't find one.

Jared Hoberock
  • 11,118
  • 3
  • 40
  • 76
Gaurav
  • 75
  • 9
  • 2
    You couldn't find such an example because what you are trying to do is not possible – talonmies Jul 28 '14 at 08:04
  • 1
    This [question/answer](http://stackoverflow.com/questions/21786495/cuda-kernel-returning-vectors) may be of interest, if you want to "roll your own". – Robert Crovella Jul 28 '14 at 15:21

0 Answers0