一句话:用和、积与复合等泛型构造统一表达并行 scan 和 FFT,无需数组并揭示算法共性。
并行编程(无论命令式还是函数式)长期以数组为核心数据类型,而类型化函数式编程探索了列表、树等多种结构。本文提出面向并行扫描(广义前缀和)与快速傅里叶变换(FFT)的泛型函数式表述。方法上,将数据类型分解为和、积、复合及其单位等少量基本构造,并在此之上定义算法,使其自动适配无限多种数据类型;数组在其中不扮演角色。得益是更简单、更可组合的风格,复用常见代数模式,避免运行期索引错误,并清晰揭示原本差异很大的算法之间的深层共性。实例化这些泛型表述后,并行 scan 与 FFT 各自自然产生两个已知算法,另有两个可能的新算法。
Parallel programming, whether imperative or functional, has long focused on arrays as the central data type. Meanwhile, typed functional programming has explored a variety of data types, including lists and various forms of trees. Generic functional programming decomposes these data types into a small set of fundamental building blocks: sum, product, composition, and their associated identities. Definitions over these few fundamental type constructions then automatically assemble into algorithms for an infinite variety of data types—some familiar and some new. This paper presents generic functional formulations for two important and well-known classes of parallel algorithms: parallel scan (generalized prefix sum) and fast Fourier transform (FFT). Notably, arrays play no role in these formulations. Consequent benefits include a simpler and more compositional style, much use of common algebraic patterns and freedom from possibility of run-time indexing errors. The functional generic style also clearly reveals deep commonality among what otherwise appear to be quite different algorithms. Instantiating the generic formulations, two well-known algorithms for each of parallel scan and FFT naturally emerge, as well as two possibly new algorithms.