pp-web / pp_examples /

#!/usr/bin/env python
# File:
# Author: Vitalii Vanovschi
# Desc: This program demonstrates parallel version of quicksort algorithm
# implemented using pp module
# Parallel Python Software:

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)
        return [srv.submit(quicksort, (a,))]

print """Usage: python [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 = ("",)
ppservers = ()

if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
    # Creates jobserver with ncpus workers
    job_server = pp.Server(ncpus, ppservers=ppservers)
    # 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):

# 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):

print "first 30 numbers in increasing order:", output[:30]


# Parallel Python Software:
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
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.