Commit 7f16ca4c authored by Maxim Onciul's avatar Maxim Onciul
Browse files

baselines

parent ee9911a0
......@@ -2,11 +2,13 @@
import argparse
import os
import subprocess
from contextlib import contextmanager
from typing import Dict, List
from contextlib import ExitStack
from dataclasses import make_dataclass
from tempfile import gettempdir
import filecmp
from time import sleep, time
from measure import MeasurePi
from pexpect import pxssh
......@@ -34,7 +36,7 @@ param_grid = ParameterGrid(
},
{
# "repeat": [0] * 3,
"num_runs": [50],
"num_runs": [1],
"k": ["8"],
"m": ["128"],
"l": ["7"],
......@@ -50,7 +52,7 @@ param_grid = ParameterGrid(
ips = ["maximPi1", "maximPi2"]
common = {
"logfile": "exp/measure.log",
"logfile": "exps/measure.log",
"exp": "some_unique_ID",
}
......@@ -68,35 +70,13 @@ Run = make_dataclass(
("derr", int),
("cerr", int),
("egy", float),
("secs", float),
("tx", str),
("rx", str),
],
)
def write_exp_description(conf, fname):
with open(conf["logfile"], "r") as f:
lines = f.readlines()
print("Measured:")
print("\t", lines[-4])
measurement = lines[-4]
with open(fname, "a") as f:
f.write(
f"""
# Experiment
- This experiment is executed as {['rx', 'tx'][conf['rx']]}
- One code word of the convolutional code is 64 bytes long
- l: {conf['l']}
- d: {conf['d']}
- k: {conf['k']}
- m: {conf['m']}
- Measured: {measurement}
"""
)
return measurement.split(" ")[1]
# Manual comparison
def hex_to_bits(line):
# ignore '\n'
......@@ -167,6 +147,50 @@ def count_errs(conf, clients):
*files[2:], conf["num_runs"]
)
def run_no_code(clients: List[pxssh.pxssh], conf: Dict[str, str]):
curr = os.path.dirname(os.path.abspath(__file__))
folder = os.path.join(curr, "exps")
os.makedirs(folder, exist_ok=True)
fname = os.path.join(folder, "desc")
rx, tx = clients[conf["rx"]], clients[conf["rx"] ^ 1]
# find data in 'pi_files/exps/{conf['exp']}/data'
tx.sendline(
f"{pi_dir}create_data.sh {conf['exp']} {conf['k']} {conf['m']} {conf['num_runs']}"
)
print(
f"{pi_dir}create_data.sh {conf['exp']} {conf['k']} {conf['m']} {conf['num_runs']}"
)
t_start = time()
picocom_file = f'{conf["logdir"]}/{conf["exp"]}'
with MeasurePi(picocom_file):
rx.sendline( f"{pi_dir}pi_receive_no_code.py {conf['exp']}")
tx.sendline(f"{pi_dir}pi_send_no_code.py {conf['exp']}")
tx.prompt()
rx.prompt()
t_end = time()
# subprocess.run(["sed", "1,200d", picocom_file])
with open(picocom_file, 'rb') as f:
energy = float(f.readlines()[-4].split()[1])
print(f"energy: {energy}")
return Run(
0, 0, 0, 0, 0, 0,
*count_errs(conf, clients),
# float(meas.curr_energy),
energy,
t_end - t_start,
ips[conf["rx"] ^ 1],
ips[conf["rx"]],
)
def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
curr = os.path.dirname(os.path.abspath(__file__))
......@@ -185,7 +209,9 @@ def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
f"{pi_dir}create_data.sh {conf['exp']} {conf['k']} {conf['m']} {conf['num_runs']}"
)
with MeasurePi(conf["logfile"]):
t_start = time()
picocom_file = f'{conf["logdir"]}/{conf["exp"]}'
with MeasurePi(picocom_file):
# start receiver
rx.sendline(
f'{pi_dir}/pi_receive.py {conf["exp"]} {conf["k"]} {conf["l"]} {conf["d"]} {conf["m"]} {conf["b"]} {conf["B"]} {conf["num_runs"]}'
......@@ -209,6 +235,13 @@ def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
# collect receiver
rx.prompt()
t_end = time()
# subprocess.run(["sed", "1,200d", picocom_file])
with open(picocom_file, 'rb') as f:
energy = float(f.readlines()[-4].split()[1])
print(f"energy: {energy}")
return Run(
conf["k"],
conf["m"],
......@@ -217,22 +250,36 @@ def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
conf["b"],
conf["B"],
*count_errs(conf, clients),
float(write_exp_description(conf, fname)),
# float(meas.curr_energy),
energy,
t_end - t_start,
ips[conf["rx"] ^ 1],
ips[conf["rx"]],
)
def create_data(args, clients):
def create_data(args):
# run
for idx, conf in enumerate(param_grid):
conf["exp"] = f"{args.name}_{idx}"
conf["logdir"] = os.path.join(
"/home/cip/iuk/ty82xile/stud/pi_files/eval", f"{args.name}"
)
conf["logfile"] = os.path.join(conf["logdir"], "picocom.log")
yield run_experiment(clients, conf)
with create_clients() as clients:
yield run_experiment(clients, conf)
@contextmanager
def create_clients():
clients = [pxssh.pxssh(timeout=None, encoding="utf8") for _ in ips]
for client, ip in zip(clients, ips):
client.login(ip, "pi")
try:
yield clients
finally:
for client in clients:
client.logout()
def main():
......@@ -241,7 +288,6 @@ def main():
parser.add_argument("name", help="experiment name")
args = parser.parse_args()
clients = [pxssh.pxssh(timeout=None, encoding="utf8") for _ in ips]
paths = [
os.path.join(f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}_{client}")
for client in ips
......@@ -249,25 +295,12 @@ def main():
os.makedirs(f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}", exist_ok=True)
with ExitStack() as stack:
files = [stack.enter_context(open(path, "w")) for path in paths]
for client, file in zip(clients, files):
client.logfile = file
print("Setup ssh.")
for client, ip in zip(clients, ips):
client.login(ip, "pi")
print("Experiment start.")
dataframe = pd.DataFrame(list(create_data(args, clients)))
print("Experiment over, cleaning up.")
for client in clients:
client.logout()
dataframe.to_csv(
print("Experiment start.")
# with MeasurePi(common["logfile"]) as meas:
pd.DataFrame(list(create_data(args))).to_csv(
f"/home/cip/iuk/ty82xile/stud/pi_files/eval/{args.name}_res.csv", sep=","
)
print("Done.")
......
#!/usr/bin/env python3
from contextlib import ContextDecorator
from contextlib import contextmanager, ContextDecorator
import time
import subprocess
import pexpect as pe
_PROG = "/usr/bin/picocom"
_ARGS = ["-b", "115200", "/dev/ttyUSB0"]
_START = "Terminal ready"
_SETUP = ["1-2:* uJ*", "3-4:* uJ*", "5-6:* uJ*", pe.TIMEOUT]
_SETUP = ["1-2:* uJ", "3-4:* uJ", "5-6:* uJ", pe.TIMEOUT]
_INIT = "V1-2:*"
_TERM = "Thanks for using picocom"
......@@ -21,30 +22,41 @@ class MeasurePi(ContextDecorator):
def __init__(self, logfile: str = "picocom.log"):
self.logfile = logfile
self.process = None
self.time = None
def __enter__(self) -> None:
self.time = time.time()
self.process = pe.spawn(_PROG, _ARGS, encoding="utf-8", codec_errors="replace")
idx = self.process.expect([_START, pe.TIMEOUT], timeout=1)
self.serial_log_tee = subprocess.Popen(
["tee", logfile],
stdin=subprocess.PIPE)
self.process = pe.spawn(
_PROG, _ARGS,
# encoding="utf-8",
# codec_errors="replace",
logfile=self.serial_log_tee.stdin,
echo=False,
timeout=1)
idx = self.process.expect_exact(_START, timeout=1)
if idx != 0:
print("Measurement could not be started.")
print("Is process alive? ", self.process.isalive())
return
self.ofile = open(self.logfile, "w")
self.process.send(str("e"))
self.process.expect(_INIT)
# egy = self.process.expect(_SETUP, timeout=1)
self.process.expect(_INIT, timeout=5)
# if egy == 3:
# print('Cannot set to energy measurement.')
# return
# @contextmanager
# def measure(self):
# self.process.send(str("0"))
# print("Sent picocom '0'")
# try:
# yield self
# finally:
# self.curr_energy = self.process.read(size=100).split()
# print(f"Energy: {self.curr_energy}")
self.process.logfile = self.ofile
print("Measure started.")
self.process.send(str("0"))
def __enter__(self):
self.time = time.time()
return self
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.process.sendcontrol(str("a"))
......@@ -55,12 +67,14 @@ class MeasurePi(ContextDecorator):
print(exc_value)
return
idx = self.process.expect([_TERM, pe.TIMEOUT], timeout=3)
self.ofile.close()
idx = self.process.expect([_TERM, pe.TIMEOUT], timeout=5)
self.process.wait()
print(f"Took: {time.time() - self.time}")
if idx == 0:
print("Terminated normally.")
else:
print("Timeout, killing process...")
self.process.close()
print("Killed process.")
print(f"Took: {time.time() - self.time}")
# self.serial_log_tee.close()
time.sleep(3)
#!/usr/bin/env python3
from contextlib import ExitStack
from typing import Any, Dict, List
import subprocess
import run_pi
def receive_data():
config: Dict[str, Any] = run_pi.common()
config["socket"].bind(config["me"])
for idx in range(config["num_runs"]):
with open(config["reconst_file"][idx], "w") as f:
for _ in range(config["k"]):
f.write(config["socket"].recvfrom(4096)[0].decode("utf-8"))
# f.write('\n')
if __name__ == "__main__":
receive_data()
#!/usr/bin/env python3
from contextlib import ExitStack
import subprocess
from typing import Any, Dict, List
import run_pi
# params: bpw wpb tx_blocks
# ZFEC=-m 50 -k 4
def send_data():
config: Dict[str, Any] = run_pi.common()
for idx in range(config["num_runs"]):
# as pi2: add corruption, pi1: use encoded data
tx_file = run_pi.map_pi(
lambda _, __: config["data_file"][idx],
lambda _, __: run_pi.create_errs(config, idx),
)
with open(tx_file, "r") as tx:
for line in tx:
config["socket"].sendto(bytes(line, "utf-8"), config["other"])
if __name__ == "__main__":
send_data()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment