source: inundation/parallel/build_commun.py @ 2225

Last change on this file since 2225 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
RevLine 
[1500]1#########################################################
[1520]2#
[1500]3# Handle the communication between the host machine
4# (processor 0) and the processors. The host machine is
[1520]5# responsible for the doing the initial grid partitioning.
[1500]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
[2094]14#  Modified: Linda Stals, Nov 2005 (optimise python code)
[1500]15#
16#
17#########################################################
18
[2130]19from Numeric import array, Int, Float, zeros
[1520]20import logging, logging.config
21logger = logging.getLogger('parallel')
22logger.setLevel(logging.WARNING)
[1500]23
[1520]24try:
25    logging.config.fileConfig('log.ini')
26except:
27    pass
28
29
[1500]30import sys
31import pypar
32
[2130]33from build_local import build_local_mesh
34
[1500]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
[1555]48def send_submesh(submesh, triangles_per_proc, p):
[1520]49
[1555]50    print "pypar sending submesh to processor ",p
[1559]51   
[1555]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
[1580]63    # send the quantities key information
64   
65    pypar.send(submesh["full_quan"].keys(), p)
66   
[1500]67    # send the number of triangles per processor
[1520]68
[1500]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])):
[1520]77            flat_full_commun.append([c,submesh["full_commun"][p][c][i]])
78
[1500]79    # send the array sizes so memory can be allocated
[1520]80
[1580]81    setup_array = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[1500]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)
[1555]89
[1500]90    pypar.send(setup_array, p)
[1559]91   
[1500]92    # send the nodes
[2090]93   
[1500]94    pypar.send(submesh["full_nodes"][p], p, use_buffer=True)
95    pypar.send(submesh["ghost_nodes"][p], p, use_buffer=True)
[1520]96
[1500]97    # send the triangles
[2090]98
[1559]99    pypar.send(array(submesh["full_triangles"][p], Int), p, use_buffer=True)
[1500]100    pypar.send(submesh["ghost_triangles"][p], p, use_buffer=True)
101
[2094]102    # send the boundary
[1520]103
[1500]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
[1580]114    # send the quantities
115   
116    for k in submesh["full_quan"]:
117        pypar.send(submesh["full_quan"][k][p], p, use_buffer=True)
[1582]118       
[1580]119    for k in submesh["ghost_quan"]:
120        pypar.send(submesh["ghost_quan"][k][p], p, use_buffer=True)
121       
[2090]122
[1500]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
[2090]140def rec_submesh_flat(p):
[1520]141
[1500]142    numproc = pypar.size()
143    myid = pypar.rank()
144
145    submesh_cell = {}
[1555]146   
147    print "pypar receiving submesh from processor ",p
[1520]148
[1555]149    # receive the tagmap for the boundary conditions
150   
151    tagmap = pypar.receive(p)
152
[1580]153    # receive the quantities key information
154   
155    qkeys = pypar.receive(p)
156
[1500]157    # receive the number of triangles per processor
[1520]158
[1500]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)
[1520]164
[1500]165    # recieve information about the array sizes
[1520]166
[1500]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
[1520]171
[1500]172    no_full_nodes = setup_array[0]
[2090]173    full_nodes = zeros((no_full_nodes, 3), Float)
[1500]174    submesh_cell["full_nodes"] = pypar.receive(p, full_nodes)
[2090]175   
[1500]176    # receive the ghost nodes
[1520]177
[1500]178    no_ghost_nodes = setup_array[1]
[2090]179    ghost_nodes = zeros((no_ghost_nodes, 3), Float)
[1500]180    submesh_cell["ghost_nodes"] = pypar.receive(p, ghost_nodes)
[1520]181
[2090]182   
[1500]183    # receive the full triangles
[1520]184
[1500]185    no_full_triangles = setup_array[2]
[2090]186    full_triangles = zeros((no_full_triangles, 3), Int)
187    submesh_cell["full_triangles"] = pypar.receive(p, full_triangles)
[1559]188   
[1500]189    # receive the ghost triangles
[1520]190
[1500]191    no_ghost_triangles = setup_array[3]
[2090]192    ghost_triangles = zeros((no_ghost_triangles, 4), Int)
[1500]193    submesh_cell["ghost_triangles"] = pypar.receive(p, ghost_triangles)
[2090]194   
[1500]195    # receive the full boundary
[1520]196
[1500]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
[1520]206
[1500]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
[1520]212
[1500]213    no_ghost_commun = setup_array[5]
[2090]214    ghost_commun = zeros((no_ghost_commun, 2), Int)
[1500]215    submesh_cell["ghost_commun"] = pypar.receive(p, ghost_commun)
[2090]216   
[1500]217    # receive the full communication pattern
[1520]218
[1500]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])
[1555]223
[1500]224    full_commun = pypar.receive(p, full_commun)
[1520]225
[1500]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
[1580]232    # receive the quantities
233
234    no_quantities = len(qkeys)
235    new_quan = zeros((no_full_triangles, 3), Float)
236    submesh_cell["full_quan"]={}
[1582]237   
[1580]238    for i in range(no_quantities):
[1582]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[:]
[1580]242
243    new_quan = zeros((no_ghost_triangles, 3), Float)
244    submesh_cell["ghost_quan"]={}
245    for i in range(no_quantities):
[1582]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[:]
[1580]249   
[2090]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   
[1500]278    # find the full triangles assigned to this processor
[1520]279
[1500]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
[1520]287
[1580]288    [GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send] = \
[1559]289              build_local_mesh(submesh_cell, lower_t, upper_t, \
290                               numproc)
[2090]291   
[1580]292    return GAnodes, GAtriangles, boundary, quantities, ghost_rec, full_send
[1500]293
294
Note: See TracBrowser for help on using the repository browser.