def test2(self): kmax = 32 M = N = 5 real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) assert numpy.allclose( A, [[1, 1, 1, 1, 1], [1, 3, 5, 5, 3], [2, 4, 9, 9, 4], [2, 9, 32, 32, 9], [2, 3, 15, 15, 3]])
def test2(self): kmax = 32 M = N = 5 real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) assert numpy.allclose(A, [[1, 1, 1, 1, 1], [1, 3, 5, 5, 3], [2, 4, 9, 9, 4], [2, 9, 32, 32, 9], [2, 3, 15, 15, 3]])
def test3(self): kmax = 32 M = N = 100 real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) assert numpy.allclose(A[:, 50], [ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 16, 11, 9, 7, 7, 6, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 ])
def test3(self): kmax = 32 M = N = 100 real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) assert numpy.allclose(A[:, 50], [32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 16, 11, 9, 7, 7, 6, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
"""Sequential program computing the Mandelbrot set. Ole Nielsen, SUT 2003 """ from mandelbrot import calculate_region from mandelplot import plot import time # User definable parameters kmax = 2**15 # Maximal number of iterations (=number of colors) M = N = 700 # width = height = N (200, 400, 600, 700 are good) #M = N = 400 # width = height = N (200, 400, 600, 700 are good) # Region in complex plane real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 # Compute Mandelbrot set t0 = time.time() A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) print 'Computed region in %.2f seconds' % (time.time() - t0) # Plot result try: plot(A, kmax) except: pass
# Send new work to slave d pypar.send(workpool[w], destination=d, tag=work_tag) w += 1 else: # Tell slave d to terminate pypar.send(None, destination=d, tag=work_tag) terminated += 1 print 'Computed region in %.2f seconds' % (pypar.time() - t) try: plot(A, kmax) except: pass else: while(True): # Receive work (or None) W = pypar.receive(source=0, tag=work_tag) if W is None: print 'Slave p%d finished: time = %.2f' % (p, pypar.time() - t) break # Compute allocated work A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N, Mlo=W[0], Mhi=W[1]) # Return result pypar.send(A, destination=0, tag=result_tag) pypar.finalize()
print 'Computed region in %.2f seconds' % (pypar.time() - t) try: plot(A, kmax) except: pass else: while (True): # Receive work (or None) W = pypar.receive(source=0, tag=work_tag) if W is None: print 'Slave p%d finished: time = %.2f' % (p, pypar.time() - t) break # Compute allocated work A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N, Mlo=W[0], Mhi=W[1]) # Return result pypar.send(A, destination=0, tag=result_tag) pypar.finalize()
#Initialise t = pypar.time() P = pypar.size() p = pypar.rank() processor_name = pypar.get_processor_name() print 'Processor %d initialised on node %s' %(p,processor_name) # Balanced work partitioning (row wise) Mlo, Mhi = pypar.balance(M, P, p) print 'p%d: [%d, %d], Interval length=%d' %(p, Mlo, Mhi, Mhi-Mlo) # Parallel computation A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N, Mlo = Mlo, Mhi = Mhi) print 'Processor %d: time = %.2f' %(p, pypar.time() - t) # Communication phase if p == 0: for d in range(1, P): A += pypar.receive(source=d) print 'Computed region in %.2f seconds' %(pypar.time()-t) try: plot(A, kmax) except: pass else:
Ole Nielsen, SUT 2003 """ from mandelbrot import calculate_region from mandelplot import plot import time # User definable parameters kmax = 2**15 # Maximal number of iterations (=number of colors) M = N = 700 # width = height = N (200, 400, 600, 700 are good) #M = N = 400 # width = height = N (200, 400, 600, 700 are good) # Region in complex plane real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 # Compute Mandelbrot set t0 = time.time() A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N) print 'Computed region in %.2f seconds' %(time.time()-t0) # Plot result try: plot(A, kmax) except: pass
# Initialise t = pypar.time() P = pypar.size() p = pypar.rank() processor_name = pypar.get_processor_name() print 'Processor %d initialised on node %s' % (p, processor_name) # Balanced work partitioning (row wise) Mlo, Mhi = pypar.balance(M, P, p) print 'p%d: [%d, %d], Interval length=%d' % (p, Mlo, Mhi, Mhi - Mlo) # Parallel computation A = calculate_region(real_min, real_max, imag_min, imag_max, kmax, M, N, Mlo=Mlo, Mhi=Mhi) print 'Processor %d: time = %.2f' % (p, pypar.time() - t) # Communication phase if p == 0: for d in range(1, P): A += pypar.receive(source=d) print 'Computed region in %.2f seconds' % (pypar.time() - t) try: plot(A, kmax) except: pass else: