Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Maxim Onciul
pi_files
Commits
45879ae0
Commit
45879ae0
authored
Sep 05, 2021
by
Maxim Onciul
Browse files
sollte
parent
90869845
Changes
6
Hide whitespace changes
Inline
Side-by-side
experiment/create_data.sh
View file @
45879ae0
...
...
@@ -15,6 +15,8 @@ touch $rand_file
k
=
$2
m
=
$3
for
i
in
`
seq
$k
`
;
do
openssl rand
-hex
$m
>>
$rand_file
for
i
in
`
seq
$4
`
;
do
for
j
in
`
seq
$k
`
;
do
openssl rand
-hex
$m
>>
$rand_file
"_"
$i
done
done
experiment/experiment
View file @
45879ae0
...
...
@@ -18,14 +18,17 @@ os.makedirs(tempdir, exist_ok=True)
param_grid
=
ParameterGrid
(
{
# "repeat": [0] * 3,
"num_runs"
:
[
1
],
"num_runs"
:
[
5
],
"k"
:
[
"8"
],
"m"
:
[
"32"
],
"l"
:
[
"5"
],
"d"
:
[
"31"
,
"25"
,
"20"
],
"d"
:
[
"31"
],
# "d": ["31", "25", "20"],
"rx"
:
[
0
,
1
],
"b"
:
[
"0"
,
"1"
,
"5"
,
"10"
,
"20"
],
"B"
:
[
"10"
,
"20"
,
"50"
],
"b"
:
[
"0"
],
# "b": ["0", "1", "5", "10", "20"],
"B"
:
[
"10"
],
# "B": ["10", "20", "50"],
}
)
...
...
@@ -40,7 +43,6 @@ pi_dir = "/home/pi/pi_files/experiment/"
Run
=
make_dataclass
(
"Run"
,
[
(
"id"
,
int
),
(
"k"
,
int
),
(
"m"
,
int
),
(
"l"
,
int
),
...
...
@@ -56,8 +58,8 @@ Run = make_dataclass(
)
def
write_exp_description
(
conf
,
idx
,
fname
):
with
open
(
conf
[
"logfile"
]
(
idx
)
,
"r"
)
as
f
:
def
write_exp_description
(
conf
,
fname
):
with
open
(
conf
[
"logfile"
],
"r"
)
as
f
:
lines
=
f
.
readlines
()
print
(
"Measured:"
)
print
(
"
\t
"
,
lines
[
-
4
])
...
...
@@ -79,7 +81,47 @@ def write_exp_description(conf, idx, fname):
return
measurement
.
split
(
" "
)[
1
]
def
count_errs
(
conf
,
idx
):
# Manual comparison
def
hex_to_bits
(
line
):
# ignore '\n'
for
ch
in
line
[:
-
1
]:
yield
bin
(
int
(
ch
,
16
))[
2
:].
zfill
(
4
)
def
cmp_bits
(
line1
,
line2
):
for
four_bits1
,
four_bits2
in
zip
(
hex_to_bits
(
line1
.
replace
(
" "
,
""
)),
hex_to_bits
(
line2
.
replace
(
" "
,
""
))
):
for
b1
,
b2
in
zip
(
four_bits1
,
four_bits2
):
if
b1
==
b2
:
yield
0
else
:
yield
1
def
cmp_two_lines
(
line1
,
line2
):
return
sum
(
cmp_bits
(
line1
,
line2
))
def
cmp_two_files
(
file1
,
file2
):
with
ExitStack
()
as
exit_stack
:
sf
=
exit_stack
.
enter_context
(
open
(
file1
,
"r"
))
rf
=
exit_stack
.
enter_context
(
open
(
file2
,
"r"
))
s_all
=
sf
.
read
().
splitlines
()
r_all
=
rf
.
read
().
splitlines
()
try
:
return
sum
(
min
(
cmp_two_lines
(
fix_line
,
dyn_line
)
for
dyn_line
in
s_all
)
for
fix_line
in
r_all
)
except
ValueError
:
print
(
f
"source:
\n
{
s_all
}
\n\n
other:
\n
{
r_all
}
"
)
return
0
def
count_errs
(
conf
):
rx
,
tx
=
ips
[
conf
[
"rx"
]],
ips
[
conf
[
"rx"
]
^
1
]
source
=
f
"pi@
{
tx
}
:/home/pi/pi_files/exps/
{
conf
[
'exp'
]
}
/data"
...
...
@@ -88,38 +130,12 @@ def count_errs(conf, idx):
cipher_clean
=
f
"pi@
{
ips
[
1
]
}
:/home/pi/pi_files/exps/
{
conf
[
'exp'
]
}
/cipher"
cipher_corrupt
=
f
"pi@
{
ips
[
1
]
}
:/home/pi/pi_files/exps/
{
conf
[
'exp'
]
}
/corrupt"
remotes
=
[
source
,
reconst
,
cipher_clean
,
cipher_corrupt
]
remotes
=
[
reconst
,
source
,
cipher_corrupt
,
cipher_clean
]
files
=
[
f
"
{
tempdir
}
/
{
file
}
"
for
file
in
[
"data"
,
"reconst"
,
"cipher"
,
"corrupt"
]]
for
file
,
remote
in
zip
(
files
,
remotes
):
subprocess
.
run
([
"scp"
,
remote
,
file
])
# Manual comparison
def
hex_to_bits
(
line
):
# ignore '\n'
for
ch
in
line
[:
-
1
]:
yield
bin
(
int
(
ch
,
16
))[
2
:].
zfill
(
4
)
def
cmp_bits
(
line1
,
line2
):
for
four_bits1
,
four_bits2
in
zip
(
hex_to_bits
(
line1
.
replace
(
" "
,
""
)),
hex_to_bits
(
line2
.
replace
(
" "
,
""
))
):
for
b1
,
b2
in
zip
(
four_bits1
,
four_bits2
):
if
b1
==
b2
:
yield
0
else
:
yield
1
def
cmp_two_files
(
file1
,
file2
):
with
ExitStack
()
as
exit_stack
:
sf
=
exit_stack
.
enter_context
(
open
(
file1
,
"r"
))
rf
=
exit_stack
.
enter_context
(
open
(
file2
,
"r"
))
return
sum
(
sum
(
cmp_bits
(
sline
,
rline
))
for
sline
,
rline
in
zip
(
sf
.
readlines
(),
rf
.
readlines
())
)
return
cmp_two_files
(
*
files
[:
2
]),
cmp_two_files
(
*
files
[
2
:])
...
...
@@ -132,58 +148,58 @@ def run_experiment(clients: List[pxssh.pxssh], conf: Dict[str, str]):
rx
,
tx
=
clients
[
conf
[
"rx"
]],
clients
[
conf
[
"rx"
]
^
1
]
# find data in 'pi_files/exps/{conf['exp']}/data'
for
idx
in
range
(
conf
[
"num_runs"
]):
tx
.
sendline
(
f
"
{
pi_dir
}
/create_data.sh
{
conf
[
'exp'
]
}
{
conf
[
'k'
]
}
{
conf
[
'm'
]
}
{
idx
}
"
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'
]
}
"
)
with
MeasurePi
(
conf
[
"logfile"
]):
# 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"
]
}
'
)
with
MeasurePi
(
conf
[
"logfile"
](
idx
)):
# start receiver
rx
.
sendline
(
f
'
{
pi_dir
}
/pi_receive.py
{
conf
[
"exp"
]
}
{
conf
[
"k"
]
}
{
conf
[
"l"
]
}
{
conf
[
"d"
]
}
{
conf
[
"m"
]
}
{
conf
[
"b"
]
}
{
conf
[
"B"
]
}
'
)
# start sender
tx
.
sendline
(
f
'
{
pi_dir
}
/pi_send.py
{
conf
[
"exp"
]
}
{
conf
[
"k"
]
}
{
conf
[
"l"
]
}
{
conf
[
"d"
]
}
{
conf
[
"m"
]
}
{
conf
[
"b"
]
}
{
conf
[
"B"
]
}
'
)
# collect sender
tx
.
prompt
()
# collect receiver
rx
.
prompt
()
yield
Run
(
idx
,
conf
[
"k"
],
conf
[
"m"
],
conf
[
"l"
],
conf
[
"d"
],
conf
[
"b"
],
conf
[
"B"
],
*
count_errs
(
conf
,
idx
),
float
(
write_exp_description
(
conf
,
idx
,
fname
)),
ips
[
conf
[
"rx"
]
^
1
],
ips
[
conf
[
"rx"
]],
# start sender
tx
.
sendline
(
f
'
{
pi_dir
}
/pi_send.py
{
conf
[
"exp"
]
}
{
conf
[
"k"
]
}
{
conf
[
"l"
]
}
{
conf
[
"d"
]
}
{
conf
[
"m"
]
}
{
conf
[
"b"
]
}
{
conf
[
"B"
]
}
{
conf
[
"num_runs"
]
}
'
)
# collect sender
tx
.
prompt
()
# collect receiver
rx
.
prompt
()
return
Run
(
conf
[
"k"
],
conf
[
"m"
],
conf
[
"l"
],
conf
[
"d"
],
conf
[
"b"
],
conf
[
"B"
],
*
count_errs
(
conf
),
float
(
write_exp_description
(
conf
,
fname
)),
ips
[
conf
[
"rx"
]
^
1
],
ips
[
conf
[
"rx"
]],
)
def
create_data
(
args
,
clients
):
# run
for
idx
,
conf
in
enumerate
(
param_grid
):
conf
[
"exp"
]
=
f
"
{
args
.
name
}
_
{
idx
}
"
conf
[
"logdir"
]
=
lambda
i
:
os
.
path
.
join
(
"/home/cip/iuk/ty82xile/stud/pi_files/eval"
,
f
"
{
args
.
name
}
_
{
i
}
"
conf
[
"logdir"
]
=
os
.
path
.
join
(
"/home/cip/iuk/ty82xile/stud/pi_files/eval"
,
f
"
{
args
.
name
}
"
)
conf
[
"logfile"
]
=
lambda
i
:
os
.
path
.
join
(
conf
[
"logdir"
]
(
i
)
,
"picocom.log"
)
conf
[
"logfile"
]
=
os
.
path
.
join
(
conf
[
"logdir"
],
"picocom.log"
)
for
i
in
range
(
conf
[
"num_runs"
]):
os
.
makedirs
(
conf
[
"logdir"
](
i
),
exist_ok
=
True
)
yield
from
run_experiment
(
clients
,
conf
)
yield
run_experiment
(
clients
,
conf
)
def
main
():
print
(
"Preparing"
)
parser
=
argparse
.
ArgumentParser
(
description
=
"run full experiment"
)
parser
.
add_argument
(
"name"
,
help
=
"experiment name"
)
args
=
parser
.
parse_args
()
...
...
@@ -194,15 +210,20 @@ def main():
for
client
in
ips
]
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
()
...
...
@@ -210,6 +231,7 @@ def main():
dataframe
.
to_csv
(
f
"/home/cip/iuk/ty82xile/stud/pi_files/eval/
{
args
.
name
}
_res.csv"
,
sep
=
","
)
print
(
"Done."
)
if
__name__
==
"__main__"
:
...
...
experiment/pi_receive.py
View file @
45879ae0
...
...
@@ -12,38 +12,39 @@ def receive_data():
# cipher_file: str = '/home/pi/pi_files/exps/' + config['exp'] + "/cipher"
# reconst_file: str = '/home/pi/pi_files/exps/' + config['exp'] + "/reconst"
config
[
"socket"
].
bind
(
config
[
"me"
])
with
open
(
config
[
"cipher_file"
],
"w"
)
as
f
:
for
_
in
range
(
config
[
"k"
]):
f
.
write
(
config
[
"socket"
].
recvfrom
(
4096
)[
0
].
decode
(
"utf-8"
))
# f.write('\n')
# clean_lines = ["/bin/sed", "-i", "/^$/d", config['cipher_file']]
# subprocess.call(clean_lines)
# as pi2: add corruption, pi1: use received data
rx_file
=
run_pi
.
map_pi
(
lambda
_
,
__
:
config
[
"cipher_file"
],
lambda
_
,
__
:
run_pi
.
create_errs
(
config
)
)
call
:
List
[
str
]
=
[
config
[
"code"
]]
+
[
"-e"
,
"-l"
,
str
(
config
[
"l"
]),
"-d"
,
str
(
config
[
"d"
]),
"-m"
,
str
(
config
[
"m"
]),
"-k"
,
str
(
config
[
"k"
]),
]
print
(
call
)
with
ExitStack
()
as
exit_stack
:
rx_stream
=
exit_stack
.
enter_context
(
open
(
rx_file
,
"r"
))
reconst
=
exit_stack
.
enter_context
(
open
(
config
[
"reconst_file"
],
"w"
))
subprocess
.
run
(
call
,
encoding
=
"UTF-8"
,
stdin
=
rx_stream
,
stdout
=
reconst
)
for
idx
in
range
(
config
[
"num_runs"
]):
config
[
"socket"
].
bind
(
config
[
"me"
])
with
open
(
config
[
"cipher_file"
][
idx
],
"w"
)
as
f
:
for
_
in
range
(
config
[
"k"
]):
f
.
write
(
config
[
"socket"
].
recvfrom
(
4096
)[
0
].
decode
(
"utf-8"
))
# f.write('\n')
# clean_lines = ["/bin/sed", "-i", "/^$/d", config['cipher_file']]
# subprocess.call(clean_lines)
# as pi2: add corruption, pi1: use received data
rx_file
=
run_pi
.
map_pi
(
lambda
_
,
__
:
config
[
"cipher_file"
][
idx
],
lambda
_
,
__
:
run_pi
.
create_errs
(
config
)
)
call
:
List
[
str
]
=
[
config
[
"code"
]]
+
[
"-e"
,
"-l"
,
str
(
config
[
"l"
]),
"-d"
,
str
(
config
[
"d"
]),
"-m"
,
str
(
config
[
"m"
]),
"-k"
,
str
(
config
[
"k"
]),
]
print
(
call
)
with
ExitStack
()
as
exit_stack
:
rx_stream
=
exit_stack
.
enter_context
(
open
(
rx_file
,
"r"
))
reconst
=
exit_stack
.
enter_context
(
open
(
config
[
"reconst_file"
][
idx
],
"w"
))
subprocess
.
run
(
call
,
encoding
=
"UTF-8"
,
stdin
=
rx_stream
,
stdout
=
reconst
)
if
__name__
==
"__main__"
:
...
...
experiment/pi_send.py
View file @
45879ae0
...
...
@@ -12,37 +12,34 @@ def send_data():
config
:
Dict
[
str
,
Any
]
=
run_pi
.
common
()
call
:
List
[
str
]
=
[
config
[
"code"
]]
+
[
"-l"
,
str
(
config
[
"l"
]),
"-d"
,
str
(
config
[
"d"
]),
"-m"
,
str
(
config
[
"m"
]),
"-k"
,
str
(
config
[
"k"
]),
]
print
(
call
)
with
ExitStack
()
as
exit_stack
:
plain_data
=
exit_stack
.
enter_context
(
open
(
config
[
"data_file"
],
"r"
))
outfile
=
exit_stack
.
enter_context
(
open
(
config
[
"cipher_file"
],
"w"
))
subprocess
.
run
(
call
,
encoding
=
"UTF-8"
,
stdin
=
plain_data
,
stdout
=
outfile
)
# as pi2: add corruption, pi1: use encoded data
tx_file
=
run_pi
.
map_pi
(
lambda
_
,
__
:
config
[
"cipher_file"
],
lambda
_
,
__
:
run_pi
.
create_errs
(
config
),
)
print
(
f
"tx file:
{
tx_file
}
"
)
# TODO last 6 characters are swallowed!
with
open
(
tx_file
,
"r"
)
as
tx
:
for
line
in
tx
:
print
(
f
"len line:
{
len
(
line
)
}
"
)
config
[
"socket"
].
sendto
(
bytes
(
line
,
"utf-8"
),
config
[
"other"
])
for
idx
in
range
(
config
[
"num_runs"
]):
call
:
List
[
str
]
=
[
config
[
"code"
]]
+
[
"-l"
,
str
(
config
[
"l"
]),
"-d"
,
str
(
config
[
"d"
]),
"-m"
,
str
(
config
[
"m"
]),
"-k"
,
str
(
config
[
"k"
]),
]
with
ExitStack
()
as
exit_stack
:
plain_data
=
exit_stack
.
enter_context
(
open
(
config
[
"data_file"
][
idx
],
"r"
))
outfile
=
exit_stack
.
enter_context
(
open
(
config
[
"cipher_file"
][
idx
],
"w"
))
subprocess
.
run
(
call
,
encoding
=
"UTF-8"
,
stdin
=
plain_data
,
stdout
=
outfile
)
# as pi2: add corruption, pi1: use encoded data
tx_file
=
run_pi
.
map_pi
(
lambda
_
,
__
:
config
[
"cipher_file"
][
idx
],
lambda
_
,
__
:
run_pi
.
create_errs
(
config
,
idx
),
)
with
open
(
tx_file
,
"r"
)
as
tx
:
for
line
in
tx
:
print
(
f
"len line:
{
len
(
line
)
}
"
)
config
[
"socket"
].
sendto
(
bytes
(
line
,
"utf-8"
),
config
[
"other"
])
if
__name__
==
"__main__"
:
...
...
experiment/run_pi.py
View file @
45879ae0
...
...
@@ -45,7 +45,7 @@ def common() -> Dict[str, Any]:
parser
.
add_argument
(
"b"
,
type
=
int
,
help
=
"noise variance"
)
parser
.
add_argument
(
"B"
,
type
=
int
,
help
=
"Block error rate"
)
#
parser.add_argument("
idx
", type=int, help="
index of experiment
")
parser
.
add_argument
(
"
num
"
,
type
=
int
,
help
=
"
num measure runs
"
)
args
=
parser
.
parse_args
()
...
...
@@ -63,19 +63,20 @@ def common() -> Dict[str, Any]:
"code"
:
"/home/pi/pi_files/polar_rs/code"
,
"errs"
:
"/home/pi/pi_files/polar_rs/errs"
,
"exp_dir"
:
exp_dir
,
"cipher_file"
:
f
"
{
exp_dir
}
/cipher
"
,
"reconst_file"
:
f
"
{
exp_dir
}
/reconst
"
,
"corrupt_file"
:
f
"
{
exp_dir
}
/corrupt
"
,
"data_file"
:
f
"
{
exp_dir
}
/data
"
,
"cipher_file"
:
[
f
"
{
exp_dir
}
/cipher
_
{
i
}
"
for
i
in
range
(
args
.
num
)]
,
"reconst_file"
:
[
f
"
{
exp_dir
}
/reconst
_
{
i
}
"
for
i
in
range
(
args
.
num
)]
,
"corrupt_file"
:
[
f
"
{
exp_dir
}
/corrupt
_
{
i
}
"
for
i
in
range
(
args
.
num
)]
,
"data_file"
:
[
f
"
{
exp_dir
}
/data
_
{
i
}
"
for
i
in
range
(
args
.
num
)]
,
"socket"
:
connect
(),
"num_runs"
:
args
.
num
,
# "hostname": socket.gethostname(),
"other"
:
map_pi
(
lambda
_
,
pi2
:
pi2
,
lambda
pi1
,
_
:
pi1
),
"me"
:
map_pi
(
lambda
pi1
,
_
:
pi1
,
lambda
_
,
pi2
:
pi2
),
}
def
create_errs
(
config
):
with
open
(
config
[
"cipher_file"
],
"r"
)
as
f
:
def
create_errs
(
config
,
idx
):
with
open
(
config
[
"cipher_file"
]
[
idx
]
,
"r"
)
as
f
:
len_word
=
len
(
f
.
readline
())
call
=
[
config
[
"errs"
]]
+
[
...
...
@@ -85,9 +86,9 @@ def create_errs(config):
]
with
ExitStack
()
as
exit_stack
:
in_file
=
exit_stack
.
enter_context
(
open
(
config
[
"cipher_file"
],
"r"
))
out_file
=
exit_stack
.
enter_context
(
open
(
config
[
"corrupt_file"
],
"w"
))
in_file
=
exit_stack
.
enter_context
(
open
(
config
[
"cipher_file"
]
[
idx
]
,
"r"
))
out_file
=
exit_stack
.
enter_context
(
open
(
config
[
"corrupt_file"
]
[
idx
]
,
"w"
))
subprocess
.
run
(
call
,
encoding
=
"UTF-8"
,
stdin
=
in_file
,
stdout
=
out_file
)
return
config
[
"corrupt_file"
]
return
config
[
"corrupt_file"
]
[
idx
]
polar_rs/src/errs.cpp
View file @
45879ae0
...
...
@@ -61,6 +61,7 @@ int main(int argc, char** argv)
size_t
num_out_blocks
=
0
;
size_t
num_blocks
=
0
;
// assignment
while
(
(
blocks
[
num_out_blocks
]
=
inputToBytes
())
)
{
int
r
=
rand
()
%
1001
;
...
...
@@ -92,8 +93,7 @@ int main(int argc, char** argv)
for
(
size_t
i
=
1
;
i
<=
num_blocks
/
2
;
i
++
)
corruptBlock
(
blocks
[
i
],
block_len
,
bit_err
,
std
::
round
(
d
(
gen
)
)
);
// This is the output, TODO flip some bits
for
(
size_t
i
=
0
;
i
<
num_out_blocks
;
i
++
)
for
(
size_t
i
=
0
;
i
<
num_blocks
/
2
;
i
++
)
{
for
(
size_t
j
=
0
;
j
<
block_len
;
j
++
)
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment