推力:如何直接控制算法调用的执行? [英] Thrust: How to directly control where an algorithm invocation executes?

查看:197
本文介绍了推力:如何直接控制算法调用的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码没有可能导致其在CPU或GPU上运行的信息。我不知道reduce操作在哪里执行?

The following code has no information that may lead it to run at CPU or GPU. I wonder where is the "reduce" operation executed?

#include <thrust/iterator/counting_iterator.h>
...
// create iterators
thrust::counting_iterator<int> first(10);
thrust::counting_iterator<int> last = first + 3;

first[0]   // returns 10
first[1]   // returns 11
first[100] // returns 110

// sum of [first, last)
thrust::reduce(first, last);   // returns 33 (i.e. 10 + 11 + 12)

此外,

thrust::transform_reduce(
    thrust::counting_iterator<unsigned int>(0), 
    thrust::counting_iterator<unsigned int>(N), 
    MyOperation(data), 0 ,thrust::plus<unsigned int>())

即使数据被定义为thrust :: host_vector,这个函数试图在GPU上执行(编译器给出相关的错误,因为文件名以.cpp结尾)。我如何让代码在CPU上运行。或者我应该寻找另一种方式来执行相同的操作,例如。不使用counting_iterator?

Even though data is defined as thrust::host_vector, this function tries to be executed at GPU (compiler gives related errors, because the filename ends with .cpp). How may I make the code to run at CPU. Or should I look for another way to perform the same operation, e.g. not using counting_iterator?

推荐答案

默认情况下,像这样的算法调用在设备后端执行。

By default, algorithm invocations like this execute on the device backend (i.e., the GPU in your case).

如果您使用的是Thrust 1.7或更高版本,请使用 thrust :: host 执行策略强制算法调用以在主机(即CPU)上执行:

If you're using Thrust 1.7 or better, use the thrust::host execution policy to force an algorithm invocation to execute on the host (i.e., the CPU):

#include <thrust/execution_policy.h>

...

thrust::reduce(thrust::host, first, last);

...

thrust::transform_reduce(thrust::host,
                         first,
                         last,
                         MyOperation(data),
                         0,
                         thrust::plus<unsigned int>());

如果您使用的是Thrust 1.6,您可以通过 retag 取得现有的迭代器:

If you're using Thrust 1.6, you can retarget the invocations to the host by retagging an existing iterator:

#include <thrust/iterator/retag.h>

...

thrust::reduce(thrust::retag<thrust::host_system_tag>(first),
               thrust::retag<thrust::host_system_tag>(last));

...

thrust::transform_reduce(thrust::retag<thrust::host_system_tag>(first),
                         thrust::retag<thrust::host_system_tag>(last),
                         MyOperation(data),
                         0,
                         thrust::plus<unsigned int>());

如果您使用的是1.6版之前的Thrust版本,您需要传递 host_space_tag counting_iterator 作为模板参数:

If you're using an older version of Thrust prior to 1.6, you need to pass host_space_tag to counting_iterator as a template parameter:

thrust::reduce(thrust::counting_iterator<unsigned int, thrust::host_space_tag>(0),
               thrust::counting_iterator<unsigned int, thrust::host_space_tag>(N));

这篇关于推力:如何直接控制算法调用的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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