1 | #------------------------------------------------------------------------------ |
---|
2 | # Import necessary modules |
---|
3 | #------------------------------------------------------------------------------ |
---|
4 | import anuga |
---|
5 | import subprocess |
---|
6 | import csv |
---|
7 | import os |
---|
8 | import numpy |
---|
9 | import time |
---|
10 | import liststore |
---|
11 | from anuga.abstract_2d_finite_volumes.util import add_directories |
---|
12 | from anuga.utilities.log_analyser import analyse_log |
---|
13 | |
---|
14 | #------------------------------------------------------------------------------ |
---|
15 | # Set up variables for the correct directories to store the output |
---|
16 | #------------------------------------------------------------------------------ |
---|
17 | home = os.getenv('INUNDATIONHOME') |
---|
18 | host = os.getenv('HOST') |
---|
19 | scenariodir = add_directories(home, ["data","mem_time_test", |
---|
20 | "linearregression","run"]) |
---|
21 | meta = 'metalog.csv' |
---|
22 | meta_path = os.path.join(scenariodir, meta) |
---|
23 | final = 'final.csv' |
---|
24 | final_path = os.path.join(scenariodir, final) |
---|
25 | equation = 'equation.csv' |
---|
26 | equation_path = os.path.join(scenariodir, equation) |
---|
27 | |
---|
28 | #------------------------------------------------------------------------------ |
---|
29 | # MAIN LOOP |
---|
30 | # this is the main loops that assigns the maximum triangle area (m) |
---|
31 | # and the map side length(l) |
---|
32 | #------------------------------------------------------------------------------ |
---|
33 | n = 1 #number of processors to use |
---|
34 | for seed in range(0,30): |
---|
35 | stdsidelength = 154.90 |
---|
36 | stdtimelength = 3600.0 |
---|
37 | stdtimestep = stdtimelength / 60.0 |
---|
38 | |
---|
39 | varsidelength = 5 + seed * (800.0/30.0) |
---|
40 | vartimelength = 10 + seed * (86400.0/30.0) |
---|
41 | vartimestep = 0.5 + seed *(1800 / 30) |
---|
42 | vartimestep2 =vartimelength / 60.0 |
---|
43 | |
---|
44 | # side length experiment |
---|
45 | if (host == 'cyclone.agso.gov.au'): |
---|
46 | subprocess.call(['mpirun', '-np', str(n), '-hostfile' , |
---|
47 | '~/machinefiles/lnrrun.machines_cyclone', '-x','PYTHONPATH','-x', |
---|
48 | 'INUNDATIONHOME','python', 'runcairns.py',str(stdtimestep), |
---|
49 | str(stdtimelength),str(varsidelength)]) |
---|
50 | elif (host == 'tornado.agso.gov.au'): |
---|
51 | subprocess.call(['mpirun', '-np', str(n), '-hostfile' , |
---|
52 | '~/machinefiles/lnrrun.machines_tornado', '-x','PYTHONPATH','-x', |
---|
53 | 'INUNDATIONHOME','python', 'runcairns.py',str(stdtimestep), |
---|
54 | str(stdtimelength),str(varsidelength)]) |
---|
55 | elif (host == 'rhe-compute1.ga.gov.au'): |
---|
56 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
57 | 'INUNDATIONHOME','python2.6', 'runcairns.py',str(stdtimestep), |
---|
58 | str(stdtimelength),str(varsidelength)]) |
---|
59 | else: |
---|
60 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
61 | 'INUNDATIONHOME','python', 'runcairns.py',str(stdtimestep), |
---|
62 | str(stdtimelength),str(varsidelength)]) |
---|
63 | |
---|
64 | # time length experiment |
---|
65 | if (host == 'cyclone.agso.gov.au'): |
---|
66 | subprocess.call(['mpirun', '-np', str(n), |
---|
67 | '-hostfile' ,'~/machinefiles/lnrrun.machines_cyclone', '-x', |
---|
68 | 'PYTHONPATH','-x','INUNDATIONHOME','python', 'runcairns.py', |
---|
69 | str(vartimestep2),str(vartimelength),str(stdsidelength)]) |
---|
70 | elif (host == 'tornado.agso.gov.au'): |
---|
71 | subprocess.call(['mpirun', '-np', str(n), '-hostfile' , |
---|
72 | '~/machinefiles/lnrrun.machines_tornado', '-x','PYTHONPATH','-x', |
---|
73 | 'INUNDATIONHOME','python', 'runcairns.py',str(vartimestep2), |
---|
74 | str(vartimelength),str(stdsidelength)]) |
---|
75 | elif (host == 'rhe-compute1.ga.gov.au'): |
---|
76 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
77 | 'INUNDATIONHOME','python2.6', 'runcairns.py',str(vartimestep2), |
---|
78 | str(vartimelength),str(stdsidelength)]) |
---|
79 | else: |
---|
80 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
81 | 'INUNDATIONHOME','python', 'runcairns.py',str(vartimestep2), |
---|
82 | str(vartimelength),str(stdsidelength)]) |
---|
83 | |
---|
84 | |
---|
85 | # time step experiment |
---|
86 | if (host == 'cyclone.agso.gov.au'): |
---|
87 | subprocess.call(['mpirun', '-np', str(n), '-hostfile' , |
---|
88 | '~/machinefiles/lnrrun.machines_cyclone', '-x','PYTHONPATH','-x', |
---|
89 | 'INUNDATIONHOME','python', 'runcairns.py',str(vartimestep), |
---|
90 | str(stdtimelength),str(stdsidelength)]) |
---|
91 | elif (host == 'tornado.agso.gov.au'): |
---|
92 | subprocess.call(['mpirun', '-np', str(n), '-hostfile' , |
---|
93 | '~/machinefiles/lnrrun.machines_tornado', '-x','PYTHONPATH','-x', |
---|
94 | 'INUNDATIONHOME','python', 'runcairns.py',str(vartimestep), |
---|
95 | str(stdtimelength),str(stdsidelength)]) |
---|
96 | elif (host == 'rhe-compute1.ga.gov.au'): |
---|
97 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
98 | 'INUNDATIONHOME','python2.6', 'runcairns.py',str(vartimestep), |
---|
99 | str(stdtimelength),str(stdsidelength)]) |
---|
100 | else: |
---|
101 | subprocess.call(['mpirun', '-np', str(n), '-x','PYTHONPATH','-x', |
---|
102 | 'INUNDATIONHOME','python', 'runcairns.py',str(vartimestep), |
---|
103 | str(stdtimelength),str(stdsidelength)]) |
---|
104 | |
---|
105 | print 'Done' |
---|
106 | |
---|
107 | #------------------------------------------------------------------------------ |
---|
108 | # LOG ANALYSE |
---|
109 | # and the Linear Regression |
---|
110 | #------------------------------------------------------------------------------ |
---|
111 | |
---|
112 | #get the important data for the experiments from the anuga experiments |
---|
113 | analyse_log(scenariodir, os.path.join(scenariodir,'metalog.csv')) |
---|
114 | |
---|
115 | #open files to read from and write to |
---|
116 | metalog = csv.reader(open(meta_path,'rb')) |
---|
117 | final = csv.writer(open(final_path,'wb')) |
---|
118 | eq = csv.writer(open(equation_path,'wb')) |
---|
119 | |
---|
120 | #lists used to condense the metalog file and the regression equation |
---|
121 | indexlist = [] |
---|
122 | triangles = [] |
---|
123 | trianglessquared = [] |
---|
124 | timelength = [] |
---|
125 | filewrites = [] |
---|
126 | timelist = [] |
---|
127 | space = [] |
---|
128 | |
---|
129 | #read in the first row |
---|
130 | firstrow = metalog.next() |
---|
131 | |
---|
132 | #get the indices of the values we want, so that the data can be condensed |
---|
133 | indexlist.append(firstrow.index("beforetime")) |
---|
134 | indexlist.append(firstrow.index("aftertime")) |
---|
135 | indexlist.append(firstrow.index("aftermeshtime")) |
---|
136 | indexlist.append(firstrow.index("beforesimulationmemory")) |
---|
137 | indexlist.append(firstrow.index("aftermeshmemory")) |
---|
138 | indexlist.append(firstrow.index("afterinitialconditionsmemory")) |
---|
139 | indexlist.append(firstrow.index("afterboundarymemory")) |
---|
140 | indexlist.append(firstrow.index("aftersimulationmemory")) |
---|
141 | |
---|
142 | indexlist.append(firstrow.index("numberoftriangles")) |
---|
143 | indexlist.append(firstrow.index("finaltime")) |
---|
144 | indexlist.append(firstrow.index("timestep")) |
---|
145 | indexlist.append(firstrow.index("extent")) |
---|
146 | indexlist.append(firstrow.index("sidelength")) |
---|
147 | indexlist.append(firstrow.index("maxtrianglearea")) |
---|
148 | indexlist.append(firstrow.index("velocity")) |
---|
149 | indexlist.append(firstrow.index("depthofwater")) |
---|
150 | indexlist.append(firstrow.index("percentageofwatercover")) |
---|
151 | indexlist.append(firstrow.index("numberofcpus")) |
---|
152 | indexlist.append(firstrow.index("host")) |
---|
153 | |
---|
154 | #write the header for the final csv |
---|
155 | final.writerow(["TimeTaken(s)","MeshTime(s)", |
---|
156 | firstrow[(indexlist[3])],firstrow[(indexlist[4])], |
---|
157 | firstrow[(indexlist[5])],firstrow[(indexlist[6])], |
---|
158 | firstrow[(indexlist[7])],firstrow[(indexlist[8])], |
---|
159 | firstrow[(indexlist[9])],firstrow[(indexlist[10])], |
---|
160 | firstrow[(indexlist[11])],firstrow[(indexlist[12])], |
---|
161 | firstrow[(indexlist[13])],firstrow[(indexlist[14])], |
---|
162 | firstrow[(indexlist[15])],firstrow[(indexlist[16])], |
---|
163 | firstrow[(indexlist[17])],firstrow[(indexlist[18])]]) |
---|
164 | |
---|
165 | #write the data for each column in the final csv |
---|
166 | for row in metalog: |
---|
167 | |
---|
168 | #manipulate the beginning and end time to get the time taken |
---|
169 | begin = time.strptime(row[(indexlist[0])],'%Y%m%d_%H%M%S') |
---|
170 | end = time.strptime(row[(indexlist[1])],'%Y%m%d_%H%M%S') |
---|
171 | aftermesh = time.strptime(row[(indexlist[2])],'%Y%m%d_%H%M%S') |
---|
172 | taken = time.mktime(end) - time.mktime(begin) |
---|
173 | mesh = time.mktime(aftermesh) - time.mktime(begin) |
---|
174 | |
---|
175 | #write to file |
---|
176 | final.writerow([str(taken),str(mesh), |
---|
177 | row[(indexlist[3])],row[(indexlist[4])], |
---|
178 | row[(indexlist[5])],row[(indexlist[6])], |
---|
179 | row[(indexlist[7])],row[(indexlist[8])], |
---|
180 | row[(indexlist[9])],row[(indexlist[10])], |
---|
181 | row[(indexlist[11])],row[(indexlist[12])], |
---|
182 | row[(indexlist[13])],row[(indexlist[14])], |
---|
183 | row[(indexlist[15])],row[(indexlist[16])], |
---|
184 | row[(indexlist[17])],row[(indexlist[18])]]) |
---|
185 | |
---|
186 | #make a list of all the data points, to be used in the regression |
---|
187 | timelist.append(float(taken)) |
---|
188 | space.append(float(row[(indexlist[7])])) |
---|
189 | triangles.append(float(row[(indexlist[8])])) |
---|
190 | trianglessquared.append(float(row[(indexlist[8])])**2) |
---|
191 | timelength.append(float(row[(indexlist[9])])) |
---|
192 | filewrites.append(float(row[(indexlist[9])])/float(row[(indexlist[10])])) |
---|
193 | |
---|
194 | #convert these lists to arrays |
---|
195 | spa = numpy.asarray(space) |
---|
196 | tim = numpy.asarray(timelist) |
---|
197 | tri = numpy.asarray(triangles) |
---|
198 | trisq = numpy.asarray(trianglessquared) |
---|
199 | timlen = numpy.asarray(timelength) |
---|
200 | filwrit = numpy.asarray(filewrites) |
---|
201 | |
---|
202 | # Manipulate the arrays, to stack them into one array that will work the |
---|
203 | # way we want for regression |
---|
204 | multivariatearray = numpy.column_stack([tri.flat,trisq.flat,timlen.flat, |
---|
205 | filwrit.flat, numpy.ones(90,float)]) |
---|
206 | multivariatearrayspace = numpy.column_stack([tri.flat, numpy.ones(90,float)]) |
---|
207 | |
---|
208 | #get the equations for both time and memory |
---|
209 | timequation = numpy.linalg.lstsq(multivariatearray,tim)[0] |
---|
210 | spaequation = numpy.linalg.lstsq(multivariatearrayspace,spa)[0] |
---|
211 | |
---|
212 | #calculate the R squared values for each equation |
---|
213 | resid1 = numpy.linalg.lstsq(multivariatearray,tim)[1] |
---|
214 | resid2 = numpy.linalg.lstsq(multivariatearrayspace,spa)[1] |
---|
215 | r2 = 1 - resid1 / (tim.size * tim.var()) |
---|
216 | r3 = 1 - resid2 / (spa.size * spa.var()) |
---|
217 | |
---|
218 | #write these values back to lists |
---|
219 | listtime = timequation.tolist() |
---|
220 | listspace = spaequation.tolist() |
---|
221 | |
---|
222 | #write the equation data to file |
---|
223 | eq.writerow(['Dependent Variable', 'Number of Triangles', |
---|
224 | 'Number of Triangles Squared','Time Length','File Writes', |
---|
225 | 'Constant', 'R Squared']) |
---|
226 | eq.writerow(['Time']+listtime + r2.tolist()) |
---|
227 | eq.writerow(['Dependent Variable', 'Number of Triangles','Constant', |
---|
228 | 'R Squared']) |
---|
229 | eq.writerow(['Space']+listspace + r3.tolist()) |
---|
230 | |
---|
231 | |
---|