首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得Concurrency::completion_future的状态?

如何获得Concurrency::completion_future的状态?
EN

Stack Overflow用户
提问于 2013-11-15 07:46:17
回答 2查看 199关注 0票数 1

我刚读到:

Get the status of a std::future

由于Concurrency::completion_future的功能似乎模仿了std::future,我认为我可以做一些类似的事情,但是这个相对简单的例子失败了:

代码语言:javascript
复制
#include <assert.h>
#include <chrono>
#include <iostream>
#include <amp.h>

int main()
{
    using namespace Concurrency;
    int big = 1000000; // this should take a while to send back to the host
    array_view<int> av(big);

    parallel_for_each(extent<1>(big), [=](index<1> idx) restrict(amp)
    {
        av[idx] = idx[0];
    });
    int i = 0;
    completion_future future = av.synchronize_async();

    // this should be false; how could it instantly sent back so much data?
    bool const gpuFinished = future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;

    assert(!gpuFinished); // FAIL! why?

    future.wait();

    system("pause");
}

这为什么会失败呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-18 04:16:55

在OP中观察到的行为是正确的。

array_view<int> av(big)在没有数据源的情况下创建array_view ,而av.synchronize_async()则同步对数据源的修改。因此,对于没有数据源的array_view,按照定义,它是非op的。通过扩展,它也没有强制执行前面的parallel_for_each

如果目的是将数据同步到CPU内存,那么在这种情况下,需要使用av.synchronize_to_async(accelerator(accelerator::cpu_accelerator).default_view)显式地请求数据。当然,返回的completion_future只有在前面的parallel_for_each和(可选)复制操作完成后才能就绪。

将前一个同步调用替换为后者使断言成功,同时要记住,在具有CPU共享内存的系统上,或者在一些罕见的时间内,它仍然可能(通过设计)失败。

票数 4
EN

Stack Overflow用户

发布于 2013-11-15 08:53:44

免责声明:我不是AMP的专家。

AFAIK,array_view本身并不代表任何东西。这只是一种你应该把东西绑在一起的观点。所以你的代码,基本上,对我来说没有意义。CPU上没有任何需要同步的后端内存。

尝试以下代码:

代码语言:javascript
复制
#include <assert.h>
#include <chrono>
#include <iostream>
#include <amp.h>
#include <numeric>

int main()
{
    using namespace Concurrency;
    using namespace std;
    int big = 100000000; // this should take a while to send back to the host
    vector<int> vec(big);
    iota(begin(vec), end(vec), 0);
    array_view<int, 1> av(big, vec);

    parallel_for_each(Concurrency::extent<1>(big), [=](index<1> idx) restrict(amp)
    {
        av[idx] = av[idx] * av[idx];
    });
    int i = 0;
    completion_future future = av.synchronize_async();

    // this should be false; how could it instantly sent back so much data?
    bool const gpuFinished = future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;

    assert(!gpuFinished); // FAIL! why?

    future.wait();
    std::cout << vec[5];
}

这只是你的修改,效果如预期。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19995996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档