I have seen structures passed to Cuda global functions, but was wondering if it was possible to do the same with thrust functors.
For example, if I wanted a functor that took a pointer to a structure, and used the structure to access data, is that possible? I have tried and failed.
Example Code:
struct A {
double var = 1.0;
};
A a_struct;
host device
struct functor {
A* a_ptr;
functor(A* _a_ptr):
a_ptr(_a_ptr) {}
device
void operator() (const input_type& input_name ){
//do stuff using a_ptr to access var and modify it.
}
};
I have been using thrust custom functors and thrust::raw_pointer_cast with device_vectors and the .data() method, but it failed with structures.
I tried passing different pointers (device_ptr to struct A) and using thrust::raw_pointer_case(&dev_ptr[0]) to input data to the functor but that failed as well.
Any direction or advice would be greatly appreciated.
Thanks for the link.
I see how you can pass structures through the operator, but I was wondering about using functor arguments.
For example, given two device_vectors and an add functor, we could access elements:
thrust::device_vector vct_a;
thrust::device_vector vct_b;
vct_a.resize(1);
vct_a.resize(1);
vct_a[0] = 1.0;
vct_b[0] = 2.0;
struct add {
unsigned* vct_a_addr;
unsigned* vct_b_addr;
host device
add(unsigned* _vct_a_addr,
unsigned* _vct_b_addr) :
vct_a_addr(_vct_a_addr),
vct_b_addr(_vct_b_addr){}
device
unsigned operator()(const T& t) {
//access vct_a
unsigned var_a = vct_a_addr[0];
unsigned var_b = vct_b_addr[0];
return (var_a + var_b);
}
}
We could use the functor with the call:
add(thrust::raw_pointer_cast(vct_a.data()),
thrust::raw_pointer_cast(vct_a.data()));
I am trying to do the same with a structure instead of a device vector.
Functors like the following compile, but I do not know how to pass a the pointer correctly to access the elements of A if I do not pass them as above.
For example, the following compiles but fails when I access A:
struct A {
thrust::device_vector vec;
}
struct functor {
A* a_ptr;
host device
functor(A* _a_ptr):
a_ptr(_a_ptr) {}
device
void operator() (const input_type& input_name ){
//do stuff using a_ptr to access var and modify it.
unsigned temp = A.vec[0];//does not work
}
};