# print the input to screen x = np.arange(N * nodes, dtype=np.float64) print("Input: %s\n" % x) # run sin2 in series, then print to screen print("Running serial python ...") y = list(map(sin2, x)) print("Output: %s\n" % np.asarray(y)) if HAS_PYINA: # map sin2 to the workers, then print to screen print("Running mpi4py on %d cores..." % nodes) y = MpiPool(nodes).map(sin2, x) print("Output: %s\n" % np.asarray(y)) # map sin2 to the workers, then print to screen print("Running multiprocesing on %d processors..." % nodes) y = ProcessPool(nodes).map(sin2, x) print("Output: %s\n" % np.asarray(y)) # map sin2 to the workers, then print to screen print("Running multiprocesing on %d threads..." % nodes) y = ThreadPool(nodes).map(sin2, x) print("Output: %s\n" % np.asarray(y))
freeze_support() # print the input to screen x = np.arange(N * nodes, dtype=np.float64) xp = np.arange(N * nodes, dtype=np.float64)[::-1] print("Input: %s\n" % x) # map sin_diff to the workers, then print to screen print("Running serial python ...") y = list(map(sin_diff, x, xp)) print("Output: %s\n" % np.asarray(y)) if HAS_PYINA: # map sin_diff to the workers, then print to screen print("Running mpi4py on %d cores..." % nodes) y = MpiPool(nodes).map(sin_diff, x, xp) print("Output: %s\n" % np.asarray(y)) # map sin_diff to the workers, then print to screen print("Running multiprocesing on %d processors..." % nodes) y = ProcessPool(nodes).map(sin_diff, x, xp) print("Output: %s\n" % np.asarray(y)) # map sin_diff to the workers, then print to screen print("Running multiprocesing on %d threads..." % nodes) y = ThreadPool(nodes).map(sin_diff, x, xp) print("Output: %s\n" % np.asarray(y)) # map sin_diff to the workers, then print to screen print("Running parallelpython on %d cpus..." % nodes) y = ParallelPool(nodes).map(sin_diff, x, xp)
kill_all() elif len(sys.argv) > 2: if sys.argv[1] == "--workers": print("seting up mpi...") MASTERINFO = set_master() nodes = sys.argv[2:] nodes = [node.strip('[()]').strip(',').strip() for node in nodes] #nodes = nodes.strip('[()]').split(',') set_workers(nodes, MASTERINFO) #elif sys.argv[1] == "--alias": # print "setting up mpi python..." # nodes = sys.argv[2:] # nodes = [node.strip('[()]').strip(',').strip() for node in nodes] # for node in nodes: # alias(int(node)) elif sys.argv[1] == "--fetch": nnodes = int(sys.argv[2]) try: pool = MpiPool() pool.nodes = nnodes hostnames = pool.map(host, range(nnodes)) print('\n'.join(hostnames)) except: # "--help" print(__doc__) else: # "--help" print(__doc__) else: # "--help" print(__doc__) # End of file
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 1997-2016 California Institute of Technology. # Copyright (c) 2016-2018 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/pyina/blob/master/LICENSE from pyina.launchers import MpiScatter, MpiPool def host(id): import socket return "Rank: %d -- %s" % (id, socket.gethostname()) print("Evaluate 10 items on 3 nodes using a worker pool:") pool = MpiPool(3) res1 = pool.map(host, range(10)) print(pool) print('\n'.join(res1)) print('') print("Evaluate 10 items on 3 nodes using scatter-gather:") scat = MpiScatter(3) res2 = scat.map(host, range(10)) print(scat) print('\n'.join(res2)) print('') print("Evaluate 5 items on 2 nodes using a worker pool:") pool.nodes = 2 res3 = pool.map(host, range(5))
id, l = Q import numpy return "3 x %d = %d" % (id, numpy.sum(l)) def play2(id, l): import numpy return "3 x %d = %d" % (id, numpy.sum(l)) args = [(i, range(3) * i) for i in range(5)] arg1 = [i for i in range(5)] arg2 = [range(3) * i for i in range(5)] print("Using 12 nodes and a worker pool...") print('Evaluate a function that expects a n-tuple argument "map(f,args)"') pool = MpiPool(12) res1 = pool.map(play, args) #res1 = map(play, args) print(pool) print('\n'.join(res1)) print('') print('Evaluate a function that expects n arguments "map(f,arg1,arg2)"') res2 = pool.map(play2, arg1, arg2) #res2 = map(play2, arg1, arg2) print(pool) print('\n'.join(res2)) # end of file