Skip to content

Commit

Permalink
Lock free skiplist with size (#99)
Browse files Browse the repository at this point in the history
Add skiplist with generalized size computation

---------

Co-authored-by: Carine Morel <[email protected]>, Sooraj Srinivasan <[email protected]>
  • Loading branch information
polytypic and sooraj-srini authored Jan 11, 2024
1 parent 719846f commit 65211c5
Show file tree
Hide file tree
Showing 12 changed files with 873 additions and 0 deletions.
57 changes: 57 additions & 0 deletions bench/bench_skiplist.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
open Saturn

let workload num_elems num_threads add remove =
let sl = Skiplist.create ~compare:Int.compare () in
let elems = Array.init num_elems (fun _ -> Random.int 10000) in
let push () =
Domain.spawn (fun () ->
let start_time = Unix.gettimeofday () in
for i = 0 to (num_elems - 1) / num_threads do
Domain.cpu_relax ();
let prob = Random.float 1.0 in
if prob < add then Skiplist.try_add sl (Random.int 10000) () |> ignore
else if prob >= add && prob < add +. remove then
Skiplist.try_remove sl (Random.int 10000) |> ignore
else Skiplist.mem sl elems.(i) |> ignore
done;
start_time)
in
let threads = List.init num_threads (fun _ -> push ()) in
let start_time_threads =
List.map (fun domain -> Domain.join domain) threads
in
let end_time = Unix.gettimeofday () in
let time_diff = end_time -. List.nth start_time_threads 0 in
time_diff

(* A write heavy workload with threads with 50% adds and 50% removes. *)
let write_heavy_workload num_elems num_threads =
workload num_elems num_threads 0.5 0.5

(* A regular workload with 90% reads, 9% adds and 1% removes. *)
let read_heavy_workload num_elems num_threads =
workload num_elems num_threads 0.09 0.01

let moderate_heavy_workload num_elems num_threads =
workload num_elems num_threads 0.2 0.1

let balanced_heavy_workload num_elems num_threads =
workload num_elems num_threads 0.3 0.2

let bench ~workload_type ~num_elems ~num_threads () =
let workload =
if workload_type = "read_heavy" then read_heavy_workload
else if workload_type = "moderate_heavy" then moderate_heavy_workload
else if workload_type = "balanced_heavy" then balanced_heavy_workload
else write_heavy_workload
in
let results = ref [] in
for i = 1 to 10 do
let time = workload num_elems num_threads in
if i > 1 then results := time :: !results
done;
let results = List.sort Float.compare !results in
let median_time = List.nth results 4 in
let median_throughput = Float.of_int num_elems /. median_time in
Benchmark_result.create_generic ~median_time ~median_throughput
("atomic_skiplist_" ^ workload_type)
4 changes: 4 additions & 0 deletions bench/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ let benchmark_list =
Mpmc_queue.bench ~use_cas:true ~takers:4 ~pushers:4;
Mpmc_queue.bench ~use_cas:true ~takers:1 ~pushers:8;
Mpmc_queue.bench ~use_cas:true ~takers:8 ~pushers:1;
Bench_skiplist.bench ~workload_type:"read_heavy" ~num_elems:2000000
~num_threads:2;
Bench_skiplist.bench ~workload_type:"moderate_heavy" ~num_elems:2000000
~num_threads:2;
]

let () =
Expand Down
1 change: 1 addition & 0 deletions src/saturn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ module Single_prod_single_cons_queue =

module Single_consumer_queue = Saturn_lockfree.Single_consumer_queue
module Relaxed_queue = Mpmc_relaxed_queue
module Skiplist = Saturn_lockfree.Skiplist
1 change: 1 addition & 0 deletions src/saturn.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ module Single_prod_single_cons_queue =

module Single_consumer_queue = Saturn_lockfree.Single_consumer_queue
module Relaxed_queue = Mpmc_relaxed_queue
module Skiplist = Saturn_lockfree.Skiplist
1 change: 1 addition & 0 deletions src_lockfree/saturn_lockfree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ module Single_prod_single_cons_queue = Spsc_queue
module Single_consumer_queue = Mpsc_queue
module Relaxed_queue = Mpmc_relaxed_queue
module Size = Size
module Skiplist = Skiplist
1 change: 1 addition & 0 deletions src_lockfree/saturn_lockfree.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ module Work_stealing_deque = Ws_deque
module Single_prod_single_cons_queue = Spsc_queue
module Single_consumer_queue = Mpsc_queue
module Relaxed_queue = Mpmc_relaxed_queue
module Skiplist = Skiplist
module Size = Size
Loading

0 comments on commit 65211c5

Please sign in to comment.