# rpc_lesson2 Example¶

## Python Version¶

Model Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import numpy as np from cis_interface.interface.CisInterface import CisRpcServer def get_fibonacci(n): r"""Compute the nth number of the Fibonacci sequence. Args: n (int): Index of the Fibonacci number in the Fibonacci sequence that should be returned. Returns: int: The nth Fibonacci number. """ pprev = 0 prev = 1 result = 1 fib_no = 1 while fib_no < n: result = prev + pprev pprev = prev prev = result fib_no = fib_no + 1 return result def main(): r"""Function to execute server communication and computation in a loop.""" print('Hello from Python server!') # Create server-side rpc conneciton using model name rpc = CisRpcServer("server", "%d", "%d") # Continue receiving requests until the connection is closed when all # clients have disconnected. while True: print('server: receiving...') retval, rpc_in = rpc.recv() if not retval: print('server: end of input') break # Compute fibonacci number n = rpc_in[0] print('server: Received request for Fibonacci number %d' % n) result = get_fibonacci(n) print('server: Sending response for Fibonacci number %d: %d' % (n, result)) # Send response back flag = rpc.send(np.int32(result)) if not flag: raise RuntimeError('server: ERROR sending') print('Goodbye from Python server') if __name__ == '__main__': main()
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import sys import numpy as np from cis_interface.interface.CisInterface import ( CisRpcClient, CisOutput) def main(iterations, client_index): 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. client_index (int): Index of the client in total list of clients. """ print('Hello from Python client%d: iterations = %d ' % (client_index, iterations)) # Set up connections matching yaml # RPC client-side connection will be \$(server_name)_\$(client_name) rpc = CisRpcClient("server_client%d" % client_index, "%d", "%d") log = CisOutput("output_log%d" % client_index, 'fib(%-2d) = %-2d\n') # Iterate over Fibonacci sequence for i in range(1, iterations + 1): # Call the server and receive response print('client%d(Python): Calling fib(%d)' % (client_index, i)) ret, result = rpc.call(np.int32(i)) if not ret: raise RuntimeError('client%d(Python): RPC CALL ERROR' % client_index) fib = result[0] print('client%d(Python): Response fib(%d) = %d' % (client_index, i, fib)) # Log result by sending it to the log connection ret = log.send(np.int32(i), fib) if not ret: raise RuntimeError('client%d(Python): SEND ERROR' % client_index) print('Goodbye from Python client%d' % client_index) if __name__ == '__main__': # Take number of iterations from the first argument and the # client index from the second main(int(sys.argv[1]), int(sys.argv[2]))

Model YAML:

 1 2 3 4 5 6 7 --- model: name: server language: python args: ./src/server.py is_server: True # Creates a RPC server queue called "server"
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 --- models: - name: client1 language: python args: - ./src/client.py - 3 # Pass the number of iterations that should be performed - 1 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log1 - name: client2 language: python args: - ./src/client.py - 5 # Pass the number of iterations that should be performed - 2 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log2 connections: - input: output_log1 output: client_output1.txt in_temp: True - input: output_log2 output: client_output2.txt in_temp: True

## Matlab Version¶

Model Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import numpy as np from cis_interface.interface.CisInterface import CisRpcServer def get_fibonacci(n): r"""Compute the nth number of the Fibonacci sequence. Args: n (int): Index of the Fibonacci number in the Fibonacci sequence that should be returned. Returns: int: The nth Fibonacci number. """ pprev = 0 prev = 1 result = 1 fib_no = 1 while fib_no < n: result = prev + pprev pprev = prev prev = result fib_no = fib_no + 1 return result def main(): r"""Function to execute server communication and computation in a loop.""" print('Hello from Python server!') # Create server-side rpc conneciton using model name rpc = CisRpcServer("server", "%d", "%d") # Continue receiving requests until the connection is closed when all # clients have disconnected. while True: print('server: receiving...') retval, rpc_in = rpc.recv() if not retval: print('server: end of input') break # Compute fibonacci number n = rpc_in[0] print('server: Received request for Fibonacci number %d' % n) result = get_fibonacci(n) print('server: Sending response for Fibonacci number %d: %d' % (n, result)) # Send response back flag = rpc.send(np.int32(result)) if not flag: raise RuntimeError('server: ERROR sending') print('Goodbye from Python server') if __name__ == '__main__': main()
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 function client(iterations, client_index) iterations = str2num(iterations); client_index = str2num(client_index); fprintf('Hello from Matlab client%d: iterations = %d\n', ... client_index, iterations); % Set up connections matching yaml % RPC client-side connection will be \$(server_name)_\$(client_name) rpc_name = sprintf('server_client%d', client_index); log_name = sprintf('output_log%d', client_index); rpc = CisInterface('CisRpcClient', rpc_name, '%d', '%d'); log = CisInterface('CisOutput', log_name, 'fib(%-2d) = %-2d\n'); % Iterate over Fibonacci sequence for i = 1:iterations % Call the server and receive response fprintf('client%d(Matlab): Calling fib(%d)\n', client_index, i); [ret, result] = rpc.call(int32(i)); if (~ret); error(sprintf('client%d(Matlab): RPC CALL ERROR\n', client_index)); end; fib = result{1}; fprintf('client%d(Matlab): Response fib(%d) = %d\n', client_index, ... i, fib); % Log result by sending it to the log connection ret = log.send(int32(i), fib); if (~ret); error(sprintf('client%d(Matlab): SEND ERROR\n', client_index)); end; end; fprintf('Goodbye from Matlab client%d\n', client_index); end

Model YAML:

 1 2 3 4 5 6 7 --- model: name: server language: python args: ./src/server.py is_server: True # Creates a RPC server queue called "server"
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 --- models: - name: client1 language: matlab args: - ./src/client.m - 3 # Pass the number of iterations that should be performed - 1 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log1 - name: client2 language: matlab args: - ./src/client.m - 5 # Pass the number of iterations that should be performed - 2 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log2 connections: - input: output_log1 output: client_output1.txt in_temp: True - input: output_log2 output: client_output2.txt in_temp: True

## C Version¶

Model Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import numpy as np from cis_interface.interface.CisInterface import CisRpcServer def get_fibonacci(n): r"""Compute the nth number of the Fibonacci sequence. Args: n (int): Index of the Fibonacci number in the Fibonacci sequence that should be returned. Returns: int: The nth Fibonacci number. """ pprev = 0 prev = 1 result = 1 fib_no = 1 while fib_no < n: result = prev + pprev pprev = prev prev = result fib_no = fib_no + 1 return result def main(): r"""Function to execute server communication and computation in a loop.""" print('Hello from Python server!') # Create server-side rpc conneciton using model name rpc = CisRpcServer("server", "%d", "%d") # Continue receiving requests until the connection is closed when all # clients have disconnected. while True: print('server: receiving...') retval, rpc_in = rpc.recv() if not retval: print('server: end of input') break # Compute fibonacci number n = rpc_in[0] print('server: Received request for Fibonacci number %d' % n) result = get_fibonacci(n) print('server: Sending response for Fibonacci number %d: %d' % (n, result)) # Send response back flag = rpc.send(np.int32(result)) if not flag: raise RuntimeError('server: ERROR sending') print('Goodbye from Python server') if __name__ == '__main__': main()
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include "CisInterface.h" #include int main(int argc, char *argv[]) { int iterations = atoi(argv[1]); int client_index = atoi(argv[2]); int exit_code = 0; printf("Hello from C client%d: iterations %d\n", client_index, iterations); // Set up connections matching yaml // RPC client-side connection will be \$(server_name)_\$(client_name) char rpc_name[100]; char log_name[100]; sprintf(rpc_name, "server_client%d", client_index); sprintf(log_name, "output_log%d", client_index); cisRpc_t rpc = cisRpcClient(rpc_name, "%d", "%d"); cisOutput_t log = cisOutputFmt(log_name, "fib(%-2d) = %-2d\n"); // Initialize variables int ret = 0; int fib = -1; char *logmsg = (char*)malloc(CIS_MSG_MAX*sizeof(char)); int i; // Iterate over Fibonacci sequence for (i = 1; i <= iterations; i++) { // Call the server and receive response printf("client%d(C): Calling fib(%d)\n", client_index, i); ret = rpcCall(rpc, i, &fib); if (ret < 0) { printf("client%d(C): RPC CALL ERROR\n", client_index); exit_code = -1; break; } printf("client%d(C): Response fib(%d) = %d\n", client_index, i, fib); // Log result by sending it to the log connection ret = cisSend(log, i, fib); if (ret < 0) { printf("client%d(C): SEND ERROR\n", client_index); exit_code = -1; break; } } free(logmsg); printf("Goodbye from C client%d\n", client_index); return exit_code; }

Model YAML:

 1 2 3 4 5 6 7 --- model: name: server language: python args: ./src/server.py is_server: True # Creates a RPC server queue called "server"
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 --- models: - name: client1 language: c args: - ./src/client.c - 3 # Pass the number of iterations that should be performed - 1 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log1 - name: client2 language: c args: - ./src/client.c - 5 # Pass the number of iterations that should be performed - 2 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log2 connections: - input: output_log1 output: client_output1.txt in_temp: True - input: output_log2 output: client_output2.txt in_temp: True

## C++ Version¶

Model Code:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import numpy as np from cis_interface.interface.CisInterface import CisRpcServer def get_fibonacci(n): r"""Compute the nth number of the Fibonacci sequence. Args: n (int): Index of the Fibonacci number in the Fibonacci sequence that should be returned. Returns: int: The nth Fibonacci number. """ pprev = 0 prev = 1 result = 1 fib_no = 1 while fib_no < n: result = prev + pprev pprev = prev prev = result fib_no = fib_no + 1 return result def main(): r"""Function to execute server communication and computation in a loop.""" print('Hello from Python server!') # Create server-side rpc conneciton using model name rpc = CisRpcServer("server", "%d", "%d") # Continue receiving requests until the connection is closed when all # clients have disconnected. while True: print('server: receiving...') retval, rpc_in = rpc.recv() if not retval: print('server: end of input') break # Compute fibonacci number n = rpc_in[0] print('server: Received request for Fibonacci number %d' % n) result = get_fibonacci(n) print('server: Sending response for Fibonacci number %d: %d' % (n, result)) # Send response back flag = rpc.send(np.int32(result)) if not flag: raise RuntimeError('server: ERROR sending') print('Goodbye from Python server') if __name__ == '__main__': main()
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include "CisInterface.hpp" #include int main(int argc, char *argv[]) { int iterations = atoi(argv[1]); int client_index = atoi(argv[2]); int exit_code = 0; printf("Hello from C++ client%d: iterations %d\n", client_index, iterations); // Set up connections matching yaml // RPC client-side connection will be \$(server_name)_\$(client_name) char rpc_name[100]; char log_name[100]; sprintf(rpc_name, "server_client%d", client_index); sprintf(log_name, "output_log%d", client_index); CisRpcClient rpc(rpc_name, "%d", "%d"); CisOutput log(log_name, "fib(%-2d) = %-2d\n"); // Initialize variables int ret = 0; int fib = -1; char *logmsg = (char*)malloc(CIS_MSG_MAX*sizeof(char)); int i; // Iterate over Fibonacci sequence for (i = 1; i <= iterations; i++) { // Call the server and receive response printf("client%d(C++): Calling fib(%d)\n", client_index, i); ret = rpc.call(2, i, &fib); if (ret < 0) { printf("client%d(C++): RPC CALL ERROR\n", client_index); exit_code = -1; break; } printf("client%d(C++): Response fib(%d) = %d\n", client_index, i, fib); // Log result by sending it to the log connection ret = log.send(2, i, fib); if (ret < 0) { printf("client%d(C++): SEND ERROR\n", client_index); exit_code = -1; break; } } free(logmsg); printf("Goodbye from C++ client%d\n", client_index); return exit_code; }

Model YAML:

 1 2 3 4 5 6 7 --- model: name: server language: python args: ./src/server.py is_server: True # Creates a RPC server queue called "server"
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 --- models: - name: client1 language: c++ args: - ./src/client.cpp - 3 # Pass the number of iterations that should be performed - 1 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log1 - name: client2 language: c++ args: - ./src/client.cpp - 5 # Pass the number of iterations that should be performed - 2 # Pass index of the client client_of: server # Creates an RPC client queue "server_client" outputs: output_log2 connections: - input: output_log1 output: client_output1.txt in_temp: True - input: output_log2 output: client_output2.txt in_temp: True