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

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

small changes to parallel code

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