我刚读到:
由于Concurrency::completion_future的功能似乎模仿了std::future,我认为我可以做一些类似的事情,但是这个相对简单的例子失败了:
#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");
}这为什么会失败呢?
发布于 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共享内存的系统上,或者在一些罕见的时间内,它仍然可能(通过设计)失败。
发布于 2013-11-15 08:53:44
免责声明:我不是AMP的专家。
AFAIK,array_view本身并不代表任何东西。这只是一种你应该把东西绑在一起的观点。所以你的代码,基本上,对我来说没有意义。CPU上没有任何需要同步的后端内存。
尝试以下代码:
#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];
}这只是你的修改,效果如预期。
https://stackoverflow.com/questions/19995996
复制相似问题