def distributed_slice(iterable): """ Distribute a 'slice'able iterable across workers. """ P = pp.size() if P == 1: for el in iterable: yield el else: if pp.rank() == 0: lst = list(iterable) N = len(lst) for p in range(1, P): Nlo, Nhi = pp.balance(N, P, p) pp.send(lst[Nlo:Nhi], p) Nlo, Nhi = pp.balance(N, P, 0) for el in lst[Nlo:Nhi]: yield el else: llst = pp.receive(0) for el in llst: yield el
real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 #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)
def parallel_rectangle(m_g, n_g, len1_g=1.0, len2_g=1.0, origin_g = (0.0, 0.0)): """Setup a rectangular grid of triangles with m+1 by n+1 grid points and side lengths len1, len2. If side lengths are omitted the mesh defaults to the unit square, divided between all the processors len1: x direction (left to right) len2: y direction (bottom to top) """ import pypar m_low, m_high = pypar.balance(m_g, numprocs, myid) n = n_g m_low = m_low-1 m_high = m_high+1 #print 'm_low, m_high', m_low, m_high m = m_high - m_low delta1 = float(len1_g)/m_g delta2 = float(len2_g)/n_g len1 = len1_g*float(m)/float(m_g) len2 = len2_g origin = ( origin_g[0]+float(m_low)/float(m_g)*len1_g, origin_g[1] ) #Calculate number of points Np = (m+1)*(n+1) class VIndex: def __init__(self, n,m): self.n = n self.m = m def __call__(self, i,j): return j+i*(self.n+1) class EIndex: def __init__(self, n,m): self.n = n self.m = m def __call__(self, i,j): return 2*(j+i*self.n) I = VIndex(n,m) E = EIndex(n,m) points = num.zeros( (Np,2), num.float) for i in range(m+1): for j in range(n+1): points[I(i,j),:] = [i*delta1 + origin[0], j*delta2 + origin[1]] #Construct 2 triangles per rectangular element and assign tags to boundary #Calculate number of triangles Nt = 2*m*n elements = num.zeros( (Nt,3), num.int) boundary = {} Idgl = [] Idfl = [] Idgr = [] Idfr = [] full_send_dict = {} ghost_recv_dict = {} nt = -1 for i in range(m): for j in range(n): i1 = I(i,j+1) i2 = I(i,j) i3 = I(i+1,j+1) i4 = I(i+1,j) #Lower Element nt = E(i,j) if i == 0: Idgl.append(nt) if i == 1: Idfl.append(nt) if i == m-2: Idfr.append(nt) if i == m-1: Idgr.append(nt) if i == m-1: if myid == numprocs-1: boundary[nt, 2] = 'right' else: boundary[nt, 2] = 'ghost' if j == 0: boundary[nt, 1] = 'bottom' elements[nt,:] = [i4,i3,i2] #Upper Element nt = E(i,j)+1 if i == 0: Idgl.append(nt) if i == 1: Idfl.append(nt) if i == m-2: Idfr.append(nt) if i == m-1: Idgr.append(nt) if i == 0: if myid == 0: boundary[nt, 2] = 'left' else: boundary[nt, 2] = 'ghost' if j == n-1: boundary[nt, 1] = 'top' elements[nt,:] = [i1,i2,i3] if numprocs==1: Idfl.extend(Idfr) Idgr.extend(Idgl) #print Idfl #print Idgr Idfl = num.array(Idfl,num.int) Idgr = num.array(Idgr,num.int) #print Idfl #print Idgr full_send_dict[myid] = [Idfl, Idfl] ghost_recv_dict[myid] = [Idgr, Idgr] elif numprocs == 2: Idfl.extend(Idfr) Idgr.extend(Idgl) Idfl = num.array(Idfl,num.int) Idgr = num.array(Idgr,num.int) full_send_dict[(myid-1)%numprocs] = [Idfl, Idfl] ghost_recv_dict[(myid-1)%numprocs] = [Idgr, Idgr] else: Idfl = num.array(Idfl,num.int) Idgl = num.array(Idgl,num.int) Idfr = num.array(Idfr,num.int) Idgr = num.array(Idgr,num.int) full_send_dict[(myid-1)%numprocs] = [Idfl, Idfl] ghost_recv_dict[(myid-1)%numprocs] = [Idgl, Idgl] full_send_dict[(myid+1)%numprocs] = [Idfr, Idfr] ghost_recv_dict[(myid+1)%numprocs] = [Idgr, Idgr] #print full_send_dict #print ghost_recv_dict return points, elements, boundary, full_send_dict, ghost_recv_dict
real_min = -2.0 real_max = 1.0 imag_min = -1.5 imag_max = 1.5 # 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)
def run( pos_file, neg_file, out_dir, format, align_count, mapping ): # Open merit output merit_out = open( os.path.join( out_dir, 'merits.txt' ), 'w' ) # Read integer sequences pos_strings = list( rp.io.get_reader( pos_file, format, None ) ) neg_strings = list( rp.io.get_reader( neg_file, format, None ) ) symbol_count = mapping.get_out_size() # Collapse while symbol_count > stop_size: # Sync nodes on each pass, may not be required pypar.barrier() if node_id == 0: print "Collapsing from:", symbol_count pairs = all_pairs( symbol_count ) # Decide which subrange of all pairs this node will handle lo, hi = pypar.balance( len( pairs ), nodes, node_id ) # Find best collapsed mapping in interval best_i, best_j, best_merit = None, None, 0 for i, j in pairs[lo:hi]: merit = calc_merit( pos_strings, neg_strings, mapping.collapse( i, j ) ) if merit > best_merit: best_i, best_j, best_merit = i, j, merit # Aggregate results if node_id != 0: # Send best i, j, merit to the master pypar.send( ( best_i, best_j, merit ), 0 ) else: # I am the master, get results from all other nodes and determine # which had the best merit for other_node_id in range( 1, nodes ): i, j, merit = pypar.receive( other_node_id ) if merit > best_merit: best_i, best_j, best_merit = i, j, merit # Collapse the two symbols that resulted in the best merit mapping = mapping.collapse( best_i, best_j ) symbol_count -= 1 # Ensure only the master writes files if node_id == 0: # Append merit to merit output print >>merit_out, symbol_count, best_merit print "\nBest Merit %d." % best_merit, # Write best mapping to a file mapping_out = open( os.path.join( out_dir, "%03d.mapping" % symbol_count ), 'w' ) for i, symbol in enumerate( mapping.get_table() ): print >>mapping_out, str.join( '', rp.mapping.DNA.reverse_map( i, align_count ) ), symbol mapping_out.close()