Commit 4bf638a4 authored by jclamber's avatar jclamber
Browse files

Estimated ETA for gadget simulation

git-svn-id: http://svn.oamp.fr/repos/uns_projects/trunk@101 f264a43e-d52d-4b82-913b-c2bd5215a18a
parent 664b5408
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
# cnd line
import sys, getopt,os.path
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main
def main(argv):
prog = os.path.basename(argv.pop(0)) # program name
dt=0.005 # check cpu time between 2 dt
file="cpu.txt"
tf=0.0 # time first
tl=-1. # time last
out='' # if blank plot on screen, else on png file
tmax=10. # gadget.param file
try:
opts,args=getopt.getopt(argv,"h",["in=","tm=","out=","dt=","tf=","tl="])
except getopt.GetoptError:
printHelp(prog,file,dt,tf,tl,out)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printHelp(prog,file,dt,tf,tl,out)
sys.exit()
elif opt in ("--in"):
file = arg
elif opt in ("--dt"):
dt = float(arg)
elif opt in ("--tf"):
tf = float(arg)
elif opt in ("--tl"):
tl = float(arg)
elif opt in ("--out"):
out = arg
elif opt in ("--tm"):
tmax = float(arg)
fig = plt.figure(figsize=(8,8),dpi=100)
#status,gparam=parseGadgetParam(param) # parse gadget param file
#if len(gparam)>1:
# dt = float(gparam['MaxSizeTimestep'])
# print "Overwrite dt from gadget.patam = ",dt
for f in file.split(","):
try:
cpu_file,mylegend=f.split(":") # parse filename:legend
except ValueError:
f_label=f
cpu_file=f
else:
f_label=mylegend
time,cpu,ncores=parseCPU(cpu_file,dt,tf,tl)
f_label=f_label+" cpus=%d"%ncores
if tmax>0 and (tmax-time[-1])>0.01:
mcp=min(10,cpu.size)
meancpu=cpu[cpu.size-mcp:].mean(dtype=np.float64)
print "last cpu :",time[-1],cpu[-1],mcp,meancpu
sec=(tmax-time[-1])/dt*meancpu
#print "Secondes remaining :",sec,time[-1],cpu[-1]
dhms=getHMS(int(sec),"ETA")
print "HMS=",dhms
f_label=f_label+"\n%s"%(dhms)
print time.size
elapsed=np.sum(cpu)#-cpu[0])
print "Elapsed : ",elapsed, " cpu0=",cpu[0],cpu[-1]
ddd=getHMS(int(elapsed),"ELD")
f_label=f_label+"\n%s"%(ddd)
plt.plot(time,cpu,label=f_label) #,'r.-')
plt.xlabel('Simulation time')
plt.ylabel('Cpu time (seconds)')
plt.title('Cpu time every dt='+'%f'%dt)
leg=plt.legend(loc='best', fancybox=True)
leg.get_frame().set_alpha(0.5)
if (out==''):
plt.show()
else:
print "out fig=",out
plt.savefig(out)#, bbox_inches=0)
plt.close(fig)
sys.exit()
# Parse gadget.param file and return a dictionary key pair value
def parseGadgetParam(param):
print "Trying file[",param,"]"
gparam={}
try:
gp=open(param,"r")
except IOError:
print "no gadget param"
return False,gparam
for line in gp:
data=line.split()
if (len(data)>1 and data[0]!='%' and data[0]!='#'):
gparam[data[0]] = data[1]
#print data
return True,gparam
def getHMS(sec,tag):
days = sec / 86400
sec -= 86400*days
hrs = sec / 3600
sec -= 3600*hrs
mins = sec / 60
sec -= 60*mins
dhms="%s :%dd %dh %dmn %ds"%(tag,days,hrs,mins,sec)
print dhms
return dhms
def parseCPU(file,dt,tf,tl):
print "Processing file[",file,"]"
a = open(file, "r")
time=np.array([], dtype=np.float32)
cpu =np.array([], dtype=np.float32)
first=True
tlast=0.0
t=0.0
cputotlast=0.0
cputot=0.0
ncores=0
for line in a:
if line.startswith("Step"): # Step line
if (tl != -1 and t>tl): # exit if time last reach
break
t = float(line.split()[3].split(",")[0]) # get simu time
if first:
ncores=int(line.split()[5])
cputot=float(a.next().split()[1]) # get from next line cpu time
if (t>=tf and first): # time >= time first and first
tlast= t # save time
cputotlast=cputot # save cpu time
first=False
elif (first==False and (tl<0 or t<=tl)): # not first and time in selected range
diff_t = (t-tlast) # diff time current and last
diff_cpu = (cputot-cputotlast) # diff cput time current and last
if ( (diff_t)>=dt) : # reach dt time
tlast=t
cputotlast=cputot
#total=a.readline()
#print "diff =",t, diff_t,diff_cpu
time=np.append(time,t) # save time in numpy array
cpu=np.append(cpu,diff_cpu) # save cputime in numpy array
a.close()
return time,cpu,ncores
def printHelp(prog,file,dt,tf,tl,out):
help= """\
----------------------------------------------
Plot gadget3 cpu time between 2 delta time
----------------------------------------------
Syntaxe : %s --in <inputfile[:legend]> --dt <delta time> --tf <time first> --tl <time last> --out <image name>
Notes :
inputfile : cpu.txt file from gadget3 simulation [%s]
you can give several files separated by ","
you can set a legend by giving a name after a ":"
cpu.txt:mdf001
dt : delta time to compute cpu time [%f]
tf : simulation time first [%f]
tl : simulation time last [%f]
out : output image filename, or plot on screen if blank [%s]
"""
print help % (prog,file,dt,tf,tl,out)
if __name__=='__main__':
main(sys.argv[0:])
Markdown is supported
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