How many times is data copied in a C++ AMP array?

57 views Asked by At

The default_cpu_access_type property for my accelerator is access_type_read_write. Let's say I run this:

std::vector<int> v{ 1, 2, 3, 4 };
Concurrency::array<int, 1> a { 4, v.begin(), v.end() };
parallel_for_each(a.extent, [=, &a](Concurrency::index<1> i) restrict(amp) { ++a[i]; });
for (int i { 0 }; i < 4; ++i) std::cout << a[i] << " ";

How many times are my four ints copied? Are they copied in RAM when the Concurrency::array is constructed? Are they copied again when the parallel_for_each loop begins, or does my graphics card somehow use the data in RAM? What if I use array_view instead of array - does that save a copy?

1

There are 1 answers

1
Esteban On

As far as I can tell there is one transfer from CPU to GPU when you declare the array and initialize it.

Then you can use it freely on the GPU side (inside the parallel_for_each loop). Array to do not contain auto-synchronisation mechanism for data so I'm not certain of what happens when you pick a value in your for loop.

If you choose array_view the synchronisation of data is implicit. One copy will happened at initialisation and one to bring data back from GPU to CPU when you pick a value.

I recommend you this blog post http://www.danielmoth.com/Blog/array-And-Arrayview-From-Amph.aspx or even the complete serie on amp.