[2769] | 1 | #!/usr/bin/env python |
---|
| 2 | ### |
---|
| 3 | ######################################################### |
---|
| 4 | # |
---|
| 5 | # Main file for parallel mesh testing. |
---|
| 6 | # |
---|
| 7 | # This is a modification of the run_parallel_advection.py |
---|
| 8 | # file. |
---|
| 9 | # |
---|
| 10 | # |
---|
| 11 | # *) The (new) files that have been added to manage the |
---|
| 12 | # grid partitioning are |
---|
| 13 | # +) pmesh_divide_metis.py: subdivide a pmesh |
---|
| 14 | # +) build_submesh.py: build the submeshes on the host |
---|
| 15 | # processor. |
---|
| 16 | # +) build_local.py: build the GA mesh datastructure |
---|
| 17 | # on each processor. |
---|
| 18 | # +) build_commun.py: handle the communication between |
---|
| 19 | # the host and processors |
---|
| 20 | # |
---|
| 21 | # *) Things still to do: |
---|
| 22 | # +) Overlap the communication and computation: The |
---|
| 23 | # communication routines in build_commun.py should be |
---|
| 24 | # interdispersed in the build_submesh.py and build_local.py |
---|
| 25 | # files. This will overlap the communication and |
---|
| 26 | # computation and will be far more efficient. This should |
---|
| 27 | # be done after more testing and there more confidence in |
---|
| 28 | # the subpartioning. |
---|
| 29 | # +) Much more testing especially with large numbers of |
---|
| 30 | # processors. |
---|
| 31 | # Authors: Linda Stals, Steve Roberts and Matthew Hardy, |
---|
| 32 | # June 2005 |
---|
| 33 | # |
---|
| 34 | # |
---|
| 35 | # |
---|
| 36 | ######################################################### |
---|
| 37 | import sys |
---|
| 38 | import pypar # The Python-MPI interface |
---|
| 39 | import time |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | from os import sep |
---|
| 43 | sys.path.append('..'+sep+'pyvolution') |
---|
| 44 | |
---|
| 45 | # Numeric arrays |
---|
| 46 | |
---|
| 47 | from Numeric import array, zeros, Float |
---|
| 48 | |
---|
| 49 | # Print debugging information |
---|
| 50 | |
---|
| 51 | from print_stats import print_test_stats, build_full_flag |
---|
| 52 | |
---|
| 53 | # pmesh |
---|
| 54 | |
---|
| 55 | from shallow_water import Domain |
---|
| 56 | from parallel_shallow_water import Parallel_Domain |
---|
| 57 | from pmesh2domain import pmesh_to_domain_instance |
---|
| 58 | |
---|
| 59 | # Reuse previous mesh import |
---|
| 60 | |
---|
| 61 | from caching import cache |
---|
| 62 | |
---|
| 63 | # Mesh partition routines |
---|
| 64 | |
---|
| 65 | from pmesh_divide import pmesh_divide_metis |
---|
[2909] | 66 | from build_submesh import build_submesh |
---|
[2769] | 67 | from build_local import build_local_mesh |
---|
[2909] | 68 | from build_commun import send_submesh, rec_submesh, extract_hostmesh |
---|
[2769] | 69 | |
---|
| 70 | ############################### |
---|
| 71 | # Read in processor information |
---|
| 72 | ############################### |
---|
| 73 | |
---|
| 74 | numprocs = pypar.size() |
---|
| 75 | myid = pypar.rank() |
---|
| 76 | processor_name = pypar.Get_processor_name() |
---|
| 77 | |
---|
| 78 | ############################ |
---|
| 79 | # Set the initial conditions |
---|
| 80 | ############################ |
---|
| 81 | |
---|
| 82 | rect = zeros( 4, Float) # Buffer for results |
---|
| 83 | |
---|
| 84 | class Set_Stage: |
---|
| 85 | """Set an initial condition with constant water height, for x<x0 |
---|
| 86 | """ |
---|
| 87 | |
---|
| 88 | def __init__(self, x0=0.25, x1=0.5, h=1.0): |
---|
| 89 | self.x0 = x0 |
---|
| 90 | self.x1 = x1 |
---|
| 91 | self.h = h |
---|
| 92 | |
---|
| 93 | def __call__(self, x, y): |
---|
| 94 | return self.h*((x>self.x0)&(x<self.x1)) |
---|
| 95 | |
---|
| 96 | ####################### |
---|
| 97 | # Partition the domain |
---|
| 98 | ####################### |
---|
| 99 | |
---|
| 100 | if myid == 0: |
---|
| 101 | |
---|
| 102 | # Read in the test files |
---|
| 103 | |
---|
[2906] | 104 | filename = 'test-100.tsh' |
---|
| 105 | # filename = 'merimbula_10785_1.tsh' |
---|
[2769] | 106 | |
---|
| 107 | # Build the whole domain |
---|
| 108 | |
---|
| 109 | domain_full = pmesh_to_domain_instance(filename, Domain) |
---|
| 110 | |
---|
| 111 | # domain_full = cache(pmesh_to_domain_instance, |
---|
| 112 | # (filename, Domain), |
---|
| 113 | # dependencies = [filename]) |
---|
| 114 | |
---|
| 115 | rect = array(domain_full.xy_extent, Float) |
---|
| 116 | |
---|
| 117 | # Initialise the wave |
---|
| 118 | |
---|
[2906] | 119 | domain_full.set_quantity('stage', Set_Stage(200.0,300.0,1.0)) |
---|
| 120 | # domain_full.set_quantity('stage', Set_Stage(756000.0,756500.0,2.0)) |
---|
[2769] | 121 | # domain_full.set_quantity('stage', Set_Stage(756000.0,756500.0,0.0)) |
---|
| 122 | |
---|
| 123 | # Subdivide the domain |
---|
| 124 | |
---|
| 125 | # Note the different arguments compared with pmesh_divide, |
---|
| 126 | # pmesh_divide_steve etc. |
---|
| 127 | |
---|
| 128 | nodes, triangles, boundary, triangles_per_proc, quantities = \ |
---|
| 129 | pmesh_divide_metis(domain_full, numprocs) |
---|
| 130 | |
---|
| 131 | print triangles_per_proc |
---|
| 132 | |
---|
| 133 | rect = array(domain_full.xy_extent, Float) |
---|
| 134 | |
---|
| 135 | submesh = build_submesh(nodes, triangles, boundary,\ |
---|
| 136 | quantities, triangles_per_proc) |
---|
| 137 | |
---|
| 138 | # Send the mesh partition to the appropriate processor |
---|
| 139 | |
---|
| 140 | for p in range(1, numprocs): |
---|
| 141 | send_submesh(submesh, triangles_per_proc, p) |
---|
| 142 | |
---|
| 143 | # Build the local mesh for processor 0 |
---|
| 144 | |
---|
| 145 | points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict = \ |
---|
[2909] | 146 | extract_hostmesh(submesh, triangles_per_proc) |
---|
[2769] | 147 | |
---|
| 148 | # Read in the mesh partition that belongs to this |
---|
| 149 | # processor (note that the information is in the |
---|
| 150 | # correct form for the GA data structure |
---|
| 151 | |
---|
| 152 | else: |
---|
| 153 | points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict \ |
---|
| 154 | = rec_submesh(0) |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | ########################################### |
---|
| 158 | # Start the computations on each subpartion |
---|
| 159 | ########################################### |
---|
| 160 | |
---|
| 161 | #if myid == 0: |
---|
| 162 | # print 'ghost' |
---|
| 163 | # print ghost_recv_dict |
---|
| 164 | #processor_name |
---|
| 165 | #if myid == 0: |
---|
| 166 | # print 'full' |
---|
| 167 | # print full_send_dict |
---|
| 168 | |
---|
| 169 | # The visualiser needs to know the size of the whole domain |
---|
| 170 | |
---|
| 171 | pypar.broadcast(rect,0) |
---|
| 172 | |
---|
| 173 | domain = Parallel_Domain(points, vertices, boundary, |
---|
| 174 | full_send_dict = full_send_dict, |
---|
| 175 | ghost_recv_dict = ghost_recv_dict) |
---|
| 176 | |
---|
| 177 | # Make a note of which triangles are full and which are ghost |
---|
| 178 | |
---|
| 179 | tri_full_flag = build_full_flag(domain, ghost_recv_dict) |
---|
| 180 | |
---|
| 181 | try: |
---|
| 182 | domain.initialise_visualiser(rect=rect) |
---|
| 183 | #domain.visualiser.coloring['stage'] = True |
---|
| 184 | domain.visualiser.scale_z['stage'] = 0.2 |
---|
| 185 | domain.visualiser.scale_z['elevation'] = 0.05 |
---|
| 186 | except: |
---|
| 187 | print 'No visualiser' |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | domain.default_order = 1 |
---|
| 191 | |
---|
| 192 | #Boundaries |
---|
| 193 | from parallel_shallow_water import Transmissive_boundary, Reflective_boundary |
---|
| 194 | |
---|
| 195 | T = Transmissive_boundary(domain) |
---|
| 196 | R = Reflective_boundary(domain) |
---|
| 197 | domain.set_boundary( {'outflow': R, 'inflow': R, 'inner':R, 'exterior': R, 'open':R, 'ghost':None} ) |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | domain.set_quantity('stage', quantities['stage']) |
---|
| 201 | domain.set_quantity('elevation', quantities['elevation']) |
---|
| 202 | |
---|
| 203 | domain.store = False |
---|
| 204 | #domain.filename = 'merimbula-%d' %domain.processor |
---|
| 205 | |
---|
| 206 | #--------- |
---|
| 207 | # Evolution |
---|
| 208 | t0 = time.time() |
---|
| 209 | |
---|
| 210 | print 'Processor %d on %s: No of elements %d'%(domain.processor,processor_name,domain.number_of_elements) |
---|
| 211 | yieldstep = 0.05 |
---|
[2906] | 212 | finaltime = 5.0 |
---|
[2769] | 213 | |
---|
| 214 | yieldstep = 1 |
---|
| 215 | finaltime = 90 |
---|
| 216 | |
---|
| 217 | #yieldstep = 1 |
---|
| 218 | #finaltime = 1 |
---|
| 219 | #processor_name |
---|
| 220 | #for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 221 | # if myid == 0: |
---|
| 222 | # domain.write_time() |
---|
| 223 | #print 'Processor %d, Integral of stage %d'%\ |
---|
| 224 | # (domain.processor,domain.quantities['stage'].get_integral()) |
---|
| 225 | # print_test_stats(domain, tri_full_flag) |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | # Profiling |
---|
| 229 | #import profile |
---|
| 230 | #profiler = profile.Profile() |
---|
| 231 | #result.dump_stats("profile." + str(numprocs) + "." + str(myid) + ".dat") |
---|
| 232 | |
---|
| 233 | #New hotshot profiling |
---|
| 234 | import hotshot |
---|
| 235 | profiler = hotshot.Profile("hotshot." + str(numprocs) + "." + str(myid) + ".prof") |
---|
| 236 | s = '''for t in domain.evolve(yieldstep = yieldstep, finaltime = finaltime): |
---|
| 237 | if myid == 0: |
---|
| 238 | domain.write_time() |
---|
| 239 | print_test_stats(domain, tri_full_flag) |
---|
| 240 | |
---|
| 241 | ''' |
---|
| 242 | result = profiler.runctx(s, globals(), locals()) |
---|
| 243 | profiler.close() |
---|
| 244 | |
---|
| 245 | #print 'P%d: That took %.2f seconds' %(myid, time.time()-t0) |
---|
| 246 | #print 'P%d: Communication time %.2f seconds' %(myid, domain.communication_time) |
---|
| 247 | #print 'P%d: Reduction Communication time %.2f seconds' %(myid, domain.communication_reduce_time) |
---|
| 248 | #print 'P%d: Broadcast time %.2f seconds' %(myid, domain.communication_broadcast_time) |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | if myid == 0: |
---|
| 253 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 254 | print 'Communication time %.2f seconds'%domain.communication_time |
---|
| 255 | print 'Reduction Communication time %.2f seconds'%domain.communication_reduce_time |
---|
| 256 | print 'Broadcast time %.2f seconds'%domain.communication_broadcast_time |
---|