Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- split statistics code into separate files
- output latex tables as pdfs and pngs
  • Loading branch information
romnn committed Feb 11, 2024
1 parent dba1057 commit 1ca08a7
Show file tree
Hide file tree
Showing 22 changed files with 22,180 additions and 61,554 deletions.
2 changes: 2 additions & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.pdf
**/*.png
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pycryptosat = "*"
logicmin = "*"
pyeda = "*"
pyarrow = "*"
Pygments = "*"
pathvalidate = "*"

[dev-packages]
invoke = "*"
Expand Down
74 changes: 31 additions & 43 deletions gpucachesim/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@

BENCH_TARGET_INDEX_COLS = ["target", "benchmark"]

PREVIEW_COLS = list(
BENCH_TARGET_INDEX_COLS + ["input_id"] + INDEX_COLS + SIMULATE_INPUT_COLS
)
PREVIEW_COLS = list(BENCH_TARGET_INDEX_COLS + ["input_id"] + INDEX_COLS + SIMULATE_INPUT_COLS)

RATE_COLUMNS = [
"l2_hit_rate",
Expand Down Expand Up @@ -237,9 +235,23 @@ def _map_dtype(dtype: str) -> str:
assert len(missing_dtypes) == 0, "missing dtypes for {}".format(missing_dtypes)


CATEGORICAL_COLS = set(
[col for col, dtype in SPECIAL_DTYPES.items() if dtype == "category"]
)
CATEGORICAL_COLS = set([col for col, dtype in SPECIAL_DTYPES.items() if dtype == "category"])


def benchmark_name_human_readable(name: str) -> str:
match name.lower():
case "vectoradd":
return "VectorAdd"
case "matrixmul":
return "Matrixmul"
case "simple_matrixmul":
return "Naive Matrixmul"
case "transpose":
return "Transpose"
case "babelstream":
return "BabelStream"
case other:
return str(other)


# def default_dtypes() -> typing.Dict[str, str]:
Expand Down Expand Up @@ -449,18 +461,10 @@ def construct_playground_simulate_target_config(self, node):
BenchmarkLoader.add_constructor("!Profile", construct_profile_target_config)
BenchmarkLoader.add_constructor("!Trace", construct_trace_target_config)
BenchmarkLoader.add_constructor("!Simulate", construct_simulate_target_config)
BenchmarkLoader.add_constructor(
"!ExecDrivenSimulate", construct_exec_driven_simulate_target_config
)
BenchmarkLoader.add_constructor(
"!AccelsimSimulate", construct_accelsim_simulate_target_config
)
BenchmarkLoader.add_constructor(
"!AccelsimTrace", construct_accelsim_trace_target_config
)
BenchmarkLoader.add_constructor(
"!PlaygroundSimulate", construct_playground_simulate_target_config
)
BenchmarkLoader.add_constructor("!ExecDrivenSimulate", construct_exec_driven_simulate_target_config)
BenchmarkLoader.add_constructor("!AccelsimSimulate", construct_accelsim_simulate_target_config)
BenchmarkLoader.add_constructor("!AccelsimTrace", construct_accelsim_trace_target_config)
BenchmarkLoader.add_constructor("!PlaygroundSimulate", construct_playground_simulate_target_config)


class Benchmarks:
Expand Down Expand Up @@ -504,24 +508,18 @@ def main():


@main.command()
@click.option(
"--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config"
)
@click.option("--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config")
def count_bench_configs(path):
print("loading", path)
b = Benchmarks(path)
benches = b.benchmarks[Target.Simulate.value]

total_bench_configs = sum(
[len(bench_configs) for bench_configs in benches.values()]
)
total_bench_configs = sum([len(bench_configs) for bench_configs in benches.values()])
print("total bench configs: {}".format(total_bench_configs))


@main.command()
@click.option(
"--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config"
)
@click.option("--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config")
@click.option("--baseline", type=bool, default=True, help="Baseline configurations")
def table(path, baseline):
print("loading", path)
Expand Down Expand Up @@ -552,17 +550,13 @@ def is_baseline(config):
return not baseline or all(
[
config["values"].get("memory_only") in [False, None],
config["values"].get("num_clusters")
in [int(BASELINE["num_clusters"]), None],
config["values"].get("cores_per_cluster")
in [int(BASELINE["cores_per_cluster"]), None],
config["values"].get("num_clusters") in [int(BASELINE["num_clusters"]), None],
config["values"].get("cores_per_cluster") in [int(BASELINE["cores_per_cluster"]), None],
config["values"].get("mode") in ["serial", None],
]
)

baseline_bench_configs = [
config for config in bench_configs if is_baseline(config)
]
baseline_bench_configs = [config for config in bench_configs if is_baseline(config)]

print(bench_name)

Expand Down Expand Up @@ -621,9 +615,7 @@ def is_baseline(config):


@main.command()
@click.option(
"--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config"
)
@click.option("--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config")
def list(path):
print("loading", path)
b = Benchmarks(path)
Expand All @@ -633,9 +625,7 @@ def list(path):


@main.command()
@click.option(
"--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config"
)
@click.option("--path", default=DEFAULT_BENCH_FILE, help="Path to materialized benchmark config")
def fix(path):
print("loading", path)
b = Benchmarks(path)
Expand Down Expand Up @@ -705,9 +695,7 @@ def fix(path):
pass

try:
if (result_dir / "Simulate").is_dir() and (
result_dir / "simulate"
).is_dir():
if (result_dir / "Simulate").is_dir() and (result_dir / "simulate").is_dir():
# Simulate is newer
shutil.rmtree(result_dir / "simulate")
os.rename(result_dir / "Simulate", result_dir / "simulate")
Expand Down
16 changes: 10 additions & 6 deletions gpucachesim/microbench/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,11 @@ def custom_dist(a, b):
# "xor_bit": sym.logic.boolalg.Or(*[sym.logic.boolalg.And(term, sym.symbols(f"b{bit}")) for term in ff.args]),
"xor_bit": sym.logic.boolalg.Or(
*[
sym.logic.boolalg.Xor(term, bit)
if contains_var(f.args[i], var=bit)
else term
(
sym.logic.boolalg.Xor(term, bit)
if contains_var(f.args[i], var=bit)
else term
)
for i, term in enumerate(ff.args)
]
),
Expand Down Expand Up @@ -892,9 +894,11 @@ def custom_dist(a, b):
str(
color(
int(predicted),
fg="green"
if bool(predicted) == bool(target_bit)
else "red",
fg=(
"green"
if bool(predicted) == bool(target_bit)
else "red"
),
)
),
str(color("<==", fg="blue")) if addr in marks else "",
Expand Down
15 changes: 12 additions & 3 deletions gpucachesim/microbench/pchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,15 @@ def find_cache_set_mapping(
print(combined.head(n=10))
print(combined.shape)

combined_binary = combined.copy()
combined_binary["offset"] = combined_binary["offset"].apply(lambda x: np.binary_repr(x, width=num_sets_log2))
combined_binary["set"] = combined_binary["set"].apply(lambda x: np.binary_repr(x, width=num_sets_log2))
highest_virt_addr = combined_binary["virt_addr"].max()
combined_binary["virt_addr"] = combined_binary["virt_addr"].apply(lambda x: np.binary_repr(x, width=int(np.ceil(np.log2(highest_virt_addr))) + 1))
print(combined_binary.head(n=10))
print(combined_binary.shape)


print(compute_set_probability(combined))

# return
Expand Down Expand Up @@ -2977,9 +2986,9 @@ def is_periodic(
last_unique_miss_cache_lines = unique_miss_cache_lines.copy()

for miss_cache_line in unique_miss_cache_lines:
combined.loc[
combined["cache_line"] == miss_cache_line, "mapped_set"
] = set_idx
combined.loc[combined["cache_line"] == miss_cache_line, "mapped_set"] = (
set_idx
)

cache_line_set_mapping.loc[
cache_line_set_mapping["cache_line"] == miss_cache_line, "mapped_set"
Expand Down
6 changes: 3 additions & 3 deletions gpucachesim/plot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from gpucachesim import REPO_ROOT_DIR

PLOT_DIR = REPO_ROOT_DIR / "plot"
TABLE_DIR = REPO_ROOT_DIR / "plot/tables"
EQUATIONS_DIR = REPO_ROOT_DIR / "plot/equations"

PLOTLY_PDF_OPTS = dict(format="pdf", scale=8)

Expand Down Expand Up @@ -131,8 +133,6 @@ def human_format_thousands(num, round_to=2, variable_precision=False):
["", "K", "M", "G", "T", "P"][magnitude],
)
return "{}{}".format(
round_to_precision_str(
num, round_to=round_to, variable_precision=variable_precision
),
round_to_precision_str(num, round_to=round_to, variable_precision=variable_precision),
["", "K", "M", "G", "T", "P"][magnitude],
)
6 changes: 3 additions & 3 deletions gpucachesim/plot/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def title(id) -> str:
alloc_times[int(partition_id), int(set_id), int(assoc_id), :] = row_df[
"sector_alloc_time"
].to_numpy()
last_access_times[
int(partition_id), int(set_id), int(assoc_id), :
] = row_df["last_sector_access_time"].to_numpy()
last_access_times[int(partition_id), int(set_id), int(assoc_id), :] = (
row_df["last_sector_access_time"].to_numpy()
)

states = states.reshape((partitions, sets, -1))
allocations = allocations.reshape((partitions, sets, -1))
Expand Down
149 changes: 149 additions & 0 deletions gpucachesim/plot/equations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import click
import gpucachesim.plot as plot
import gpucachesim.utils as utils
from wasabi import color


@click.group()
# @click.pass_context
def main():
# ctx.ensure_object(dict)
pass


@main.command()
def equations():
equations = [
# (
# "amdahl_n",
# r"""
# \begin{equation}
# n=8
# \end{equation}
# """,
# ),
(
"amdahl",
# 1 / ((1 - p) + p / n)
r"""
\begin{align*}
S_{\text{Amdahl}}(p, n) = \frac{1}{1 - (1-p) + \frac{p}{n}}
\end{align*}
""",
),
(
"gustafson",
# 1 + (n - 1) * p
r"""
\begin{align*}
S_{\text{Gustafson}}(p, n) = 1 + (n-1)p
\end{align*}
""",
),
(
"amdahl_p_90",
r"""
\begin{align*}
S_{\text{Amdahl}}(0.90, 8) = 4.8
\end{align*}
""",
),
(
"amdahl_p_83",
r"""
\begin{align*}
S_{\text{Amdahl}}(0.83, 8) = 3.6
\end{align*}
""",
),
(
"gustafson_p_90",
r"""
\begin{align*}
S_{\text{Gustafson}}(0.90, 8) = 7.3
\end{align*}
""",
),
(
"gustafson_p_83",
r"""
\begin{align*}
S_{\text{Gustafson}}(0.83, 8) = 6.8
\end{align*}
""",
),
(
"offset_bit_0",
r"\begin{align*}"
+ "\n"
+ r"\Delta_0(a)=& (a_{10} \lor a_{12} \lor a_{14}) "
+ r" \land (a_{10} \lor \neg a_{12} \lor \neg a_{14}) "
+ r" \land (a_{12} \lor \neg a_{10} \lor \neg a_{14}) "
+ r" \land (a_{14} \lor \neg a_{10} \lor \neg a_{12}) \\"
+ r" & \land (\neg a_{11} \lor \neg a_{13} \lor \neg a_{14}) "
+ r" \land (\neg a_{12} \lor \neg a_{13} \lor \neg a_{14}) "
+ r" \land (\neg a_{13} \lor \neg a_{14} \lor \neg a_{9}) "
+ "\n"
+ r"\end{align*}",
),
# (b10 | b12 | b14) & (b10 | ~b12 | ~b14) & (b12 | ~b10 | ~b14) & (b14 | ~b10 | ~b12) & (~b11 | ~b13 | ~b14) & (~b12 | ~b13 | ~b14) & (~b13 | ~b14 | ~b9)
(
"offset_bit_1",
r"\begin{align*}"
+ "\n"
+ r"\Delta_1(a)=(a_{11} \land a_{13} \land \neg a_{9}) "
+ r"\lor (a_{11} \land a_{9} \land \neg a_{13}) "
+ r"\lor (a_{13} \land a_{9} \land \neg a_{11}) "
+ r"\lor (\neg a_{11} \land \neg a_{13} \land \neg a_{9}) "
+ "\n"
+ r"\end{align*}",
),
# (b11 & b13 & ~b9) | (b11 & b9 & ~b13) | (b13 & b9 & ~b11) | (~b11 & ~b13 & ~b9
(
"offset_bit_xor",
r"\begin{align*}"
+ "\n"
+ r"\Delta_0(a)&=a_{10} \xor a_{12} \xor a_{14} \\"
+ r"\Delta_1(a)&=\neg \Delta_0(a) \xor a_{9} \xor a_{10} \xor a_{11} \xor a_{12} \xor a_{13} \xor a_{14}"
+ "\n"
+ r"\end{align*}",
),
]

for name, tex in equations:
tex_code = r"""
\documentclass[preview]{standalone}
"""
tex_code += utils.TEX_PACKAGES
tex_code += r"""
\begin{document}
"""
tex_code += tex
tex_code += r"""
\end{document}
"""

assert isinstance(tex_code, str)
print(tex_code)
pdf_output_path = (plot.EQUATIONS_DIR / name).with_suffix(".pdf")
try:
utils.render_latex(tex_code, output_path=pdf_output_path)
pass
except Exception as e:
print(tex_code)
print("##################")
raise ValueError(tex_code)
# return
raise e

print(color("wrote {}".format(pdf_output_path), fg="cyan"))

png_output_path = (plot.EQUATIONS_DIR / "png" / name).with_suffix(".png")
utils.convert_to_png(input_path=pdf_output_path, output_path=png_output_path, density=600)
print(color("wrote {}".format(png_output_path), fg="cyan"))

pass


if __name__ == "__main__":
main()
Loading

0 comments on commit 1ca08a7

Please sign in to comment.