Parallel Algebraic Effect Handlers

Ningning Xie, Daniel D. Johnson, Dougal Maclaurin, Adam PaszkeProc. ACM Program. Lang 2021 · DOI

Abstract

Algebraic effects and handlers support composable and structured control-flow abstraction. However, existing designs of algebraic effects often require effects to be executed sequentially. This paper studies parallel algebraic effect handlers. In particular, we formalize $\lambda^p$, an untyped lambda calculus which models two key features, effect handlers and parallelizable computations, the latter of which takes the form of a for expression as inspired by the Dex programming language. We present various interesting examples expressible in our calculus, and provide a Haskell implementation. We hope this paper provides a basis for future designs and implementations of parallel algebraic effect handlers.

Introduction

Algebraic effects and handlers have emerged as a powerful modular framework for managing side effects like state, exceptions, and I/O. By separating the definition of an effectful operation from its implementation, they allow for highly composable software. However, a significant limitation persists: most effect handler implementations are inherently sequential. They assume a linear execution order, which clashes with the requirements of high-performance array programming and GPU-accelerated computing.

In languages like Dex, parallel for loops are first-class citizens. When such loops contain effects—for instance, updating a global accumulator—standard handlers typically force sequential execution to maintain consistency. This "parallelism gap" prevents developers from defining custom, high-performance parallel effects. This paper introduces $\lambda^p$, a formal calculus designed to bridge this gap by allowing users to define how handlers interact with parallel loops.

The $\lambda^p$ Calculus and Parallel Handlers

The core contribution of this work is the formalization of $\lambda^p$, an untyped lambda calculus extended with parallel handlers. Unlike traditional handlers that only define return and op clauses, $\lambda^p$ introduces a third component: the traverse clause.

Syntax and operational semantics of the $\lambda^p$ calculus, highlighting the novel traverse rule.

As shown in Figure 1, the syntax includes a for x : n. e expression for parallel iteration. The handler $h$ is defined as a triple $\{return \mapsto f_r, op \mapsto f_p, traverse \mapsto f_t\}$. The traverse clause $f_t$ defines the strategy for "walking over" a parallel loop. When a handled computation encounters a for loop, the traverse rule (shown in the operational semantics) allows the handler to restructure the computation. This typically involves pushing the handler inside the loop iterations, enabling them to run in parallel, and then specifying how to aggregate the results.

Key Insights: Parallelism as an Algebraic Property

The traverse clause effectively decouples the loop logic from the effect logic. Instead of the compiler guessing if a loop can be parallelized, the programmer provides the "recipe" for parallel execution within the handler. This is particularly useful for effects that rely on associative operations, such as parallel accumulation (Accum).

Furthermore, the calculus supports "weak exceptions." In a parallel context, a standard exception might require expensive global synchronization to stop all threads immediately. $\lambda^p$ allows for a model where an error in one iteration prevents the final result but doesn't necessarily force an immediate, costly abort of other concurrent iterations.

Expressivity and Results

The authors demonstrate the power of $\lambda^p$ through several sophisticated examples:

  • Parallel Accumulation: Implementing efficient sum or reduction operations.
  • Splittable PRNGs: Managing independent random number streams for parallel iterations using splittable state.
  • Parallel Nondeterminism (Amb): Implementing a handler that expands sequential-looking code into a parallel search across multiple branches.

Beyond the formal calculus, the authors provide a Haskell implementation based on Free monads. This implementation serves as a proof-of-concept, showing that the theoretical traverse clause can be realized in a functional programming environment.

Limitations and Future Work

While $\lambda^p$ provides a robust foundation, several areas remain for future exploration. The current calculus is untyped; a future version incorporating a type system (likely with row polymorphism) would improve safety and enable compiler optimizations. Additionally, while the logic is parallel, achieving true high-performance execution on hardware like GPUs would require deeper integration into a compiler backend. Finally, the authors note that certain handler implementations could introduce implicit synchronization points, suggesting a need for further refinement to ensure performance remains predictable.

Summary updated

BibTeX
@article{xie2021parallel,
  author = {Ningning Xie and Daniel D. Johnson and Dougal Maclaurin and Adam Paszke},
  title = {{Parallel Algebraic Effect Handlers}},
  journal = {Proc. ACM Program. Lang.},
  year = {2021},
  doi = {10.1145/3674651},
}
Figure