Exemplo n.º 1
0
	def routine(self):
		log_info(self.logger,"Started")

		rewrite_loop(self.rule_classes, self.init_goals, self.logger, lambda: self.get_msg, self.send_msgs, self.create_new_worker
                            ,self.location, output_logger=self.output_logger)

		log_info(self.logger,"Shutting Down")
Exemplo n.º 2
0
def rewrite_loop(rule_classes, init_goals, logger, recv_msg_future_func, send_msgs_func, create_new_location_func, location, output_logger=None):
	fact_stores = new_stores()
	histories = new_histories()

	rules = map(lambda rule_class: rule_class(), rule_classes)
	if location != None:
		for rule in rules:
			rule.set_rank( loc_rank(location.value) )
	interp_rules = interpret_rules(rules, fact_stores)

	goals = HeapGoals()
	matching_funcs = generate_matching_functions(goals, fact_stores, histories, interp_rules, logger, send_msgs_func, create_new_location_func, location=location)

	for goal in init_goals:
		goals.push( make_fact_repr(goal) )

	recv_msg_func = recv_msg_future_func()
	current_steps  = default_steps
	current_factor = backoff_factor
	done = False

	while not done:
		current_steps = default_steps
		ext_fact_repr = recv_msg_func()

		if ext_fact_repr != None:
			while ext_fact_repr != None:
				goals.push(ext_fact_repr)
				recv_msg_func = recv_msg_future_func()
				ext_fact_repr = recv_msg_func()
			current_factor = backoff_factor
		else:
			current_steps  *= current_factor 
			current_factor *= 2

		while current_steps > 0:
			try:
				act_fact_repr = goals.pop()
				matching_funcs[act_fact_repr['sym_id']](act_fact_repr)
				current_steps -= 1
			except IndexError:
				ext_fact_repr = try_until(recv_msg_func, times=pause_times)
				if ext_fact_repr != None:
					goals.push(ext_fact_repr)
					recv_msg_func = recv_msg_future_func()
					current_factor = backoff_factor
					current_steps  = 0
				else:
					done = True
					break

	log_info(logger,pretty_stores( fact_stores ))

	if not (output_logger == None):
		log_info(output_logger,"Location %s:\n%s" % (location.value,pretty_stores(fact_stores,brief=True)) )
Exemplo n.º 3
0
def execute_msr(init_goals, rule_classes, use_mpi=True):

	if not use_mpi:
		logger = init_logger("msr", log_file="msr.log")
		log_info(logger,"Started")
		rewrite_loop(rule_classes, init_goals, logger, (lambda: lambda: None), (lambda _: None), (lambda x: None), None )
		log_info(logger,"Shutting Down!")
	else:
		comm = MPI.COMM_WORLD
		rank = comm.Get_rank()

		# Check rules for dynamic spawning pattern.
		allow_dynamic_spawning = False
		for rule_class in rule_classes:
			rule = rule_class()
			if rule.req_dynamic_spawning:
				allow_dynamic_spawning = True
				break

		if allow_dynamic_spawning:
			mp = MSRMasterProcess(rank, init_goals, rule_classes, file_logging=True, output_file="output.log")
			mp.start()
		else:
			logger = init_logger("rank_%s" % rank, log_file="rank_%s.log" % rank)
			output_logger = init_logger("output", log_file="output.log")
			log_info(logger,"Started")
			init_goals = filter_goals_by_rank(init_goals, rank)
			rewrite_loop(rule_classes, init_goals, logger, receive_fact_future_mpi, send_facts, lambda x: None, lift(loc(rank)), output_logger=output_logger)
			log_info(logger,"Shutting Down!")
Exemplo n.º 4
0
	def is_alive(self):
		'''
		procs = self.worker_procs
		for proc_id in procs:
			proc = procs[proc_id]
			if proc.is_alive():
				return True
		'''
		if len(self.worker_procs) > 0:
			return True

		# debug("Master %s finds all workers Dead!" % self.rank)
		log_info(self.logger,"All workers Dead!")

		if self.curr_sleep_limit > 0:
			return True
		else:
			return False
Exemplo n.º 5
0
    def is_alive(self):
        '''
		procs = self.worker_procs
		for proc_id in procs:
			proc = procs[proc_id]
			if proc.is_alive():
				return True
		'''
        if len(self.worker_procs) > 0:
            return True

        # debug("Master %s finds all workers Dead!" % self.rank)
        log_info(self.logger, "All workers Dead!")

        if self.curr_sleep_limit > 0:
            return True
        else:
            return False
Exemplo n.º 6
0
			def exec_rhs():
				log_info(logger, "Applying rule %s" % rule_id)
				# sys.stdout.write("Local: ")
				# for goal in local_goals:
				#	sys.stdout.write("%s, " % goal)
				# sys.stdout.write("\nExternal: ")
				# for goal in external_goals:
				# 	sys.stdout.write("%s, " % goal)
				# sys.stdout.write("\n\n")
				# add_goals(goals, local_goals)
				(local_goals,external_goals) = partition_goals_by_proc_id(rhs(), loc_proc_id(location.value))
				spawn_new_locs()
				log_info(logger, "Sending internal goals: %s" % map(make_fact_repr_loc,local_goals) )
				log_info(logger, "Sending external goals: %s" % map(make_fact_repr_loc,external_goals) )
				goals.push_many( map(make_fact_repr,local_goals) )
				send_goal_func( map(make_fact_repr_loc,external_goals) )
