def Sacc(self, expr, opts): # self.S(expr, opts) self.scattering += 1 out = expr.getOut() if self.context.bindingTable.isBound(out): return print(expr.inexpr[0].__class__.__name__) getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) #added code #end of added code sub = expr.getInexprMat(0) if sub.size[0] * sub.size[1] <= opts['nu'] * opts['nu']: outPhys = Array(out.name, out.size, opts, safelyScalarize=opts['scarep']) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys] else: outPhys = Array(out.name, out.size, opts) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys] self.scattering -= 1
def _populate_binding_table(self, mat_dict, opts): import networkx as nx g = nx.DiGraph() for name, mat in mat_dict.iteritems(): ow = mat.attr.get('ow', None) if ow is None: g.add_node(name) else: g.add_edge(name, ow) order_of_decl = nx.topological_sort(g, reverse=True) for name in order_of_decl: mat = mat_dict[name] if mat.attr.get('ow', None) is not None: self.context.bindingTable.add_binding_overwrite(mat) else: if not mat.attr['o'] and mat.isScalar(): physLayout = Scalars(mat.name, mat.size, opts, isIn=mat.attr['i'], isParam=True) else: physLayout = Array(mat.name, mat.size, opts, isIn=mat.attr['i'], isOut=mat.attr['o']) if self.context.bindingTable.addBinding(mat, physLayout): if mat.attr['t']: physLayout.safelyScalarize = opts['scarep'] self.context.declare += [physLayout] else: self.context.signature += [physLayout]
def S(self, expr, opts): self.scattering += 1 out = expr.getOut() if self.context.bindingTable.isBound(out): return getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) sub = expr.getInexprMat(0) safelyScalarize = opts['scarep'] and sub.size[0] <= opts[ 'nu'] and sub.size[1] <= opts['nu'] outPhys = Array(out.name, out.size, opts, safelyScalarize=safelyScalarize) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys] ####### PhysLayout replacement should be delayed until structures and accesses are computed ############ # if sub.size[0] <= opts['nu'] and sub.size[1] <= opts['nu']: # outPhys = Array(out.name, out.size, opts, safelyScalarize=opts['scarep']) # if self.context.bindingTable.addBinding(out, outPhys): # self.context.declare += [outPhys] # # if not isinstance(expr.inexpr[0], G): # # If we directly scatter a gather we should keep the phys. layout separated # # Otherwise we can bind the subexpr's phys. layout to a larger one where # # the op. S is supposed to scatter its input. # self.replaceConnectedPhysicalLayout(outPhys, expr, 0) # sub.fL, sub.fR = expr.fL, expr.fR # else: # outPhys = Array(out.name, out.size, opts) # if self.context.bindingTable.addBinding(out, outPhys): # self.context.declare += [outPhys] self.scattering -= 1
def declare(self, m, subM, opts): if subM is not None and icode.bindingTable.isBound(subM): subPhys = icode.bindingTable.getPhysicalLayout(subM) icode.bindingTable.addBinding(m, subPhys) else: outPhys = Array(m.name, m.size, opts, safelyScalarize=opts['scarep']) if icode.bindingTable.addBinding(m, outPhys): icode.declare += [outPhys]
def reshape(self, m, M, N, bounds, opts): # With Heterogeneous layouts, the size of certain base cases may depend upon some of the indices. # In MMM, the "accumulation" matrix's PhysLayout is one such a case. currPhys = icode.getPhysicalLayout(m) if isinstance(currPhys, Array): if any(map(lambda idx: currPhys.hasIdx(idx), bounds)): currPhys.subs(bounds) elif currPhys.size < M * N: newPhys = Array(m.name, (M, N), opts, safelyScalarize=opts['scarep']) icode.replacePhysicalLayout(currPhys, newPhys)
def HRed(self, expr, opts): out = expr.getOut() if self.context.bindingTable.isBound(out): return getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) outPhys = Array(out.name, out.size, opts, safelyScalarize=opts['scarep']) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys]
def G(self, expr, opts): out = expr.getOut() if out.size[0] <= opts['nu'] and out.size[1] <= opts['nu']: getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) subPhys = self.context.bindingTable.getPhysicalLayout( expr.getInexprMat(0)) self.context.bindingTable.addBinding(out, subPhys) out.fL, out.fR = expr.fL, expr.fR else: if self.context.bindingTable.isBound(out): return getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) outPhys = Array(out.name, out.size, opts) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys]
def S(self, expr, opts): out = expr.getOut() if self.context.bindingTable.isBound(out): return getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) outPhys = Array(out.name, out.size, opts, safelyScalarize=opts['scarep']) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys] if not isinstance(expr.inexpr[0], G): # If we directly scatter a gather we should keep the phys. layout separated # Otherwise we can bind the subexpr's phys. layout to a larger one where # the op. S is supposed to scatter its input. self.replaceConnectedPhysicalLayout(outPhys, expr, 0)
def Sum(self, expr, opts): out = expr.getOut() if self.context.bindingTable.isBound(out) or expr.isBinding: return expr.isBinding = True getattr(self, expr.inexpr[0].__class__.__name__)(expr.inexpr[0], opts) outPhys = None if isinstance(expr.inexpr[0], S): # In case of summing up scattered matrices we can either use S's physLayout # or making S use a new one. Here we go for the first option. outPhys = self.context.bindingTable.getPhysicalLayout( expr.getInexprMat(0)) self.context.bindingTable.addBinding(out, outPhys) elif not isinstance(expr.inexpr[0], G): # If not directly summing up gathered matrices we can bind sub-expressions to the same # phys. layout. outPhys = Array(out.name, out.size, opts, safelyScalarize=opts['scarep']) if self.context.bindingTable.addBinding(out, outPhys): self.context.declare += [outPhys] self.replaceConnectedPhysicalLayout(outPhys, expr, 0) # If the sum is accumulating, it should do it referring to same phys. layout if len(expr.inexpr) > 1: for i in range(1, len(expr.inexpr)): getattr(self, expr.inexpr[i].__class__.__name__)(expr.inexpr[i], opts) if not isinstance(expr.inexpr[i], G): # I'm not sure it can happen to have G subexprs when accumulating. # By now checking such a condition just in case. self.replaceConnectedPhysicalLayout(outPhys, expr, i) expr.isBinding = False
def createArray(mat, opts, useIn=False, useOut=False, useScalarize=False, useComplex=False, field=None): if useComplex: phys_size = list(mat.size) if opts['complexlayout'] == 'BlkInterLeaved': #assuming that nu is going to be the block size blocksize = opts['nu'] if phys_size[1] % blocksize != 0: phys_size[1] += (blocksize - (phys_size[1] % blocksize)) phys_size[1] *= 2 elif opts['complexlayout'] == 'Split': pass phys_size = tuple(phys_size) if useIn and useOut: if field is None: return Array(mat.name, phys_size, opts, isIn=mat.attr['i'], isOut=mat.attr['o'], field=opts['complexlayout']) else: return Array(mat.name, phys_size, opts, isIn=mat.attr['i'], isOut=mat.attr['o'], field=field) elif useScalarize: if field is None: return Array(mat.name, phys_size, opts, safelyScalarize=opts['scarep'], field=opts['complexlayout']) else: return Array(mat.name, phys_size, opts, safelyScalarize=opts['scarep'], field=field) else: if field is None: return Array(mat.name, phys_size, opts, field=opts['complexlayout']) else: return Array(mat.name, phys_size, opts, field=field) else: if useIn and useOut: return Array(mat.name, mat.size, opts, isIn=mat.attr['i'], isOut=mat.attr['o']) elif useScalarize: return Array(mat.name, mat.size, opts, safelyScalarize=opts['scarep']) else: return Array(mat.name, mat.size, opts)