Skip to content

Commit e14143f

Browse files
Add count_change_iter and count_chnage_iter_multicore and their configs
1 parent 3ad24a0 commit e14143f

File tree

6 files changed

+223
-10
lines changed

6 files changed

+223
-10
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
(*************************************************************************************
3+
* The main procudure cc enumerates all possible ways to distribute change for a *
4+
* given set of different denominations of coins of certain quantities for some given *
5+
* amount. *
6+
*************************************************************************************)
7+
8+
let n = try int_of_string @@ Sys.argv.(1) with _ -> 960
9+
module L = List
10+
11+
(* Selectors for tuples *)
12+
let get_1 (x,_,_) = x
13+
14+
let get_2 (_,y,_) = y
15+
16+
let get_3 (_,_,z) = z
17+
18+
19+
let rec des amt coins curr acc stack =
20+
match amt, coins, stack with
21+
| _, _, [] -> acc
22+
| 0, _, _ -> begin
23+
let stack_top = L.hd stack in
24+
let stack_rest = L.tl stack in
25+
let get_amt = get_1 in
26+
let get_coins = get_2 in
27+
let get_curr = get_3 in
28+
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) (curr::acc) stack_rest
29+
end
30+
| _, [], _ -> begin
31+
let stack_top = L.hd stack in
32+
let stack_rest = L.tl stack in
33+
let get_amt = get_1 in
34+
let get_coins = get_2 in
35+
let get_curr = get_3 in
36+
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) acc stack_rest
37+
end
38+
| _, (den, qty)::rst, _ -> begin
39+
let new_amt = amt - den in
40+
let new_coins = (den, qty -1)::rst in
41+
if den > amt then
42+
des amt rst curr acc stack
43+
else if qty = 1 then
44+
des new_amt rst (den::curr) acc stack
45+
else if (L.tl coins) = [] || curr = [] then
46+
des new_amt new_coins (den::curr) acc stack
47+
else
48+
des new_amt new_coins (den::curr) acc ((amt, rst, curr)::stack)
49+
end
50+
51+
let cc amt (coins : ((int * int) list)) =
52+
let rec aux c stack =
53+
match c with
54+
| [] -> des amt coins [] [] stack
55+
| _ -> aux (L.tl c) (((amt, c, []))::stack) in
56+
aux coins []
57+
58+
let coins_input : (int * int) list =
59+
let cs = [250 ; 100 ; 25 ; 10 ; 5 ; 1] in
60+
let qs = [55 ; 88 ; 88 ; 99 ; 122 ; 177] in
61+
L.combine cs qs
62+
63+
let () =
64+
let x = cc n coins_input in
65+
Printf.printf "possibilities = %d\n" (L.length x)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
let num_domains = try int_of_string @@ Sys.argv.(1) with _ -> 1
2+
let n = try int_of_string @@ Sys.argv.(2) with _ -> 960
3+
4+
module A = Array
5+
module L = List
6+
module T = Domainslib.Task
7+
8+
(* Selectors for tuples *)
9+
let get_1 (x,_,_) = x
10+
11+
let get_2 (_,y,_) = y
12+
13+
let get_3 (_,_,z) = z
14+
15+
let rec des amt coins curr acc stack =
16+
(* Descends down the left branch *)
17+
match amt, coins, stack with
18+
| _, _, [] -> acc
19+
| 0, _, _ -> begin
20+
let stack_top = L.hd stack in
21+
let stack_rest = L.tl stack in
22+
let get_amt = get_1 in
23+
let get_coins = get_2 in
24+
let get_curr = get_3 in
25+
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) (curr::acc) stack_rest
26+
end
27+
| _, [], _ -> begin
28+
let stack_top = L.hd stack in
29+
let stack_rest = L.tl stack in
30+
let get_amt = get_1 in
31+
let get_coins = get_2 in
32+
let get_curr = get_3 in
33+
des (get_amt stack_top) (get_coins stack_top) (get_curr stack_top) acc stack_rest
34+
end
35+
| _, (den, qty)::rst, _ -> begin
36+
let new_amt = amt - den in
37+
let new_coins = (den, qty -1)::rst in
38+
if den > amt then
39+
des amt rst curr acc stack
40+
else if qty = 1 then
41+
des new_amt rst (den::curr) acc stack
42+
else if (L.tl coins) = [] || curr = [] then
43+
des new_amt new_coins (den::curr) acc stack
44+
else
45+
des new_amt new_coins (den::curr) acc ((amt, rst, curr)::stack)
46+
end
47+
48+
let setup_stacks amt coins =
49+
(* Assumes that the that qty for each den in coins is greater than or equal to 2 *)
50+
let a = A.init (L.length coins) (fun _ -> (0,[], [], [])) in
51+
let rec aux count c =
52+
match c with
53+
| [] -> a
54+
| (den, qty)::rst -> begin
55+
let new_amt = amt - den in
56+
let new_c = (den, qty-1)::rst in
57+
if den > amt then
58+
aux count (L.tl c)
59+
else if qty = 1 then begin
60+
a.(count) <- (new_amt, (L.tl c), (den::[]), [(new_amt, rst, den::[])]);
61+
aux (count+1) (L.tl c)
62+
end else begin
63+
a.(count) <- (new_amt, new_c, (den::[]), [(new_amt, rst, den::[])]);
64+
aux (count+1) (L.tl c)
65+
end
66+
end
67+
in
68+
aux 0 coins
69+
70+
let cc_par pool amt (coins : ((int * int) list)) arr =
71+
let setup = setup_stacks amt coins in
72+
let len = A.length arr in
73+
let amt = fun (x, _, _, _) -> x in
74+
let c = fun (_, x, _, _) -> x in
75+
let curr = fun (_, _, x, _) -> x in
76+
let stack = fun (_, _, _, x) -> x in
77+
T.parallel_for pool ~start:0 ~finish:(len-1) ~body:(fun i ->
78+
Printf.printf "%d\n" i;
79+
arr.(i) <- des (amt setup.(i)) (c setup.(i)) (curr setup.(i)) [] (stack setup.(i));
80+
)
81+
82+
let coins_input : (int * int) list =
83+
let cs = [250 ; 100 ; 25 ; 10 ; 5 ; 1] in
84+
let qs = [55 ; 88 ; 88 ; 99 ; 122 ; 177] in
85+
L.combine cs qs
86+
87+
let arr = A.init (L.length coins_input) (fun _ -> [])
88+
89+
let _ =
90+
let pool = T.setup_pool ~num_additional_domains:(num_domains - 1) () in
91+
T.run pool (fun () -> cc_par pool n coins_input arr);
92+
Printf.printf "possibilites = %d\n" (A.fold_left (+) 0 (A.map L.length arr));
93+
T.teardown_pool pool

