-
Notifications
You must be signed in to change notification settings - Fork 38
Add change-counting benchmark #369
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
Open
moazzammoriani
wants to merge
5
commits into
ocaml-bench:main
Choose a base branch
from
moazzammoriani:coins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c63aae3
Add count_change_iter and count_chnage_iter_multicore and their configs
moazzammoriani 52fc018
Insert more denominations in coins_input and change params based on this
moazzammoriani 234b704
Refactor completely
moazzammoriani b314b79
Merge branch 'main' into coins
moazzammoriani cee6438
Add missing curly brace
moazzammoriani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| let n = try int_of_string @@ Sys.argv.(1) with _ -> 120 | ||
|
|
||
| module L = List | ||
|
|
||
| type coins = (int * int) list | ||
|
|
||
| type frame = { amt : int; coins : coins; current_enum : int list } | ||
|
|
||
| let top = L.hd | ||
|
|
||
| let rest = L.tl | ||
|
|
||
| let rec run_cc (acc: int list list) (f : frame) (stack : frame list) : (int list list) = | ||
| match f.amt, f.coins, stack with | ||
| | 0, _, [] -> acc | ||
| | 0, _, _ -> run_cc (f.current_enum::acc) (top stack) (rest stack) | ||
| | _, [], [] -> acc | ||
| | _, [], _ -> run_cc acc (top stack) (rest stack) | ||
| | _, (den,qty)::rst ,_ -> | ||
| if den > f.amt then | ||
| let new_f = { amt = f.amt; coins = (rest f.coins); current_enum = f.current_enum } in | ||
| run_cc acc new_f stack | ||
| else | ||
| let new_coins = if qty == 1 then | ||
| rst | ||
| else (den, qty-1)::rst in | ||
| let left = { amt = (f.amt-den); coins = new_coins; current_enum = (den :: f.current_enum) } in | ||
| let right = { amt = f.amt; coins = rst; current_enum = f.current_enum } in | ||
| run_cc acc left (right::stack) | ||
|
|
||
| let cc amt (coins : (int * int) list) = | ||
| run_cc [] { amt = amt; coins = coins; current_enum = [] } [] | ||
|
|
||
| let coins_input : (int * int) list = | ||
| let den = [500 ; 250 ; 150; 100 ; 75 ; 50 ; 25 ; 20 ; 10 ; 5 ; 2 ; 1] in | ||
| let qty = [22; 55 ; 88 ; 88 ; 99 ; 99 ; 122; 122; 122 ; 122; 177; 177] in | ||
| L.combine den qty | ||
|
|
||
| let () = | ||
| let x = cc n coins_input in | ||
| Printf.printf "possibilities = %d\n" (L.length x) | ||
84 changes: 84 additions & 0 deletions
84
benchmarks/multicore-numerical/count_change_iter_multicore.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| let n = try int_of_string @@ Sys.argv.(1) with _ -> 120 | ||
| let num_domains = try int_of_string @@ Sys.argv.(1) with _ -> 1 | ||
|
|
||
| module A = Array | ||
| module L = List | ||
| module T = Domainslib.Task | ||
|
|
||
| type coins = (int * int) list | ||
|
|
||
| type frame = { amt : int; coins : coins; current_enum : int list } | ||
|
|
||
| let top = L.hd | ||
|
|
||
| let rest = L.tl | ||
|
|
||
| let rec run_cc first_call (acc: int list list) (f : frame) (stack : frame list) : (int list list) = | ||
| match f.amt, f.coins, stack with | ||
| | 0, _, [] -> acc | ||
| | 0, _, _ -> run_cc false (f.current_enum::acc) (top stack) (rest stack) | ||
| | _, [], [] -> acc | ||
| | _, [], _ -> run_cc false acc (top stack) (rest stack) | ||
| | _, (den,qty)::rst ,_ -> | ||
| if den > f.amt then | ||
| let new_f = { amt = f.amt; coins = (rest f.coins); current_enum = f.current_enum } in | ||
| run_cc false acc new_f stack | ||
| else | ||
| let new_coins = if qty == 1 then | ||
| rst | ||
| else (den, qty-1)::rst in | ||
| let left = { amt = (f.amt-den); coins = new_coins; current_enum = (den :: f.current_enum) } in | ||
| let right = { amt = f.amt; coins = rst; current_enum = f.current_enum } in | ||
| if not first_call then run_cc false acc left (right::stack) | ||
| else | ||
| run_cc false acc left stack | ||
|
|
||
| let cc amt (coins : (int * int) list) = | ||
| run_cc true [] { amt = amt; coins = coins; current_enum = [] } [] | ||
|
|
||
| let rec get_deductibles amt coins = | ||
| let den = fst @@ L.hd coins in | ||
| match (den > amt) with | ||
| | false -> coins | ||
| | true -> get_deductibles amt (L.tl coins) | ||
|
|
||
| let setup_frames amt coins = | ||
| let coins = get_deductibles amt coins in | ||
| let clen = L.length coins in | ||
| let a = Array.make clen { amt = 0; coins = []; current_enum = [] } in | ||
| let rec aux count coins = | ||
| match count = clen with | ||
| | true -> a | ||
| | false -> begin | ||
| let f = { | ||
| amt = amt; | ||
| coins = coins; | ||
| current_enum = [] | ||
| } in | ||
| a.(count) <- f; | ||
| aux (count+1) (L.tl coins) | ||
| end | ||
| in | ||
| aux 0 coins | ||
|
|
||
| let sum_lengths arr = A.fold_left (+) 0 (A.map L.length arr) | ||
|
|
||
| let cc_par pool amt (coins : ((int * int) list)) = | ||
| let stacks = setup_frames amt coins in | ||
| let arr = A.init (A.length stacks) (fun _ -> []) in | ||
| let len = A.length arr in | ||
| T.parallel_for pool ~start:0 ~finish:(len-1) ~body:(fun i -> | ||
| let f = stacks.(i) in | ||
| arr.(i) <- cc f.amt f.coins | ||
| ); | ||
| Printf.printf "possibilites = %d\n" (sum_lengths arr) | ||
|
|
||
| let coins_input : (int * int) list = | ||
| let den = [500 ; 250 ; 150; 100 ; 75 ; 50 ; 25 ; 20 ; 10 ; 5 ; 2 ; 1] in | ||
| let qty = [22; 55 ; 88 ; 88 ; 99 ; 99 ; 122; 122; 122 ; 122; 177; 177] in | ||
| L.combine den qty | ||
|
|
||
| let _ = | ||
| let pool = T.setup_pool ~num_additional_domains:(num_domains - 1) () in | ||
| T.run pool (fun () -> cc_par pool n coins_input); | ||
| T.teardown_pool pool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
This would make it easier to read what's stored in the tuple.