def getExecutionCode(self): assert not self.in_loop, "Bad state, in loop at end of compilation" # list -> string scan_linearized = emitlist(self.scan_pipelines) # Make sure we emitted all the wait statements if self.sequence_wait_statements: waits_linearized = emitlist( list(self.getAndFlushSeqWaitStatements())) else: waits_linearized = "" mem_linearized = \ emitlist(self.pipelines) + waits_linearized flush_linearized = emitlist(self.flush_pipelines) scan_linearize_wrap = self.language.group_wrap(gensym(), scan_linearized, {'type': 'scan'}) mem_linearize_wrap = self.language.group_wrap(gensym(), mem_linearized, {'type': 'in_memory'}) linearized = \ scan_linearize_wrap + mem_linearize_wrap + flush_linearized # substitute all lazily resolved symbols resolved = ResolvingSymbol.substitute(linearized, self.resolving_symbols) return resolved
def getExecutionCode(self): assert not self.in_loop, "Bad state, in loop at end of compilation" # list -> string scan_linearized = emitlist(self.scan_pipelines) # Make sure we emitted all the wait statements if self.sequence_wait_statements: waits_linearized = emitlist( list(self.getAndFlushSeqWaitStatements())) else: waits_linearized = "" mem_linearized = \ emitlist(self.pipelines) + waits_linearized flush_linearized = emitlist(self.flush_pipelines) scan_linearize_wrap = self.language.group_wrap(gensym(), scan_linearized, {'type': 'scan'}) mem_linearize_wrap = self.language.group_wrap(gensym(), mem_linearized, {'type': 'in_memory'}) linearized = \ scan_linearize_wrap + mem_linearize_wrap + flush_linearized # substitute all lazily resolved symbols resolved = ResolvingSymbol.substitute(linearized, self.resolving_symbols) return resolved
def getExecutionCode(self): # list -> string scan_linearized = emitlist(self.scan_pipelines) mem_linearized = emitlist(self.pipelines) scan_linearized_wrapped = self.language.group_wrap(gensym(), scan_linearized, {'type': 'scan'}) mem_linearized_wrapped = self.language.group_wrap(gensym(), mem_linearized, {'type': 'in_memory'}) linearized = scan_linearized_wrapped + mem_linearized_wrapped # substitute all lazily resolved symbols resolved = linearized % self.resolving_symbols return resolved
def getDeclCode(self): # declarations is a set # If this ever becomes a bottleneck when declarations are strings, # as in clang, then resort to at least symbol name deduping. s = set() def f(x): if x in s: return False else: s.add(x) return True # keep in original order code = emitlist(filter(f, self.declarations)) code += emitlist(filter(f, self.declarations_later)) return code % self.resolving_symbols
def addPipeline(self, p): pipeline_code = emitlist(self.current_pipeline_precode) +\ self.language.pipeline_wrap(self.pipeline_count, p, self.current_pipeline_properties) +\ emitlist(self.current_pipeline_postcode) # force scan pipelines to go first if self.current_pipeline_properties.get('type') == 'scan': self.scan_pipelines.append(pipeline_code) else: self.pipelines.append(pipeline_code) self.pipeline_count += 1 self.current_pipeline_properties = {} self.current_pipeline_precode = [] self.current_pipeline_postcode = []
def getDeclCode(self): # declarations is a set # If this ever becomes a bottleneck when declarations are strings, # as in cpp, then resort to at least symbol name deduping. s = set() def f(x): if x in s: return False else: s.add(x) return True # keep in original order code = emitlist(filter(f, self.declarations)) code += emitlist(filter(f, self.declarations_later)) return ResolvingSymbol.substitute(code, self.resolving_symbols)
def pipeline_wrap(ident, plcode, attrs): def apply_wrappers(code, wrappers): """ Wraps the code successively with wrappers. First wrapper is innermost @param code the initial code to wrap @param wrappers tuple of format (template, bindings). The template must include {{inner_code}} """ current_result = code for template, bindings in wrappers: allbindings = bindings.copy() allbindings.update({'inner_code': current_result}) current_result = template.render(allbindings) return current_result wrappers = [] timing_template = GrappaLanguage.cgenv().get_template( 'grappa_pipeline_timing.cpp') wrappers.append((timing_template, locals())) dependences = attrs.get('dependences', set()) assert isinstance(dependences, set) _LOG.debug("pipeline %s dependences %s", ident, dependences) dependence_code = emitlist([wait_statement(d) for d in dependences]) dependence_template = GrappaLanguage.cgenv().from_string(""" {{dependence_code}} {{inner_code}} """) wrappers.append((dependence_template, locals())) syncname = attrs.get('sync') if syncname: dependence_captures = emitlist( [",&{dep}".format(dep=d) for d in dependences]) sync_template = GrappaLanguage.cgenv().get_template('spawn.cpp') wrappers.append((sync_template, locals())) return apply_wrappers(plcode, wrappers)
def pipeline_wrap(ident, plcode, attrs): code = plcode # timing code if True: inner_code = code timing_template = ct("""auto start_%(ident)s = walltime(); %(inner_code)s auto end_%(ident)s = walltime(); auto runtime_%(ident)s = end_%(ident)s - start_%(ident)s; VLOG(1) << "pipeline %(ident)s: " << runtime_%(ident)s << " s"; VLOG(1) << "timestamp %(ident)s start " << std::setprecision(15)\ << start_%(ident)s; VLOG(1) << "timestamp %(ident)s end " << std::setprecision(15)\ << end_%(ident)s; """) code = timing_template % locals() dependences = attrs.get('dependences', set()) assert isinstance(dependences, set) _LOG.debug("pipeline %s dependences %s", ident, dependences) dependence_code = emitlist([wait_statement(d) for d in dependences]) dependence_captures = emitlist( [",&{dep}".format(dep=d) for d in dependences]) code = """{dependence_code} {inner_code} """.format(dependence_code=dependence_code, inner_code=code) syncname = attrs.get('sync') if syncname: inner_code = code sync_template = ct(""" CompletionEvent %(syncname)s; spawn(&%(syncname)s, [=%(dependence_captures)s] { %(inner_code)s }); """) code = sync_template % locals() return code
def getExecutionCode(self): # list -> string scan_linearized = emitlist(self.scan_pipelines) mem_linearized = \ emitlist(self.pipelines) + emitlist(self.main_wait_statements) flush_linearized = emitlist(self.flush_pipelines) scan_linearize_wrap = self.language.group_wrap(gensym(), scan_linearized, {'type': 'scan'}) mem_linearize_wrap = self.language.group_wrap(gensym(), mem_linearized, {'type': 'in_memory'}) linearized = \ scan_linearize_wrap + mem_linearize_wrap + flush_linearized # substitute all lazily resolved symbols resolved = linearized % self.resolving_symbols return resolved
def addPipeline(self, p): LOG.debug("output pipeline %s", self.current_pipeline_properties) all_p = emitlist(self.current_pipeline_precode) \ + p \ + emitlist(self.current_pipeline_postcode) pipeline_code = \ self.language.pipeline_wrap(self.pipeline_count, all_p, self.current_pipeline_properties) # force scan pipelines to go first if self.current_pipeline_properties.get('type') == 'scan': assert not self.in_loop, "scan pipeline not supported in loop" self.scan_pipelines.append(pipeline_code) else: self._append_pipeline_code(pipeline_code) self.pipeline_count += 1 self.current_pipeline_properties = {} self.current_pipeline_precode = [] self.current_pipeline_postcode = []
def addPipeline(self, p): LOG.debug("output pipeline %s", self.current_pipeline_properties) all_p = emitlist(self.current_pipeline_precode) \ + p \ + emitlist(self.current_pipeline_postcode) pipeline_code = \ self.language.pipeline_wrap(self.pipeline_count, all_p, self.current_pipeline_properties) # force scan pipelines to go first if self.current_pipeline_properties.get('type') == 'scan': assert not self.in_loop, "scan pipeline not supported in loop" self.scan_pipelines.append(pipeline_code) else: self._append_pipeline_code(pipeline_code) self.pipeline_count += 1 self.current_pipeline_properties = {} self.current_pipeline_precode = [] self.current_pipeline_postcode = []
def compile(self, resultsym): """Compile this plan. Result sym is the variable name to use to hold the result of this operator.""" # TODO: Why is language not an argument? code = self.language.comment("Compiled subplan for %s" % self) code += self.language.log("Evaluating subplan %s" % self) if self.bound: code += self.language.assignment(resultsym, self.bound) else: argsyms = [gensym() for arg in self.args] code += emitlist([arg.compile(sym) for arg, sym in zip(self.args, argsyms)] + [self.compileme(resultsym, argsyms)]) return code
def getCleanupCode(self): # cleanups is a set. # If this ever becomes a bottleneck when declarations are strings, # as in cpp, then resort to at least symbol name deduping. # TODO: better would be to mark elements of self.cleanups as # TODO: "do dedup" or "don't dedup" s = set() def f(x): if x in s: return False else: s.add(x) return True code = emitlist(filter(f, self.cleanups)) return ResolvingSymbol.substitute(code, self.resolving_symbols)
def getCleanupCode(self): # cleanups is a set. # If this ever becomes a bottleneck when declarations are strings, # as in cpp, then resort to at least symbol name deduping. # TODO: better would be to mark elements of self.cleanups as # TODO: "do dedup" or "don't dedup" s = set() def f(x): if x in s: return False else: s.add(x) return True code = emitlist(filter(f, self.cleanups)) return ResolvingSymbol.substitute(code, self.resolving_symbols)
def getInitCode(self): # inits is a set. # If this ever becomes a bottleneck when declarations are strings, # as in clang, then resort to at least symbol name deduping. # TODO: better would be to mark elements of self.initializers as # TODO: "do dedup" or "don't dedup" s = set() def f(x): if x in s: return False else: s.add(x) return True code = emitlist(filter(f, self.initializers)) return code % self.resolving_symbols
def __file_code__(self, t, state): code = "" state.addPreCode('std::ofstream logfile;\n') resultfile = str(self.relation_key).split(":")[2] opentuple = 'logfile.open("%s");\n' % resultfile schemafile = self.write_schema(self.scheme()) state.addPreCode(schemafile) state.addPreCode(opentuple) loggings = emitlist([self.language().log_file_unquoted( "{0}".format(t.get_code(i))) for i in range(len(t.scheme))]) code += loggings code += "logfile << '\\n';" state.addPostCode('logfile.close();') return code
def group_wrap(ident, grpcode, attrs): timing_template = GrappaLanguage.cgenv().get_template( 'grappa_group_timing.cpp') inner_code = grpcode timer_metric = None if attrs['type'] == 'in_memory': timer_metric = "in_memory_runtime" # only trace in_memory tracing_on = "Grappa::Metrics::start_tracing();" tracing_off = "Grappa::Metrics::stop_tracing();" elif attrs['type'] == 'scan': timer_metric = "saved_scan_runtime" tracing_on = "" tracing_off = "" code = emitlist(["Grappa::Metrics::reset();", timing_template.render(locals())]) return code
def getInitCode(self): code = emitlist(self.initializers) return code % self.resolving_symbols