benchmarks/multicore-numerical/dune

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,37 @@
113113
(modules evolutionary_algorithm_multicore)
114114
(libraries domainslib))
115115

116+
(executable
117+
(name count_change_iter)
118+
(modes native byte)
119+
(modules count_change_iter))
120+
121+
(executable
122+
(name count_change_iter_multicore)
123+
(modules count_change_iter_multicore)
124+
(libraries domainslib))
125+
116126
(alias
117127
(name multibench_parallel)
118128
(deps mandelbrot6_multicore.exe spectralnorm2_multicore.exe quicksort.exe
119129
quicksort_multicore.exe binarytrees5_multicore.exe
120-
game_of_life.exe game_of_life_multicore.exe
121-
matrix_multiplication.exe matrix_multiplication_multicore.exe
122-
matrix_multiplication_tiling_multicore.exe nbody.exe
123-
nbody_multicore.exe nqueens_multicore.exe mergesort.exe mergesort_multicore.exe
124-
floyd_warshall.exe floyd_warshall_multicore.exe
125-
LU_decomposition.exe LU_decomposition_multicore.exe
126-
evolutionary_algorithm_multicore.exe evolutionary_algorithm.exe nqueens.exe))
130+
game_of_life.exe game_of_life_multicore.exe
131+
matrix_multiplication.exe matrix_multiplication_multicore.exe
132+
matrix_multiplication_tiling_multicore.exe nbody.exe
133+
nbody_multicore.exe nqueens_multicore.exe mergesort.exe mergesort_multicore.exe
134+
floyd_warshall.exe floyd_warshall_multicore.exe
135+
LU_decomposition.exe LU_decomposition_multicore.exe
136+
evolutionary_algorithm_multicore.exe evolutionary_algorithm.exe nqueens.exe
137+
count_change_iter_multicore.exe count_change_iter.exe))
127138

