def explicit_relay(): """Intermediate component with explicit settings. Sends and receives overlay settings explicitly, rather than having MUSCLE handle them. This just passes all information on. """ instance = Instance({ Operator.F_INIT: ['in[]'], Operator.O_F: ['out[]']}) while instance.reuse_instance(False): # f_init assert instance.get_setting('test2', 'float') == 13.3 assert instance.get_port_length('in') == instance.get_port_length( 'out') msgs = list() for slot in range(instance.get_port_length('in')): msg = instance.receive_with_settings('in', slot) assert msg.data.startswith('testing') assert msg.settings['test2'] == 14.4 msgs.append(msg) assert instance.get_setting('test2') == 13.3 # o_f for slot in range(instance.get_port_length('out')): instance.send('out', msgs[slot], slot)
def load_balancer() -> None: """A proxy which divides many calls over few instances. Put this component between a driver and a set of models, or between a macro model and a set of micro models. It will let the driver or macro-model submit as many calls as it wants, and divide them over the available (micro)model instances in a round-robin fashion. Assumes a fixed number of micro-model instances. Ports: front_in: Input for calls, connect to driver/macro-model O_I. back_out: Output to workers, connect to F_INIT of instances that do the work. back_in: Input for results, connect to O_F of instances that do the work. front_out: Output back to driver/macro-model S. """ instance = Instance({ Operator.F_INIT: ['front_in[]'], Operator.O_I: ['back_out[]'], Operator.S: ['back_in[]'], Operator.O_F: ['front_out[]'] }) while instance.reuse_instance(False): # F_INIT started = 0 # number started and index of next to start done = 0 # number done and index of next to return num_calls = instance.get_port_length('front_in') num_workers = instance.get_port_length('back_out') instance.set_port_length('front_out', num_calls) while done < num_calls: while started - done < num_workers and started < num_calls: msg = instance.receive_with_settings('front_in', started) instance.send('back_out', msg, started % num_workers) started += 1 msg = instance.receive_with_settings('back_in', done % num_workers) instance.send('front_out', msg, done) done += 1
def qmc(): """qMC implementation. """ instance = Instance({Operator.O_F: ['settings_out[]']}) while instance.reuse_instance(): # o_f settings0 = Settings({'test2': 14.4}) assert instance.is_connected('settings_out') assert instance.is_vector_port('settings_out') assert not instance.is_resizable('settings_out') length = instance.get_port_length('settings_out') assert length == 10 for slot in range(length): instance.send('settings_out', Message(0.0, None, settings0), slot)