def run(self, processor, completed=False): """Essential method for performing calculations on the slave processors. @param processor: The slave processor object. @type processor: Processor instance @keyword completed: A flag specifying if the slave calculation is completed. This is currently meaningless, but will be passed to this run() method anyway so it needs to be present. @type completed: bool """ # Get the invariant data from this slave's data store. vect = fetch_data('vect') # Perform some random useless time-consuming stuff. num_calcs = 0 for i in range(self.N): # Random rotation matrix. R_random_hypersphere(self.R) # Rotate the vector. new_vect = dot(self.R, vect) # The length sum. self.length += norm(new_vect) # Keep track of the number of calculations. num_calcs += 1 # Process the results on the master. processor.return_object(Test_result_command(processor, memo_id=self.memo_id, num=num_calcs, length=self.length))
def run(self): """This required method executes the entire program.""" # Send the invariant data to the slaves' data stores. send_data_to_slaves('vect', self.vect) # Initialise the Processor box singleton. processor_box = Processor_box() # Loop over the slaves. num = processor_box.processor.processor_size() for i in range(num): # Partition out the calculations to one slave. slave = Test_slave_command(N=self.N/num) # Initialise the memo object. memo = Test_memo(name="Memo_"+repr(i), sum_fn=self.sum_fn) # Queue the slave command and its memo. processor_box.processor.add_to_queue(slave, memo) # Execute the calculations, waiting for completion. processor_box.processor.run_queue() # Calculate the average length. ave_len = fetch_data('total_length') / self.N # Final program printout. print("\n\nTotal number of calculations: %s" % self.num) print("Real length: %s" % self.real_length) print("Averaged vector length: %s" % ave_len)