• Stars
    star
    161
  • Rank 226,612 (Top 5 %)
  • Language
    OCaml
  • License
    ISC License
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parallel Programming over Domains

Domainslib - Nested-parallel programming

Domainslib provides support for nested-parallel programming. Domainslib provides async/await mechanism for spawning parallel tasks and awaiting their results. On top of this mechanism, domainslib provides parallel iteration functions. At its core, domainslib has an efficient implementation of work-stealing queue in order to efficiently share tasks with other domains.

Here is a sequential program that computes nth Fibonacci number using recursion:

(* fib.ml *)
let n = try int_of_string Sys.argv.(1) with _ -> 1

let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2)

let main () =
  let r = fib n in
  Printf.printf "fib(%d) = %d\n%!" n r

let _ = main ()

We can parallelise this program using Domainslib:

(* fib_par.ml *)
let num_domains = try int_of_string Sys.argv.(1) with _ -> 1
let n = try int_of_string Sys.argv.(2) with _ -> 1

(* Sequential Fibonacci *)
let rec fib n = 
  if n < 2 then 1 else fib (n - 1) + fib (n - 2)

module T = Domainslib.Task

let rec fib_par pool n =
  if n > 20 then begin
    let a = T.async pool (fun _ -> fib_par pool (n-1)) in
    let b = T.async pool (fun _ -> fib_par pool (n-2)) in
    T.await pool a + T.await pool b
  end else 
    (* Call sequential Fibonacci if the available work is small *)
    fib n

let main () =
  let pool = T.setup_pool ~num_domains:(num_domains - 1) () in
  let res = T.run pool (fun _ -> fib_par pool n) in
  T.teardown_pool pool;
  Printf.printf "fib(%d) = %d\n" n res

let _ = main ()

The parallel program scales nicely compared to the sequential version. The results presented below were obtained on a 2.3 GHz Quad-Core Intel Core i7 MacBook Pro with 4 cores and 8 hardware threads.

$ hyperfine './fib.exe 42' './fib_par.exe 2 42' \
            './fib_par.exe 4 42' './fib_par.exe 8 42'
Benchmark 1: ./fib.exe 42
  Time (mean ± sd):     1.217 s ±  0.018 s    [User: 1.203 s, System: 0.004 s]
  Range (min … max):    1.202 s …  1.261 s    10 runs

Benchmark 2: ./fib_par.exe 2 42
  Time (mean ± sd):    628.2 ms ±   2.9 ms    [User: 1243.1 ms, System: 4.9 ms]
  Range (min … max):   625.7 ms … 634.5 ms    10 runs

Benchmark 3: ./fib_par.exe 4 42
  Time (mean ± sd):    337.6 ms ±  23.4 ms    [User: 1321.8 ms, System: 8.4 ms]
  Range (min … max):   318.5 ms … 377.6 ms    10 runs

Benchmark 4: ./fib_par.exe 8 42
  Time (mean ± sd):    250.0 ms ±   9.4 ms    [User: 1877.1 ms, System: 12.6 ms]
  Range (min … max):   242.5 ms … 277.3 ms    11 runs

Summary
  './fib_par2.exe 8 42' ran
    1.35 ± 0.11 times faster than './fib_par.exe 4 42'
    2.51 ± 0.10 times faster than './fib_par.exe 2 42'
    4.87 ± 0.20 times faster than './fib.exe 42'

More example programs are available here.

Installation

You can install this library using OPAM.

$ opam switch create 5.0.0+trunk --repo=default,alpha=git+https://github.com/kit-ty-kate/opam-alpha-repository.git
$ opam install domainslib

Development

If you are interested in hacking on the implementation, then opam pin this repository:

$ opam switch create 5.0.0+trunk --repo=default,alpha=git+https://github.com/kit-ty-kate/opam-alpha-repository.git
$ git clone https://github.com/ocaml-multicore/domainslib
$ cd domainslib
$ opam pin add domainslib file://`pwd`

More Repositories

1

ocaml-multicore

Multicore OCaml
OCaml
763
star
2

ocaml-effects-tutorial

Concurrent Programming with Effect Handlers
OCaml
642
star
3

eio

Effects-based direct-style IO for multicore OCaml
OCaml
509
star
4

effects-examples

Examples to illustrate the use of algebraic effects in Multicore OCaml
OCaml
403
star
5

parallel-programming-in-multicore-ocaml

Tutorial on Multicore OCaml parallel programming with domainslib
OCaml
277
star
6

ocaml5-tutorial

A hands-on tutorial on the new parallelism features in OCaml 5
OCaml
193
star
7

saturn

Lock-free data structures for multicore OCaml
OCaml
173
star
8

awesome-multicore-ocaml

A collection of libraries, experiments and ideas relating to OCaml 5 (multicore + effects)
141
star
9

reagents

Reagents for multicore OCaml
OCaml
126
star
10

kcas

Software Transactional Memory for OCaml
OCaml
91
star
11

meio

Monitor Eio programs
OCaml
76
star
12

ocaml-uring

Bindings to io_uring for OCaml
OCaml
62
star
13

multicore-opam

OPAM repo for OCaml multicore development
53
star
14

picos

Interoperable effects based concurrency
OCaml
51
star
15

multicoretests

PBT testsuite and libraries for testing multicore OCaml
OCaml
36
star
16

lwt_eio

Use Lwt libraries from within Eio
OCaml
33
star
17

eventlog-tools

Tools for the runtime tracing in OCaml 4.11.0 and higher
OCaml
31
star
18

multicore-talks

Repository containing slides and examples from the 2020 OCaml Workshop talk on "Parallelising your OCaml code with Multicore OCaml"
TeX
30
star
19

dscheck

Experimental model checker for testing concurrent algorithms
OCaml
29
star
20

ocaml-iomux

Io multiplexers bindings for ocaml (poll/kqueue/epoll and so on)
OCaml
27
star
21

retro-httpaf-bench

Benchmarking environment for http servers
Jupyter Notebook
21
star
22

icfp-2023-eio-tutorial

Lwt to Eio tutorial
OCaml
20
star
23

ocaml-tsan

Race detection in OCaml using the ThreadSanitizer runtime analysis.
OCaml
20
star
24

par_incr

Parallel version of incremental library
OCaml
19
star
25

ocaml-iocp

OCaml bindings to Windows' IOCP API
OCaml
17
star
26

hdr_histogram_ocaml

C
12
star
27

tezos

Tezos running on Multicore OCaml
OCaml
12
star
28

multicore-ocaml-verify

Verifying bits of Multicore OCaml implementation
Makefile
11
star
29

multicore-magic

Low-level multicore utilities for OCaml
OCaml
11
star
30

domain-local-await

A scheduler independent blocking mechanism
OCaml
11
star
31

eio-trace

Trace visualisation tool for Eio programs
OCaml
10
star
32

parafuzz

Property-based grey-box fuzzing for Multicore OCaml
OCaml
9
star
33

backoff

Exponential backoff mechanism
OCaml
9
star
34

eio_js

Eio for JavaScript environments
OCaml
7
star
35

multicore-bench

Framework for benchmarking on multiple cores on current-bench
OCaml
7
star
36

docs

Docs
5
star
37

thread-table

A lock-free thread-safe integer keyed hash table
OCaml
4
star
38

domain-local-timeout

A scheduler independent timeout mechanism
OCaml
3
star
39

dwarf_validator

Tool for validating wellformedness of DWARF unwind information
Python
3
star
40

eio_browser

Eio backend for the browser
OCaml
3
star
41

single-use-event

A scheduler agnostic blocking mechanism
OCaml
1
star