c++ - array_view.synchronize_asynch 会等待 parallel_for_each 完成吗?

标签 c++ c++-amp

如果我有一个 concurrency::array_viewconcurrency::parallel_for_each 循环中运行,我的理解是我可以在 CPU 上继续其他任务,同时循环正在执行:

using namespace Concurrency;

array_view<int> av;
parallel_for_each(extent<1>(number),[=](index<1> idx)
{
  // do some intense computations on av
}

// do some stuff on the CPU while we wait

av.synchronize(); // wait for the parallel_for_each loop to finish and copy the data

但是,如果我不想等待并行 for 循环,而是尽快开始从 GPU 复制数据, 怎么办?以下是否有效?

using namespace Concurrency;

array_view<int> av;
parallel_for_each(extent<1>(number),[=](index<1> idx)
{
  // do some intense computations on av
}

// I know that we won't be waiting to synch when I call this, but will we be waiting here
// until the data is available on the GPU end to START copying?
completion_future waitOnThis = av.synchronize_asynch();

// will this line execute before parallel_for_each has finished processing, or only once it
// has finished processing an the data from "av" has started copying back?

completion_future.wait();

我在 The Moth 上读到了这个话题, 但在阅读以下内容后,我并没有真正变得更聪明:

Please note that the parallel_for_each executes as if synchronous to the calling code, but in reality, it is asynchronous. I.e. once the parallel_for_each call is made and the kernel has been passed to the runtime, the some_code_B region continues to execute immediately by the CPU thread, while in parallel the kernel is executed by the GPU threads. However, if you try to access the (array or array_view) data that you captured in the lambda in the some_code_B region, your code will block until the results become available. Hence the correct statement: the parallel_for_each is as-if synchronous in terms of visible side-effects, but asynchronous in reality.

最佳答案

我不喜欢这种解释方式。更好的思考方式是 parallel_for_each 队列对 GPU 起作用,因此它几乎立即返回。在排队的工作完成之前,您的 CPU 端代码可以通过多种方式阻塞,例如,显式调用 synchronize,或从使用的 array_view 实例之一访问数据在 parallel_for_each

using namespace concurrency;

array_view<int> av;
parallel_for_each(extent<1>(number),[=](index<1> idx)
{
  // Queue (or schedule if you like) some intense computations on av
}

主机代码现在可以执行了。 AMP 计算可能已经开始也可能还没有开始。如果这里的代码访问av,那么它将阻塞直到GPU上的工作完成并且av中的数据已经写入并且可以与主机内存同步。

这是一个 future ,所以它也是一个计划任务。不能保证 在任何特定点执行。如果它被调度,那么它将阻塞它正在运行的线程,直到 av 与主机内存正确同步(如上所述)。

completion_future waitOnThis = av.synchronize_asynch();

更多主机代码可以在这里执行。如果主机代码访问 av,那么它将阻塞直到 parallel_for_each 完成(如上所述)。在某个时候,运行时将执行 future 并阻塞,直到 av 与主机内存同步。如果它是可写的并且已被更改,那么它将被复制回主机内存。

completion_future.wait();

wait 的调用将阻塞,直到 future 完成(在调用 wait 之前,不能保证任何事情都已实际执行)。此时可以保证 GPU 计算已完成,av 可以在 CPU 上访问。

话虽如此,添加 waitOnThis future 似乎过于复杂了。

array_view<int> av;
parallel_for_each(extent<1>(number),[=](index<1> idx)
{
  // do some intense computations on av on the GPU
}

// do some independent CPU computation here.

av.synchronize();

// do some computation on the CPU that relies on av here.

MSDN 文档在这个主题上不是很好。以下blog post更好。在同一个博客上还有一些关于异步 API 的其他帖子。

关于c++ - array_view.synchronize_asynch 会等待 parallel_for_each 完成吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19830470/

相关文章:

c++ - 使用 C++-AMP 减少未排序 vector 搜索的运行时间

c++ - 在 gotoxy(80, 25) 位置写一个 char 而不将光标移动到新行

C++ stringstream 返回额外的字符?

c++ - csound 是否能够从互联网上提取简单的信息/数据?

c++ - 如何使用 POSIX 在 C++ 中执行命令并获取命令的输出?

c++ - 我可以将多少内存声明为tile_static?

c++ - 在 Pro*C 中使用 LIKE

c++ - 在 MSVC++ 2012 下使用 C++Amp 将程序与库链接时未解析的符号

c++ - 使用受限函数时的尾随返回类型问题

c++ - 如何同时使用 parallel_for_each 函数和 __declspec(dllexport) 属性