Concurrency, Parallelism, and Distributed Systems
Concurrency refers to running multiple computations and switching from one to the other rapidly (green threads), whereas parallelism refers to using multiple OS-level threads to coordinate computation. Since OCaml 5.0, OCaml supports concurrency with the Effect system and parallelism with Domains.
A concurrency library using the OCaml effect system.
Unlike the other concurrency libraries,
eiodoesn’t require the usage of monads. This makes it easier to code using ‘plain` OCaml code.
- video presentation on
- video presentation on
- lwt: a monadic concurrency library. Concurrent code uses monads to express the higher-level abstractions of control flow.
- Async: another monadic concurrency library developed by Jane Street. This library is covered in Real World OCaml. While the concept is very similar to lwt, small discrepancies make compatibility between the libraries difficult.
- Riot: Riot is an in-development library to support actor-based processing (similar to Erlang) on OCaml 5.0.
Bindings to libuv,
an event loop-based system that runs
node.io. This is also a replacement for the
Unixmodule, allowing for full process control in a system-independent manner.
- The blog post that introduced Async
- A user gives up on Async
- Cooperative Concurrency in OCaml: Using Async
Domain (thread)-based Parallelism
OCaml 5.0 introduced
domains, which roughly map to the number of cores in a CPU.
They allow for true parallelism in OCaml.
- Parallel Programming in Multicore OCaml: great article on using the OCaml’s multicore capabilities.
- kCAS: Software-Transactional Memory (STM) in OCaml. STM allows for programming across threads (domains) via lockless data structures and interfaces that make the difficult work of parallelism easier for average programmers.
- MoonPool: Thread pools with work-stealing for domains.
Pre-5.0, OCaml supported parallelism only by running multiple processes. This option still exists and is supported by many libraries.
- Parmap: Provides easy-to-use parallel map and fold functions. The library makes use of forking to create short-lived child processes, and memory mapping to feed the data back to the parent process.
- Parany: Generalized map reduce for multicore computers (unfold, map in parallel, fold). Parany can process in parallel an “infinite” stream of elements (too big to fit in memory). Any Parmap functionality can be reimplemented using parany.
- hack-parallel: Parallel processing library using shared memory. Used by Facebook’s Hack.
- lwt-parallel: Lower level mechanism to create child processes in lwt and have it communicate with the parent via socket.
- ForkWork: Similar to Parmap above.
- By interfacing with external C code through the FFI, OCaml can pass off long-running computations to C threads running at the same time as OCaml code. This is made easier nowadays due to CTypes (see ffi)
- Nproc: A process pool implementation for OCaml using lwt. Rather than creating or forking processes as needed, preallocates them and sends them units of work as required.
- Ocamlnet: An enhanced system platform library. It contains the netmulticore library to compute tasks on as many cores of the machine as needed. This is the most powerful implementation of parellelism currently available for OCaml, as it is capable of creating a shared memory region, and running a custom-made garbage collector on said region.
Distributed computing is similar to process-based parallelism, except that the child processes may or may not be on remote machines. Therefore, distributed computing libraries generally also perform parallelism on the same machine as well.
- Rpc.Parallel: a library for spawning processes on a cluster of machines, and passing typed messages between them.
- zmq: ZeroMQ an open-source universal messaging library.
- Functory: a distributed computing library which facilitates distributed execution of parallelizable computations in a seamless fashion.
- MPI: message Passing Interface bindings for OCaml.
- ocaml-rpc: light library to deal with RPCs in OCaml.
- distributed: Library for distributed computation in OCaml.
Similar to Erlang’s model and inspired by Cloud Haskell.
- reactor (alpha): Actor model for OCaml, similar to Erlang Elixir.