Add Python submit solution.
authorW. Trevor King <wking@drexel.edu>
Thu, 7 Oct 2010 12:29:03 +0000 (08:29 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 7 Oct 2010 12:29:03 +0000 (08:29 -0400)
assignments/archive/submit_command/soln/submit.py [new file with mode: 0755]

diff --git a/assignments/archive/submit_command/soln/submit.py b/assignments/archive/submit_command/soln/submit.py
new file mode 100755 (executable)
index 0000000..1792e51
--- /dev/null
@@ -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)