hcc icon indicating copy to clipboard operation
hcc copied to clipboard

Inconsistencies of hc::array vs hc::array_view

Open misos1 opened this issue 7 years ago • 1 comments

Seems hc::array_view can be captured into kernel only by value:

hc::array_view<int> arr(1);
parallel_for_each(hc::extent<1>(1), [=](hc::index<1> i) [[hc]]
{
	arr[0] = 10;
});
printf("%i\n", arr[0]);

But hc::array only by reference:

hc::array<int> arr(1);
parallel_for_each(hc::extent<1>(1), [&](hc::index<1> i) [[hc]]
{
	arr[0] = 10;
});
printf("%i\n", arr.get_cpu_access_type());
//printf("%i\n", arr[0]); // segmentation fault

That has some logic. Array by value would mean deep copy (but with more complex code with kernel arguments, arrays and array views this needs to explicitly specify any captured hc::array in capture list to be captured by reference).

But seems in code above hc::array is not accessible from cpu nevertheless that arr.get_cpu_access_type() returns access_type_read_write (3).

After some magic it can be made available from cpu (this works even when I specify access_type_none in arr constructor):

hc::array<int> arr(1);
parallel_for_each(hc::extent<1>(1), [&](hc::index<1> i) [[hc]]
{
	arr[0] = 10;
});
(hc::array_view<int>(arr))[0];
printf("%i\n", arr[0]);

Actually (hc::array_view<int>(arr))[0] will call get_cpu_access on hc::array::m_device and same effect can be achieved by calling arr.internal().get_cpu_access(). Maybe this should does access to hc::array::operator[] like hc::array_view::operator[] does? (Of course only in case when we have cpu access.) Or if not implicit then at least some explicit way like hc::array_view::synchronize() method does (which calls get_cpu_access()). Or it is intended to access gpu allocated hc::array only through hc::array_view from cpu?

So hc::array_view behaves more consistently but hc::array has advantage that it can provide cpu accessible pointer by hc::array::data() also for arrays with rank > 1 unlike hc::array_view.

misos1 avatar Aug 12 '18 14:08 misos1

I had noticed some difficulties using "array" which didn't happen for "array_view" - I'm not so certain of the difference in use case, perhaps @AlexVlx can expand on it.

PhilipDeegan avatar Aug 23 '18 13:08 PhilipDeegan