pp-web / pp / examples / quicksort.py

#!/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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.