Hello,
I want to move nvstd::function-objects to another other class, where the nvstd::function-objects should be executed. I wrote an example, that my problem becomes more clear:
[Code-Description:]
The GPUClass process the nvstd::function-object by using the global kernel-method to call the nvstd::function-object on the GPU/device. In main, I create a nvstd::function-object with a lambda expression and give it into the GPUClass-object.
[Code:]
template<class F>
_global_
void kernel(F f) {
f();
}
class GPUClass {
public:
template<class K>
void processLambda(K func_gpu){
kernel<<<1,1>>>(func_gpu);
}
};
int main() {
nvstd::function<void(void)> func = [=]__device__(){printf("On GPU!\n");};
GPUClass* gpuclass = new GPUClass();
gpuclass->processLambda(func);
return 0;
}
[Problem:]
The Example doesnt output “On GPU!\n”. I think, its because the first line in main():
nvstd::function<void(void)> func = [=]__device__
(){printf(“On GPU!\n”);};
The nvstd::function has no __device__
part and because of that the function is managed as a host-function and cant processed at the device. But when I add the __device__
part, than I get the following error-message:
error: an automatic __device__
variable declaration is not allowed inside a host function body
[Question:]
Is there a way that I can move a lambda expression to another class, where this lambda expression get processed at the device?
Best Greetings,
Tobi