Skip to content

Instantly share code, notes, and snippets.

@brgmnn
Last active December 13, 2015 21:48
Show Gist options
  • Save brgmnn/4979969 to your computer and use it in GitHub Desktop.
Save brgmnn/4979969 to your computer and use it in GitHub Desktop.
HPC averaging python script. Run this from the same folder as your ./d2q5-bgk.exe executable. Works on BlueCrystal in the job queue as well. Edit the variable n at the top to change the number of times to run your code (default at 20). You must make sure you're ./d2q5-bgk.exe only outputs the number of seconds your program took to run. Nothing e…
#!/usr/bin/python
import sys
import os
import math
import subprocess
# how many samples you want to average over.
n = 20
# basic statistic functions
# calculates the mean of a list of values.
def mean(vals):
return sum(vals)/len(vals)
# calculates the normalisied standard deviation of a list of values.
def stddev(vals):
mn = mean(vals)
diff = []
for i in range(0,len(vals)):
diff.append(math.pow(vals[i]-mn, 2))
return math.sqrt(sum(diff)/(len(diff)-1))
# the main program
sum_out = 0.0
vals = []
for i in range(0,n):
sys.stdout.write("iteration "+str(i+1)+": ")
sys.stdout.flush()
p = subprocess.Popen("./d2q5-bgk.exe",stdout=subprocess.PIPE);
out, err = p.communicate()
vals.append(float(out));
sys.stdout.write(str(vals[i])+" (s)\n")
print "\nAverage: "+str(mean(vals))
print "Stdev: "+str(stddev(vals))
# example output
# iteration 1: 21.247908 (s)
# iteration 2: 22.306712 (s)
# iteration 3: 21.469522 (s)
# iteration 4: 21.491823 (s)
# iteration 5: 20.896366 (s)
# iteration 6: 21.340044 (s)
# iteration 7: 21.40215 (s)
# iteration 8: 21.633003 (s)
# iteration 9: 21.050969 (s)
# iteration 10: 21.849105 (s)
# iteration 11: 21.038256 (s)
# iteration 12: 21.494544 (s)
# iteration 13: 21.221064 (s)
# iteration 14: 21.088841 (s)
# iteration 15: 21.435045 (s)
# iteration 16: 21.138266 (s)
# iteration 17: 21.324398 (s)
# iteration 18: 20.968818 (s)
# iteration 19: 21.520342 (s)
# iteration 20: 21.892744 (s)
#
# Average: 21.390496
# Stdev: 0.346338162779
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment