【发布时间】:2013-01-09 19:07:14
【问题描述】:
由于 C++11 没有 future.then,因此我开始使用 Microsoft PPL 库中的 concurrency::task。它在大多数情况下都很好用。
但是,现在我正在使用 GPGPU,因此在 PPL 调度程序中安排 .then 延续会导致 GPU 空闲时出现不必要的延迟。
我的问题是concurrency::task 和concurrency::task::then 是否有任何可能的解决方法让它们直接执行。
据我了解,由于缓存效率的原因,定期安排的任务在大多数情况下会立即继续执行。但是,对于使用 concurrency::task_completion_event 从显式线程(即 GPU 线程)调度的任务,情况并非如此。
我正在做的一个例子:
template<typename F>
auto execute(F f) -> concurrency::task<decltype(f())>
{
concurrency::task_completion_event<decltype(f())> e;
gpu_execution_queue_.push([=]
{
try
{
e.set(copy(f())); // Skipped meta-template programming for void.
}
catch(...)
{
e.set_exception(std::current_exception());
}
});
// Any continuation will be delayed since it will first be
// enqueued into the task-scheduler.
return concurrency::task<decltype(f())>(std::move(e));
}
void foo()
{
std::vector<char> data /* = ... */;
execute([=]() -> texture
{
return copy(data)
})
.then(concurrency::task<texture> t)
{
return execute([=]
{
render(t.get());
});
})
.get();
}
【问题讨论】:
-
您的问题似乎源于需要多个任务队列,这些任务队列代表不同类型的计算资源(CPU 与 GPU),具体取决于任务类型(要执行的代码)。在 PPL 中,这将涉及使用 Scheduler class。
标签: c++ visual-studio-2012 task future ppl