Exemplo n.º 7
0
				def spawn_new_locs():
					new_locations = exist_locs_func()
					log_info(logger, "Creating new %s locations: %s" % (len(new_locations),new_locations))
					for location in new_locations:
						proc_id = loc_proc_id( location )
						create_new_location_func( proc_id )
Exemplo n.º 8
0
	def is_alive(self):
		log_info(self.logger,"Testing liveness: %s" % (self.proc_id,self.proc.is_alive()))
		return self.proc.is_alive()
Exemplo n.º 9
0
	def start(self):
		log_info(self.logger, "Started")
		master_channel  = self.master_channel
		worker_channels = self.worker_channels

		self.create_new_worker(str(self.rank))
		for _ in range(0,self.init_workers-1):
			self.create_new_worker()

		done_something = False
		recv_future = receive_fact_future_mpi()
		while self.is_alive():

			if not master_channel.empty():
				int_msg = master_channel.get()
				task_type = int_msg['task']
				if task_type == DELIVER_MSG:
					msgs = int_msg['msgs']
					log_info(self.logger, "Delivering messages %s" % msgs)
					for data in msgs:
						if data['rank'] == self.rank:
							log_info(self.logger, "Internal message, sending to worker %s" % data['proc_id'])
							worker_channels[data['proc_id']].put( data )
						else:
							log_info(self.logger, "External message, sending to MPI rank %s" % data['rank'])
							send_fact( data )
				elif task_type == CREATE_WORKER:
					proc_id = int_msg['proc_id']
					log_info(self.logger, "Creating worker %s" % proc_id)
					worker_channel = Queue()
					worker_channels[proc_id] = worker_channel
					p = self.new_worker(self.rank, proc_id, worker_channel, master_channel)
					p.start()
					self.worker_procs[proc_id] = p
				elif task_type == KILL_SELF:
					proc_id = int_msg['proc_id']
					log_info(self.logger, "Killing worker %s" % proc_id)
					# self.worker_procs[proc_id].terminate()
					del self.worker_channels[proc_id]
					del self.worker_procs[proc_id]
				done_something = True
	
			mpi_msg = recv_future()
			
			if mpi_msg != None:
				worker_channels[mpi_msg['proc_id']].put( mpi_msg )
				done_something = True
				recv_future = receive_fact_future_mpi()

			if done_something:
				done_something = False
				self.reset_sleep()
			else:
				self.sleep()
		log_info(self.logger,"Shutting Down")
Exemplo n.º 10
0
	def sleep(self):
		log_info(self.logger, "Sleeping: %s" % self.curr_sleep_limit )
		time.sleep(self.curr_sleep_length)
		if self.curr_sleep_limit > 0:
			self.curr_sleep_length *= self.sleep_factor
			self.curr_sleep_limit -= 1
Exemplo n.º 11
0
 def is_alive(self):
     log_info(self.logger,
              "Testing liveness: %s" % (self.proc_id, self.proc.is_alive()))
     return self.proc.is_alive()
Exemplo n.º 12
0
    def start(self):
        log_info(self.logger, "Started")
        master_channel = self.master_channel
        worker_channels = self.worker_channels

        self.create_new_worker(str(self.rank))
        for _ in range(0, self.init_workers - 1):
            self.create_new_worker()

        done_something = False
        recv_future = receive_fact_future_mpi()
        while self.is_alive():

            if not master_channel.empty():
                int_msg = master_channel.get()
                task_type = int_msg['task']
                if task_type == DELIVER_MSG:
                    msgs = int_msg['msgs']
                    log_info(self.logger, "Delivering messages %s" % msgs)
                    for data in msgs:
                        if data['rank'] == self.rank:
                            log_info(
                                self.logger,
                                "Internal message, sending to worker %s" %
                                data['proc_id'])
                            worker_channels[data['proc_id']].put(data)
                        else:
                            log_info(
                                self.logger,
                                "External message, sending to MPI rank %s" %
                                data['rank'])
                            send_fact(data)
                elif task_type == CREATE_WORKER:
                    proc_id = int_msg['proc_id']
                    log_info(self.logger, "Creating worker %s" % proc_id)
                    worker_channel = Queue()
                    worker_channels[proc_id] = worker_channel
                    p = self.new_worker(self.rank, proc_id, worker_channel,
                                        master_channel)
                    p.start()
                    self.worker_procs[proc_id] = p
                elif task_type == KILL_SELF:
                    proc_id = int_msg['proc_id']
                    log_info(self.logger, "Killing worker %s" % proc_id)
                    # self.worker_procs[proc_id].terminate()
                    del self.worker_channels[proc_id]
                    del self.worker_procs[proc_id]
                done_something = True

            mpi_msg = recv_future()

            if mpi_msg != None:
                worker_channels[mpi_msg['proc_id']].put(mpi_msg)
                done_something = True
                recv_future = receive_fact_future_mpi()

            if done_something:
                done_something = False
                self.reset_sleep()
            else:
                self.sleep()
        log_info(self.logger, "Shutting Down")
Exemplo n.º 13
0
 def sleep(self):
     log_info(self.logger, "Sleeping: %s" % self.curr_sleep_limit)
     time.sleep(self.curr_sleep_length)
     if self.curr_sleep_limit > 0:
         self.curr_sleep_length *= self.sleep_factor
         self.curr_sleep_limit -= 1