def testing2Param(resMgr, p0, p1, opts): ''' Test the kernel opts['hfilebasename'] for the cartesian product of the values in the given intervals p0, p1. ''' sizesP0 = range(p0[0], p0[1] + 1) if len(p0) == 2 else range(p0[0], p0[1] + 1, p0[2]) sizesP1 = range(p1[0], p1[1] + 1) if len(p1) == 2 else range(p1[0], p1[1] + 1, p1[2]) sizes = [(i, j) for i in sizesP0 for j in sizesP1] for M, N in sizes: opts['static_params'] = [M, N] genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: llprog = parseLL({'M': M, 'N': N}, opts) # expr = eval(opts['hfilebasename'])(N, opts) compiler = Compiler(llprog, opts) else: compiler = LibraryCode(opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ") printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts)
def go(self, intro: str, outro: str, base: str, target: str, *, fn_name: Optional[str] = None, **kwargs: Any) -> int: base = intro + "\n" + base + "\n" + outro target = intro + "\n" + target + "\n" + outro compiler = Compiler("test/compile.sh", show_errors=True) # For debugging, to avoid the auto-deleted directory: # target_dir = tempfile.mkdtemp() with tempfile.TemporaryDirectory() as target_dir: with open(os.path.join(target_dir, "base.c"), "w") as f: f.write(base) target_o = compiler.compile(target, show_errors=True) assert target_o is not None shutil.move(target_o, os.path.join(target_dir, "target.o")) shutil.copy2("test/compile.sh", os.path.join(target_dir, "compile.sh")) if fn_name: with open(os.path.join(target_dir, "function.txt"), "w") as f: f.write(fn_name) opts = main.Options(directories=[target_dir], stop_on_zero=True, **kwargs) return main.run(opts)[0]
def setUpClass(cls): compiler = Compiler("test/compile.sh") cls.tmp_dirs = {} for test_c, test_fn in c_files_list: d = tempfile.TemporaryDirectory() file_test = os.path.join("test", test_c) file_actual = os.path.join(d.name, "actual.c") file_base = os.path.join(d.name, "base.c") file_target = os.path.join(d.name, "target.o") actual_preprocessed = preprocess(file_test, cpp_args=["-DACTUAL"]) base_preprocessed = preprocess(file_test, cpp_args=["-UACTUAL"]) strip_other_fns_and_write(actual_preprocessed, test_fn, file_actual) strip_other_fns_and_write(base_preprocessed, test_fn, file_base) actual_source = Path(file_actual).read_text() target_o = compiler.compile(actual_source, show_errors=True) assert target_o is not None shutil.copy2(target_o, file_target) os.remove(target_o) shutil.copy2("test/compile.sh", d.name) cls.tmp_dirs[(test_c, test_fn)] = d
def testing3Param(resMgr, p0, p1, p2, opts): ''' Test the kernel for the cartesian product of the values in the given intervals p0, p1, p3. ''' sizesP0 = range(p0[0], p0[1] + 1) if len(p0) == 2 else range(p0[0], p0[1] + 1, p0[2]) sizesP1 = range(p1[0], p1[1] + 1) if len(p1) == 2 else range(p1[0], p1[1] + 1, p1[2]) sizesP2 = range(p2[0], p2[1] + 1) if len(p2) == 2 else range(p2[0], p2[1] + 1, p2[2]) sizes = [(i, j, k) for i in sizesP0 for j in sizesP1 for k in sizesP2] fine = True for M, K, N in sizes: try: opts['static_params'] = [M, K, N] genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: llprog = parseLL({'M': M, 'K': K, 'N': N}, opts) # expr = eval(opts['hfilebasename'])(M, K, N, opts) compiler = Compiler(llprog, opts) else: compiler = LibraryCode(opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) fine = fine and compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ") printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts) except Exception: if opts.get('breakonexc', False): raise fine = False openLog(opts) ts = '%s.%s' % (date.today().isoformat(), time.strftime('%H-%M-%S', time.localtime(time.time()))) msg = "=@" * 10 + " Begin Exc Report from testing3Param (" + ts + ") " + "=@" * 10 + "\n" msg += "-" * 10 + " opts " + "-" * 10 + "\n" msg += str(opts) + "\n" msg += "-" * 10 + " traceback " + "-" * 10 + "\n" printToLog(msg, opts, openf=False, closef=False) traceback.print_exc(file=opts['logfile']) msg = "\n" + "=@" * 10 + " End Exc Report (" + ts + ") " + "=@" * 10 + "\n" printToLog(msg, opts, openf=False) return fine
def testingRandom(resMgr, maxops, maxdim, maxeq, numgen, opts): fine = True llgen = RandomLLGen(maxops=maxops, maxdim=maxdim, maxeq=maxeq) for _ in range(numgen): genres = llgen.gen() srcfile = open(opts['source'], 'w') srcfile.write(genres[0]) srcfile.close() for pvalues in genres[2]: try: opts['static_params'] = pvalues genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: llprog = parseLL(dict(zip(genres[1], pvalues)), opts) compiler = Compiler(llprog, opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) fine = fine and compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ") printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts) else: print "Please enable code generation." except Exception: if opts.get('breakonexc', False): raise fine = False openLog(opts) ts = '%s.%s' % (date.today().isoformat(), time.strftime('%H-%M-%S', time.localtime(time.time()))) msg = "=@" * 10 + " Begin Exc Report from testingRandom (" + ts + ") " + "=@" * 10 + "\n" msg += "Generation of:\n\n%s\n\nParams:\n\n%s = %s\n\n" % ( genres[0], str(genres[1]), str(genres[2])) msg += "-" * 10 + " opts " + "-" * 10 + "\n" msg += str(opts) + "\n" msg += "-" * 10 + " traceback " + "-" * 10 + "\n" printToLog(msg, opts, openf=False, closef=False) traceback.print_exc(file=opts['logfile']) msg = "\n" + "=@" * 10 + " End Exc Report (" + ts + ") " + "=@" * 10 + "\n" printToLog(msg, opts, openf=False) return fine
def main(): argv = sys.argv[1:] parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", type=str) parser.add_argument("-o", "--output", type=str) args = parser.parse_args(argv) input_path = Path(args.input) output_path = Path(args.output) if not input_path.exists(): print(f"ERROR: Input path '{input_path}' does not exist.") return if not output_path.exists(): print(f"ERROR: Output path '{output_path}' does not exist.") return if input_path.parent == output_path: output_path = output_path.joinpath("out") output_path = output_path.joinpath(input_path.name) start_time = time.process_time() print(f"Compiling at {output_path}...") # TODO Save compilation time by not recompiling unedited files in a datapack # by comparing compiled file timestamp and uncompiled file timestamp. # User can force compile all files by adding the --force argument if output_path.exists(): shutil.rmtree(output_path, ignore_errors=True) shutil.copytree(input_path, output_path, ignore=shutil.ignore_patterns("*.mcfunctionx")) for file in input_path.joinpath("data").glob("**/*.mcfunctionx"): compiler = Compiler(file, input_path, output_path) compiler.compile() print( f"Done! Took {(time.process_time() - start_time)*1000}ms to complete.")
def testing1Param(resMgr, p, opts): sizes = range(p[0], p[1] + 1) if len(p) == 2 else range(p[0], p[1] + 1, p[2]) fine = True for M in sizes: try: opts['static_params'] = [M] genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: llprog = parseLL({'M': M}, opts) # expr = eval(opts['hfilebasename'])(N, opts) compiler = Compiler(llprog, opts) else: compiler = LibraryCode(opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) fine = fine and compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ") printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts) except Exception: if opts.get('breakonexc', False): raise fine = False openLog(opts) ts = '%s.%s' % (date.today().isoformat(), time.strftime('%H-%M-%S', time.localtime(time.time()))) msg = "=@" * 10 + " Begin Exc Report from testing1Param (" + ts + ") " + "=@" * 10 + "\n" msg += "-" * 10 + " opts " + "-" * 10 + "\n" msg += str(opts) + "\n" msg += "-" * 10 + " traceback " + "-" * 10 + "\n" printToLog(msg, opts, openf=False, closef=False) traceback.print_exc(file=opts['logfile']) msg = "\n" + "=@" * 10 + " End Exc Report (" + ts + ") " + "=@" * 10 + "\n" printToLog(msg, opts, openf=False) return fine
def compile_base(compile_script: str, source: str, c_file: str, out_file: str) -> None: if "PERM_" in source: print( "Cannot test-compile imported code because it contains PERM macros. " "It is recommended to put in PERM macros after import.") return escaped_c_file = json.dumps(c_file) source = "#line 1 " + escaped_c_file + "\n" + source compiler = Compiler(compile_script, show_errors=True) o_file = compiler.compile(source) if o_file: shutil.move(o_file, out_file) else: print("Warning: failed to compile .c file.")
def testing1toNParam(resMgr, p0, fs, opts): sizesP0 = range(p0[0], p0[1] + 1) if len(p0) == 2 else range(p0[0], p0[1] + 1, p0[2]) params = [f[0] for f in fs] sizes = [] for i in sizesP0: sizes.append([f[1](i) for f in fs]) fine = True for pvalues, i in zip(sizes, sizesP0): try: opts['static_params'] = pvalues genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: opts['pvalues'] = { p: pvalue for p, pvalue in zip(params, pvalues) } llprog = parseLL( {p: pvalue for p, pvalue in zip(params, pvalues)}, opts) compiler = Compiler(llprog, opts) else: compiler = LibraryCode(opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) fine = fine and compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ", sizeParams=[i]) printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts) except Exception: # openLog(opts) ts = '%s.%s' % (date.today().isoformat(), time.strftime('%H-%M-%S', time.localtime(time.time()))) msg = "=@" * 10 + " Begin Exc Report from testing1toNParam (" + ts + ") " + "=@" * 10 + "\n" msg += "-" * 10 + " opts " + "-" * 10 + "\n" msg += str(opts) + "\n" msg += "-" * 10 + " traceback " + "-" * 10 + "\n" # printToLog(msg, opts, openf=False, closef=False) # traceback.print_exc(file=opts['logfile']) msg += traceback.format_exc() msg += "\n" + "=@" * 10 + " End Exc Report (" + ts + ") " + "=@" * 10 + "\n" printToLog(msg, opts) if opts.get('breakonexc', False): # openLog(opts, 'r') # body = opts['logfile'].read() # closeLog(opts) send_email_with_log(opts, subj="SLinGen Report on Exception", body=msg) raise fine = False return fine
def testingNParam(resMgr, ps, opts): ''' Test the kernel for the cartesian product of the values in the given intervals p0, p1, p2, ... ''' params = [pi[0] for pi in ps] sizesPi = [ range(pi[1], pi[2] + 1) if len(pi) == 3 else range(pi[1], pi[2] + 1, pi[3]) for pi in ps ] sizes = list(itertools.product(*sizesPi)) fine = True for pvalues in sizes: try: opts['static_params'] = list(pvalues) genCode = not 'libname' in opts onlygen = 'onlygen' in opts and opts['onlygen'] compiler = None if genCode: opts['pvalues'] = { p: pvalue for p, pvalue in zip(params, pvalues) } llprog = parseLL( {p: pvalue for p, pvalue in zip(params, pvalues)}, opts) compiler = Compiler(llprog, opts) else: compiler = LibraryCode(opts) s = datetime.now() if not onlygen: printToLog( " " + "Starting compiler at " + str(s) + " ----------------------------------------", opts) fine = fine and compiler.compile() e = datetime.now() if not (genCode and onlygen): procOutcome(resMgr, compiler, opts, " ") printToLog( " " + "Compiling took " + str(e - s) + " ----------------------------------------------------------", opts) recordTime((e - s).total_seconds(), opts) except Exception: openLog(opts) ts = '%s.%s' % (date.today().isoformat(), time.strftime('%H-%M-%S', time.localtime(time.time()))) msg = "=@" * 10 + " Begin Exc Report from testingNParam (" + ts + ") " + "=@" * 10 + "\n" msg += "-" * 10 + " opts " + "-" * 10 + "\n" msg += str(opts) + "\n" msg += "-" * 10 + " traceback " + "-" * 10 + "\n" # printToLog(msg, opts, openf=False, closef=False) # traceback.print_exc(file=opts['logfile']) msg += traceback.format_exc() msg += "\n" + "=@" * 10 + " End Exc Report (" + ts + ") " + "=@" * 10 + "\n" # printToLog(msg, opts, openf=False) printToLog(msg, opts) if opts.get('breakonexc', False): send_email_with_log(opts, subj="SLinGen Report on Exception", body=msg) raise fine = False return fine
def test_init(self): c = Compiler(self.rootPath) c.compile()