Friday, 6 September 2013

overload operator [ ] to access thrust::device_vector in a class

overload operator [ ] to access thrust::device_vector in a class

I want to overload operator [ ] to enable the following usage. It works
okay with thrust::host_vector but fails with thrust::device_vector.
usage
Matrix m = ... // initialize
std::cout << m[3] << std::endl; // access
m[3] = 0.1 // assignment
definition
double& Matrix::operator[ ](const & int i){
if (storage == HOST)
return h_data[i]; // thrust:: host_vector [Passed]
else if (storage == DEVICE)
return d_data[i]; // thrust:: device_vector [Failed here]
else
fail();
}
Error
error: initial value of reference to non-const must be an lvalue
This is strange because thrust::device_vector is assignable by index. You
can do:
thrust::device_vector<double> M(10);
M[3] = 0.1;
Thanks for your answer!!

No comments:

Post a Comment