首页 > 其他分享 >Lambda expression

Lambda expression

时间:2023-09-27 14:15:17浏览次数:39  
标签:use code host memory device lambda expression Lambda

lambda structure
[capture list] (parameter list) -> return type { function body }

(parameter list) and return type are optional

Value and Reference capture

There is a notable question: when lambda used between CPU and GPU, we need to notice the store location of variable.
For example, if we open up a device memory, but the code is written in host code and the lambda also in host code. Now, we want use lambda to capture a variable, as shown in the following code.

template <typename Func>
__global__ void myFunc(Func func) {
	std::cout << func(0) << std::endl;
}

int main() {
    std::vector<int, CudaAllocator<int>> arr(n);

	muFunc<<<gridSize, blockSize>>> ([] __device__ (int i) -> void{
		arr[i] = 0;
	});

    return 0;
}

The CudaAllocator is a custom class which implements the allocate and deallocate member function

Please note that the above code is incomplete, if we just use the [], we can't use the arr in the lambda function body. But what should we use in [] ?
In fact, we use [&] or [=] directly are both wrong method. When we use [&], its meaning is like we let a device code to access a variable which is opened up in host memory. And if we use the [=], its meaning is like we copy the entire data of vector, it's time and resource consuming.

So, what is the right way to implement it ? We can use arr.data() to get the orginal pointer which points to the device memory, note that this variable is still in the host memory, so next we can use [=] to get a copy value so that device code can use it. In summary, the first step is used to solve the resource copy problem, and the second step is used to solve the resource access between host and device memory.

标签:use,code,host,memory,device,lambda,expression,Lambda
From: https://www.cnblogs.com/hongyugao/p/17732551.html

相关文章

  • Java中CornExpression说明
    关于cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素,从左至右,这些元素约定义如下:1、秒(0~59)2、分(0~59)3、小时(0~23)4、月份中的日期(1-31)5、月份(1-12或JAN-DEC)6、星期中的日期(1-7或SUN-SAT)7、年份(1970-2099)eg:0010,14,16**?每天10点、14点、16点0015,30,45*......
  • lambda表达式递归报错
    lambda表达式递归报错报错代码:voidsolve(){intn=10;vector<int>adj[n+1];autodfs=[&](autoself,intu,intp)->void{for(autov:adj[u]){}};}在递归lambda表达式中引用的外部变量尽量不要出现形如......
  • lambdas
    lambdas形式是:[](参数列表){操作}【】里面是捕捉方式,即传参的方式可以结合lambdas来增加筛选条件vector<int>v{3,9,7,3,1};autoit=std::find_if(v.begin(),v.end(),[](intvalue){returnvalue>4;});std::cout<<*it<<std::endl;//9; ......
  • lambda HashMap 排序
    目录TreeMaplambdacomparingByKey示例代码TreeMap按key排序生成map可以有TreeMap完成,TreeMap可以按key的自然顺序排序(Comparable实现)lambdacomparingByKey使用lambda也可以很方便的对map排序Map.Entry.comparingByKey()按key排序的ComparatorMap.Entry.comparingBy......
  • Java 8 Lambda 表达式解析
    Lambda表达式,也可称为闭包,它是推动Java8发布的最重要新特性。使用Lambda表达式可以使代码变的更加简洁紧凑。坦白的说,初次看见Lambda表达式瞬间头就大了,为了更好的理解,我们可以把Lambda表达式当作是一种匿名函数(对Java而言这并不完全正确,但现在姑且这么认为),简单地说,就是......
  • 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains n
    博客园首页新随笔联系管理订阅随笔-111 文章-1 评论-3 阅读-17万 1055-Expression#1ofORDERBYclauseisnotinGROUPBYclauseandcontainsnonaggregatedcolumn'information_schema.PROFILING.SEQ'whichisnotfunctionally......
  • Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate
    MySQL有any_value(field)函数,他主要的作用就是抑制ONLY_FULL_GROUP_BY值被拒绝官方有介绍,地址:https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value我们可以把select语句中查询的属性(除聚合函数所需的参数外),全部放入any_value(field)函数......
  • JDK8新特性之Lambda表达式和四大函数接口
    在Java8中,加入了Lambda(Lambdaexpression),在使用它以前我们先聊聊为什么要加入Lamdba,使用它对于编程上有什么好处 一、Lamdba的作用1.在我们需要把一些功能传递给某些方法时,在Java8以前,我们就需要去写匿名内部类。引入lambda表达式后,你可以在一个类中简便的定义参数和方法,替代大......
  • Python实现排序的方式有:内置函数sort()和sorted()以及lambda函数
    排序是计算机编程中经常需要用到的操作,它将一组数据按照规则重新排列,以便更好地处理数据。在Python中,有多种方法可以对数组进行排序,本文将从多个方面进行介绍。一、Python中的排序方法Python中内置了多个排序算法,包括冒泡排序、插入排序、选择排序、快速排序等。使用内置的sort(......
  • Lambda表达式
    作用:用于简化匿名内部类代码的书写。Lambda表达式基本使用怎么去简化呢?Lambda是有特有的格式的,按照下面的格式来编写Lamdba。(被重写方法的形参列表)->{被重写方法的方法体代码;}需要给说明一下的是,在使用Lambda表达式之前,必须先有一个接口,而且接口中只能有一个抽象......