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