##// END OF EJS Templates
fix 2->3
fix 2->3

File last commit:

r6455:15863dc1
r7450:0f789db1
Show More
parallelpi.py
65 lines | 1.9 KiB | text/x-python | PythonLexer
MinRK
update parallel demos for newparallel
r3621 """Calculate statistics on the digits of pi in parallel.
This program uses the functions in :file:`pidigits.py` to calculate
the frequencies of 2 digit sequences in the digits of pi. The
results are plotted using matplotlib.
To run, text files from http://www.super-computing.org/
must be installed in the working directory of the IPython engines.
The actual filenames to be used can be set with the ``filestring``
variable below.
The dataset we have been using for this is the 200 million digit one here:
ftp://pi.super-computing.org/.2/pi200m/
MinRK
remove kernel examples already ported to newparallel
r3675
Bernardo B. Marques
remove all trailling spaces
r4872 and the files used will be downloaded if they are not in the working directory
MinRK
remove kernel examples already ported to newparallel
r3675 of the IPython engines.
Bernardo B. Marques
remove all trailling spaces
r4872 """
Thomas Kluyver
Update print syntax in parallel examples.
r6455 from __future__ import print_function
MinRK
update parallel demos for newparallel
r3621
MinRK
remove kernel examples already ported to newparallel
r3675 from IPython.parallel import Client
MinRK
update parallel demos for newparallel
r3621 from matplotlib import pyplot as plt
import numpy as np
from pidigits import *
from timeit import default_timer as clock
# Files with digits of pi (10m digits each)
MinRK
remove kernel examples already ported to newparallel
r3675 filestring = 'pi200m.ascii.%(i)02dof20'
MinRK
let parallelpi example be used with up to 20 engines
r5485 files = [filestring % {'i':i} for i in range(1,21)]
MinRK
update parallel demos for newparallel
r3621
# Connect to the IPython cluster
MinRK
remove kernel examples already ported to newparallel
r3675 c = Client()
c[:].run('pidigits.py')
MinRK
update parallel demos for newparallel
r3621
MinRK
remove kernel examples already ported to newparallel
r3675 # the number of engines
n = len(c)
id0 = c.ids[0]
v = c[:]
v.block=True
# fetch the pi-files
Thomas Kluyver
Update print syntax in parallel examples.
r6455 print("downloading %i files of pi"%n)
MinRK
remove kernel examples already ported to newparallel
r3675 v.map(fetch_pi_file, files[:n])
Thomas Kluyver
Update print syntax in parallel examples.
r6455 print("done")
MinRK
update parallel demos for newparallel
r3621
# Run 10m digits on 1 engine
t1 = clock()
MinRK
remove kernel examples already ported to newparallel
r3675 freqs10m = c[id0].apply_sync(compute_two_digit_freqs, files[0])
MinRK
update parallel demos for newparallel
r3621 t2 = clock()
digits_per_second1 = 10.0e6/(t2-t1)
Thomas Kluyver
Update print syntax in parallel examples.
r6455 print("Digits per second (1 core, 10m digits): ", digits_per_second1)
MinRK
update parallel demos for newparallel
r3621
MinRK
remove kernel examples already ported to newparallel
r3675 # Run n*10m digits on all engines
MinRK
update parallel demos for newparallel
r3621 t1 = clock()
MinRK
remove kernel examples already ported to newparallel
r3675 freqs_all = v.map(compute_two_digit_freqs, files[:n])
MinRK
update parallel demos for newparallel
r3621 freqs150m = reduce_freqs(freqs_all)
t2 = clock()
MinRK
remove kernel examples already ported to newparallel
r3675 digits_per_second8 = n*10.0e6/(t2-t1)
Thomas Kluyver
Update print syntax in parallel examples.
r6455 print("Digits per second (%i engines, %i0m digits): "%(n,n), digits_per_second8)
MinRK
update parallel demos for newparallel
r3621
Thomas Kluyver
Update print syntax in parallel examples.
r6455 print("Speedup: ", digits_per_second8/digits_per_second1)
MinRK
update parallel demos for newparallel
r3621
plot_two_digit_freqs(freqs150m)
MinRK
remove kernel examples already ported to newparallel
r3675 plt.title("2 digit sequences in %i0m digits of pi"%n)
plt.show()
MinRK
update parallel demos for newparallel
r3621