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 61 62 63 64 65 66 67 68 69
|
#!/usr/bin/env python
# File: sum_primes.py
# Author: Vitalii Vanovschi
# Desc: This program demonstrates parallel version of quicksort algorithm
# implemented using pp module
# Parallel Python Software: http://www.parallelpython.com
import sys, random
import pp
def quicksort(a, n=-1, srv=None):
if len(a) <= 1:
return a
if n:
return quicksort([x for x in a if x < a[0]], n-1, srv) \
+ [a[0]] + quicksort([x for x in a[1:] if x >= a[0]], n-1, srv)
else:
return [srv.submit(quicksort, (a,))]
print """Usage: python quicksort.py [ncpus]
[ncpus] - the number of workers to run in parallel,
if omitted it will be set to the number of processors in the system
"""
# tuple of all parallel python servers to connect with
#ppservers = ("*",)
#ppservers = ("10.0.0.1",)
ppservers = ()
if len(sys.argv) > 1:
ncpus = int(sys.argv[1])
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers)
else:
# Creates jobserver with automatically detected number of workers
job_server = pp.Server(ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
n = 1000000
input = []
for i in xrange(n):
input.append(random.randint(0,100000))
# set n to a positive integer to create 2^n PP jobs
# or to -1 to avoid using PP
# 32 PP jobs
n = 5
# no PP
#n = -1
outputraw = quicksort(input, n, job_server)
output = []
for x in outputraw:
if callable(x):
output.extend(x())
else:
output.append(x)
print "first 30 numbers in increasing order:", output[:30]
job_server.print_stats()
# Parallel Python Software: http://www.parallelpython.com
|