128139
(alias
129140
(name buildbench)
130141
(deps game_of_life.exe matrix_multiplication.exe quicksort.exe
131142
mergesort.exe floyd_warshall.exe LU_decomposition.exe
132-
evolutionary_algorithm.exe nqueens.exe))
143+
evolutionary_algorithm.exe nqueens.exe count_change_iter.exe))
133144

134145
(alias
135146
(name bytebench)
136147
(deps game_of_life.bc matrix_multiplication.bc quicksort.bc
137148
mergesort.bc floyd_warshall.bc evolutionary_algorithm.bc
138-
LU_decomposition.bc nbody.bc mergesort.bc nqueens.bc))
149+
LU_decomposition.bc nbody.bc mergesort.bc nqueens.bc count_change_iter.bc))

multicore_parallel_run_config.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@
194194
{ "params": "24 15", "paramwrapper": "taskset --cpu-list 2-13,16-27" }
195195
]
196196
},
197+
{
198+
"executable": "benchmarks/multicore-numerical/count_change_iter.exe",
199+
"name": "count_change_iter",
200+
"tags": ["10s_100s", "macro_bench"],
201+
"runs": [
202+
{ "params": "2200", "paramwrapper": "taskset --cpu-list 2-13" }
203+
]
204+
},
205+
{
206+
"executable": "benchmarks/multicore-numerical/count_change_iter_multicore.exe",
207+
"name": "count_change_iter_multicore",
208+
"tags": ["macro_bench","10s_100s"],
209+
"runs": [
210+
{ "params": "1 2200", "paramwrapper": "taskset --cpu-list 2-13"},
211+
{ "params": "2 2200", "paramwrapper": "taskset --cpu-list 2-13" },
212+
{ "params": "4 2200", "paramwrapper": "taskset --cpu-list 2-13" },
213+
{ "params": "8 2200", "paramwrapper": "taskset --cpu-list 2-13" },
214+
{ "params": "12 2200", "paramwrapper": "taskset --cpu-list 2-13" },
215+
{ "params": "16 2200", "paramwrapper": "taskset --cpu-list 2-13,16-27" },
216+
{ "params": "20 2200", "paramwrapper": "taskset --cpu-list 2-13,16-27" },
217+
{ "params": "24 2200", "paramwrapper": "taskset --cpu-list 2-13,16-27" }
218+
]
219+
},
197220

198221
{
199222
"executable": "benchmarks/multicore-numerical/quicksort.exe",

run_config.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,19 @@
289289
}
290290
]
291291
},
292-
292+
{
293+
"executable": "benchmarks/multicore-numerical/count_change_iter.exe",
294+
"name": "count_change_iter",
295+
"tags": [
296+
"1s_10s",
297+
"macro_bench"
298+
],
299+
"runs": [
300+
{
301+
"params": "1600"
302+
}
303+
]
304+
},
293305
{
294306
"executable": "benchmarks/multicore-grammatrix/grammatrix.exe",
295307
"name": "grammatrix",

run_config_byte.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@
217217
}
218218
]
219219
},
220+
{
221+
"executable": "benchmarks/multicore-numerical/count_change_iter.bc",
222+
"name": "count_change_iter.bc",
223+
"runs": [
224+
{
225+
"params": "2200"
226+
}
227+
]
228+
},
220229
{
221230
"executable": "benchmarks/valet/test_lwt.bc",
222231
"name": "test_lwt.bc",

0 commit comments

Comments
 (0)