File: threads.py

package info (click to toggle)
mpi4py 2.0.0-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,680 kB
  • sloc: python: 15,291; ansic: 7,099; makefile: 719; f90: 158; sh: 156; cpp: 121
file content (37 lines) | stat: -rw-r--r-- 910 bytes parent folder | download | duplicates (4)
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
mpi4py.rc.threads = True
mpi4py.rc.thread_level = "funneled"
mpi4py.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))