def run(self): while True: start = self.env.now # First look up the metadata if self.lookup_time > 0: lookup_req = self.hdfs.get_namenode_req(self.client_id, self.lookup_time) yield lookup_req.submit() lookup_done = simpy.AllOf(self.env, lookup_req.all_done_list()) yield lookup_done # Then read the data if self.read_size > 0: read_req, done_evt = self.hdfs.get_read_req(self.client_id, self.read_size) yield read_req.submit() yield done_evt # Then write if self.write_size > 0: write_req, ack_done = self.hdfs.get_write_req(None, self.client_id, self.write_size) yield write_req.submit() yield ack_done latency = self.env.now - start self.num_reqs += 1 # a lookup + a read + a write counts as one req self.total_latency += latency if self.think_time != 0: yield self.env.timeout(self.think_time)
def reset_and_sim(self, policies): self.simpy_env = simpy.Environment() self.done = False self.fire_extinguish_events = [simpy.Event(self.simpy_env) for i in range(self.n_fires)] fire_levels = [] for i, n in enumerate(self.num_fires_of_each_size): fire_levels += [i+1] * n if self.fire_locations is not None: self.fires = [ Fire(self, self.simpy_env, i, fire_levels[i], fl) for i, fl in enumerate(self.fire_locations) ] else: # we want to randomize fire_locations = ( 2.*np.random.random_sample((self.n_fires,2)) - 1.).tolist() self.fires = [ Fire(self, self.simpy_env, i, fire_levels[i], fl) for i, fl in enumerate(fire_locations) ] if self.start_positions is not None: self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i,sp in enumerate(self.start_positions) ] else: # we want to randomize start_positions = ( 2.*np.random.random_sample((self.n_agents,2)) - 1.).tolist() self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i,sp in enumerate(start_positions) ] # Process all UAVs for uav in self.env_agents: self.simpy_env.process( uav.sim() ) # Process all fires for fire in self.fires: self.simpy_env.process( fire.sim() ) done = simpy.AllOf(self.simpy_env, self.fire_extinguish_events) self.simpy_env.run(until = simpy.AnyOf(self.simpy_env, [done, self.simpy_env.timeout(MAX_SIMTIME)]) ) self.done = True if(True): for uav in self.env_agents: try: uav.action_event.interrupt() except RuntimeError: pass self.simpy_env.run(until = self.simpy_env.now*(1.0001)) assert sum([uav.get_reward() for uav in self.env_agents]) == 0, 'There were unaccounted for rewards' # Collect observations, actions, etc.. and return them observations = [ u.observations for u in self.env_agents] actions = [ u.actions for u in self.env_agents] rewards = [ u.rewards for u in self.env_agents] agent_infos = [ u.agent_infos for u in self.env_agents] env_infos = [ u.env_infos for u in self.env_agents] offset_t_sojourn = [ u.offset_t_sojourn for u in self.env_agents ] return observations, actions, rewards, agent_infos, env_infos, offset_t_sojourn
def _barrier(self): """ Grab and hold the rest of the ncq slots (already holding one) """ reqs = [] for i in range(self.ncq.ncq_depth - 1): slot_req = self.ncq.slots.request() reqs.append(slot_req) yield simpy.AllOf(self.env, reqs) for req in reqs: self.ncq.slots.release(req)
def run(self): while True: req = self.new_req_func() assert req.client_id == self.client_id start = self.env.now yield req.submit() all_done = simpy.AllOf(self.env, req.all_done_list()) yield all_done latency = self.env.now - start self.num_reqs += 1 self.total_latency += latency if latency > self.max_latency: self.max_latency = latency think_time = self.get_think_time() if think_time != 0: yield self.env.timeout(think_time)
def run_apps(self, num, comms_per_app=(500, 1000)): """ Start a number of apps to generate submission commands. Args: ---- num (int): The number of apps to start. comms_per_app: (int, int): The number of commands in this range an app will generate. If (a, b) is the range, the number of commands to generate will be a <= x < b. """ events = [] for app_idx in range(num): event_submitted = self.__env.event() self.__env.process( self.__app(app_idx, comms_per_app, event_submitted)) events.append(event_submitted) self.__all_command_submitted = simpy.AllOf(self.__env, events)
def process_write_request(self, request: WriteRequest) -> int: data = self.env.process(self.__data_plane(request)) control = self.env.process(self.__control_plane(request)) yield simpy.AllOf(self.env, [data, control]) self.__manager.answer_client_write(request)
def __control_plane(self, request: WriteRequest): journal = self.env.process(self.__perform_journal_operation(request)) metadata = self.env.process(self.__propagate_metadata(request)) yield simpy.AllOf(self.env, [journal, metadata])
def reset_and_sim(self, policies): self.simpy_env = simpy.Environment() self.done = False self.fire_extinguish_events = [ simpy.Event(self.simpy_env) for i in range(self.n_fires) ] if self.fire_locations is True: # Use presets assert self.num_fire_clusters == 3, 'Only 3 clusters / fires per cluster implemented right now :(' assert self.n_fires / self.num_fire_clusters == 3, 'Only 3 clusters / fires per cluster implemented right now :(' R = np.array([[np.cos(120 * deg), np.sin(-120 * deg)], [np.sin(120 * deg), np.cos(120 * deg)]]) f1 = np.reshape(np.array([-0.01, 1]), (2, 1)) f2 = np.reshape(np.array([0.01, 1]), (2, 1)) f3 = np.reshape(np.array([0, 1 - 0.02 * math.sin(60 * deg)]), (2, 1)) fire_locations = [ f1, f2, f3, R.dot(f1), R.dot(f2), R.dot(f3), R.T.dot(f1), R.T.dot(f2), R.T.dot(f3) ] fire_locations = [ np.reshape(f, (2, )).tolist() for f in fire_locations ] self.fires = [ Fire(self, self.simpy_env, i, 1, fl) for i, fl in enumerate(fire_locations) ] else: raise NotImplementedError # we want to randomize fire_locations = (2. * np.random.random_sample( (self.n_fires, 2)) - 1.).tolist() self.fires = [ Fire(self, self.simpy_env, i, fire_levels[i], fl) for i, fl in enumerate(fire_locations) ] if self.start_positions is not None: self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i, sp in enumerate(self.start_positions) ] else: # we want to randomize start_positions = (2. * np.random.random_sample( (self.n_agents, 2)) - 1.).tolist() self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i, sp in enumerate(start_positions) ] # Process all UAVs agent_events = [] for uav in self.env_agents: agent_events.append(self.simpy_env.process(uav.sim())) # Process all fires for fire in self.fires: self.simpy_env.process(fire.sim()) self.max_simtime_event = self.simpy_env.timeout(MAX_SIMTIME) self.done_event = simpy.Event(self.simpy_env) self.simpy_env.run( until=simpy.AllOf(self.simpy_env, self.fire_extinguish_events) | self.max_simtime_event) self.done_event.succeed() self.done = True self.simpy_env.run(until=simpy.AllOf(self.simpy_env, agent_events)) rewards = [uav.get_reward() for uav in self.env_agents] if sum(rewards) != 0: print('There were unaccounted for rewards') [print(r) for r in rewards] raise RuntimeError # Collect observations, actions, etc.. and return them observations = [u.observations for u in self.env_agents] actions = [u.actions for u in self.env_agents] rewards = [u.rewards for u in self.env_agents] agent_infos = [u.agent_infos for u in self.env_agents] env_infos = [u.env_infos for u in self.env_agents] offset_t_sojourn = [u.offset_t_sojourn for u in self.env_agents] return observations, actions, rewards, agent_infos, env_infos, offset_t_sojourn