I'm using thrust to deal with GPU arrays. Here is a toy code to demonstrate the problem:
Код: Выделить всё
$ cat ss.cu #include #include #include #include struct A { float p; A(float p) : p(p) {} template __host__ __device__ void func(T& con) { /// complie error /*thrust::for_each(con.begin(),con.end(), [p]__host__ __device__(float& val) { val +=p; });*/ /// runtime error : illegal memory access thrust::for_each(con.begin(),con.end(), [this]__host__ __device__(float& val) { val +=p; }); /// An ugly work around /*float P = this->p; thrust::for_each(con.begin(),con.end(), [P]__host__ __device__(float& val) { val +=P; });*/ } }; int main() { thrust::device_vector dvec(100); A a(10); a.func(dvec); thrust::copy(dvec.begin(),dvec.end(), std::ostream_iterator(std::cout," ")); std::cout
Источник: [url]https://stackoverflow.com/questions/78127707/using-thrust-how-can-a-lambda-expression-inside-a-struct-class-capture-the-memb[/url]
Мобильная версия