g3cpu_parse.py 5.88 KB
Newer Older
jclamber's avatar
jclamber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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)
134
    print "cpu.size = ", cpu.size
jclamber's avatar
jclamber committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    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
163
164
165
166
167
168
169
170

                idx=time.size
                time.resize(idx+1)
                time[idx]=t
                cpu.resize(idx+1)
                cpu[idx]=diff_cpu
                #time=np.append(time,t)       # save time in numpy array
                #cpu=np.append(cpu,diff_cpu)  # save cputime in numpy array
jclamber's avatar
jclamber committed
171
172

    a.close()
173
    print "cpu.size = ", cpu.size
jclamber's avatar
jclamber committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    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:])