def main(t_step, t_units): r"""Function to execute integration. Args: t_step (float): The time step that should be used. t_units (str): Units of the time step. """ print('Hello from Python other_model: timestep = %s %s' % (t_step, t_units)) t_step = units.add_units(t_step, t_units) t_start = units.add_units(0.0, t_units) t_end = units.add_units(1.0, 'day') state = timestep_calc(t_start) # Set up connections matching yaml # Timestep synchonization connection will default to 'timesync' timesync = YggTimesync('timesync') out = YggOutput('output') # Initialize state and synchronize with other models t = t_start ret, state = timesync.call(t, state) if not ret: raise RuntimeError("other_model(Python): Initial sync failed.") print('other_model(Python): t = % 8s' % t, end='') for k, v in state.items(): print(', %s = %+ 5.2f' % (k, v), end='') print('') # Send initial state to output flag = out.send(dict(state, time=t)) if not flag: raise RuntimeError("other_model(Python): Failed to send " "initial output for t=%s." % t) # Iterate until end while t < t_end: # Perform calculations to update the state t = t + t_step state = timestep_calc(t) # Synchronize the state ret, state = timesync.call(t, state) if not ret: raise RuntimeError("other_model(Python): sync for t=%f failed." % t) print('other_model(Python): t = % 8s' % t, end='') for k, v in state.items(): print(', %s = %+ 5.2f' % (k, v), end='') print('') # Send output flag = out.send(dict(state, time=t)) if not flag: raise RuntimeError( "other_model(Python): Failed to send output for t=%s." % t) print('Goodbye from Python other_model')
def fibClient(args): iterations = int(args[0]) print('Hello from Python rpcFibCli: iterations = %d ' % iterations) # Set up connections matching yaml # RPC client-side connection will be $(server_name)_$(client_name) ymlfile = YggInput("yaml_in") rpc = YggRpcClient("rpcFibSrv_rpcFibCli", "%d", "%d %d") log = YggOutput("output_log") # Read entire contents of yaml ret, ycontent = ymlfile.recv() if not ret: raise RuntimeError('rpcFibCli(P): RECV ERROR') print('rpcFibCli: yaml has %d lines' % len(ycontent.split(b'\n'))) for i in range(1, iterations + 1): # Call the server and receive response print('rpcFibCli(P): fib(->%-2d) ::: ' % i, end='') ret, fib = rpc.rpcCall(np.int32(i)) if not ret: raise RuntimeError('rpcFibCli(P): RPC CALL ERROR') # Log result by sending it to the log connection s = 'fib(%2d<-) = %-2d<-\n' % tuple(fib) print(s, end='') ret = log.send(s) if not ret: raise RuntimeError('rpcFibCli(P): SEND ERROR') print('Goodbye from Python rpcFibCli')
def main(iterations): r"""Function to execute client communication with server that computes numbers in the Fibonacci sequence. Args: iterations (int): The number of Fibonacci numbers to log. """ print('Hello from Python client: iterations = %d ' % iterations) # Set up connections matching yaml # RPC client-side connection will be $(server_name)_$(client_name) rpc = YggRpcClient("server_client", "%d", "%d") log = YggOutput("output_log", 'fib(%-2d) = %-2d\n') # Iterate over Fibonacci sequence for i in range(1, iterations + 1): # Call the server and receive response print('client(Python): Calling fib(%d)' % i) ret, result = rpc.call(np.int32(i)) if not ret: raise RuntimeError('client(Python): RPC CALL ERROR') fib = result[0] print('client(Python): Response fib(%d) = %d' % (i, fib)) # Log result by sending it to the log connection ret = log.send(np.int32(i), fib) if not ret: raise RuntimeError('client(Python): SEND ERROR') print('Goodbye from Python client')
def run(args): msg_count = int(args[0]) msg_size = int(args[1]) print('Hello from Python pipe_src: msg_count = %d, msg_size = %d' % ( msg_count, msg_size)) # Ins/outs matching with the the model yaml outq = YggOutput('output_pipe') print("pipe_src(P): Created I/O channels") # Send test message multiple times test_msg = b'0' * msg_size count = 0 for i in range(msg_count): ret = outq.send(test_msg) if not ret: raise RuntimeError('pipe_src(P): SEND ERROR ON MSG %d' % i) count += 1 print('Goodbye from Python source. Sent %d messages.' % count)
def run(): print('Hello from Python pipe_dst') # Ins/outs matching with the the model yaml inq = YggInput('input_pipe') outf = YggOutput('output_file') print("pipe_dst(P): Created I/O channels") # Continue receiving input from the queue count = 0 while True: ret, buf = inq.recv() if not ret: print("pipe_dst(P): Input channel closed") break ret = outf.send(buf) if not ret: raise RuntimeError("pipe_dst(P): SEND ERROR ON MSG %d" % count) count += 1 print('Goodbye from Python destination. Received %d messages.' % count)
def runhello(): print('Hello from Python') # Ins/outs matching with the the model yaml inf = YggInput('inFile') outf = YggOutput('outFile') inq = YggInput('helloQueueIn') outq = YggOutput('helloQueueOut') print("hello(P): Created I/O channels") # Receive input from a local file ret, buf = inf.recv() if not ret: raise RuntimeError('hello(P): ERROR FILE RECV') print('hello(P): Received %d bytes from file: %s' % (len(buf), buf)) # Send output to the output queue ret = outq.send(buf) if not ret: raise RuntimeError('hello(P): ERROR QUEUE SEND') print('hello(P): Sent to outq') # Receive input form the input queue ret, buf = inq.recv() if not ret: raise RuntimeError('hello(P): ERROR QUEUE RECV') print('hello(P): Received %d bytes from queue: %s' % (len(buf), buf)) # Send output to a local file ret = outf.send(buf) if not ret: raise RuntimeError('hello(P): ERROR FILE SEND') print('hello(P): Sent to outf') print('Goodbye from Python')
#!/usr/bin/python from yggdrasil.interface.YggInterface import YggInput, YggOutput if __name__ == '__main__': # Get input and output channels matching yaml in1 = YggInput('input1_python') in2 = YggInput('static_python') out1 = YggOutput('output_python') print('SaM(P): Set up I/O channels') # Get input from input1 channel ret, adata = in1.recv() if not ret: raise RuntimeError('SaM(P): ERROR RECV from input1') a = int(adata) print('SaM(P): Received %d from input1' % a) # Get input from static channel ret, bdata = in2.recv() if not ret: raise RuntimeError('SaM(P): ERROR RECV from static') b = int(bdata) print('SaM(P): Received %d from static' % b) # Compute sum and send message to output channel sum = a + b outdata = '%d' % sum ret = out1.send(outdata) if not ret: raise RuntimeError('SaM(P): ERROR SEND to output')
# Import classes for input/output channels from yggdrasil.interface.YggInterface import YggInput, YggOutput # Initialize input/output channels in_channel = YggInput('inputB') out_channel = YggOutput('outputB') # Loop until there is no longer input or the queues are closed while True: # Receive input from input channel # If there is an error, the flag will be False flag, obj = in_channel.recv() if not flag: print("Model B: No more input.") break # Print received message print('Model B: %s' % str(obj)) # Send output to output channel # If there is an error, the flag will be False flag = out_channel.send(obj) if not flag: raise RuntimeError("Model B: Error sending output.")
import numpy as np numb = np.array(np.random.randint(low=1, high=1000, size=50000)) # Import classes for input/output channels from yggdrasil.interface.YggInterface import YggInput, YggOutput # Initialize input/output channels in_channel = YggInput('inputA') out_channel = YggOutput('outputA') out_channel.send(numb)
# Import classes for input/output channels from yggdrasil.interface.YggInterface import YggInput, YggOutput # Initialize input/output channels in_channel = YggInput('inputA') out_channel = YggOutput('outputA') # Loop until there is no longer input or the queues are closed while True: # Receive input from input channel # If there is an error, the flag will be False flag, msg = in_channel.recv() if not flag: print("Model A: No more input.") break # Print received message print('Model A: %s' % msg) # Send output to output channel # If there is an error, the flag will be False flag = out_channel.send(msg) if not flag: raise RuntimeError("Model A: Error sending output.")
import numpy as np # Import classes for input/output channels from yggdrasil.interface.YggInterface import YggInput, YggOutput # Initialize input/output channels in_channel = YggInput('inputB') out_channel = YggOutput('outputB') # flag, msg = in_channel.recv() # out_channel.send(msg**2) # Loop until there is no longer input or the queues are closed while True: # Receive input from input channel # If there is an error, the flag will be False flag, msg = in_channel.recv() if not flag: print("Model B: No more input.") break print(msg**2) # Print received message print('Model B: %s' % msg) # Send output to output channel # If there is an error, the flag will be False flag = out_channel.send(msg, msg**2) if not flag: raise RuntimeError("Model B: Error sending output.")