def recv(self, source): from rexfw import Parcel return Parcel(source, 'remaster0', (self.calculate_work_from_source(source), self.calculate_heat_from_source(source)))
def terminate_replicas(self): ''' Makes all replicas break from their listening loop and quit ''' for r in self.replica_names: parcel = Parcel(self.name, r, DieRequest(self.name)) self._comm.send(parcel, dest=r)
def recv(self, source): from rexfw import Parcel from rexfw.replicas.requests import DoNothingRequest parcel = Parcel(source, 'remaster0', DoNothingRequest(source)) self.received.append(parcel) return parcel
def _send_reject_exchange_request(self, dest): ''' Sends a request to reject a proposed swap state. :param dest: name of destination replica :type dest: str ''' parcel = Parcel(self.name, dest, AcceptBufferedProposalRequest(self.name, False)) self._comm.send(parcel, dest)
def _send_works_heats(self, proposal): ''' Sends works and heats corresponding to a swap proposal to the master object :param proposal: a state proposed for swapping :type proposal: depends on your application ''' self._comm.send(Parcel(self.name, self._current_master, (float(proposal.work), float(proposal.heat))), self._current_master)
def _send_sample_requests(self, replicas): ''' Send requests to replicas to sample from their respective PDFs :param replicas: replicas which are supposed to perform a sampling step :type replicas: list ''' for replica_name in replicas: parcel = Parcel(self.name, replica_name, SampleRequest(self.name)) self._comm.send(parcel, dest=replica_name)
def _send_send_stats_requests(self, replicas): ''' Send requests to replicas to send sampling statistics to this master object. :param replica: replica names :type replicas: list ''' for r in replicas: parcel = Parcel(self.name, r, SendStatsRequest(self.name)) self._comm.send(parcel, dest=r)
def _send_state_and_energy(self, request): ''' Sends the current state and energy to another replica and makes it store them :param request: a request object telling this replica which other replica to send state and energy to :type request: :class:`.GetStateAndEnergyRequest` ''' new_request = StoreStateEnergyRequest(self.name, self.state, self.energy) self._comm.send(Parcel(self.name, request.sender, new_request), request.sender)
def _send_stats(self, request): ''' Sends sampling statistics to master object. Also empties sampling stats list :param request: a request object asking this replica to do the above :type request: :class:`.SendStatsRequest` ''' parcel = Parcel(self.name, request.sender, self.sampler_stats) self._comm.send(parcel, request.sender) self.sampler_stats = []
def _send_get_state_and_energy_request(self, request): ''' Sends a :class:`.GetStateAndEnergyRequest` to another replica given in the request parameter :param request: a request object telling this replica which other replica to ask for its state and energy :type request: :class:`.SendGetStateAndEnergyRequest` ''' self._current_master = request.sender self._comm.send(Parcel(self.name, request.partner, GetStateAndEnergyRequest(self.name)), request.partner)
def _store_state_energy(self, request): ''' Stores state and energy given in request parameter. Also sends a :class:`.DoNothingRequest` to the master object. This is neccessary to keep communication in sync :param request: a request object containing current state and energy of a different replica :type request: :class:`.StoreStateEnergyRequest` ''' self._buffered_partner_state = request.state self._buffered_partner_energy = request.energy from rexfw.replicas.requests import DoNothingRequest parcel = Parcel(self.name, self._current_master, DoNothingRequest(self.name)) self._comm.send(parcel, dest=self._current_master)
def _propose(self, request): partner_name = request.partner params = request.params proposer_params = params.proposer_params self._current_master = request.sender proposer = list(set(self.proposers.keys()).intersection(set(params.proposers)))[-1] self.proposers[proposer].partner_name = partner_name proposal = self.proposers[proposer].propose(self, self._buffered_partner_state, self._buffered_partner_energy, proposer_params) self._comm.send(Parcel(self.name, self._current_master, proposal), self._current_master) self._buffered_proposal = proposal[-1]
def _send_propose_request(self, replica1, replica2, params): ''' Sends a request to replica1 telling it to propose a state for replica2 using information in params (an ExchangeParams object defined in ) :param replica1: name of 1st replica involved in swap :type replica1: str :param replica2: name of 2nd replica involved in swap :type replica2: str :param params: an :class:`.ExchangeParams` object holding information required to perform the swap :type params: :class:`.ExchangeParams` ''' request = ProposeRequest(self.name, replica2, params) self._comm.send(Parcel(self.name, replica1, request), dest=replica1)
def _send_get_state_and_energy_request(self, replica1, replica2): ''' Sends a request to replica1 to make it send a request to replica2 ordering it (replica2) to send its (replica2) state and and energy to replica1. Receives a None from replica1 and replica2; sent once buffered state / energies have been set. This is to sync everything and really hacky :param replica1: name of 1st replica involved in swap :type replica1: str :param replica2: name of 2nd replica involved in swap :type replica2: str ''' self._comm.send( Parcel(self.name, replica2, SendGetStateAndEnergyRequest(self.name, replica1)), replica2) self._comm.recv(source=replica2)
def _send_dump_samples_request(self, smin, smax, offset, dump_step): ''' Send requests to write samples to files :param smin: first sample index :type smin: int :param smax: last sample index :type smax: int :param offset: offset which to add to sample index :type offset: int :param dump_step: sub-sampling step; write only every dump_step-th sample :type dump_step: int ''' for r in self.replica_names: request = DumpSamplesRequest(self.name, smin, smax, offset, dump_step) self._comm.send(Parcel(self.name, r, request), dest=r)
def _accept_buffered_proposal(self, request): ''' If the information in the request object says so, accepts a proposal by setting the replica state to the buffered proposal and, in any case, appending the current replica state to the list of stored samples. Also syncs communication with master object and updates stored replica energies and the sample counter :param request: a request containing information whether the proposal should be accepted or not :type request: :class:`.AcceptBufferedProposalRequest` ''' from copy import deepcopy if request.accept: self.state = self._buffered_proposal self.samples.append(deepcopy(self.state)) from rexfw.replicas.requests import DoNothingRequest self._comm.send(Parcel(self.name, self._current_master, DoNothingRequest(self.name)), self._current_master) self._update_energy_trace() self._increase_sample_counter()
def recv(self, source): obj = Parcel(source, 'remaster0', None) self.received.append([obj, source]) return obj