问题
I've enocuntered a very strange exception when writting code in C++Amp. I define two concurrency::array objects as follows:
concurrency::array<float, 2> img_amp_data(11, 11, image_data.begin());
concurrency::array<float> a_amp_result(121, empty_vec.begin());
When I want to access the elements of the first of them
std::cout << img_amp_data[0][0] << std::endl;
everything runs properly, but when I want to access the second one
std::cout << a_amp_result[0] << std::endl;
I get a following exception:
Exception: (The array is not accessible on CPU)
It is a very odd situatuon because I can access 2+ dimensional arrays and it is forbidden to access only one dimensional array? Any ideas?
回答1:
You are running into a syntax quirk.
For an array
of rank larger than 1, operator[]
with integral parameter returns an array_view
referring to the slice of the original array
. The subsequent operator[]
operates on the array_view
object, which is allowed on the host -- and in your case causes an implicit data copy before returning a reference to the element.
For an array
of rank 1 operator[]
with integral parameter, or operator[]
with index
parameter for any array
, return a reference to the element. Both are allowed only on the location where the array
is resident - by default it is the default accelerator_view
, however you can request to create the array
in the CPU memory as well.
Unless you want to have fine-grained control on the data movement between the host and the accelerator_view
, it is suggested to use the array_view
type ubiquitously.
来源:https://stackoverflow.com/questions/21147991/behavior-of-operator-on-1d-and-2d-arrays-in-c-amp