File: using_parallel.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (31 lines) | stat: -rw-r--r-- 1,054 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
#!/usr/bin/env python

"""An example of how to distribute jobs across multiple cpu's. Note that this example works even if on a single cpu since the parallel assigns a `fake` communicator in that instance.
"""

from cogent.util import parallel

__author__ = "Gavin Huttleu"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__contributors__ = ["Gavin Huttley", "Peter Maxwell"]
__license__ = "GPL"
__version__ = "1.3.0.dev"
__maintainer__ = "Gavin Huttley"
__email__ = "gavin.huttley@anu.edu.au"
__status__ = "Production"

JOBS = range(12) # nonsense jobs, just a list of numbers to be printed.

# we divide up the CPUs into (at most) 12 groups of (at least) 1 CPU.
(comm, leftover) = parallel.getSplitCommunicators(len(JOBS))
# and set the cpu's available to lower levels
parallel.push(leftover)
try:
    for job in JOBS:
        if not job % comm.size == comm.rank:
            continue
        print "My ID=%d, my message=%s" % (comm.rank, JOBS[job])
finally:
    # always restore the original parallel context
    parallel.pop(leftover)