From 4c7b11bb593a2817f96b2b72837403540bfa1a12 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 7 Oct 2010 08:29:03 -0400 Subject: [PATCH] Add Python submit solution. --- .../archive/submit_command/soln/submit.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 assignments/archive/submit_command/soln/submit.py diff --git a/assignments/archive/submit_command/soln/submit.py b/assignments/archive/submit_command/soln/submit.py new file mode 100755 index 0000000..1792e51 --- /dev/null +++ b/assignments/archive/submit_command/soln/submit.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import subprocess +import sys + +from mpi4py import MPI + +REQUEST_TAG = 100 +RESPONSE_TAG = 101 + +def master(comm): + slaves = comm.Get_size() - 1 + command = sys.argv[1:] + for slave in range(1,slaves+1): + comm.send(command, dest=slave, tag=REQUEST_TAG) + code = execute(command) + print "node %d code: %d" % (comm.Get_rank(), code) + s = MPI.Status() + for slave in range(1,slaves+1): + code = comm.recv(source=MPI.ANY_SOURCE, tag=RESPONSE_TAG, status=s) + print "node %d code: %d" % (s.Get_source(), code) + +def slave(comm): + command = comm.recv(source=0, tag=REQUEST_TAG) + code = execute(command) + comm.send(code, dest=0, tag=RESPONSE_TAG) + +def execute(command): + return subprocess.call(command) + + +if __name__ == '__main__': + comm = MPI.COMM_WORLD + rank = comm.Get_rank() + if rank == 0: + master(comm) + else: + slave(comm) -- 2.26.2