NeoPZ
test.py
Go to the documentation of this file.
1 #! /usr/bin/env python2.7
2 
21 
22 # libraries
23 import sys
24 import os.path
25 import shlex, subprocess
26 import resource
27 import getopt
28 import math # sqrt
29 
30 # program information
31 executable='./Perf-TBB'
32 perfdata='dec.rdt'
33 inputdata='cube1.txt'
34 rundir='.'
35 
36 # limits for this test
37 limits = { "cpu" : (resource.RLIMIT_CPU, 3600, "Max CPU time in seconds") }
38 
39 # Read a RDT file into a dictionary.
40 # The dictionary is indexed by the column index.
41 # The value is a 2-entry tuple describing the column header and the list of
42 # values at the column
43 def read(filename):
44  # Check configuration file
45  if not os.path.isfile(filename):
46  raise RdtError(filename+' is not a valid rdt file.')
47  d={}
48  with open(filename) as f:
49  # Process the first row to setup the headers
50  hlist=f.readline().split(',')
51  hi=1
52  for h in hlist:
53  header = h.strip()
54  d[hi] = (header,[])
55  hi=hi+1
56  # process the remaining rows
57  for line in f:
58  vlist = line.split(',')
59  first = vlist[1]
60  if first.strip() != "#":
61  hi=1
62  for v in vlist:
63  d[hi][1].append(float(v.strip()))
64  hi=hi+1
65  return d
66 
67 def get_column_values(rdt_d,field):
68  for k, v in rdt_d.iteritems():
69  if v[0] == field :
70  return v[1]
71  return []
72 
73 def average(vlist) :
74  sz = len(vlist)
75  if sz == 0 :
76  return 0.0
77  else :
78  s=reduce(lambda x,y : float(x)+float(y), vlist, 0.0)
79  return s/sz
80 
81 def variance(vlist) :
82  size=len(vlist)
83  if size == 1 or size == 0:
84  raise StatsError("Cannot calculate variance for a sample of size "+str(size))
85  sum1=reduce(lambda x,y : float(x)+float(y), vlist, 0.0)
86  sum2=reduce(lambda x,y : float(x)+(float(y)*float(y)), vlist, 0.0)
87  v = (sum2-(pow(sum1,2)/float(size)))/float(size-1)
88  if v < 0.0 :
89  if v > -1.e-15 : # tolerate floating point rounding errors
90  return 0.0
91  else :
92  raise StatsError("Something went wrong while calculating the variance (result < 0.0)")
93  return v
94 
95 def stdev(vlist) :
96  var = variance(vlist)
97  return math.sqrt(var)
98 
99 # set the rlimits of the chidren process (see limits above)
100 def setlimits():
101  print "Setting resource limit in child"
102  for k, v in limits.iteritems() :
103  resource.setrlimit(v[0], (v[1],v[1]))
104 def show_results(res_data, ths):
105  # read and show stats
106  stat_res = read(res_data)
107  elapsed_list = get_column_values(stat_res, "ELAPSED")
108  self_list = get_column_values(stat_res, "SELF_RU_UTIME")
109  lst = 0
110  print '{0:7s} \t {1:7s} \t {2:7s} \t {3:7s} \t {4:7s}'.format('threads', 'elapsed', 'user', 'elapsed_error', 'user_error')
111  for t in ths:
112  print '{0:7s} \t {1:5.2f} \t {2:5.2f} \t '.format(t, average(elapsed_list[lst:lst+ntimes]), average(self_list[lst:lst+ntimes])),
113  if ntimes > 1:
114  print '{0:5.2f} \t {1:5.2f} '.format(stdev(elapsed_list[lst:lst+ntimes]), stdev(self_list[lst:lst+ntimes]))
115  else:
116  print '{0:5.2f} \t {1:5.2f} '.format(0, 0)
117  lst = lst + ntimes
118 def run_exp(nthreads, ntimes, res_data, t):
119  # setup the command line
120  arguments = executable + ' -p 3'
121  arguments = arguments + ' -perf ' + res_data
122  arguments = arguments + ' -mc ' + inputdata
123  arguments = arguments + ' -nt ' + nthreads
124  arguments = arguments + ' -type ' + t
125  # execute
126  print arguments
127  args = shlex.split(arguments)
128  sout = None
129  serr = None
130  for i in range(ntimes) :
131  p = subprocess.Popen(args, preexec_fn=setlimits, stdout=sout, stderr=serr, cwd=rundir)
132  p.wait()
133  if (p.returncode != 0) :
134  print "Execution [FAILED]"
135 
136 # main - principal execution
137 if __name__ == "__main__":
138  # init arguments
139  ntimes = 1
140  threads = [ '2', '4', '6', '8', '12' ]
141  # process arguments
142  try :
143  opts, extra_args = getopt.getopt(sys.argv[1:], 't:n:')
144  except getopt.GetoptError, e:
145  error(str(e), 1)
146  for f, v in opts:
147  if f == '-n':
148  # number of times to repeat the experiments
149  ntimes=int(v)
150  # running serial
151  res = 'res_data_1.rdt'
152  run_exp('1', ntimes, res, '1')
153 
154  # running experiments parallel
155  for t in range(2,6):
156  res = 'res_data_' + str(t) + '.rdt'
157  for i in threads:
158  run_exp(i, ntimes, res, str(t))
159  print '# Execution Type : 1 #'
160  show_results(res, ['0'])
161  for t in range(2,6):
162  res = 'res_data_'+str(t)+'.rdt'
163  print '# Execution Type : ' + str(t) + ' #'
164  show_results(res, threads)
165 
def run_exp(nthreads, ntimes, res_data, t)
Definition: test.py:118
def variance(vlist)
Definition: test.py:81
def get_column_values(rdt_d, field)
Definition: test.py:67
def setlimits()
Definition: test.py:100
def stdev(vlist)
Definition: test.py:95
def read(filename)
Definition: test.py:43
def show_results(res_data, ths)
Definition: test.py:104
def average(vlist)
Definition: test.py:73
TPZFlopCounter pow(const TPZFlopCounter &orig, const TPZFlopCounter &xp)
Returns the power and increments the counter of the power.
Definition: pzreal.h:487
def error(message, status)
Definition: test.py:138