Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a call to "synchronize()" not necessary in C++ AMP?

Tags:

c++

c++-amp

Background: For a C++ AMP overview, see Daniel Moth's recent BUILD talk.

Going through the initial walk-throughs here, here, here, and here.

Only in that last reference do they make a call to array_view.synchronize().

In these simple examples, is a call to synchronize() not needed? When is it safe to exclude? Can we trust parallel_for_each to behave "synchronously" without it (w/r/t the proceeding code)?

like image 736
lightw8 Avatar asked Dec 02 '22 01:12

lightw8


2 Answers

Use synchronize() when you want to access the data without going through the array_view interface. If all of your access to the data uses array_view operators and functions, you don't need to use synchronize(). As Daniel mentioned, the destructor of an array_view forces a synchronize as well, and it's better to call synchronize() in that case so you can get any exceptions that might be thrown.

The synchronize function forces an update to the buffer within the calling context -- that is if you write data on the GPU and then call synchronize in CPU code, at that point the updated values are copied to CPU memory.

This seems obvious from the name, but I mention it because other array_view operations can cause a 'synchronize' as well. C++ AMP array_view tries it best to make copying between the CPU and GPU memory implict -- any operation which reads data through the array view interface will cause a copy as well.

std::vector<int> v(10);
array_view<int, 1> av(10, v);
parallel_for_each(av.grid, [=](index<1> i) restrict(direct3d) {
   av[i] = 7;
}
// at this point, data isn't copied back
std::wcout << v[0]; // should print 0

// using the array_view to access data will force a copy
std::wcout << av[0]; // should print 7

// at this point data is copied back
std::wcout << v[0]; // should print 7
like image 86
Ryan Nowak Avatar answered Dec 05 '22 18:12

Ryan Nowak


my_array_view_instance.synchronize is not required for the simple examples I showed because the destructor calls synchronize. Having said that, I am not following best practice (sorry), which is to explicitly call synchronize. The reason is that if any exceptions are thrown at that point, you would not observe them if you left them up to the destructor, so please call synchronize explicitly.

Cheers

Daniel

like image 25
Daniel Moth Avatar answered Dec 05 '22 16:12

Daniel Moth