source: inundation/parallel/build_commun.py @ 2197

Last change on this file since 2197 was 2130, checked in by linda, 19 years ago

Modified the parallel code to agree with the python style files

File size: 8.4 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    pypar.send(tagmap, p)
62
63    # send the quantities key information
64   
65    pypar.send(submesh["full_quan"].keys(), p)
66   
67    # send the number of triangles per processor
68
69    pypar.send(triangles_per_proc, p, use_buffer=True)
70
71    # compress full_commun
72
73    flat_full_commun = []
74
75    for c in submesh["full_commun"][p]:
76        for i in range(len(submesh["full_commun"][p][c])):
77            flat_full_commun.append([c,submesh["full_commun"][p][c][i]])
78
79    # send the array sizes so memory can be allocated
80
81    setup_array = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
82    setup_array[0] = len(submesh["full_nodes"][p])
83    setup_array[1] = len(submesh["ghost_nodes"][p])
84    setup_array[2] = len(submesh["full_triangles"][p])
85    setup_array[3] = len(submesh["ghost_triangles"][p])
86    setup_array[4] = len(submesh["full_boundary"][p])
87    setup_array[5] = len(submesh["ghost_commun"][p])
88    setup_array[6] = len(flat_full_commun)
89
90    pypar.send(setup_array, p)
91   
92    # send the nodes
93   
94    pypar.send(submesh["full_nodes"][p], p, use_buffer=True)
95    pypar.send(submesh["ghost_nodes"][p], p, use_buffer=True)
96
97    # send the triangles
98
99    pypar.send(array(submesh["full_triangles"][p], Int), p, use_buffer=True)
100    pypar.send(submesh["ghost_triangles"][p], p, use_buffer=True)
101
102    # send the boundary
103
104    bc = []
105    for b in submesh["full_boundary"][p]:
106        bc.append([b[0], b[1], tagmap[submesh["full_boundary"][p][b]]])
107    pypar.send(bc, p, use_buffer=True)
108
109    # send the communication pattern
110
111    pypar.send(submesh["ghost_commun"][p], p, use_buffer=True)
112    pypar.send(flat_full_commun, p, use_buffer=True)
113
114    # send the quantities
115   
116    for k in submesh["full_quan"]:
117        pypar.send(submesh["full_quan"][k][p], p, use_buffer=True)
118       
119    for k in submesh["ghost_quan"]:
120        pypar.send(submesh["ghost_quan"][k][p], p, use_buffer=True)
121       
122
123#########################################################
124#
125# Receive the submesh from processor p.
126#
127# *) The order and form is strongly coupled with
128# send_submesh.
129#
130# -------------------------------------------------------
131#
132# *) All of the information has been received by the
133# processor p and passed into build_local.
134#
135# *) The information is returned in a form needed by the
136# GA datastructure.
137#
138#########################################################
139
140def rec_submesh_flat(p):
141
142    numproc = pypar.size()
143    myid = pypar.rank()
144
145    submesh_cell = {}
146   
147    print "pypar receiving submesh from processor ",p
148
149    # receive the tagmap for the boundary conditions
150   
151    tagmap = pypar.receive(p)
152
153    # receive the quantities key information
154   
155    qkeys = pypar.receive(p)
156
157    # receive the number of triangles per processor
158
159    triangles_per_proc = []
160    for i in range(numproc):
161        triangles_per_proc.append([0])
162
163    triangles_per_proc = pypar.receive(p, triangles_per_proc)
164
165    # recieve information about the array sizes
166
167    setup_array = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
168    setup_array = pypar.receive(p, setup_array)
169
170    # receive the full nodes
171
172    no_full_nodes = setup_array[0]
173    full_nodes = zeros((no_full_nodes, 3), Float)
174    submesh_cell["full_nodes"] = pypar.receive(p, full_nodes)
175   
176    # receive the ghost nodes
177
178    no_ghost_nodes = setup_array[1]
179    ghost_nodes = zeros((no_ghost_nodes, 3), Float)
180    submesh_cell["ghost_nodes"] = pypar.receive(p, ghost_nodes)
181
182   
183    # receive the full triangles
184
185    no_full_triangles = setup_array[2]
186    full_triangles = zeros((no_full_triangles, 3), Int)
187    submesh_cell["full_triangles"] = pypar.receive(p, full_triangles)
188   
189    # receive the ghost triangles
190
191    no_ghost_triangles = setup_array[3]
192    ghost_triangles = zeros((no_ghost_triangles, 4), Int)
193    submesh_cell["ghost_triangles"] = pypar.receive(p, ghost_triangles)
194   
195    # receive the full boundary
196
197    no_full_boundary = setup_array[4]
198    bc = []
199    for i in range(no_full_boundary):
200        bc.append([0.0, 0.0, 0.0])
201    bnd_c = pypar.receive(p, bc)
202
203    itagmap = {}
204    for t in tagmap:
205        itagmap[tagmap[t]]=t
206
207    submesh_cell["full_boundary"] = {}
208    for b in bnd_c:
209        submesh_cell["full_boundary"][b[0],b[1]]=itagmap[b[2]]
210
211    # receive the ghost communication pattern
212
213    no_ghost_commun = setup_array[5]
214    ghost_commun = zeros((no_ghost_commun, 2), Int)
215    submesh_cell["ghost_commun"] = pypar.receive(p, ghost_commun)
216   
217    # receive the full communication pattern
218
219    no_full_commun = setup_array[6]
220    full_commun = []
221    for i in range(no_full_commun):
222        full_commun.append([0.0, 0.0])
223
224    full_commun = pypar.receive(p, full_commun)
225
226    submesh_cell["full_commun"] = {}
227    for c in full_commun:
228        submesh_cell["full_commun"][c[0]] = []
229    for c in full_commun:
230        submesh_cell["full_commun"][c[0]].append(c[1])
231
232    # receive the quantities
233
234    no_quantities = len(qkeys)
235    new_quan = zeros((no_full_triangles, 3), Float)
236    submesh_cell["full_quan"]={}
237   
238    for i in range(no_quantities):
239        tmp = pypar.receive(p, new_quan)
240        submesh_cell["full_quan"][qkeys[i]]=zeros((no_full_triangles,3), Float)
241        submesh_cell["full_quan"][qkeys[i]][:] = tmp[:]
242
243    new_quan = zeros((no_ghost_triangles, 3), Float)
244    submesh_cell["ghost_quan"]={}
245    for i in range(no_quantities):
246        tmp = pypar.receive(p, new_quan)
247        submesh_cell["ghost_quan"][qkeys[i]]= zeros((no_ghost_triangles,3), Float)
248        submesh_cell["ghost_quan"][qkeys[i]][:] = tmp[:]
249   
250    return submesh_cell, triangles_per_proc
251
252
253
254#########################################################
255#
256# Receive the submesh from processor p.
257#
258# *) The order and form is strongly coupled with
259# send_submesh.
260#
261# -------------------------------------------------------
262#
263# *) All of the information has been received by the
264# processor p and passed into build_local.
265#
266# *) The information is returned in a form needed by the
267# GA datastructure.
268#
269#########################################################
270
271def rec_submesh(p):
272
273    numproc = pypar.size()
274    myid = pypar.rank()
275
276    [submesh_cell, triangles_per_proc] = rec_submesh_flat(p)
277   
278    # find the full triangles assigned to this processor
279
280    lower_t = 0
281    for i in range(myid):
282        lower_t = lower_t+triangles_per_proc[i]
283    upper_t = lower_t+triangles_per_proc[myid]
284
285    # convert the information into a form needed by the GA
286    # datastructure
287
288    [GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send] = \
289              build_local_mesh(submesh_cell, lower_t, upper_t, \
290                               numproc)
291   
292    return GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send
293
294
Note: See TracBrowser for help on using the repository browser.