Quantcast
Channel: Parallel Computing in C++ and Native Code forum
Viewing all articles
Browse latest Browse all 856

How do I declare the type of a amp-restricted lambda?

$
0
0

I want to define a lambda which gets called by the lambda passed to a "parallel_for_each" function call. For example, I can define locally a lambda "test" and capture that in the lambda passed to parallel_for_each in the following code.

void test1()
{
	int size = 10;
	int* visited = new int[size];
	for (int i = 0; i < size; ++i) visited[i] = 0;
	extent<1> data_sz(size);
	array_view<int, 1> a(data_sz, &visited[0]);

	auto test = [=](int t) restrict(amp) -> bool
	{
		return t == 1;
	};

	parallel_for_each(extent<1>(size), [=](index<1> idx) restrict(amp)
	{
		int i = idx[0];
		a[i] = i + ((test(i)) ? 1 : 0);
	});

	a.synchronize();
	for (int i = 0; i < size; ++i) printf("%d\n", a[i]);
}

Notice I declared the lambda "test" using "auto". The compiler magically resolves the type of the lambda "test" and reconciles it with the use in the kernel passed to parallel_for_each.

What I want to do is to actually define the type of the lambda. For example, I could try--and fail--with the following code:

	const std::function<int(int)> test = [=](int t) restrict(amp) -> int
	{
		return t + 1;
	};

Notice the only difference between is the use of "std::function<>" for the type of variable "test" instead of auto.  The assignment compiles if I remove "restrict(amp)" from the lambda declaration, but then won't compile the parallel_for_each later on in the code.

I can pass the function test() as a parameter by defining a templated function, e.g.:

template<typename _K>
void test3aux(const _K & f) ...

but that cleverly avoids the question: what is the type of the variable "test" in the 1st example so I can actually use the type elsewhere?

Ken Domino


Viewing all articles
Browse latest Browse all 856

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>