Nondeterminism in Futhark

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

The Futhark Programming Language High-performance purely functional data-parallel array programming OverviewExamplesDocsPublicationsGotta Go Fast!Get InvolvedBlog Fork me on GitHub Nondeterminism in Futhark Posted on November 4, 2026

Futhark 编程语言:高性能纯函数式数据并行数组编程。概述、示例、文档、出版物、追求速度!参与其中、博客。在 GitHub 上关注我。Futhark 中的非确定性,发布于 2026 年 11 月 4 日。

Last week I attended IFL2026, which was probably one of the best conference experiences I have ever had. One of the many good presentations was by Michael Haddon, who presented The Replay Lattice, which is a type system for floating-point reproducibility. The type system was presented for a demonstration language called Lane, but it inspired me to think more carefully about nondeterminism in Futhark (there is some), and what it might cost to get rid of it.

上周我参加了 IFL2026,这可能是我有史以来最好的会议体验之一。其中许多精彩的演讲之一来自 Michael Haddon,他介绍了“重放格”(The Replay Lattice),这是一种用于浮点可重现性的类型系统。该类型系统是为一种名为 Lane 的演示语言提出的,但它启发我更仔细地思考 Futhark 中的非确定性(确实存在一些),以及消除它可能需要付出的代价。

I often use the terms “determinism” or “deterministic” when explaining why I think data parallel programming is such a good paradigm, so perhaps it is surprising to see me write a blog post about nondeterminism in Futhark. But as is so often the case with such bold promises, they often tend to get a little weaker once you look closely enough.

在解释为什么我认为数据并行编程是一种如此好的范式时,我经常使用“确定性”或“确定性的”这些术语,所以看到我写一篇关于 Futhark 中非确定性的博客文章可能会让人感到惊讶。但正如这类大胆的承诺经常发生的那样,一旦你仔细观察,它们往往会变得有些站不住脚。

Michael Haddon’s work defines some nomenclature that is helpful for this discussion: Exact means that the computation produces the same result every time, guaranteed. (Michael describes this as matching the “serial reference”, which I think is not the best way of looking at it, but that’s a question of perspective) Replay means that if you rerun the computation with the same “schedule” (in some sense “machine”), you will get the same result, but other machines may give different results. Approx means potentially nondeterministic results.

Michael Haddon 的工作定义了一些对本次讨论有帮助的术语:精确(Exact)意味着计算每次都能保证产生相同的结果。(Michael 将其描述为与“串行参考”匹配,我认为这不是最好的看待方式,但这属于视角问题)。重放(Replay)意味着如果你用相同的“调度”(在某种意义上是“机器”)重新运行计算,你将得到相同的结果,但其他机器可能会给出不同的结果。近似(Approx)意味着潜在的非确定性结果。

The kind of nondeterminism we are talking about here is not the kind of free-form nondeterminism that arises from classic shared-state concurrent programming. Indeed, both Lane and Futhark are pretty much exclusively concerned with the kind of nondeterminism that arises from floating-point rounding errors. The simplest example is the non-associativity of addition: > (0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3) false

我们在这里讨论的非确定性,并非源于经典共享状态并发编程的那种自由形式的非确定性。事实上,Lane 和 Futhark 几乎只关注由浮点舍入误差引起的非确定性。最简单的例子是加法的非结合性:> (0.1 + 0.2) + 0.3 == 0.1 + (0.2 + 0.3) 为假。

If we have an array of numbers we need to sum, then the result depends on the order in which you sum them. When parallelising a summation, you often do things like split the numbers into blocks, sum each block on a separate processor, then combine the partial results. This changes the order of addition, and hence the result.

如果我们有一个需要求和的数字数组,那么结果取决于你求和的顺序。在并行化求和时,你通常会做一些事情,比如将数字分成块,在不同的处理器上对每个块求和,然后合并部分结果。这改变了加法的顺序,从而改变了结果。

Now, an important point is that there is no reason to think that the parallelised result is numerically worse than a serial left-to-right summation, but it will be different. Further, an efficient implementation will likely choose a number of chunks somewhat proportional to the number of processors on the machine, which means the result may also be different on other machines (but again, not necessarily worse). This corresponds to the “Replay” class of nondeterminism.

现在,一个重要的观点是,没有理由认为并行化的结果在数值上比串行从左到右的求和更差,但它会不同。此外,高效的实现可能会选择与机器上的处理器数量成比例的块数,这意味着结果在其他机器上也可能不同(但同样,不一定更差)。这对应于“重放”类别的非确定性。

Finally, the partial results may be combined using atomic instructions, which may be resolved in a different order when the program is rerun on the same machine. This is then “Approx”. Even in this simple example of summing a bunch of numbers, we encounter both classes of nondeterminism.

最后,部分结果可以使用原子指令进行合并,当程序在同一台机器上重新运行时,这些指令可能会以不同的顺序解析。这就是“近似”。即使在这个对一堆数字求和的简单例子中,我们也遇到了这两类非确定性。

Note that none of them are “race conditions” in the usual sense of the program sometimes just malfunctioning: the point is that the result changes, but given that the result is in some sense already “wrong” (due to roundoff error), the main issue is that there are cases where we care about getting the same “wrong” result every time.

请注意,它们都不是通常意义上程序有时会发生故障的“竞态条件”:重点是结果发生了变化,但考虑到结果在某种意义上已经是“错误”的(由于舍入误差),主要问题在于,在某些情况下,我们关心的是每次都能得到相同的“错误”结果。

So in Futhark, ostensibly a deterministic language, where does the nondeterminism come from? There are a few sources. Some of them come when programmers violate mandated constraints (but which are unchecked for various reasons), and others are basically down to floating-point roundoff error.

那么在表面上是确定性语言的 Futhark 中,非确定性从何而来?有几个来源。其中一些源于程序员违反了强制约束(但由于各种原因未进行检查),而另一些基本上归结为浮点舍入误差。

Language abuse Let us start by considering language abuse. The worst but also least interesting case is using the #[unsafe] attribute to disable safety checks, after which you can do arbitrary memory reads and writes. This can be avoided by passing —safe to the compiler, or perhaps by just not doing this kind of cowboy coding in the first place.

语言滥用。让我们从考虑语言滥用开始。最糟糕但也最无趣的情况是使用 #[unsafe] 属性来禁用安全检查,之后你可以进行任意的内存读写。这可以通过向编译器传递 —safe 参数来避免,或者也许只是从一开始就不进行这种鲁莽的编码。

If you are a good enough programmer that your code does not need any safety checks, why not just go and write in C? Those folks never make mistakes. Another more insidious problem is related to the scatter construct. The semantics of an expression scatter d is vs are simple enough, and can be expressed with this imperative pseudocode: for (i,v) in zip(is, vs): res[i] = v

如果你是一个足够优秀的程序员,以至于你的代码不需要任何安全检查,为什么不去用 C 语言编写呢?那些人从不犯错。另一个更隐蔽的问题与 scatter 构造有关。表达式 scatter d is vs 的语义足够简单,可以用这种命令式伪代码表示:for (i,v) in zip(is, vs): res[i] = v。

That is, scatter d is vs pairs the indexes in is with the corresponding values in vs and writes each value to the corresponding index. It is a parallel random-access write. To ensure that the writes can indeed be written in parallel, Futhark requires that is contains no duplicates, or in a slight weakening, that if is contains duplicates, then the corresponding values are also identical.

也就是说,scatter d is vs 将 is 中的索引与 vs 中的相应值配对,并将每个值写入相应的索引。这是一种并行随机访问写入。为了确保写入确实可以并行进行,Futhark 要求 is 不包含重复项,或者稍微放宽一点,如果 is 包含重复项,那么相应的值也必须相同。

This is not checked dynamically, and a program can get in an awful lot of trouble by violating this property. Which write “wins” is unpredictable - in the worst case, you can even get a “torn” write that combines two different values. This behaviour of scatter is thorny, and even experienced Futhark programmers sometimes make this mistake.

这不会被动态检查,程序如果违反此属性可能会陷入巨大的麻烦。哪个写入“获胜”是不可预测的——在最坏的情况下,你甚至可能得到一个结合了两个不同值的“撕裂”写入。scatter 的这种行为很棘手,即使是经验丰富的 Futhark 程序员有时也会犯这个错误。

The reason it has no safety checks is twofold: scatter is a fundamental construct used in most sophisticated parallel algorithms, and so its performance is paramount. I’m not sure how to detect duplicate writes in a way that does not violate the cost model (although scatter is already a bit iffy in that regard).

它没有安全检查的原因有两个:scatter 是大多数复杂并行算法中使用的基本构造,因此其性能至关重要。我不确定如何以不违反成本模型的方式检测重复写入(尽管 scatter 在这方面已经有点问题了)。

Basically, the cost of scatter must be proportional to the number of elements written, not to the total size of the target array, and that makes it difficult to construct appropriate auxiliary structures for safety checks. And yes, I do realise the irony of insisting on an unsafe scatter after making fun of C programmers for using an unsafe language. Anyway, let’s move on to reduce. Parallel reductions, just like parallel scans, histograms, and so on, work only if the provided operator is associative and in so

基本上,scatter 的成本必须与写入的元素数量成正比,而不是与目标数组的总大小成正比,这使得构建用于安全检查的适当辅助结构变得困难。是的,我确实意识到在嘲笑 C 程序员使用不安全语言后,坚持使用不安全的 scatter 是多么讽刺。无论如何,让我们继续讨论 reduce。并行归约,就像并行扫描、直方图等一样,只有在提供的运算符是结合律的情况下才能工作,并且在……中。