Futhark 示例教程
摘要
通过一系列带注释的示例程序,对Futhark编程语言进行实践性介绍,涵盖基本特性和并行计算的编程技巧。
暂无内容
查看缓存全文
缓存时间: 2026/05/16 12:39
# Futhark 示例
来源: https://futhark-lang.org/examples.html
以下是通过一系列带注释的程序对 Futhark 进行的实践性介绍,按复杂度大致递增排列。你可以将程序加载到解释器 (https://futhark.readthedocs.io/en/stable/man/futhark-repl.html) 中进行实验。如需了解该语言的常规介绍,Parallel Programming in Futhark (https://futhark-book.readthedocs.io/) 可能是更好的选择。更多示例可查看我们已实现的基准测试 (https://github.com/diku-dk/futhark-benchmarks)。我们还维护了一个使用 Futhark 的项目列表 (https://futhark-lang.org/examples.html#projects-using-futhark)。
其中一些示例程序使用了指令 (https://futhark.readthedocs.io/en/latest/man/futhark-literate.html) 用于绘图或渲染图形。
## 基本语言特性 (https://futhark-lang.org/examples.html#basic-language-features)
- 阶乘函数的基本用法 (https://futhark-lang.org/examples/fact.html)
- 原始值 (https://futhark-lang.org/examples/values.html)
- 将值转换为不同类型 (https://futhark-lang.org/examples/converting.html)
- 数学函数 (https://futhark-lang.org/examples/math-functions.html)
- 函数 (https://futhark-lang.org/examples/functions.html)
- 数组 (https://futhark-lang.org/examples/arrays.html)
- 最小值和最大值 (https://futhark-lang.org/examples/minmax.html)
- 类型标注 (https://futhark-lang.org/examples/type-ascriptions.html)
- 基本并行性 (https://futhark-lang.org/examples/basic-parallelism.html)
- 元组和记录 (https://futhark-lang.org/examples/tuples-and-records.html)
- 大小强制转换 (https://futhark-lang.org/examples/size-coercions.html)
- 范围 (https://futhark-lang.org/examples/ranges.html)
- 反引号运算符 (https://futhark-lang.org/examples/backticks.html)
- 扫描和规约 (https://futhark-lang.org/examples/scan-reduce.html)
- 参数多态 (https://futhark-lang.org/examples/parametric-polymorphism.html)
- 多态的最小值和最大值 (https://futhark-lang.org/examples/polyminmax.html)
- Gather 和 Scatter (https://futhark-lang.org/examples/gather-and-scatter.html)
- 独占扫描 (https://futhark-lang.org/examples/exclusive-scan.html)
- 独占前缀和 (https://futhark-lang.org/examples/exclusive-prefix-sum.html)
- Pipe 运算符 (https://futhark-lang.org/examples/piping.html)
- 复杂范围 (https://futhark-lang.org/examples/complex-ranges.html)
- 和类型与模式匹配 (https://futhark-lang.org/examples/sum-types.html)
- 循环 (https://futhark-lang.org/examples/loops.html)
- 数组的展开与折叠 (https://futhark-lang.org/examples/flatten-unflatten.html)
- 原地更新 (https://futhark-lang.org/examples/inplace.html)
## 编程技巧 (https://futhark-lang.org/examples.html#programming-techniques)
- 基准测试 (https://futhark-lang.org/examples/benchmarking.html)
- 统计满足条件的元素个数 (https://futhark-lang.org/examples/filter-length.html)
- 最小元素的索引 (argmin) (https://futhark-lang.org/examples/argmin.html)
- 对过滤结果进行规约 (https://futhark-lang.org/examples/filter-reduce.html)
- 散布过滤结果 (https://futhark-lang.org/examples/filter-scatter.html)
- 大小参数 (https://futhark-lang.org/examples/size-parameters.html)
- 矩阵乘法 (https://futhark-lang.org/examples/matrix-multiplication.html)
- 逐对 L1 距离 (https://futhark-lang.org/examples/pairwise-l1.html)
- 外积 (https://futhark-lang.org/examples/outer-product.html)
- 比较数组相等性 (https://futhark-lang.org/examples/array-equality.html)
- 搜索 (https://futhark-lang.org/examples/searching.html)
- Option 类型 (https://futhark-lang.org/examples/opt.html)
- 计算直方图 (https://futhark-lang.org/examples/histograms.html)
- 移动平均 (https://futhark-lang.org/examples/moving-average.html)
- 均值 (https://futhark-lang.org/examples/means.html)
- 交换数组中的两个元素 (https://futhark-lang.org/examples/swap.html)
- 整数对数 (https://futhark-lang.org/examples/integer-logarithm.html)
- 基数排序 (https://futhark-lang.org/examples/radix-sort.html)
- 按键基数排序 (https://futhark-lang.org/examples/radix-sort-key.html)
- 归并排序 (https://futhark-lang.org/examples/merge-sort.html)
- 去除重复 (https://futhark-lang.org/examples/removing-duplicates.html)
- 抽象数据类型 (https://futhark-lang.org/examples/abstract-data-types.html)
- 复数 (https://futhark-lang.org/examples/complex-numbers.html)
- 结合性测试 (https://futhark-lang.org/examples/testing-associativity.html)
- 不含中性元素的规约或扫描 (https://futhark-lang.org/examples/no-neutral-element.html)
- 洞 (https://futhark-lang.org/examples/holes.html)
- Kahan 求和 (https://futhark-lang.org/examples/kahan.html)
- 随机数 (https://futhark-lang.org/examples/random-numbers.html)
- 高斯模糊(含 Python 集成) (https://futhark-lang.org/examples/gaussian-blur.html)
- 三维向量 (https://futhark-lang.org/examples/3d-vectors.html)
- 模拟名义类型 (https://futhark-lang.org/examples/nominal-types.html)
- 三角矩阵 (https://futhark-lang.org/examples/triangular.html)
- 二分查找 (https://futhark-lang.org/examples/binary-search.html)
- 对偶数的自动微分 (https://futhark-lang.org/examples/dual-numbers.html)
- 方差 (https://futhark-lang.org/examples/variance.html)
- 括号匹配 (https://futhark-lang.org/examples/parens.html)
- 多项式求值 (https://futhark-lang.org/examples/polynomials.html)
- 直线拟合 (https://futhark-lang.org/examples/line-fitting.html)
- 解码 MNIST 数据文件 (https://futhark-lang.org/examples/mnist.html)
## 自动微分 (https://futhark-lang.org/examples.html#automatic-differentiation)
- 前向模式自动微分 (https://futhark-lang.org/examples/forward-ad.html)
- 反向模式自动微分 (https://futhark-lang.org/examples/reverse-ad.html)
- 牛顿法 (https://futhark-lang.org/examples/newton-ad.html)
## Literate Futhark (https://futhark-lang.org/examples.html#literate-futhark)
- Literate Futhark 的基本使用 (https://futhark-lang.org/examples/literate-basics.html)
- 使用 Literate Futhark 生成视频 (https://futhark-lang.org/examples/literate-video.html)
- 在 Literate Futhark 中读写文件 (https://futhark-lang.org/examples/literate-files.html)
## 绘图 (https://futhark-lang.org/examples.html#plotting)
Literate Futhark 允许直接使用 gnuplot (http://www.gnuplot.info/)。以下示例展示了简单和常见的情况。
- 绘制直方图 (https://futhark-lang.org/examples/plot-histogram.html)
## 来自 Dex 的示例 (https://futhark-lang.org/examples.html#examples-from-dex)
以下示例移植自 Dex (https://github.com/google-research/dex-lang),一种依赖类型的函数式数组语言,其描述循环处理的方式略有不同。我们尽量保留了原始的命名方案和编程风格。
- 前奏 (https://futhark-lang.org/examples/dex-prelude.html)
- Mandelbrot 集 (https://futhark-lang.org/examples/dex-mandelbrot.html)
- 多步骤光线追踪 (https://futhark-lang.org/examples/dex-raytrace.html)
- 蒙特卡洛估计圆周率 (https://futhark-lang.org/examples/dex-pi.html)
- 布朗运动 (https://futhark-lang.org/examples/dex-brownian-motion.html)
- Sierpinski 三角形 (https://futhark-lang.org/examples/dex-sierpinski.html)
## 外部示例 (https://futhark-lang.org/examples.html#external-examples)
- OCaml-GPU wordcount 示例 (https://github.com/ArulselvanMadhavan/ocaml_gpu_wordcount),通过调用 Futhark 实现。
## 使用 Futhark 的项目 (https://futhark-lang.org/examples.html#projects-using-futhark)
目前大多数 Futhark 代码可能仍然是 Futhark 自身的测试和基准测试套件。不过,也有一些程序是用 Futhark 编写的,因为它适合解决特定问题,而不仅仅是为了测试编译器。以下是一个可能不完整的列表:
Diving Beet (https://github.com/Athas/diving-beet) 是一个*落沙*游戏,一种简单的粒子模拟玩具。它的主要目的是产生漂亮的视觉效果。有一篇博客文章 (https://futhark-lang.org/blog/2016-12-04-diving-beet.html) 提供了详细信息和视频。
Futball (https://github.com/Athas/futball) 是一个关于躲避被球击中的游戏。渲染引擎是用 Futhark 编写的光线追踪器。
Futcam (https://github.com/nqpz/futcam) 是一个应用程序,它将一组可交互配置的滤镜应用于摄像头流。滤镜用 Futhark 实现。
Futracer (https://github.com/nqpz/futracer) 是一个用 Futhark 编写的相当慢的暴力光线追踪器。
Futswirl (https://github.com/nqpz/futswirl) 是一个基于迭代函数系统 (https://en.wikipedia.org/wiki/Iterated_function_system) 的分形生成器。
Neptune (https://github.com/filecoin-project/neptune) 是一个为 Filecoin (https://filecoin.io/) 优化的 Poseidon 哈希函数 (https://www.poseidon-hash.info/) 实现,其中 GPU 部分 (https://github.com/filecoin-project/neptune-triton) 用 Futhark 实现。
Palathark (https://githepia.hesge.ch/orestis.malaspin/palathark) 是一个格子玻尔兹曼方法的 Futhark 实现。
Ray Tracing in One Weekend in Futhark (https://github.com/athas/raytracinginoneweekendinfuthark) 和 Ray Tracing: the Next Week in Futhark (https://github.com/athas/raytracingthenextweekinfuthark/) 是基于 Peter Shirley 系列丛书的实现。这些绝不是实时光线追踪器,但支持高级效果,并使用了诸如 BVH 树等加速结构。
Pareas (https://github.com/Snektron/pareas) 是一个用 Futhark 和 C++ 实现的编译器。
相似文章
系统编程入门,第一部分:程序员编写程序(2025)
一篇系统编程入门文章,涵盖诸如位操作、解析、文件系统、系统调用和内存管理等基础知识,面向程序员。
7行代码,3分钟:实现一种编程语言(2010)
本文介绍了一种基于 Lambda 演算的图灵完备函数式语言的极简 7 行解释器,展示了 eval/apply 设计模式。
以理论构建的视角阅读编程
本文推荐 Peter Naur 的著作《编程即理论构建》,主张编程的本质在于构建和传达对软件的心理模型,而不仅仅是编写代码。
七大编程原语言(2022)
一篇文章探讨了七种构成大多数现代编程语言基础的编程语言原型(原语言),认为学习植根于这些原型的基础知识比选择特定语言更重要。
自己造一门编程语言比你想象的简单(但也更难)
作者分享了为游戏模组系统构建自定义编程语言的经验,讨论了设计动机和技术挑战。这篇文章反思了在个人项目中实现一门非玩具级编程语言的可行性与复杂度。