【问题标题】:execute .then without delay立即执行 .then
【发布时间】:2013-01-09 19:07:14
【问题描述】:

由于 C++11 没有 future.then,因此我开始使用 Microsoft PPL 库中的 concurrency::task。它在大多数情况下都很好用。

但是,现在我正在使用 GPGPU,因此在 PPL 调度程序中安排 .then 延续会导致 GPU 空闲时出现不必要的延迟。

我的问题是concurrency::taskconcurrency::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


【解决方案1】:

.then 的全部意义在于执行一个依赖于前一个块完成的代码块。如果不是这种情况,那么您首先不应该使用 .then,而应该只编写内联代码。另一方面,如果您确实依赖于前一个块的完成,则别无选择,只能等待它完成。我一定是错过了什么。

【讨论】:

  • 你错过了一些东西。问题在于,在执行之前将继续排入任务调度程序,而不是直接执行,从而不必要地延迟了继续。
【解决方案2】:

PPL 调度程序不保证 .then lambda 何时执行,或者它在哪个线程上执行。您可以将其视为一个单独的任务,仅在前一个任务完成后才开始。调度器可以立即执行后续任务,它甚至可以使用同一个线程,但这并不确定。

有关调度程序实现的讨论,请参阅:

Appendix A: The Task Scheduler and Resource Manager

最新的 MSDN 文档也有更多信息:

Task Scheduler (Concurrency Runtime)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2019-05-04
    相关资源
    最近更新 更多