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

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.

主站蜘蛛池模板: 方山县| 黑水县| 山西省| 穆棱市| 宣汉县| 清涧县| 镇江市| 柞水县| 白山市| 泰来县| 澄迈县| 伽师县| 张北县| 昭苏县| 盐山县| 达日县| 革吉县| 临江市| 麻江县| 高邮市| 镇赉县| 黔西县| 会理县| 寿阳县| 西昌市| 南宁市| 三门县| 沙田区| 佛山市| 通江县| 海安县| 楚雄市| 兴化市| 启东市| 乌鲁木齐县| 公主岭市| 永川市| 南安市| 高阳县| 武邑县| 江安县|