Parallel Programming - Final version
[matches/honours.git] / course / semester2 / pprog / assignment1 / get_data.py
index 359c418..b753590 100755 (executable)
@@ -1,23 +1,28 @@
 #!/usr/bin/python -u
 
 # @file get_data.py
-# @purpose Extract statistics from repeated runs of N-Body simulators
+# @purpose Extract statistics from repeated runs of N-Body simulators and graph them
 # @author Sam Moore (20503628) - 2012
+# Not documented very well, because it's not part of the task requirements
+# Mostly to get a different graph I just comment out a lot of lines and uncomment others. Well designed.
 
 import sys
 import os
 import subprocess
+import odict
 
 import Gnuplot, Gnuplot.funcutils
 plot = Gnuplot.Gnuplot(persist=0)
 
-programs = {
-       "single-thread" : "./single-thread/nbody -v 5"
-}
-for n in range(2, 20, 4):
-       programs.update({"mthread"+str(n) : "./mthread/nbody -v 5 -n "+str(n)})
-       #programs.update({"slow-mthread"+str(n) : "./mthread/nbody-slow -v 5 -n "+str(n)})
-       programs.update({"openmp"+str(n) : "./openmp/nbody -v 5 -n " +str(n)})
+programs = odict.odict([("single-thread" , "./single-thread/nbody -g")])
+
+for n in range(4, 5, 1):
+       programs.update({"mthread"+str(n) : "./mthread/nbody -g -n "+str(n)})
+       programs.update({"openmp"+str(n) : "./openmp/nbody -g -n " +str(n)})
+       for theta in [0.1, 1.0, 10.0]:
+               programs.update({"nbody-bh"+str(n)+":theta"+str(theta) : "./nbody-bh/nbody -g -n "+str(n)+" --theta " + str(theta)})
+
+
 
 
 
@@ -31,8 +36,8 @@ def RunProgram(string):
 
 def RunForSteps(program, field, steps):
        results = []
-       print(str(program) + " -s "+str(steps) + " " + str(field))
-       results = RunProgram(str(program) + " -s "+str(steps) + " " + str(field))
+       print(str(program) + " -s "+str(steps) + " -v 5 " + str(field))
+       results = RunProgram(str(program) + " -s "+str(steps) + " -v 5 " + str(field))
        return results
 
 def VaryField(program, steps, fields):
@@ -55,6 +60,9 @@ def main(argv):
        data = {}
        data.update(programs)
 
+       Plot_PerformanceN(data, 10, 1, 3000, 100)
+       
+       """
        fields = map(int, os.listdir("fields"))
        fields.sort(key = lambda e : e)
        
@@ -63,6 +71,7 @@ def main(argv):
                if n in ignore:
                        continue
                for p in data.keys():
+                       #Plot_MeanErrors(programs["single-thread"], programs[p], "fields/"+str(n), 0, 100)
                        data[p] = RunForSteps(programs[p], "fields/"+str(n), 200)
                        #print(str(data[p]))
 
@@ -76,9 +85,73 @@ def main(argv):
        
        #for p in sorted(data.items(), key = lambda e : e[1][0][len(e[1][0])-1][1]):
        #       print(str(p[0])+":"+str(n)+"\t"+str(p[1][0][len(p[1][0])-1][1]))
-               
+       """
        sys.stdin.readline()
        return 0
 
+def Plot_MeanErrors(control, program, field, sMin, sMax):
+       plot("set xlabel \"steps\"")
+       plot("set ylabel \"Error\"")
+       plot("set title \"Mean Position Error .vs. Steps\\n(Mean Error = Mean absolute error of all position components)\\nField: "+str(field)+"\"")
+       plot("set key outside right")
+       plot("set log y")
+       if (control == program):
+               return
+       d = []
+       for s in range(sMin, sMax, (sMax - sMin) / 20):
+               d.append([s, MeanError(control, program, field, s)])
+
+       plot.replot(Gnuplot.Data(d, title=str(program), with_="lp")) #linecolor "+str(fields.index(n))))
+       
+def Plot_PerformanceN(d, steps, nMin, nMax, nStep):
+
+       plot("set xlabel \"N bodies\"")
+       plot("set ylabel \"Real Time (s)\"")
+       plot("set title \"Time taken to simulate N randomly generated bodies for 1000 steps\"")
+       plot("set key outside right")
+       for p in d.items():
+               data = []
+               for n in range(nMin, nMax, nStep):
+                       print("Run for " + str(n) + " bodies; " + str(p[1]))
+                       r = RunProgram(str(p[1]) + " -r " + str(n) + " -s " + str(steps))
+                       data.append([n, r[len(r)-1][1]])
+               plot.replot(Gnuplot.Data(data, title=str(p[0]), with_="lp"))
+
+def MeanError(programA, programB, field, steps):
+       print("Calculate error for "+str(steps) + " steps: " + str(programB) + " .vs. " + str(programA))
+       RunProgram(str(programA) + " -s "+str(steps) + " " + str(field) + " " + "A.tmp")
+       RunProgram(str(programB) + " -s "+str(steps) + " " + str(field) + " " + "B.tmp")
+
+       A = open("A.tmp", "r", 0)
+       B = open("B.tmp", "r", 0)
+       
+       nA = int(A.readline().strip("\r\n\t "))
+       nB = int(B.readline().strip("\r\n\t "))
+       if (nA != nB):
+               sys.stderr.write("Body number error; A: " + str(nA) + " vs B: " + str(nB) + "\n")
+               sys.exit(1)
+
+       total_error = 0.0
+       values = 0
+       for i in range(0, nA):
+               dataA = A.readline().strip("\r\n\t ").split(" ")
+               dataB = B.readline().strip("\r\n\t ").split(" ")
+               #print("A: "+str(dataA))
+               #print("B: "+str(dataB))
+               dataA = map(float, dataA)[1:4] # Only count positions
+               dataB = map(float, dataB)[1:4]
+               #print("Length " + str(len(dataA)))
+               if (len(dataB) != len(dataA)):
+                       sys.stderr.write("Number of values in line are different\n")
+                       sys.exit(1)
+       
+               
+
+               values += len(dataA)
+               for j in range(0, len(dataA)-1):
+                       total_error += abs(dataB[j] - dataA[j])
+       os.system("rm A.tmp; rm B.tmp;")
+       return total_error / values     
+
 if __name__ == "__main__":
        sys.exit(main(sys.argv))

UCC git Repository :: git.ucc.asn.au