1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """Parallel program computing the Mandelbrot set using static partitioned |
---|
4 | load balancing. |
---|
5 | |
---|
6 | Due to the uneven work load of different regions, |
---|
7 | this strategy will *not* generally lead to linear speedup |
---|
8 | |
---|
9 | See mandel_parallel_dynamic.py for a better approach. |
---|
10 | |
---|
11 | Ole Nielsen, SUT 2003 |
---|
12 | """ |
---|
13 | |
---|
14 | from mandelbrot import calculate_region, balance |
---|
15 | from mandelplot import plot |
---|
16 | import pypar |
---|
17 | |
---|
18 | # User definable parameters |
---|
19 | kmax = 2**15 # Maximal number of iterations (=number of colors) |
---|
20 | M = N = 700 # width = height = N (200 or 700) |
---|
21 | |
---|
22 | # Region in complex plane |
---|
23 | real_min = -2.0 |
---|
24 | real_max = 1.0 |
---|
25 | imag_min = -1.5 |
---|
26 | imag_max = 1.5 |
---|
27 | |
---|
28 | #Initialise |
---|
29 | t = pypar.time() |
---|
30 | P = pypar.size() |
---|
31 | p = pypar.rank() |
---|
32 | processor_name = pypar.get_processor_name() |
---|
33 | |
---|
34 | print 'Processor %d initialised on node %s' %(p,processor_name) |
---|
35 | |
---|
36 | |
---|
37 | # Balanced work partitioning (row wise) |
---|
38 | Mlo, Mhi = pypar.balance(M, P, p) |
---|
39 | print 'p%d: [%d, %d], Interval length=%d' %(p, Mlo, Mhi, Mhi-Mlo) |
---|
40 | |
---|
41 | # Parallel computation |
---|
42 | A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, |
---|
43 | M, N, Mlo = Mlo, Mhi = Mhi) |
---|
44 | |
---|
45 | print 'Processor %d: time = %.2f' %(p, pypar.time() - t) |
---|
46 | |
---|
47 | |
---|
48 | # Communication phase |
---|
49 | if p == 0: |
---|
50 | for d in range(1, P): |
---|
51 | A += pypar.receive(source=d) |
---|
52 | |
---|
53 | print 'Computed region in %.2f seconds' %(pypar.time()-t) |
---|
54 | plot(A, kmax) |
---|
55 | else: |
---|
56 | pypar.send(A, destination=0) |
---|
57 | |
---|
58 | pypar.finalize() |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | |
---|