官术网_书友最值得收藏!

A brief foray into functional programming

Before we continue, let's briefly do a review of two functions available in Python for functional programmingmap and reduce. These are both considered to be functional because they both act on functions for their operation. We find these interesting because these both correspond to common design patterns in programming, so we can swap out different functions in the input to get a multitude of different (and useful) operations.

Let's first recall the lambda keyword in Python. This allows us to define an anonymous function—in most cases, these can be thought of as a throwaway function that we may only wish to use once, or functions that are able to be defined on a single line. Let's open up IPython right now and define a little function that squares a number as such—pow2 = lambda x : x**2. Let's test it out on a few numbers:

Let's recall that map acts on two input values: a function and a list of objects that the given function can act on. map outputs a list of the function's output for each element in the original list. Let's now define our squaring operation as an anonymous function which we input into map, and a list of the last few numbers we checked with the following—map(lambda x : x**2, [2,3,4]):

We see that map acts as ElementwiseKernel! This is actually a standard design pattern in functional programming. Now, let's look at reduce; rather than taking in a list and outputting a directly corresponding list, reduce takes in a list, performs a recursive binary operation on it, and outputs a singleton. Let's get a notion of this design pattern by typing reduce(lambda x, y : x + y, [1,2,3,4]). When we type this in IPython, we will see that this will output a single number, 10, which is indeed the sum of 1+2+3+4. You can try replacing the summation above with multiplication, and seeing that this indeed works for recursively multiplying a long list of numbers together. Generally speaking, we use reduce operations with associative binary operations; this means that, no matter the order we perform our operation between sequential elements of the list, will always invariably give the same result, provided that the list is kept in order. (This is not to be confused with the commutative property.)

We will now see how PyCUDA handles programming patterns akin to reduce—with parallel scan and reduction kernels.

主站蜘蛛池模板: 桑植县| 南丰县| 盈江县| 淮安市| 全椒县| 滦南县| 昌吉市| 老河口市| 安阳县| 莱阳市| 廊坊市| 井研县| 民县| 庆元县| 隆子县| 泰和县| 梁河县| 宝清县| 华蓥市| 大丰市| 江陵县| 石台县| 平昌县| 南华县| 东乌珠穆沁旗| 谢通门县| 兰考县| 嘉禾县| 福泉市| 航空| 正镶白旗| 安陆市| 临澧县| 托克逊县| 涟水县| 定襄县| 敦煌市| 离岛区| 沙雅县| 昆明市| 甘孜县|