def get_memory_list(db, mem, mod, baseline, replace): """Get the specified memory. mem is the name of the memory to get. mod is the model. baseline is the name of the file containing a baseline memory. replace is the memory to use as the main memory (or None). Returns the memory to simulate. """ if mem == 'model': # Use the subsystem from the model. ml = mod.memory elif mem == 'baseline': # Use the baseline subsystem. with open(baseline, 'r') as f: ml = memory.parse_memory_list(lex.Lexer(f)) elif mem == 'best': # Use the best subsystem. ml = get_best(db, mod) else: print('ERROR: invalid memory selected:', mem) sys.exit(-1) if replace: with open(replace, 'r') as f: mod.memory = memory.parse_memory(lex.Lexer(f)) for m in ml.memories: ptr = m while not isinstance(ptr.get_next(), MainMemory): ptr = ptr.get_next() ptr.set_next(mod.memory) return ml
def get_frequency(experiment, mem, baseline, keep): m = model.parse_model_file(experiment) db = database.get_instance() if mem == 'model': subsystem = m.memory elif mem == 'baseline': with open(baseline, 'r') as f: subsystem = memory.parse_memory_list(lex.Lexer(f)) elif mem == 'best': best_name, _, _ = db.get_best(m) best_file = StringIO.StringIO(best_name) subsystem = memory.parse_memory_list(lex.Lexer(best_file)) else: print('ERROR: invalid memory selected:', mem) sys.exit(-1) m.machine.frequency = 1 << 31 result = xilinx.run_xilinx(m.machine, subsystem, keep) print(get_experiment_name(experiment) + ',' + str(result.frequency) + ',' + str(result.bram_count))
def parse_model(lexer, model=None): if model is None: model = Model() while lexer.get_type() != lex.TOKEN_EOF: lexer.match(lex.TOKEN_OPEN) name = lexer.get_value() lexer.match(lex.TOKEN_LITERAL) if name == 'machine': model.machine = machine.parse_machine(lexer) elif name == 'memory': model.memory = memory.parse_memory_list(lexer) elif name == 'benchmarks': model.benchmarks = _parse_benchmarks(lexer) elif name == 'label': model.label = _parse_str(lexer) elif name == 'include': value = lexer.get_value() lexer.match(lex.TOKEN_LITERAL) parse_model(lex.Lexer(open(value, 'r')), model) else: lex.ParseError(lexer, 'invalid top-level component: ' + name) lexer.match(lex.TOKEN_CLOSE) return model
def get_best(db, mod): best_name, _, _ = db.get_best(mod) best_file = StringIO.StringIO(best_name) return memory.parse_memory_list(lex.Lexer(best_file))
def get_initial_memory(db, m, dist, directory, full): """Get the initial subsystem and its total access time. This runs from a process in the process pool. """ # Load the model from the database. # If not found, we need to collect statistics first. state = db.load(m) if len(state) == 0: if main_context.verbose: print('Collecting statistics') for b in m.benchmarks: mem = m.memory.get_subsystem(b.index) sd = dist.get_subsystem_distribution(mem) b.collect_stats(directory, sd) dist.save(state) db.save(m, state) # Determine if we should use the best or the initial # memory as a starting point. if random.randint(0, 7) < 7: # Attempt to load the best subsystem from the database. # If not found, we need to evaluate it. best_name, _, _ = db.get_best(m) if best_name: # Load statistics from the database. state = db.load(m) dist.load(state, m) # Create the initial memory subsystem. lexer = lex.Lexer(StringIO(best_name)) ml = memory.parse_memory_list(lexer) # Load the values for each subsystem. values, fstats = get_subsystem_values(db, m, ml, directory, full) return ml, values, fstats else: # Start from a random location. while True: # Loop until we get a valid subsystem. ml = m.memory.clone() for b in m.benchmarks: name = db.get_random(m, b.index) if name: lexer = lex.Lexer(StringIO(name)) ml.update(memory.parse_memory(lexer)) cost = ml.get_cost(m.machine, full) if cost.fits(m.machine.get_max_cost()): break state = db.load(m) dist.load(state, m) values, fstats = get_subsystem_values(db, m, ml, directory, full) return ml, values, fstats # Get the current value. if main_context.verbose: print('Initial Memory: {}'.format(m.memory)) ml = m.memory.clone() best_value, fstats = get_subsystem_values(db, m, ml, directory, full) total = get_total_value(db, m, ml, best_value, fstats) if not full: verify_model(db, m, ml, directory, total) db.insert_best(m, str(ml), total, ml.get_cost(m.machine, full)) if main_context.verbose: print('Memory: {}'.format(ml)) print('Value: {}'.format(total)) print('Cost: {}'.format(ml.get_cost(m.machine, full))) return get_initial_memory(db, m, dist, directory, full)