source: inundation/parallel/build_commun.py @ 2920

Last change on this file since 2920 was 2909, checked in by linda, 18 years ago

The parallel listed in the documentation is stored in the documentation/code directory

File size: 10.8 KB
Line 
1#########################################################
2#
3# Handle the communication between the host machine
4# (processor 0) and the processors. The host machine is
5# responsible for the doing the initial grid partitioning.
6#
7# The routines given below should be moved to the
8# build_submesh.py and build_local.py file to allow
9# overlapping of  communication and computation.
10# This should be done after more debugging.
11#
12#
13#  Author: Linda Stals, June 2005
14#  Modified: Linda Stals, Nov 2005 (optimise python code)
15#
16#
17#########################################################
18
19from Numeric import array, Int, Float, zeros
20import logging, logging.config
21logger = logging.getLogger('parallel')
22logger.setLevel(logging.WARNING)
23
24try:
25    logging.config.fileConfig('log.ini')
26except:
27    pass
28
29
30import sys
31import pypar
32
33from build_local import build_local_mesh
34
35#########################################################
36#
37# Send the submesh to processor p.
38#
39# *) The order and form is strongly coupled with
40# rec_submesh.
41#
42# -------------------------------------------------------
43#
44# *) All of the information has been sent to processor p.
45#
46#########################################################
47
48def send_submesh(submesh, triangles_per_proc, p):
49
50    print "pypar sending submesh to processor ",p
51   
52    # build and send the tagmap for the boundary conditions
53   
54    tagmap = {}
55    counter = 1
56    for b in submesh["full_boundary"][p]:
57         bkey = submesh["full_boundary"][p][b]
58         if not tagmap.has_key(bkey):
59             tagmap[bkey] = counter
60             counter = counter+1
61    for b in submesh["ghost_boundary"][p]:
62         bkey = submesh["ghost_boundary"][p][b]
63         if not tagmap.has_key(bkey):
64             tagmap[bkey] = counter
65             counter = counter+1
66    pypar.send(tagmap, p)
67
68    # send the quantities key information
69   
70    pypar.send(submesh["full_quan"].keys(), p)
71   
72    # send the number of triangles per processor
73
74    pypar.send(triangles_per_proc, p, use_buffer=True)
75
76    # compress full_commun
77
78    flat_full_commun = []
79
80    for c in submesh["full_commun"][p]:
81        for i in range(len(submesh["full_commun"][p][c])):
82            flat_full_commun.append([c,submesh["full_commun"][p][c][i]])
83
84    # send the array sizes so memory can be allocated
85
86    setup_array = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
87    setup_array[0] = len(submesh["full_nodes"][p])
88    setup_array[1] = len(submesh["ghost_nodes"][p])
89    setup_array[2] = len(submesh["full_triangles"][p])
90    setup_array[3] = len(submesh["ghost_triangles"][p])
91    setup_array[4] = len(submesh["full_boundary"][p])
92    setup_array[5] = len(submesh["ghost_boundary"][p])
93    setup_array[6] = len(submesh["ghost_commun"][p])
94    setup_array[7] = len(flat_full_commun)
95
96    pypar.send(setup_array, p)
97   
98    # send the nodes
99   
100    pypar.send(submesh["full_nodes"][p], p, use_buffer=True)
101    pypar.send(submesh["ghost_nodes"][p], p, use_buffer=True)
102
103    # send the triangles
104
105    pypar.send(array(submesh["full_triangles"][p], Int), p, use_buffer=True)
106    pypar.send(submesh["ghost_triangles"][p], p, use_buffer=True)
107
108    # send the boundary
109
110    bc = []
111    for b in submesh["full_boundary"][p]:
112        bc.append([b[0], b[1], tagmap[submesh["full_boundary"][p][b]]])
113    pypar.send(bc, p, use_buffer=True)
114    bc = []
115    for b in submesh["ghost_boundary"][p]:
116        bc.append([b[0], b[1], tagmap[submesh["ghost_boundary"][p][b]]])
117    pypar.send(bc, p, use_buffer=True)
118
119    # send the communication pattern
120
121    pypar.send(submesh["ghost_commun"][p], p, use_buffer=True)
122    pypar.send(flat_full_commun, p, use_buffer=True)
123
124    # send the quantities
125   
126    for k in submesh["full_quan"]:
127        pypar.send(submesh["full_quan"][k][p], p, use_buffer=True)
128       
129    for k in submesh["ghost_quan"]:
130        pypar.send(submesh["ghost_quan"][k][p], p, use_buffer=True)
131       
132
133#########################################################
134#
135# Receive the submesh from processor p.
136#
137# *) The order and form is strongly coupled with
138# send_submesh.
139#
140# -------------------------------------------------------
141#
142# *) All of the information has been received by the
143# processor p and passed into build_local.
144#
145# *) The information is returned in a form needed by the
146# GA datastructure.
147#
148#########################################################
149
150def rec_submesh_flat(p):
151
152    numproc = pypar.size()
153    myid = pypar.rank()
154
155    submesh_cell = {}
156   
157    print "pypar receiving submesh from processor ",p
158
159    # receive the tagmap for the boundary conditions
160   
161    tagmap = pypar.receive(p)
162    itagmap = {}
163    for t in tagmap:
164        itagmap[tagmap[t]]=t
165
166    # receive the quantities key information
167   
168    qkeys = pypar.receive(p)
169
170    # receive the number of triangles per processor
171
172    triangles_per_proc = []
173    for i in range(numproc):
174        triangles_per_proc.append([0])
175
176    triangles_per_proc = pypar.receive(p, triangles_per_proc)
177
178    # recieve information about the array sizes
179
180    setup_array = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
181    setup_array = pypar.receive(p, setup_array)
182
183    # receive the full nodes
184
185    no_full_nodes = setup_array[0]
186    full_nodes = zeros((no_full_nodes, 3), Float)
187    submesh_cell["full_nodes"] = pypar.receive(p, full_nodes)
188   
189    # receive the ghost nodes
190
191    no_ghost_nodes = setup_array[1]
192    ghost_nodes = zeros((no_ghost_nodes, 3), Float)
193    submesh_cell["ghost_nodes"] = pypar.receive(p, ghost_nodes)
194
195   
196    # receive the full triangles
197
198    no_full_triangles = setup_array[2]
199    full_triangles = zeros((no_full_triangles, 3), Int)
200    submesh_cell["full_triangles"] = pypar.receive(p, full_triangles)
201   
202    # receive the ghost triangles
203
204    no_ghost_triangles = setup_array[3]
205    ghost_triangles = zeros((no_ghost_triangles, 4), Int)
206    submesh_cell["ghost_triangles"] = pypar.receive(p, ghost_triangles)
207   
208    # receive the full boundary
209
210    no_full_boundary = setup_array[4]
211    bc = []
212    for i in range(no_full_boundary):
213        bc.append([0.0, 0.0, 0.0])
214    bnd_c = pypar.receive(p, bc)
215
216    submesh_cell["full_boundary"] = {}
217    for b in bnd_c:
218        submesh_cell["full_boundary"][b[0],b[1]]=itagmap[b[2]]
219
220    # receive the ghost boundary
221
222    no_ghost_boundary = setup_array[5]
223    bc = []
224    for i in range(no_ghost_boundary):
225        bc.append([0.0, 0.0, 0.0])
226    bnd_c = pypar.receive(p, bc)
227
228    submesh_cell["ghost_boundary"] = {}
229    for b in bnd_c:
230        submesh_cell["ghost_boundary"][b[0],b[1]]=itagmap[b[2]]
231
232    # receive the ghost communication pattern
233
234    no_ghost_commun = setup_array[6]
235    ghost_commun = zeros((no_ghost_commun, 2), Int)
236    submesh_cell["ghost_commun"] = pypar.receive(p, ghost_commun)
237   
238    # receive the full communication pattern
239
240    no_full_commun = setup_array[7]
241    full_commun = []
242    for i in range(no_full_commun):
243        full_commun.append([0.0, 0.0])
244
245    full_commun = pypar.receive(p, full_commun)
246
247    submesh_cell["full_commun"] = {}
248    for c in full_commun:
249        submesh_cell["full_commun"][c[0]] = []
250    for c in full_commun:
251        submesh_cell["full_commun"][c[0]].append(c[1])
252
253    # receive the quantities
254
255    no_quantities = len(qkeys)
256    new_quan = zeros((no_full_triangles, 3), Float)
257    submesh_cell["full_quan"]={}
258   
259    for i in range(no_quantities):
260        tmp = pypar.receive(p, new_quan)
261        submesh_cell["full_quan"][qkeys[i]]=zeros((no_full_triangles,3), Float)
262        submesh_cell["full_quan"][qkeys[i]][:] = tmp[:]
263
264    new_quan = zeros((no_ghost_triangles, 3), Float)
265    submesh_cell["ghost_quan"]={}
266    for i in range(no_quantities):
267        tmp = pypar.receive(p, new_quan)
268        submesh_cell["ghost_quan"][qkeys[i]]= zeros((no_ghost_triangles,3), Float)
269        submesh_cell["ghost_quan"][qkeys[i]][:] = tmp[:]
270   
271    return submesh_cell, triangles_per_proc
272
273
274
275#########################################################
276#
277# Receive the submesh from processor p.
278#
279# *) The order and form is strongly coupled with
280# send_submesh.
281#
282# -------------------------------------------------------
283#
284# *) All of the information has been received by the
285# processor p and passed into build_local.
286#
287# *) The information is returned in a form needed by the
288# GA datastructure.
289#
290#########################################################
291
292def rec_submesh(p):
293
294    numproc = pypar.size()
295    myid = pypar.rank()
296
297    [submesh_cell, triangles_per_proc] = rec_submesh_flat(p)
298   
299    # find the full triangles assigned to this processor
300
301    lower_t = 0
302    for i in range(myid):
303        lower_t = lower_t+triangles_per_proc[i]
304    upper_t = lower_t+triangles_per_proc[myid]
305
306    # convert the information into a form needed by the GA
307    # datastructure
308
309    [GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send] = \
310              build_local_mesh(submesh_cell, lower_t, upper_t, \
311                               numproc)
312   
313    return GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send
314
315#########################################################
316#
317# Extract the submesh that will belong to the
318# "host processor" (i.e. processor zero)
319#
320#  *) See the documentation for build_submesh
321#
322# -------------------------------------------------------
323#
324#  *) A dictionary containing the full_triangles,
325# full_nodes, full_boundary, ghost_triangles, ghost_nodes,
326# ghost_boundary, ghost_commun and full_commun belonging
327# to processor zero are returned.
328#
329#########################################################
330def extract_hostmesh(submesh, triangles_per_proc):
331
332    submesh_cell = {}
333    submesh_cell["full_nodes"] = submesh["full_nodes"][0]
334    submesh_cell["ghost_nodes"] = submesh["ghost_nodes"][0]
335    submesh_cell["full_triangles"] = submesh["full_triangles"][0]
336    submesh_cell["ghost_triangles"] = submesh["ghost_triangles"][0]
337    submesh_cell["full_boundary"] = submesh["full_boundary"][0]
338    submesh_cell["ghost_boundary"] = submesh["ghost_boundary"][0]
339    submesh_cell["ghost_commun"] = submesh["ghost_commun"][0]
340    submesh_cell["full_commun"] = submesh["full_commun"][0]
341    submesh_cell["full_quan"] ={}
342    submesh_cell["ghost_quan"]={}
343    for k in submesh["full_quan"]:
344        submesh_cell["full_quan"][k] = submesh["full_quan"][k][0]
345        submesh_cell["ghost_quan"][k] = submesh["ghost_quan"][k][0]
346
347    numprocs = pypar.size()
348    points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict = \
349            build_local_mesh(submesh_cell, 0, triangles_per_proc[0], numprocs)
350    return  points, vertices, boundary, quantities, ghost_recv_dict, \
351           full_send_dict
352
353
354
Note: See TracBrowser for help on using the repository browser.