Skip to content
Snippets Groups Projects
Commit 35129c52 authored by Hans-Jörg's avatar Hans-Jörg
Browse files

Start work on the simulator

Most of the work is done, but the GridTPT output is not done yet.
parent bb747b08
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
import argparse
import numpy as np
from datetime import datetime
from schedgen.opt.schedule import read_from_file
from schedgen.opt.helper import message, file_or_dir_path, new_file_path, file_path
from schedgen.parsers.gridtpt import GridTPT
from schedgen.parsers.csvresult import CSVResult
args_parser = argparse.ArgumentParser(
description="Simulate a schedule on a set of bechmarks."
)
args_parser.add_argument(
"-d",
"--data",
metavar="DIR",
required=True,
dest="data",
help="Location of experimental data. A folder if the GridTPT parser is selected, a CSV file if the CSV parser is selected.",
type=file_or_dir_path,
)
args_parser.add_argument(
"-l",
"--logics",
dest="logics",
metavar="L",
nargs="*",
default=["UF"],
type=str,
help="The logics to use for simulation. 'all' for all logics.",
)
args_parser.add_argument(
"-t",
"--time",
dest="time",
metavar="T",
default=None,
type=float,
help="Total time avialable to the schedule in seconds.",
)
args_parser.add_argument(
"-c",
"--cvc",
dest="csv",
action="store_true",
help="Use CSV instead of the GridTPT parser to read and write data.",
)
args_parser.add_argument(
"--mu",
metavar="MEAN",
dest="mu",
help="Mean value of the normal distribution used to add jitter to solving time.",
type=float,
default=0.0,
)
args_parser.add_argument(
"--sigma",
metavar="DEVIATION",
dest="sigma",
help="Standard deviation of the normal distribution used to add jitter to solving time.",
type=float,
default=0.0,
)
args_parser.add_argument(
"--seed",
metavar="INT",
dest="seed",
help="Seed to use for the random number generator.",
default=None,
type=int,
)
args_parser.add_argument(
metavar="FILE",
dest="schedule",
help="Pre-schedule to simulate.",
type=file_path,
)
args_parser.add_argument(
metavar="OUTPUT",
dest="output",
help="File name to write the generated file into.",
type=new_file_path,
)
def writeResult(outFile, args, result, csv=False):
message(f"Writing result to {outFile}.", logics)
with open(outFile, "w") as af:
af.write("-gridTPT report---------------\n")
af.write(f"Date : {datetime.now():%Y%m%d%H%M%S}\n")
af.write("Type : perf\n")
af.write("Comment : Simulated run\n")
af.write("-informations-----------------\n")
af.write("Hardware : \n")
af.write(f"Executable : {args.schedule}\n")
af.write("Options : \n")
af.write("Benchmarks type : {args.logics}\n")
if args.time:
af.write(f"CPU limit : {args.time:.0f}s\n")
else:
af.write("CPU limit : 99999999s\n")
if __name__ == "__main__":
args = args_parser.parse_args()
logics = args.logics
message(f"Logics: {logics}", logics)
timeout = args.time
message(f"Data: {args.data}", logics)
if not args.csv:
experiments = list(args.data.glob("**/*.txt"))
message(f"Found {len(experiments)} .txt files.", logics)
message(f"Timeout: {timeout}", logics)
message(f"μ: {args.mu}", logics)
message(f"σ: {args.sigma}", logics)
if "all" in logics:
filter = None
else:
filter = logics
if args.csv:
r = CSVResult(args.data, filter)
else:
r = GridTPT(args.data, filter)
exps = r.frame.copy()
pre_schedule = None
pre_schedule = read_from_file(args.schedule)
sc = "solved"
# Find a unique name
while sc in exps.columns:
sc = sc + "_"
exps[sc] = float("inf")
time = 0.0
rng = np.random.default_rng(args.seed)
# Some pandas magic to do the simulation
for strategy_time, strategy in pre_schedule:
print(strategy_time, strategy)
jitter = rng.normal(args.mu, args.sigma, exps.shape[0])
solved = (exps[strategy]+jitter).loc[(exps[strategy]+jitter) <= strategy_time]
solving_time = solved.clip(0.0) + time
solving_time.name = sc
time = time + strategy_time
update_idx = exps.loc[exps[sc] == float("inf")].index.intersection(solved.index)
exps.update(solving_time.loc[update_idx])
if timeout:
exps[sc].loc[exps[sc] > timeout] = float("inf")
message("Writing output.", logics)
writeResult(args.output, exps)
message("All done.", logics)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment