source: trunk/anuga_core/source/anuga_parallel/parallel_api.py @ 8540

Last change on this file since 8540 was 8538, checked in by steve, 13 years ago

added variable ghost_layer_width which is set by the partitioning routine
(distribute) or set to 2

File size: 10.4 KB
Line 
1"""Trying to lump parallel stuff into simpler interface
2
3
4"""
5
6
7
8# The abstract Python-MPI interface
9from anuga_parallel.parallel_abstraction import size, rank, get_processor_name
10from anuga_parallel.parallel_abstraction import finalize, send, receive
11from anuga_parallel.parallel_abstraction import pypar_available, barrier
12
13
14# ANUGA parallel engine (only load if pypar can)
15if pypar_available:
16    from anuga_parallel.distribute_mesh  import send_submesh
17    from anuga_parallel.distribute_mesh  import rec_submesh
18    from anuga_parallel.distribute_mesh  import extract_hostmesh
19
20    # Mesh partitioning using Metis
21    from anuga_parallel.distribute_mesh import build_submesh
22    from anuga_parallel.distribute_mesh import pmesh_divide_metis_with_map
23
24    from anuga_parallel.parallel_shallow_water import Parallel_domain
25
26#------------------------------------------------------------------------------
27# Read in processor information
28#------------------------------------------------------------------------------
29
30numprocs = size()
31myid = rank()
32processor_name = get_processor_name()
33#print 'I am processor %d of %d on node %s' %(myid, numprocs, processor_name)
34
35
36
37
38def distribute(domain, verbose=False, debug=False, parameters = None):
39    """ Distribute the domain to all processes
40    """
41
42
43    if debug:
44        verbose = True
45       
46    barrier()
47
48    # FIXME: Dummy assignment (until boundaries are refactored to
49    # be independent of domains until they are applied)
50    if myid == 0:
51        bdmap = {}
52        for tag in domain.get_boundary_tags():
53            bdmap[tag] = None
54   
55   
56        domain.set_boundary(bdmap)
57
58
59
60
61    if not pypar_available: return domain # Bypass
62
63    # For some obscure reason this communication must happen prior to
64    # the more complex mesh distribution - Oh Well!
65    if myid == 0:
66        domain_name = domain.get_name()
67        domain_dir = domain.get_datadir()
68        domain_store = domain.get_store()
69        domain_minimum_storable_height = domain.minimum_storable_height
70        georef = domain.geo_reference
71        number_of_global_triangles = domain.number_of_triangles
72        number_of_global_nodes = domain.number_of_nodes
73       
74        # FIXME - what other attributes need to be transferred?
75
76        for p in range(1, numprocs):
77            send((domain_name, domain_dir, domain_store, \
78                  domain_minimum_storable_height, georef, \
79                  number_of_global_triangles, number_of_global_nodes), p)
80    else:
81        if verbose: print 'P%d: Receiving domain attributes' %(myid)
82
83        domain_name, domain_dir, domain_store, \
84                  domain_minimum_storable_height, \
85                  georef, number_of_global_triangles, \
86                  number_of_global_nodes = receive(0)
87
88
89
90    # Distribute boundary conditions
91    # FIXME: This cannot handle e.g. Time_boundaries due to
92    # difficulties pickling functions
93    if myid == 0:
94        boundary_map = domain.boundary_map
95        for p in range(1, numprocs):
96            send(boundary_map, p)
97    else:
98        if verbose: print 'P%d: Receiving boundary map' %(myid)       
99
100        boundary_map = receive(0)
101       
102
103
104
105    if myid == 0:
106        # Partition and distribute mesh.
107        # Structures returned is in the
108        # correct form for the ANUGA data structure
109
110
111        points, vertices, boundary, quantities,\
112                ghost_recv_dict, full_send_dict,\
113                number_of_full_nodes, number_of_full_triangles,\
114                s2p_map, p2s_map, tri_map, node_map, ghost_layer_width =\
115                distribute_mesh(domain, verbose=verbose, debug=debug, parameters=parameters)
116           
117        # Extract l2g maps
118        tri_l2g  = extract_l2g_map(tri_map)
119        node_l2g = extract_l2g_map(node_map)
120
121        if debug:
122            print 'P%d' %myid
123            print 'tri_map ',tri_map
124            print 'node_map',node_map
125            print 'tri_l2g', tri_l2g
126            print 'node_l2g', node_l2g
127            print 's2p_map', s2p_map
128            print 'p2s_map', p2s_map
129
130        # Send serial to parallel (s2p) and parallel to serial (p2s) triangle mapping to proc 1 .. numprocs
131        for p in range(1, numprocs):
132            send(s2p_map, p)
133            send(p2s_map, p)
134
135        if verbose: print 'Communication done'
136       
137    else:
138        # Read in the mesh partition that belongs to this
139        # processor
140        if verbose: print 'P%d: Receiving submeshes' %(myid)               
141        points, vertices, boundary, quantities,\
142                ghost_recv_dict, full_send_dict,\
143                number_of_full_nodes, number_of_full_triangles, \
144                tri_map, node_map, ghost_layer_width =\
145                rec_submesh(0, verbose)
146
147
148
149        # Extract l2g maps
150        tri_l2g  = extract_l2g_map(tri_map)
151        node_l2g = extract_l2g_map(node_map)
152       
153        # Recieve serial to parallel (s2p) and parallel to serial (p2s) triangle mapping
154        s2p_map = receive(0)
155        p2s_map = receive(0)
156
157
158    #------------------------------------------------------------------------
159    # Build the domain for this processor using partion structures
160    #------------------------------------------------------------------------
161
162    if verbose: print 'myid = %g, no_full_nodes = %g, no_full_triangles = %g' % (myid, number_of_full_nodes, number_of_full_triangles)
163
164   
165    domain = Parallel_domain(points, vertices, boundary,
166                             full_send_dict=full_send_dict,
167                             ghost_recv_dict=ghost_recv_dict,
168                             number_of_full_nodes=number_of_full_nodes,
169                             number_of_full_triangles=number_of_full_triangles,
170                             geo_reference=georef,
171                             number_of_global_triangles = number_of_global_triangles,
172                             number_of_global_nodes = number_of_global_nodes,
173                             s2p_map = s2p_map,
174                             p2s_map = p2s_map, ## jj added this
175                             tri_l2g = tri_l2g, ## SR added this
176                             node_l2g = node_l2g,
177                             ghost_layer_width = ghost_layer_width)
178
179    #------------------------------------------------------------------------
180    # Transfer initial conditions to each subdomain
181    #------------------------------------------------------------------------
182    for q in quantities:
183        domain.set_quantity(q, quantities[q]) 
184
185
186    #------------------------------------------------------------------------
187    # Transfer boundary conditions to each subdomain
188    #------------------------------------------------------------------------
189    boundary_map['ghost'] = None  # Add binding to ghost boundary
190    domain.set_boundary(boundary_map)
191
192
193    #------------------------------------------------------------------------
194    # Transfer other attributes to each subdomain
195    #------------------------------------------------------------------------
196    domain.set_name(domain_name)
197    domain.set_datadir(domain_dir)
198    domain.set_store(domain_store)
199    domain.set_minimum_storable_height(domain_minimum_storable_height)
200    domain.geo_reference = georef   
201
202    #------------------------------------------------------------------------
203    # Return parallel domain to all nodes
204    #------------------------------------------------------------------------
205    return domain   
206
207
208
209
210
211
212def distribute_mesh(domain, verbose=False, debug=False, parameters=None):
213
214
215    if debug:
216        verbose = True
217
218    numprocs = size()
219
220   
221    # Subdivide the mesh
222    if verbose: print 'Subdivide mesh'
223    nodes, triangles, boundary, triangles_per_proc, quantities, \
224           s2p_map, p2s_map = \
225           pmesh_divide_metis_with_map(domain, numprocs)
226
227    #PETE: s2p_map (maps serial domain triangles to parallel domain triangles)
228    #      sp2_map (maps parallel domain triangles to domain triangles)
229
230
231    # Build the mesh that should be assigned to each processor,
232    # this includes ghost nodes and the communication pattern
233    if verbose: print 'Build submeshes'   
234    submesh = build_submesh(nodes, triangles, boundary,\
235                            quantities, triangles_per_proc, parameters)
236
237    if verbose:
238        for p in range(numprocs):
239            N = len(submesh['ghost_nodes'][p])               
240            M = len(submesh['ghost_triangles'][p])
241            print 'There are %d ghost nodes and %d ghost triangles on proc %d'\
242                  %(N, M, p)
243
244    #if debug:
245    #    from pprint import pprint
246    #    pprint(submesh)
247
248
249    # Send the mesh partition to the appropriate processor
250    if verbose: print 'Distribute submeshes'       
251    for p in range(1, numprocs):
252      send_submesh(submesh, triangles_per_proc, p, verbose)
253
254    # Build the local mesh for processor 0
255    points, vertices, boundary, quantities, \
256            ghost_recv_dict, full_send_dict, tri_map, node_map, ghost_layer_width =\
257              extract_hostmesh(submesh, triangles_per_proc)
258
259    # Keep track of the number full nodes and triangles.
260    # This is useful later if one needs access to a ghost-free domain
261    # Here, we do it for process 0. The others are done in rec_submesh.
262    number_of_full_nodes = len(submesh['full_nodes'][0])
263    number_of_full_triangles = len(submesh['full_triangles'][0])
264       
265    #print
266    #for p in range(numprocs):
267    #    print 'Process %d:' %(p)
268    #
269    #    print 'full_triangles:'
270    #    print submesh['full_triangles'][p]
271    #
272    #    print 'full_nodes:'
273    #    print submesh['full_nodes'][p]
274    #
275    #    print 'ghost_triangles:'
276    #    print submesh['ghost_triangles'][p]#
277    #
278    #    print 'ghost_nodes:'
279    #   print submesh['ghost_nodes'][p]                               
280    #    print
281    #
282    #print 'Receive dict'
283    #print ghost_recv_dict
284    #
285    #print 'Send dict'
286    #print full_send_dict       
287
288
289    # Return structures necessary for building the parallel domain
290    return points, vertices, boundary, quantities,\
291           ghost_recv_dict, full_send_dict,\
292           number_of_full_nodes, number_of_full_triangles, \
293           s2p_map, p2s_map, tri_map, node_map, ghost_layer_width
294   
295
296
297def extract_l2g_map(map):
298    # Extract l2g data  from corresponding map
299    # Maps
300
301    import numpy as num
302   
303    b = num.arange(len(map))
304
305    l_ids = num.extract(map>-1,map)
306    g_ids = num.extract(map>-1,b)
307
308#    print len(g_ids)
309#    print len(l_ids)
310#    print l_ids
311
312    l2g = num.zeros_like(g_ids)
313    l2g[l_ids] = g_ids
314
315    return l2g
316
Note: See TracBrowser for help on using the repository browser.