Futhark Programming Language

repository·master·Indexed 25 days ago

https://github.com/diku-dk/futhark

A 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.

Tokens
50.7K
Snippets
127
Records
377
Agent score
81%

What's inside Futhark

  1. Overview of the Futhark Programming Language

    master
    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.
  2. Overview of Futhark library backend tests

    master
    The tests in tests_lib are 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.
  3. Understand the Futhark runtime system directory

    master
    The rts directory 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.
  4. Understand Futhark Expressions and Atoms

    master

    Expressions 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, and match.

  5. Test the server protocol

    master
    While most server protocol testing is handled implicitly by the standard futhark test command, 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.
  6. Understand Futhark Size Types

    master

    Futhark 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 0 results in type [10]i32.
    • Constraints: Sizes must be expressions of type i64 that do not consume free variables.
    def f [n] (a: [n]i32) (b: [n]i32): [n]i32 =
      map2 (+) a b
  7. Features of futhark-lsp

    master

    When integrated with an LSP-compatible editor, futhark-lsp provides 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 fmt to format the current file.
    • Inlay Hints: Displays virtual text hints visualizing type-checking results, such as inferred types for lambda arguments, function arguments, let bindings, 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.
  8. Understand Futhark's Module System and Abstractions

    master

    Futhark 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.
  9. Understand the futhark-bench methodology

    master

    The benchmarking tool uses a two-phase technique to ensure statistical robustness:

    1. Warmup: A single run is performed and discarded.
    2. 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.
    3. 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-phase combined with -r <count>.
    • To limit the time spent in the convergence phase, use --convergence-max-seconds=NUM (defaults to 300 seconds).
  10. Use futhark-literate to generate Markdown documentation

    master

    The futhark literate command compiles a Futhark program and generates a Markdown file (e.g., foo.md for foo.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
  11. Translate C multidimensional arrays to Futhark

    master

    C code often simulates multidimensional arrays using a single-dimensional array and manual index calculation, such as a[i * M + j] = foo; (where M is 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 expression a[i * M + j] = foo translates to:

    let a[i,j] = foo in
    ...

    Initialization: Since you cannot allocate and then loop to initialize in Futhark, use replicate, iota, or map to create arrays with initial values. In the worst case, use replicate followed by a do-loop with in-place updates.

    let a[i,j] = foo in
    ...
  12. Use the Futhark interpreter and REPL

    master

    If 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.