-
Notifications
You must be signed in to change notification settings - Fork 0
Benchmark ready par #22
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
Draft
jazullo
wants to merge
5
commits into
main
Choose a base branch
from
benchmark-ready-par
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
277f6a1
Use allocation proposal in mergesort
jazullo 7c19278
Update piecewise fallback with mergesort optimizations
jazullo c05e64e
Add new plotting scripts and an explainer
jazullo ace12b5
Use alloc proposal in parallel mergesort
jazullo 36b3978
Construct Cilksort with reasonable parameters and connect it to bench…
jazullo 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
81 changes: 81 additions & 0 deletions
81
benchmarks/scripts/criterion-drop-in-replacement/criterionmethodology.py
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,81 @@ | ||
| #!/usr/bin/env python3 | ||
| import numpy as np | ||
| from sys import argv | ||
| import subprocess | ||
| from time import time | ||
| import math | ||
|
|
||
| from matplotlib import pyplot as plt | ||
|
|
||
| MAKE_PLOT = False | ||
|
|
||
| def linear_regression_with_std(x, y): | ||
| x = np.array(x) | ||
| y = np.array(y) | ||
| x_mean = np.mean(x) | ||
| y_mean = np.mean(y) | ||
| numerator = np.sum((x - x_mean) * (y - y_mean)) | ||
| denominator = np.sum((x - x_mean) ** 2) | ||
| slope = numerator / denominator | ||
| intercept = y_mean - slope * x_mean | ||
| y_pred = slope * x + intercept | ||
| residuals = y - y_pred | ||
| std_dev = np.std(residuals) | ||
| return slope, intercept, std_dev | ||
|
|
||
| def do_bench(cliargs, iters): | ||
| print([cliargs[1], str(iters)] + cliargs[2:]) | ||
| out = str(subprocess.check_output([cliargs[1], str(iters)] + cliargs[2:])) | ||
| s1 = out[out.find("SELFTIMED")+11:] | ||
| s2 = float(s1[:s1.find("\n")-4]) | ||
| selftimed = s2 | ||
|
|
||
| b1 = out[out.find("BATCHTIME")+11:] | ||
| b2 = float(b1[:b1.find("SELFTIMED")-2]) | ||
| batchtime = b2 | ||
|
|
||
| print(f"ITERS: {iters}, BATCHTIME: {batchtime}, SELFTIMED: {selftimed}") | ||
| return batchtime | ||
|
|
||
| def converge(cliargs): | ||
| xs = [] | ||
| ys = [] | ||
| iters = 1 | ||
| t = time() | ||
| while len(xs) == 0: | ||
| st = do_bench(cliargs, iters) | ||
| if st * iters < 0.65: | ||
| iters *= 2 | ||
| continue | ||
| xs.append(iters) | ||
| ys.append(st) | ||
| for _ in range(2): | ||
| if time() - t < 3.5: | ||
| iters = int(math.trunc(float(iters) * 1.2) + 1) | ||
| else: | ||
| iters += 1 + iters // 20 | ||
| st = do_bench(cliargs, iters) | ||
| xs.append(iters) | ||
| ys.append(st) | ||
| while time() - t < 3.5: | ||
| if time() - t < 3.5: | ||
| iters = int(math.trunc(float(iters) * 1.2) + 1) | ||
| else: | ||
| iters += 1 + iters // 20 | ||
| st = do_bench(cliargs, iters) | ||
| xs.append(iters) | ||
| ys.append(st) | ||
| m, b, sigma = linear_regression_with_std(xs, ys) | ||
| print(f"Slope (Mean): {m}, Intercept (Overhead): {b}, Stdev: {sigma}") | ||
| p, lnc, lngsd = linear_regression_with_std([math.log(x) for x in xs], [math.log(y) for y in ys]) | ||
| c, gsd = math.exp(lnc), math.exp(lngsd) | ||
| print(f"Power (Distortion): {p}, Factor (Geomean) {c}, GeoStdev {gsd}") | ||
| if MAKE_PLOT: | ||
| plt.plot(xs, ys, 'rx') | ||
| plt.plot([xs[0], xs[-1]], [m*xs[0]+b, m*xs[-1]+b], color="blue") | ||
| plt.plot(xs, [c*x**p for x in xs], color="green") | ||
| plt.savefig("plot.png") | ||
| return m, sigma, c, gsd | ||
|
|
||
| if __name__ == "__main__": | ||
| print(converge(argv)) | ||
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,5 @@ | ||
| The script `criterionmethodology.py` is my implementation of a benchrunner-runner that uses the criterion methodology. We take as input some program which takes `iters` as a command-line argument, times a function of interest in a tight loop which repeats `iters` many times, and then prints to stdout the batchtime (total loop time) and selftimed (total loop time divided by iters). The essense of criterion is then to sweep `iters` and perform a linear regression against iters and batchtime. The slope is the mean and the y-intercept represents some notion of shared overhead, insensitive to `iters`. Ultimately, criterion serves as a way to benchmark tasks with very short execution times, as startup overhead can be ignored. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's very text-heavy. What would help with it is examples of how you run it and what outputs you expect. |
||
|
|
||
| Since we have relatively precise timing over loops, I also implemented the criterion methodolgy *geometrically*. I take the logarithm of all the x and y values, compute the linear regression over that, then exponentiate the y-intercept - this represents the geomean. The other dependent portion, which is the slope, becomes a power (the equation is y = e^b x^m), which represents *geometric overhead*, e.g. how much overhead is being added per iteration. This may do well to model any slowdowns arising from pre-allocating arrays. Additionally, since performance data is non-negative and judged multiplicatively (twice as good means numbers are half, twice has bad means numbers are doubled; these are all *factors*), the geomean and geo-standard-deviation may make more sense theoretically. However, from my testing, the geomean seams to vary wildly for programs with fleeting execution times, even between repeat runs with the same parameters. | ||
|
|
||
| The scripts `criterionmethodology.py` and `sweep_seq.py` can both be ran directly. The first takes command-line arguments, e.g. `criterionmethodology benchrunner Quicksort Seq 2000` will call `benchrunner iters Quicksort Seq 2000` for various `iters`. `sweep_seq` performs a logarithmic sweep over different array sizes, invoking the criterion methdology at each point. | ||
51 changes: 51 additions & 0 deletions
51
benchmarks/scripts/criterion-drop-in-replacement/sweep_seq.py
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,51 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import numpy as np | ||
| from criterionmethodology import converge | ||
| import sys | ||
|
|
||
| # names = ["Optsort", "Insertionsort", "Mergesort", "Quicksort"] | ||
| # names = ["CopyArray", "Quicksort", "Insertionsort", "Mergesort"] | ||
| names = ["Insertionsort"] | ||
|
|
||
| # DENSITY = 4 | ||
| DENSITY = 12 | ||
| def bounds(name): | ||
| match name: | ||
| case "Insertionsort": | ||
| lo = 3 # 2**n ... | ||
| hi = 16 | ||
| case "Quicksort": | ||
| lo = 3 | ||
| hi = 22 | ||
| case "Mergesort": | ||
| # lo = 12 | ||
| lo = 3 | ||
| hi = 24 | ||
| case "Cilksort": | ||
| # lo = 12 | ||
| lo = 3 | ||
| hi = 16#24 | ||
| case "Optsort": | ||
| lo = 3 | ||
| hi = 16#24 | ||
| case _: | ||
| lo = 3 | ||
| hi = 20 | ||
| return lo, hi, (hi-lo)*DENSITY+1 | ||
|
|
||
| def dotrial(name, size): | ||
| return converge([sys.argv[0], "benchrunner", name, "Seq", str(int(size))]) | ||
|
|
||
| if __name__ == "__main__": | ||
| for name in names: | ||
| lo, hi, pts = bounds(name) | ||
| with open("%s_out3.csv" % name, "w") as f: | ||
| f.write("# size\tmean\tstddev\tgeomean\tgeostdev\n") | ||
| for i in np.unique(np.logspace(lo, hi, pts, base=2).astype(int)): | ||
| with open("%s_out3.csv" % name, "a") as f: | ||
| try: | ||
| f.write("%d" % int(i) + "\t%f\t%f\t%f\t%f\n" % dotrial(name, i)) | ||
| except: | ||
| pass | ||
|
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
I'd expect Python to have something like this in one of the libraries (should be easy to Google).