NeoPZ
test.py
Go to the documentation of this file.
1 #! /usr/bin/env python2.7
2 #***************************************************************************
3 #* Copyright (C) 2013 by Edson Borin *
4 #* edson@ic.unicamp.br *
5 #* *
6 #* This program is free software; you can redistribute it and/or modify *
7 #* it under the terms of the GNU General Public License as published by *
8 #* the Free Software Foundation; either version 2 of the License, or *
9 #* (at your option) any later version. *
10 #* *
11 #* This program is distributed in the hope that it will be useful, *
12 #* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 #* GNU General Public License for more details. *
15 #* *
16 #* You should have received a copy of the GNU General Public License *
17 #* along with this program; if not, write to the *
18 #* Free Software Foundation, Inc., *
19 #* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 #**************************************************************************/
21 
22 import sys
23 import os.path
24 import shlex, subprocess
25 import resource
26 
27 # Try to import rdt and stats modules, if available.
28 import sys
29 # Add the perf_base_dir/scripts/misc to the system path to enable importing misc modules
30 sys.path.append("@PERFTEST_SCRIPTS_SRC_DIR@/misc_modules/")
31 try:
32  import rdt, stats
33  summarize_results=True
34 except ImportError, e:
35  print "WARNING: I wont summarize results because I could not import modules: ", e
36  summarize_results=False
37 
38 # Variables to be defined by cmake
39 builddir="@PERFTEST_BASE_DIR@"
40 datadir="@PERFTEST_SMALL_DATA_DIR@"
41 
42 def error(message, status):
43  sys.stderr.write('ERROR (test.py): '+message+'\n')
44  sys.exit(status)
45 
46 # (rdt_id, rdt_opt, rdt_filename, rdt_description)
47 assfn=("ass", "-ass_rdt", "ass.rdt", "Assemble: dohrstruct->Assemble(...). Assemble element matrices and decompose matrices.")
48 crefn=("cre", "-cre_rdt", "cre.rdt", "Create: dohrstruct->Create()")
49 solfn=("sol", "-sol_rdt", "sol.rdt", "Solver: cg.Solve(...)")
50 totfn=("tot", "-tot_rdt", "tot.rdt", "Total: all the steps")
51 tpzdohrassfn=("dohrass", "-tpz_dohr_ass", "tpzdohrass.rdt", "Assemble element matrices")
52 tpzdohrdecfn=("dohrdec", "-tpz_dohr_dec", "tpzdohrdec.rdt", "Decompose matrices")
53 
54 # List of rdt files produced by the test
55 rdtfiles_l=[assfn, crefn, solfn, totfn, tpzdohrassfn, tpzdohrdecfn]
56 
57 # Setup the command line
58 def setup_cmd():
59  # Check build directory
60  if not os.path.isdir(builddir) :
61  error(builddir+' is an invalid build directory.', 1)
62  # Check run directory
63  rundir = os.path.join(builddir,'scripts','substruct_tst16')
64  if not os.path.isdir(rundir) :
65  error(rundir+' is an invalid run directory.', 1)
66  if not os.path.isdir(builddir) :
67  error(builddir+' is an invalid build directory.', 1)
68  # Check executable
69  executable=os.path.join(builddir,"progs","substruct", "substruct-perf")
70  if not os.path.isfile(executable) :
71  error(executable+' is an invalid executable file name.', 1)
72  # Check input file
73  inputfn = os.path.join(datadir,"substruct","inputs","8andares02.txt")
74  if not os.path.isfile(inputfn) :
75  error(inputfn+' is an invalid input file name.', 1)
76  # Check micnativelodex to run application on Xeon Phi
77  micloadex = "opt/intel/mic/bin/micnativeloadex"
78  if not os.path.isfile(micloadex) :
79  error(micloadex+'is an invalid path to the mic load tool.', 1)
80  # Copy input file to the Xeon Phi
81  os.system ("scp "+inputfn+" mic0:/home/edson")
82  # Put the arguments together
83  arguments = ' -mp '+ "/home/edson/8andares02.txt"
84  arguments = arguments + ' -nsub 240'
85  arguments = arguments + ' -nt_a 240'
86  arguments = arguments + ' -nt_d 240'
87  arguments = arguments + ' -nt_m 240'
88  arguments = arguments + ' -nt_sm 240'
89  arguments = arguments + ' -p 2'
90  for rdtarg in rdtfiles_l :
91  arguments = arguments + ' ' + rdtarg[1] + ' ' + rdtarg[2]
92  # TODO: Add arguments to enforce output checking!
93  return rundir, micloadex + ' ' + executable + ' -a ' + '\"' + arguments + '\"'
94 
95 # Limits for this test
96 limits = { "cpu" : (resource.RLIMIT_CPU, 3600, "Max CPU time in seconds"),
97 # "nofile": (resource.RLIMIT_NOFILE, 7, "The maximum number of open file descriptors for the current process."),
98 # "rss" : (resource.RLIMIT_RSS, 1024, "The maximum resident set size that should be made available to the process"),
99 # "fsize" : (resource.RLIMIT_FSIZE, 1, "Max size of a file which the process may create"),
100 # "data" : (resource.RLIMIT_DATA, 1024, "The maximum size (in bytes) of the process's heap"),
101 # "nproc" : (resource.RLIMIT_NPROC, 0, "The maximum number of processes the current process may create")
102  }
103 
104 # Set the rlimits of the chidren process (see limits above)
105 # TODO: Improve the handling of sandboxing limits
106 def setlimits():
107  print "Setting resource limit in child"
108  for k, v in limits.iteritems() :
109  resource.setrlimit(v[0], (v[1],v[1]))
110  #print k, " : ", v[0], " => ", v[1]
111 
112 # Sumarizes the RDT (Raw data table) results (generated by the run)
113 def sumarize_rdt_results(rundir) :
114  results = {}
115  # Compute average and confidence interval for each rdt file.
116  for f in rdtfiles_l :
117  k =f[0] # Step name
118  fn=f[2] # RDT file name
119  if summarize_results :
120  try:
121  rdtfn=os.path.join(rundir,fn)
122  rdt_d=rdt.read(rdtfn)
123  elapsed_list=rdt.get_column_values(rdt_d,"ELAPSED")
124  try:
125  av=stats.average(elapsed_list)
126  except stats.StatsError, e:
127  print "WARNING: Could not compute average for results at", fn, "(", e, ")"
128  av=0.0
129  try:
130  ci=stats.conf_int(elapsed_list, 95.0)
131  except stats.StatsError, e:
132  print "WARNING: Could not compute confidence interval for results at", fn, "(", e, ")"
133  ci=0.0
134  except rdt.RdtError, e:
135  print "WARNING: error when summarizing results for", fn, "(", e, ")"
136  av=0.0
137  ci=0.0
138  else :
139  av=0.0
140  ci=0.0
141  results[k]=(av,ci)
142  return results
143 
144 # Sumarizes the RDT (Raw data table) files information
145 def sumarize_rdt_files(rundir) :
146  results = {}
147  for f in rdtfiles_l :
148  rdt_id = f[0] # Step name
149  rdt_fn = os.path.join(rundir,f[2]) # RDT file name
150  rdt_dsc = f[3] # Description
151  results[rdt_id] = (rdt_fn, rdt_dsc)
152  return results
153 
154 def short_description() : return "substructure -- 8andares02.txt -- 240 substructures and 240 threads"
155 
156 # Execute the test.
157 def run_test(ntimes):
158  rundir,cmd=setup_cmd()
159  print short_description()
160  print "CMD:",cmd
161  error('teste')
162  args = shlex.split(cmd)
163  sout = None
164  serr = None
165  for i in range(ntimes) :
166  p = subprocess.Popen(args, preexec_fn=setlimits, stdout=sout, stderr=serr, cwd=rundir)
167  p.wait()
168  if (p.returncode != 0) :
169  return p.returncode, {}
170  #results = sumarize_rdt_files(rundir)
171  results = sumarize_rdt_results(rundir)
172  return 0, results
173 
174 # Functions for stand alone tests
175 def usage():
176  print "\nUsage: test.py -r [-h]\n"
177  print "\nARGUMENTS"
178  print "\t-r : Run the experiment."
179  print "\nDESCRIPTION"
180  print "\tExecute the substruct tool collecting statistics for the following steps:"
181  print "\t ", assfn[0], ": assembling the system (serial) -- results at", assfn[1]
182  print "\t ", tpzdohrassfn[0], ": assembling (ass part) the system (serial) -- results at", tpzdohrassfn[1]
183  print "\t ", tpzdohrdecfn[0], ": assembling (dec part) the system (serial) -- results at", tpzdohrdecfn[1]
184  print "\t ", crefn[0], ": creating the sytem (serial) -- results at", crefn[1]
185  print "\t ", solfn[0], ": solver (serial) -- results at", solfn[1]
186  print "\t ", totfn[0], ": total -- results at", totfn[1]
187  sys.exit(1)
188 
189 # Main - for stand alone tests only
190 if __name__ == "__main__":
191  import getopt
192  run=0
193  ntimes=1
194  # Process arguments
195  try :
196  opts, extra_args = getopt.getopt(sys.argv[1:], 'rn:h')
197  except getopt.GetoptError, e:
198  error(str(e), 1)
199  for f, v in opts:
200  if f == '-r': run=1
201  elif f == '-n': ntimes=int(v)
202  elif f == '-h': usage()
203 
204  # Run test
205  if run == 1:
206  status,results = run_test(ntimes)
207  if status == 0: print "Execution [OK]"
208  else : print "Execution [FAILED] (status = ", status, ")"
209  print "Results summary ----------------------------"
210  for k,v in results.iteritems() : print '{0:10s} : {1:>16f} +- {2:<16f}'.format(k, v[0], v[1])
211  print "--------------------------------------------"
212  else:
213  print "WARNING: No options provided. (use -h for help)"
def run_test(ntimes)
Definition: test.py:157
def sumarize_rdt_files(rundir)
Definition: test.py:145
def sumarize_rdt_results(rundir)
Definition: test.py:113
def setup_cmd()
Definition: test.py:58
def short_description()
Definition: test.py:154
def error(message, status)
Definition: test.py:42
def average(vlist)
Definition: stats.py:45
def conf_int(vlist, conf_level)
Definition: stats.py:59
def get_column_values(rdt_d, field)
Definition: rdt.py:70
def read(filename)
Definition: rdt.py:46