使用CUDA的Lambda表达式 [英] Lambda expressions with CUDA

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

问题描述

如果我在 thrust :: host 上使用 thrust :: transform ,lambda使用很好

If I use thrust::transform on thrust::host, the lambda usage is fine

thrust::transform(thrust::host, a, a+arraySize,b,d,[](int a, int b)->int
{
    return a + b;
});

但是,如果我更改 thrust :: host thrust :: device ,代码将不会通过编译器。这是VS2013上的错误:

However, if I change thrust::host to thrust::device, the code wouldn't pass the compiler. Here is the error on VS2013:


lambda(lambda [](int,int) - > int不能在 __ global __ 函数模板实例化的模板参数类型中使用,除非在 __ device __ 中定义了lambda __ global __ 函数

The closure type for a lambda ("lambda [](int, int)->int") cannot be used in the template argument type of a __global__ function template instantiation, unless the lambda is defined within a __device__ or __global__ function

所以,问题是如何使用 __设备__ __全局__

So, the problem is how using __device__ or __global__ in connection to device lambdas.

推荐答案

在CUDA 7中是不可能的。引用自马克·哈里斯

In CUDA 7 it is not possible. Quoting from Mark Harris:


今天在CUDA中不支持,因为lambda是主机代码。将lambdas从主机传递到设备是一个具有挑战性的问题,但我们将在未来的CUDA版本中进行调查。

That isn't supported today in CUDA, because the lambda is host code. Passing lambdas from host to device is a challenging problem, but it is something we will investigate for a future CUDA release.

您可以在CUDA 7是从你的设备代码调用推力算法,在这种情况下,你可以传递lambdas给他们...

What you can do in CUDA 7 is call thrust algorithms from your device code, and in that case you can pass lambdas to them...

7,可以从设备代码(例如CUDA内核或 __ device __ 函子)调用推力算法。在这些情况下,你可以使用(设备)lambdas与推力。在parallelforall博客此处中提供了一个示例。

With CUDA 7, thrust algorithms can be called from device code (e.g. CUDA kernels, or __device__ functors). In those situations, you can use (device) lambdas with thrust. An example is given in the parallelforall blog post here.

但是,CUDA 7.5引入了一个实验性设备lambda特性。 此处描述了此功能:

However, CUDA 7.5 introduces an experimental device lambda feature. This feature is described here:


CUDA 7.5引入了一个实验性功能:GPU lambdas。 GPU lambda是您可以在主机代码中定义的匿名设备函数对象,通过使用 __ device __ 说明符注释它们。

为了启用此功能的编译(目前,使用CUDA 7.5),必须指定编译命令行中的 nvcc 上的-expt-extended-lambda

In order to enable compilation for this feature, (currently, with CUDA 7.5) it's necessary to specify --expt-extended-lambda on the nvcc compile command line.

这篇关于使用CUDA的Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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