class TestMemoryOptimizer(unittest.TestCase): def setUp(self): self.model = Model() self.model.machine = MachineType(addr_bits=32) self.rand = MemoryDistribution(1) self.ml = MemoryList([]) self.optimizer = MemoryOptimizer(self.model, self.ml, None, None, '.') self.optimizer.constructors = [self.mocks_constructor] def mocks_constructor(self, mod, nxt, rand, cost): return MockMemory(nxt) def test_insert1(self): main = MockMemory() result = self.optimizer.insert(self.rand, main, 0, 0) self.assertEqual(str(result), "(mock (mock))") def test_insert2(self): main = MockMemory(MockMemory(), MockMemory()) result = self.optimizer.insert(self.rand, main, 1, 0) self.assertEqual(str(result), "(mock (mock (mock))(mock))") def test_remove1(self): main = MockMemory(MockMemory(), MockMemory(MockMemory(MockMemory()), MockMemory()), MockMemory(MockMemory())) result = self.optimizer.remove(self.rand, main, 3) s = "(mock (mock)(mock (mock)(mock))(mock (mock)))" self.assertEqual(str(result), s)
def setUp(self): self.model = Model() self.model.machine = MachineType(addr_bits=32) self.rand = MemoryDistribution(1) self.ml = MemoryList([]) self.optimizer = MemoryOptimizer(self.model, self.ml, None, None, '.') self.optimizer.constructors = [self.mocks_constructor]
def optimize(db, mod, iterations, seed, directory, full): random.seed(seed) # Create the random number distributions to use for modifying # the memory subsystems and create the benchmark processes. dist = sim.DistributionList(seed) pl = sim.ProcessList(mod.machine, directory) for b in mod.benchmarks: pl.add_benchmark(b) # Load the first memory to use. # This will gather statistics if necessary. last_ml, values, fstats = get_initial_memory(db, mod, dist, directory, full) last_ml.reset(mod.machine) best_cost = last_ml.get_cost(mod.machine, full) best_value = get_total_value(db, mod, last_ml, values, fstats) result_count = db.get_result_count(mod) assert(best_cost.fits(mod.machine.get_max_cost())) # Run full simulation (if we're not already) to ensure the # model is accurate enough. if not full: verify_model(db, mod, last_ml, directory, best_value) # Perform the optimization. o = MemoryOptimizer(mod, best_value, seed, dist, directory, full) db.update_status(best_value, best_cost, result_count, str(o)) max_iter = 1000 count = max_iter while True: # Get the next subsystem to try. ml, subsystem = o.get_next(last_ml) # Evaluate the current memory subsystem. new_values, fstats = get_subsystem_values(db, mod, ml, directory, full) total = get_total_value(db, mod, ml, new_values, fstats) cost = ml.get_cost(mod.machine, full) assert(cost.fits(mod.machine.get_max_cost())) # Verify the model every MAX_ITER iterations. count -= 1 if count == 0: if not full: verify_model(db, mod, ml, directory, total) count = max_iter max_iter *= 10 # Update the best. updated = False lower_cost = cost.fits(best_cost) if total < best_value or (total == best_value and lower_cost): best_value = total best_cost = cost db.update_best(mod, str(ml), total, cost) updated = True # Request the best and result count only after evaluating # a new state or updating the best. if updated: result_count = db.get_result_count(mod) db.update_status(best_value, best_cost, result_count, str(o)) if main_context.verbose: print('Best Value: {}'.format(best_value)) print('Best Cost: {}'.format(best_cost)) if result_count >= iterations: return False if main_context.verbose: print('Iteration {} / {}'.format(result_count + 1, iterations)) # Update the optimizer. if o.update(total): last_ml = ml