final v1.0 files
[weak_simulation_stab_extent.git] / timing_t_enforceddelta.bash
1 #!/bin/bash
2 # Bash script to get some timing analysis
3
4 # Runtime for different t's
5
6 # number of runs to run in parallel
7 maxthreads=10
8 # NOTE: that this is not the same as running weaksim in parallel! For that you need to set OMP_NUM_THREADS appropriately (see README.txt)
9 # Note that setting OMP_NUM_THREADS>1 will lead to more pessimistic runtime analysis, due to parallelization overhead, though the runs should complete faster!
10
11 ## For the most accurate time analysis, run with
12 ## export OMP_NUM_THREADS=1
13 ## export OMP_PROC_BIND=false
14 #export OMP_NUM_THREADS=6
15 #export OMP_PROC_BIND=true
16 export OMP_NUM_THREADS=1
17 export OMP_PROC_BIND=false
18
19 delta=0.1
20 deltasquared=`bc -l <<< $delta^2`
21
22 numruns=10 #1000
23
24 #for t in 4 8
25 for t in 8
26 #for t in 16
27 #for t in 32
28 #for t in 2 4 8 #16 #32
29 #for t in 8 16 32
30 do
31     #delta=`bc -l <<< 10^-$deltapower`
32     echo "t=$t"
33     # alpha is the fraction of t supplemental states that we will take to keep the cost the same
34     # alpha = delta*xi^t/t
35     #echo "0.25*$delta*1.17^$t/$t"
36     #alpha=`bc -l <<< 0.25*$delta*1.17^$t/$t`
37     #%echo "alpha=$alpha"
38     TIMEFORMAT='%3R';
39     #number of runs
40     for (( i=1; i<=$numruns; i++ ))
41     do
42         # Generate two t-Pauli measurements
43         ./randominputcommutingHermitianPauli2 $t $t 1000 | head -n $[2*$t+4] > inputPauli_$[i].txt
44         sleep 1
45     done
46     for (( i=1; i<=$numruns; i++ ))
47     do
48         # Exact measurements
49         ./multipauli < inputPauli_$[i].txt | tail -1 | cut -d ' ' -f 1 > tmp_$[t]_exact_$[i].txt &
50         #seed=`od -A n -t d -N 4 /dev/urandom |tr -d ' '|tr -d '-'`
51         #./weaksim 0.001 0.25 2 $seed < inputPauli_$[i].txt | tail -1 | cut -d ' ' -f 3 > tmp_$[t]_exact_$[i].txt &
52         #./weaksim 0.01 0.25 2 $seed < inputPauli_$[i].txt | tail -1 | cut -d ' ' -f 3 > tmp_$[t]_exact_$[i].txt &
53         # minus one below so as not to count grep search job
54         numthreads=$[`ps -aux | grep multipauli | wc -l`-1]
55         #numthreads=$[`ps -aux | grep weaksim | wc -l`-1]
56         while [ $numthreads -gt $[$maxthreads-1] ]
57         do
58             sleep 10
59             numthreads=$[`ps -aux | grep multipauli | wc -l`-1]
60             #numthreads=$[`ps -aux | grep weaksim | wc -l`-1]
61         done
62     done
63     
64     for (( i=1; i<=$numruns; i++ ))
65     do
66         echo "run: $i"
67         
68         approxerror=1.0
69         approxcoherror=1.0
70
71         samplePrefactor=0.001
72         seed=`od -A n -t d -N 4 /dev/urandom |tr -d ' '|tr -d '-'`
73         while (( $(echo "$approxerror > $deltasquared" |bc -l) )); do
74             echo "samplePrefactor=$samplePrefactor"
75             ( time ( ./weaksim $delta 0.25 0 $seed $samplePrefactor < inputPauli_$[i].txt >> tmp_$[t]_iid_$[i].txt ) ) 2> tmp_$[t]_iid_$[i]_timing.txt
76
77             exactanswer=`tail -1 tmp_$[t]_exact_$[i].txt`
78             approxanswer=`tail -1 tmp_$[t]_iid_$[i].txt | cut -d ' ' -f 3`
79             approxerror=`bc -l <<< "sqrt(($exactanswer-$approxanswer)^2)"`
80             echo $approxerror
81             samplePrefactor=`bc -l <<< "scale=4;($samplePrefactor*1.1)/1"`
82         done
83         samplePrefactor=`bc -l <<< "scale=4;($samplePrefactor/1.1)/1"`
84         echo "samplePrefactor used=$samplePrefactor"
85         echo $approxerror >> tmp_$[t]_iid_$[i]_error.txt
86
87         samplePrefactor=0.001
88         while (( $(echo "$approxcoherror > $deltasquared" |bc -l) )); do
89             echo "samplePrefactor=$samplePrefactor"
90             ( time ( ./weaksim $delta 0.25 2 $seed $samplePrefactor < inputPauli_$[i].txt >> tmp_$[t]_t_$[i].txt ) ) 2> tmp_$[t]_t_$[i]_timing.txt
91
92             exactanswer=`tail -1 tmp_$[t]_exact_$[i].txt`
93             approxcoherentanswer=`tail -1 tmp_$[t]_t_$[i].txt | cut -d ' ' -f 3`
94             approxcoherror=`bc -l <<< "sqrt(($exactanswer-$approxcoherentanswer)^2)"`
95             echo $approxcoherror
96             samplePrefactor=`bc -l <<< "scale=4;($samplePrefactor*1.1)/1"`
97         done
98         samplePrefactor=`bc -l <<< "scale=4;($samplePrefactor/1.1)/1"`
99         echo "samplePrefactor used=$samplePrefactor"
100         echo $approxcoherror >> tmp_$[t]_t_$[i]_error.txt
101
102     done
103
104     for (( i=1; i<=$numruns; i++ ))
105     do
106         rm tmp_$[t]_exact_$[i].txt;
107         rm tmp_$[t]_iid_$[i].txt;
108         rm tmp_$[t]_t_$[i].txt;
109         rm inputPauli_$[i].txt;
110         #echo "*******************" >> tmp_$[t]_iid.txt
111         #echo "*******************" >> tmp_$[t]_t.txt
112         #   sleep 1
113     done
114     echo "Longest runtime:" >> tmp_$[t]_iid.txt
115     cat tmp_$[t]_iid_[1-9]*_timing.txt | sort -n | tail -2 >> tmp_$[t]_iid.txt
116     rm tmp_$[t]_iid_[1-9]*_timing.txt
117     echo "Longest runtime:" >> tmp_$[t]_t.txt
118     cat tmp_$[t]_t_[1-9]*_timing.txt | sort -n | tail -2 >> tmp_$[t]_t.txt
119     rm tmp_$[t]_t_[1-9]*_timing.txt
120     echo "Largest error:" >> tmp_$[t]_iid.txt
121     cat tmp_$[t]_iid_[1-9]*_error.txt | sort -n | tail -1 >> tmp_$[t]_iid.txt
122     rm tmp_$[t]_iid_[1-9]*_error.txt
123     echo "Largest error:" >> tmp_$[t]_t.txt
124     cat tmp_$[t]_t_[1-9]*_error.txt | sort -n | tail -1 >> tmp_$[t]_t.txt
125     rm tmp_$[t]_t_[1-9]*_error.txt
126     echo "delta=$delta done!"
127 done