Futhark Programming Language
repository·master·Indexed 25 days ago
https://github.com/diku-dk/futharkA purely functional, data-parallel programming language in the ML family designed for high-performance parallel computing on CPUs and GPUs. The documentation covers language fundamentals, the C API for library integration, binary data format specifications, and WebGPU compilation via JavaScript and WebAssembly.
What's inside Futhark
- Futhark is a purely functional, data-parallel programming language in the ML family. It is designed to compile into highly efficient parallel code capable of running on both CPUs and GPUs. It is considered stable and suitable for practical programming applications.
Overview of Futhark library backend tests
masterThe tests intests_libare designed to verify Futhark's library backends. Unlike standard executables, these tests focus on library-specific concerns, most notably the handling of opaque types, which are not supported in standard executables.Understand the Futhark runtime system directory
masterThertsdirectory contains components and bits used in the code generated by the Futhark compiler. These files are kept separate from the compiler source code to allow for easier modification and to facilitate standalone testing of the runtime components.Understand Futhark Expressions and Atoms
masterExpressions are the fundamental building blocks of Futhark programs. Every expression has a statically determined type and produces a value at runtime. Futhark uses an eager/strict evaluation strategy (call-by-value).
Basic elements of expressions are called atoms, which include literals, variables, strings, characters, and parenthesized expressions. Expressions can be composed using operators, constructors, type ascriptions, and control flow constructs like
if,let,loop, andmatch.Test the server protocol
masterWhile most server protocol testing is handled implicitly by the standardfuthark testcommand, this directory contains specialized, focused tests for specific sub-parts of the protocol. Use these tests when you need to isolate and verify particular aspects of the server implementation.Understand Futhark Size Types
masterFuthark uses a system of size-dependent types to statically check that array sizes are compatible.
- Size Parameters: Represented as
[n], these quantify array sizes. They are not passed explicitly during function calls; instead, their values are implicitly deduced from the arguments. - Anonymous Sizes: Represented as
[], these allow the type checker to invent fresh size parameters to ensure all arrays have a size. - Existential Sizes: On the right-hand side of a function arrow (return types), a size might be unknown until the function is applied, denoted by
?[k].[k]t. - Size-dependent Types: You can use size parameters in return types. For example,
replicate 10 0results in type[10]i32. - Constraints: Sizes must be expressions of type
i64that do not consume free variables.
def f [n] (a: [n]i32) (b: [n]i32): [n]i32 = map2 (+) a b- Size Parameters: Represented as
Features of futhark-lsp
masterWhen integrated with an LSP-compatible editor,
futhark-lspprovides the following features:- Hover Information: Shows the type of the symbol under the cursor (note: this works on references to top-level symbols, but not on the definition of the top-level symbol itself).
- Go To Definition: Jumps to the definition of the symbol under the cursor.
- Formatting: Automatically invokes
futhark fmtto format the current file. - Inlay Hints: Displays virtual text hints visualizing type-checking results, such as inferred types for lambda arguments, function arguments,
letbindings, or loop bindings. - Code Actions: For every name binding with an inlay type hint, a code action is available to insert the exact type ascription shown in the virtual text (including inferred type variables or sizes).
- Evaluation Comments: Supports evaluating code snippets embedded in comments using the format
-- >>> expression. Editors may offer code lenses to trigger evaluation.- Safety Limits: Evaluations are aborted if they exceed 15 seconds or allocate more than 100 GB in total. Only the last 100 debugging traces are retained.
Understand Futhark's Module System and Abstractions
masterFuthark provides powerful abstraction capabilities through its module system:
- Module: A mapping from names to definitions of types, values, or nested modules.
- Parametric Module: A function from modules to modules, providing the highest level of abstraction.
- Module Type: A description of a module's interface, used for hiding contents via Module Ascription (
m : mt) or requiring implementations in parametric modules. - Defunctorisation: A compiler transformation that compiles away modules (similar to defunctionalisation) to make using parametric modules free at run-time.
Understand the futhark-bench methodology
masterThe benchmarking tool uses a two-phase technique to ensure statistical robustness:
- Warmup: A single run is performed and discarded.
- Initial Phase: Performs a set number of runs (default 10, configurable with
-r) or runs for at least half a second, whichever is longer. If measurements are statistically robust (based on standard deviation and autocorrelation), the process finishes. - Convergence Phase: If the initial phase is not robust, the tool enters a convergence phase, continuing runs until sufficient statistical quality is reached.
Customizing Control:
- To disable the convergence phase and use a fixed number of runs, use
--no-convergence-phasecombined with-r <count>. - To limit the time spent in the convergence phase, use
--convergence-max-seconds=NUM(defaults to 300 seconds).
Use futhark-literate to generate Markdown documentation
masterThe
futhark literatecommand compiles a Futhark program and generates a Markdown file (e.g.,foo.mdforfoo.fut) containing a prettyprinted version of the code. This is useful for demonstrating programming techniques.Key behaviors:
- Top-level comments starting with
--(dash-dash-space) are converted to ordinary text. - Top-level definitions are enclosed in Markdown code blocks.
- Directives (lines starting with
-- >) are executed and replaced with their output. - Generated assets (images, etc.) are placed in a directory named
<program_name>-img/.
Warning: Do not run untrusted programs as directives can execute arbitrary shell commands or file operations.
futhark literate [options...] program- Top-level comments starting with
Translate C multidimensional arrays to Futhark
masterC code often simulates multidimensional arrays using a single-dimensional array and manual index calculation, such as
a[i * M + j] = foo;(whereMis the inner dimension).In Futhark, you can represent this directly as a multidimensional array. For example, a C allocation of
malloc(N * M * sizeof(int))corresponds to a Futhark type of[N][M]i32. The update expressiona[i * M + j] = footranslates to:let a[i,j] = foo in ...Initialization: Since you cannot allocate and then loop to initialize in Futhark, use
replicate,iota, ormapto create arrays with initial values. In the worst case, usereplicatefollowed by ado-loop with in-place updates.let a[i,j] = foo in ...Use the Futhark interpreter and REPL
masterIf you do not want to compile your code, you can use the interpreter to run Futhark code directly. Note that the interpreter is significantly slower than compiled code.
futhark run: Runs a Futhark file.futhark repl: Opens an interactive prompt for experimenting with Futhark expressions.