Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add change-counting benchmark #369

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions benchmarks/multicore-numerical/count_change_iter.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

(*************************************************************************************
* The main procudure cc enumerates all possible ways to distribute change for a *
* given set of different denominations of coins of certain quantities for some given *
* amount. *
*************************************************************************************)

let n = try int_of_string @@ Sys.argv.(1) with _ -> 960
module L = List

(* Selectors for tuples *)
let get_1 (x,_,_) = x
moazzammoriani marked this conversation as resolved.
Show resolved Hide resolved

let get_2 (_,y,_) = y

let get_3 (_,_,z) = z


let rec des amt coins curr acc stack =
match amt, coins, stack with
| _, _, [] -> acc
| 0, _, _ -> begin
let stack_top = L.hd stack in
let stack_rest = L.tl stack in
let get_amt = get_1 in
let get_coins = get_2 in
let get_curr = get_3 in
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) (curr::acc) stack_rest
end
| _, [], _ -> begin
let stack_top = L.hd stack in
let stack_rest = L.tl stack in
let get_amt = get_1 in
let get_coins = get_2 in
let get_curr = get_3 in
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) acc stack_rest
end
| _, (den, qty)::rst, _ -> begin
let new_amt = amt - den in
let new_coins = (den, qty -1)::rst in
if den > amt then
des amt rst curr acc stack
else if qty = 1 then
des new_amt rst (den::curr) acc stack
else if (L.tl coins) = [] || curr = [] then
des new_amt new_coins (den::curr) acc stack
else
des new_amt new_coins (den::curr) acc ((amt, rst, curr)::stack)
end

let cc amt (coins : ((int * int) list)) =
let rec aux c stack =
match c with
| [] -> des amt coins [] [] stack
| _ -> aux (L.tl c) (((amt, c, []))::stack) in
aux coins []

let coins_input : (int * int) list =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tuple here could be replaced with a record such as:

type coin = { denom : int; num : int; }

This would make it easier to read what's stored in the tuple.

let cs = [500 ; 250 ; 150; 100 ; 75 ; 50 ; 25 ; 20 ; 10 ; 5 ; 2 ; 1] in
let qs = [22; 55 ; 88 ; 88 ; 99 ; 99 ; 122; 122; 122 ; 122; 177; 177] in
L.combine cs qs

let () =
let x = cc n coins_input in
Printf.printf "possibilities = %d\n" (L.length x)
92 changes: 92 additions & 0 deletions benchmarks/multicore-numerical/count_change_iter_multicore.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
let num_domains = try int_of_string @@ Sys.argv.(1) with _ -> 1
let n = try int_of_string @@ Sys.argv.(2) with _ -> 960

module A = Array
module L = List
module T = Domainslib.Task

(* Selectors for tuples *)
let get_1 (x,_,_) = x

let get_2 (_,y,_) = y

let get_3 (_,_,z) = z

let rec des amt coins curr acc stack =
(* Descends down the left branch *)
match amt, coins, stack with
| _, _, [] -> acc
| 0, _, _ -> begin
let stack_top = L.hd stack in
let stack_rest = L.tl stack in
let get_amt = get_1 in
let get_coins = get_2 in
let get_curr = get_3 in
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) (curr::acc) stack_rest
end
| _, [], _ -> begin
let stack_top = L.hd stack in
let stack_rest = L.tl stack in
let get_amt = get_1 in
let get_coins = get_2 in
let get_curr = get_3 in
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) acc stack_rest
end
| _, (den, qty)::rst, _ -> begin
let new_amt = amt - den in
let new_coins = (den, qty -1)::rst in
if den > amt then
des amt rst curr acc stack
else if qty = 1 then
des new_amt rst (den::curr) acc stack
else if (L.tl coins) = [] || curr = [] then
des new_amt new_coins (den::curr) acc stack
else
des new_amt new_coins (den::curr) acc ((amt, rst, curr)::stack)
end

let setup_stacks amt coins =
(* Assumes that the that qty for each den in coins is greater than or equal to 2 *)
let a = A.init (L.length coins) (fun _ -> (0,[], [], [])) in
let rec aux count c =
match c with
| [] -> a
| (den, qty)::rst -> begin
let new_amt = amt - den in
let new_c = (den, qty-1)::rst in
if den > amt then
aux count (L.tl c)
else if qty = 1 then begin
a.(count) <- (new_amt, (L.tl c), (den::[]), [(new_amt, rst, den::[])]);
aux (count+1) (L.tl c)
end else begin
a.(count) <- (new_amt, new_c, (den::[]), [(new_amt, rst, den::[])]);
aux (count+1) (L.tl c)
end
end
in
aux 0 coins

let cc_par pool amt (coins : ((int * int) list)) arr =
let setup = setup_stacks amt coins in
let len = A.length arr in
let amt = fun (x, _, _, _) -> x in
let c = fun (_, x, _, _) -> x in
let curr = fun (_, _, x, _) -> x in
let stack = fun (_, _, _, x) -> x in
T.parallel_for pool ~start:0 ~finish:(len-1) ~body:(fun i ->
arr.(i) <- des (amt setup.(i)) (c setup.(i)) (curr setup.(i)) [] (stack setup.(i));
)

let coins_input : (int * int) list =
let cs = [500 ; 250 ; 150; 100 ; 75 ; 50 ; 25 ; 20 ; 10 ; 5 ; 2 ; 1] in
let qs = [22; 55 ; 88 ; 88 ; 99 ; 99 ; 122; 122; 122 ; 122; 177; 177] in
L.combine cs qs

let arr = A.init (L.length coins_input) (fun _ -> [])

let _ =
let pool = T.setup_pool ~num_additional_domains:(num_domains - 1) () in
T.run pool (fun () -> cc_par pool n coins_input arr);
Printf.printf "possibilites = %d\n" (A.fold_left (+) 0 (A.map L.length arr));
T.teardown_pool pool
29 changes: 20 additions & 9 deletions benchmarks/multicore-numerical/dune
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,37 @@
(modules evolutionary_algorithm_multicore)
(libraries domainslib))

(executable
(name count_change_iter)
(modes native byte)
(modules count_change_iter))

(executable
(name count_change_iter_multicore)
(modules count_change_iter_multicore)
(libraries domainslib))

(alias
(name multibench_parallel)
(deps mandelbrot6_multicore.exe spectralnorm2_multicore.exe quicksort.exe
quicksort_multicore.exe binarytrees5_multicore.exe
game_of_life.exe game_of_life_multicore.exe
matrix_multiplication.exe matrix_multiplication_multicore.exe
matrix_multiplication_tiling_multicore.exe nbody.exe
nbody_multicore.exe nqueens_multicore.exe mergesort.exe mergesort_multicore.exe
floyd_warshall.exe floyd_warshall_multicore.exe
LU_decomposition.exe LU_decomposition_multicore.exe
evolutionary_algorithm_multicore.exe evolutionary_algorithm.exe nqueens.exe))
game_of_life.exe game_of_life_multicore.exe
matrix_multiplication.exe matrix_multiplication_multicore.exe
matrix_multiplication_tiling_multicore.exe nbody.exe
nbody_multicore.exe nqueens_multicore.exe mergesort.exe mergesort_multicore.exe
floyd_warshall.exe floyd_warshall_multicore.exe
LU_decomposition.exe LU_decomposition_multicore.exe
evolutionary_algorithm_multicore.exe evolutionary_algorithm.exe nqueens.exe
count_change_iter_multicore.exe count_change_iter.exe))

(alias
(name buildbench)
(deps game_of_life.exe matrix_multiplication.exe quicksort.exe
mergesort.exe floyd_warshall.exe LU_decomposition.exe
evolutionary_algorithm.exe nqueens.exe))
evolutionary_algorithm.exe nqueens.exe count_change_iter.exe))

(alias
(name bytebench)
(deps game_of_life.bc matrix_multiplication.bc quicksort.bc
mergesort.bc floyd_warshall.bc evolutionary_algorithm.bc
LU_decomposition.bc nbody.bc mergesort.bc nqueens.bc))
LU_decomposition.bc nbody.bc mergesort.bc nqueens.bc count_change_iter.bc))
23 changes: 23 additions & 0 deletions multicore_parallel_run_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,29 @@
{ "params": "24 15", "paramwrapper": "taskset --cpu-list 2-13,16-27" }
]
},
{
"executable": "benchmarks/multicore-numerical/count_change_iter.exe",
"name": "count_change_iter",
"tags": ["10s_100s", "macro_bench"],
"runs": [
{ "params": "400", "paramwrapper": "taskset --cpu-list 2-13" }
]
},
{
"executable": "benchmarks/multicore-numerical/count_change_iter_multicore.exe",
"name": "count_change_iter_multicore",
"tags": ["macro_bench","10s_100s"],
"runs": [
{ "params": "1 400", "paramwrapper": "taskset --cpu-list 2-13"},
{ "params": "2 400", "paramwrapper": "taskset --cpu-list 2-13" },
{ "params": "4 400", "paramwrapper": "taskset --cpu-list 2-13" },
{ "params": "8 400", "paramwrapper": "taskset --cpu-list 2-13" },
{ "params": "12 400", "paramwrapper": "taskset --cpu-list 2-13" },
{ "params": "16 400", "paramwrapper": "taskset --cpu-list 2-13,16-27" },
{ "params": "20 400", "paramwrapper": "taskset --cpu-list 2-13,16-27" },
{ "params": "24 400", "paramwrapper": "taskset --cpu-list 2-13,16-27" }
]
},

{
"executable": "benchmarks/multicore-numerical/quicksort.exe",
Expand Down
14 changes: 13 additions & 1 deletion run_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,19 @@
}
]
},

{
"executable": "benchmarks/multicore-numerical/count_change_iter.exe",
"name": "count_change_iter",
"tags": [
"10s_100s",
"macro_bench"
],
"runs": [
{
"params": "400"
}
]
},
{
"executable": "benchmarks/multicore-grammatrix/grammatrix.exe",
"name": "grammatrix",
Expand Down
9 changes: 9 additions & 0 deletions run_config_byte.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@
}
]
},
{
"executable": "benchmarks/multicore-numerical/count_change_iter.bc",
"name": "count_change_iter.bc",
"runs": [
{
"params": "400"
}
]
},
{
"executable": "benchmarks/valet/test_lwt.bc",
"name": "test_lwt.bc",
Expand Down