?? info.py
字號:
# Copyright (c) 2003, 2004# The Regents of The University of Michigan# All Rights Reserved## This code is part of the M5 simulator, developed by Nathan Binkert,# Erik Hallnor, Steve Raasch, and Steve Reinhardt, with contributions# from Ron Dreslinski, Dave Greene, Lisa Hsu, Kevin Lim, Ali Saidi,# and Andrew Schultz.## Permission is granted to use, copy, create derivative works and# redistribute this software and such derivative works for any purpose,# so long as the copyright notice above, this grant of permission, and# the disclaimer below appear in all copies made; and so long as the# name of The University of Michigan is not used in any advertising or# publicity pertaining to the use or distribution of this software# without specific, written prior authorization.## THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE# UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT# WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR# IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF# THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES,# INCLUDING DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL# DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION# WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER# ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.from __future__ import divisionimport operator, re, typessource = Nonedisplay_run = 0global globalTicksglobalTicks = Nonedef total(f): if isinstance(f, FormulaStat): v = f.value else: v = f f = FormulaStat() if isinstance(v, (list, tuple)): f.value = reduce(operator.add, v) else: f.value = v return fdef unaryop(op, f): if isinstance(f, FormulaStat): v = f.value else: v = f if isinstance(v, (list, tuple)): return map(op, v) else: return op(v)def zerodiv(lv, rv): if rv == 0.0: return 0.0 else: return operator.truediv(lv, rv)def wrapop(op, lv, rv): if isinstance(lv, str): return lv if isinstance(rv, str): return rv return op(lv, rv)def same(lrun, rrun): for lx,rx in zip(lrun.keys(),rrun.keys()): if lx != rx: print 'lx != rx' print lx, rx print lrun.keys() print rrun.keys() return False for ly,ry in zip(lrun[lx].keys(),rrun[rx].keys()): if ly != ry: print 'ly != ry' print ly, ry print lrun[lx].keys() print rrun[rx].keys() return False return True def binaryop(op, lf, rf): result = {} if isinstance(lf, FormulaStat) and isinstance(rf, FormulaStat): lv = lf.value rv = rf.value theruns = [] for r in lv.keys(): if rv.has_key(r): if same(lv[r], rv[r]): theruns.append(r) else: raise AttributeError for run in theruns: result[run] = {} for x in lv[run].keys(): result[run][x] = {} for y in lv[run][x].keys(): result[run][x][y] = wrapop(op, lv[run][x][y], rv[run][x][y]) elif isinstance(lf, FormulaStat): lv = lf.value for run in lv.keys(): result[run] = {} for x in lv[run].keys(): result[run][x] = {} for y in lv[run][x].keys(): result[run][x][y] = wrapop(op, lv[run][x][y], rf) elif isinstance(rf, FormulaStat): rv = rf.value for run in rv.keys(): result[run] = {} for x in rv[run].keys(): result[run][x] = {} for y in rv[run][x].keys(): result[run][x][y] = wrapop(op, lf, rv[run][x][y]) return resultdef sums(x, y): if isinstance(x, (list, tuple)): return map(lambda x, y: x + y, x, y) else: return x + ydef alltrue(seq): return reduce(lambda x, y: x and y, seq)def allfalse(seq): return not reduce(lambda x, y: x or y, seq)def enumerate(seq): return map(None, range(len(seq)), seq)def cmp(a, b): if a < b: return -1 elif a == b: return 0 else: return 1class Statistic(object): def __init__(self, data): self.__dict__.update(data.__dict__) if not self.__dict__.has_key('value'): self.__dict__['value'] = None if not self.__dict__.has_key('bins'): self.__dict__['bins'] = None if not self.__dict__.has_key('ticks'): self.__dict__['ticks'] = None if 'vc' not in self.__dict__: self.vc = {} def __getattribute__(self, attr): if attr == 'ticks': if self.__dict__['ticks'] != globalTicks: self.__dict__['value'] = None self.__dict__['ticks'] = globalTicks return self.__dict__['ticks'] if attr == 'value': if self.__dict__['ticks'] != globalTicks: if self.__dict__['ticks'] != None and \ len(self.__dict__['ticks']) == 1: self.vc[self.__dict__['ticks'][0]] = self.__dict__['value'] self.__dict__['ticks'] = globalTicks if len(globalTicks) == 1 and self.vc.has_key(globalTicks[0]): self.__dict__['value'] = self.vc[globalTicks[0]] else: self.__dict__['value'] = None if self.__dict__['value'] == None: self.__dict__['value'] = self.getValue() return self.__dict__['value'] else: return super(Statistic, self).__getattribute__(attr) def __setattr__(self, attr, value): if attr == 'bins' or attr == 'ticks': if attr == 'bins': if value is not None: value = source.getBin(value) #elif attr == 'ticks' and type(value) is str: # value = [ int(x) for x in value.split() ] self.__dict__[attr] = value self.__dict__['value'] = None self.vc = {} else: super(Statistic, self).__setattr__(attr, value) def getValue(self): raise AttributeError, 'getValue() must be defined' def zero(self): return False def __ne__(self, other): return not (self == other) def __str__(self): return '%f' % (float(self))class FormulaStat(object): def __add__(self, other): f = FormulaStat() f.value = binaryop(operator.add, self, other) return f def __sub__(self, other): f = FormulaStat() f.value = binaryop(operator.sub, self, other) return f def __mul__(self, other): f = FormulaStat() f.value = binaryop(operator.mul, self, other) return f def __truediv__(self, other): f = FormulaStat() f.value = binaryop(zerodiv, self, other) return f def __mod__(self, other): f = FormulaStat() f.value = binaryop(operator.mod, self, other) return f def __radd__(self, other): f = FormulaStat() f.value = binaryop(operator.add, other, self) return f def __rsub__(self, other): f = FormulaStat() f.value = binaryop(operator.sub, other, self) return f def __rmul__(self, other): f = FormulaStat() f.value = binaryop(operator.mul, other, self) return f def __rtruediv__(self, other): f = FormulaStat() f.value = binaryop(zerodiv, other, self) return f def __rmod__(self, other): f = FormulaStat() f.value = binaryop(operator.mod, other, self) return f def __neg__(self): f = FormulaStat() f.value = unaryop(operator.neg, self) return f def __getitem__(self, idx): f = FormulaStat() f.value = {} for key in self.value.keys(): f.value[key] = {} f.value[key][0] = {} f.value[key][0][0] = self.value[key][idx][0] return f def __float__(self): if isinstance(self.value, FormulaStat): return float(self.value) if not self.value.has_key(display_run): return (1e300*1e300) if len(self.value[display_run]) == 1: return self.value[display_run][0][0] else: #print self.value[display_run] return self.value[display_run][4][0] #raise ValueError def display(self): import display d = display.VectorDisplay() d.flags = 0 d.precision = 1 d.name = 'formula' d.desc = 'formula' val = self.value[display_run] d.value = [ val[x][0] for x in val.keys() ] d.display()class Scalar(Statistic,FormulaStat): def getValue(self): return source.data(self, self.bins, self.ticks) def display(self): import display p = display.Print() p.name = self.name p.desc = self.desc p.value = float(self) p.flags = self.flags p.precision = self.precision if display.all or (self.flags & flags.printable): p.display() def comparable(self, other): return self.name == other.name def __eq__(self, other): return self.value == other.value def __isub__(self, other): self.value -= other.value return self def __iadd__(self, other): self.value += other.value return self def __itruediv__(self, other): if not other: return self self.value /= other return self class Vector(Statistic,FormulaStat): def getValue(self): return source.data(self, self.bins, self.ticks); def display(self): import display if not display.all and not (self.flags & flags.printable): return d = display.VectorDisplay() d.__dict__.update(self.__dict__) d.display() def comparable(self, other): return self.name == other.name and \ len(self.value) == len(other.value) def __eq__(self, other): if isinstance(self.value, (list, tuple)) != \ isinstance(other.value, (list, tuple)): return False if isinstance(self.value, (list, tuple)): if len(self.value) != len(other.value): return False else: for v1,v2 in zip(self.value, other.value): if v1 != v2: return False return True else: return self.value == other.value def __isub__(self, other): self.value = binaryop(operator.sub, self.value, other.value) return self def __iadd__(self, other): self.value = binaryop(operator.add, self.value, other.value) return self def __itruediv__(self, other): if not other: return self if isinstance(self.value, (list, tuple)): for i in xrange(len(self.value)): self.value[i] /= other else: self.value /= other return self class Formula(Vector): def getValue(self):
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -