使用syncnize_async().then()方法 [英] Using synchronize_async().then() method

查看:82
本文介绍了使用syncnize_async().then()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的代码,我想在其中使用syncnize_async().then()方法.
第二部分中的代码取决于第一部分中代码的输出.
因此,我决定使用then()方法,以便CPU线程可以执行某些操作
否则.

Here is a simple code where I want to use synchronize_async().then() method.
The code in PART II is dependent on the output of the code in PART I.
Therefore I decided to use then() method so that the CPU thread can do something
else instead.

首先,第一部分的输出正常.
但是,PART II数据的输出全为零.实际上看起来像是代码
在第二部分中什么也没做.您能告诉我我在这里做错了什么吗?谢谢

First the output from PART I is ok.
However the output from PART II data is all zeros. In fact it seems like the code
in PART II did nothing at all. Can you please tell me what I did wrong here? Thanks

我查看了博客,关于使用方法的信息很少
正确的syncnize_async().then()方法,并且有

I looked at the blogs and there is very little information regarding how to use
synchronize_async().then() method properly, and there is on

	extent<2> ext1(20);
	
	array_view<int, 2> in(2, 10, INPUT);
	array_view<int, 2> out(2, 10, A_OUT);
	out.discard_data();

	/*** PART I ***/
	parallel_for_each(ext1, [=](index<2> idx) restrict(amp) 
	{	
		out[idx] = in[idx];
	});
	
		
	extent<2> ext2(20);
	array_view<int, 2> vout(2, 10, B_OUT);
	vout.discard_data();
		
	/*** PART II ***/
	out.synchronize_async().then([=]()
	{		
		parallel_for_each(ext2, [=](index<2> idx) restrict(amp) 
		{	
			vout[idx] = out[idx];  
		});
			
         });


	/**** another code here where CPU does something else ****/
	
	vout.synchronize(); <-- CPU then retrieves the data it needs


推荐答案

Laparma,

Hi Laparma,

您需要等待在第二部分中"then"内部启动"parallel_for_each"的继续操作.在vout.synchronize()之前,因为您要同步"vout"仅在调用第二个parallel_for_each之后.

You need to wait on the continuation that launches the parallel_for_each in PART II inside the "then" before vout.synchronize(), since you want to synchronize "vout" only after the 2nd parallel_for_each has been invoked.

类似的东西:

   auto t = out.synchronize_async().to_task().then([=]
    {
       parallel_for_each(vout.extent,[=](index< 2> idx)limit(amp)
       {
           vout [idx] = out [idx];
       });
    });
    t.get();
    vout.synchronize();

谢谢
玲莉

    auto t = out.synchronize_async().to_task().then([=]
    {
        parallel_for_each(vout.extent, [=](index<2> idx) restrict(amp)
        {
            vout[idx] = out[idx];
        });
    });
    t.get();
    vout.synchronize();

Thanks,
Lingli


这篇关于使用syncnize_async().then()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