1. 程式人生 > >Parallel Python(PP)平行計算測試

Parallel Python(PP)平行計算測試

測試環境:i5-2300(4核) + Win7

使用PP的測試程式碼如下:

import math, sys, time
import pp

def takeuptime(n):  
    chars = 'abcdefghijklmnopqrstuvwxyz0123456789'  
    s = chars * 1000  
    for i in range(10*n):  
        for c in chars:  
            s.count(c)  

print """Usage: test.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",)

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"

start_time = time.time()

# The following submits 4 jobs
inputs = (1000, 1000, 1000, 1000)
jobs = [(input, job_server.submit(takeuptime, (input,), (), ())) for input in inputs]

#wait for jobs in all groups to finish 
job_server.wait()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

程式執行結果如下:

I:\Webscraping\test>test

Usage: test.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

Starting pp with 4 workers

Time elapsed:  20.9220001698 s

Job execution statistics:

 job count | % of all jobs | job time sum | time per job | job server

         4 |        100.00 |      78.6590 |    19.664750 | local

Time elapsed since server creation 20.9240000248

所需的時間和之前用pprocess模組進行並行運算的結果差不多。

PP與pprocess模組相比優勢在哪裡?

1)PP不但支援Linux,Windows下也能使用。

2)PP不但支援單機多核(SMP,systems with multiple processors or cores),而且支援多臺計算機(clusters,computers connected via network)。

目前只測試了SMP,期待clusters測試。