I can create restrict(amp) function as follows:
auto f = [](int& item) restrict(amp) {item += 1;};
And I can use this function in other restrict(amp) functions, for example:
concurrency::parallel_for_each(av.extent,
[=](concurrency::index<1> idx) restrict(amp)
{
f(av[idx]);
}
);
What type of substituted instead “auto” after compilation? I tried to use the “std::function”:
std::function<void (int&) restrict(amp)> f
= [](int& item) restrict(amp) {item += 1;};
but received a compile error.
Thank you for your attention!
The result of a lambda expression is a closure object, and the type of the closure object is unknowable. You can only use
autoto declare a variable of its exact type.However, you can convert a closure object into a suitable instance of an
std::function, and if the lambda is non-capturing, you can even convert it to a function pointer. However, this conversion may come at a (significant) cost, so you should prefer usingautoas much as possible to handle the actual closure type.The same goes for
bindexpressions.The relevant standard section is 5.1.2(3):
That said, I’m not sure how the special AMP extensions behave in this context, and it’s conceivable that AMP-restricted lambdas are not convertible to anything else. I’ll try and look this up in the AMP specification.
Update: Sections 2.2.3 and 2.3 of the AMP Specification seem to apply to this question.