File: example2.py

package info (click to toggle)
python-scientific 2.8-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,456 kB
  • sloc: python: 16,436; ansic: 4,379; makefile: 141; sh: 18; csh: 1
file content (37 lines) | stat: -rw-r--r-- 1,182 bytes parent folder | download | duplicates (6)
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
from Scientific.BSP import ParData, ParSequence, ParAccumulator, \
                           ParFunction, ParRootFunction, numberOfProcessors
import operator

# Local and global computation functions.
def makepairs(sequence1, sequence2):
    pairs = []
    for item1 in sequence1:
        for item2 in sequence2:
            pairs.append((item1, item2))
    return pairs
global_makepairs = ParFunction(makepairs)

# Local and global output functions.
def output(result):
    print result
global_output = ParRootFunction(output)

# A list of data items (here letters) distributed over the processors.
my_items = ParSequence('abcdef')

# The number of the neighbour to the right (circular).
neighbour_pid = ParData(lambda pid, nprocs: [(pid+1)%nprocs])

# Loop to construct all pairs.
pairs = ParAccumulator(operator.add, [])
pairs.addValue(global_makepairs(my_items, my_items))
other_items = my_items
for i in range(numberOfProcessors-1):
    other_items = other_items.put(neighbour_pid)[0]
    pairs.addValue(global_makepairs(my_items, other_items))

# Collect results on processor 0.
all_pairs = pairs.calculateTotal()

# Output results from processor 0.
global_output(all_pairs)