File: threads.py

package info (click to toggle)
mpi4py 1.3%2Bhg20120611-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,020 kB
  • sloc: python: 9,503; ansic: 6,296; makefile: 571; f90: 158; sh: 146; cpp: 103
file content (37 lines) | stat: -rw-r--r-- 917 bytes parent folder | download
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
#!/usr/bin/env python

import mpi4py.rc
mpi4py.rc.threaded = True
mpi4py.rc.thread_level = "funneled"
mpi4py.rc.profile('vt-hyb', logfile='threads')

from mpi4py import MPI
from threading import Thread

MPI.COMM_WORLD.Barrier()

# Understanding the Python GIL
# David Beazley, http://www.dabeaz.com
# PyCon 2010, Atlanta, Georgia
# http://www.dabeaz.com/python/UnderstandingGIL.pdf

# Consider this trivial CPU-bound function
def countdown(n):
    while n > 0:
        n -= 1

# Run it once with a lot of work
COUNT = 10000000 # 10 millon
tic = MPI.Wtime()
countdown(COUNT)
toc = MPI.Wtime()
print ("sequential: %f seconds" % (toc-tic))

# Now, subdivide the work across two threads
t1 = Thread(target=countdown, args=(COUNT//2,))
t2 = Thread(target=countdown, args=(COUNT//2,))
tic = MPI.Wtime()
for t in (t1, t2): t.start()
for t in (t1, t2): t.join()
toc = MPI.Wtime()
print ("threaded:   %f seconds" % (toc-tic))