source: anuga_core/source/anuga_parallel/parallel_api.py @ 3776

Last change on this file since 3776 was 3776, checked in by ole, 17 years ago

Made parallel abstraction work on sequential machine without pypar.
Work on simple parallel example (plotting)

File size: 5.3 KB
Line 
1"""Trying to lump parallel stuff into simpler interface
2
3
4"""
5
6# Parallelism
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
12
13# Mesh partitioning
14from anuga_parallel.pmesh_divide  import pmesh_divide_metis
15from anuga_parallel.build_submesh import build_submesh
16from anuga_parallel.build_local   import build_local_mesh
17
18# ANUGA parallel engine (only load if pypar can)
19if pypar_available:
20    from anuga_parallel.build_commun  import send_submesh
21    from anuga_parallel.build_commun  import rec_submesh
22    from anuga_parallel.build_commun  import extract_hostmesh
23    from anuga_parallel.parallel_shallow_water import Parallel_Domain
24
25
26#------------------------------------------------------------------------------
27# Read in processor information
28#------------------------------------------------------------------------------
29
30numprocs = size()
31myid = rank()
32processor_name = get_processor_name()
33print 'I am processor %d of %d on node %s' %(myid, numprocs, processor_name)
34
35
36
37
38def distribute(domain, verbose=False):
39    """ Distribute the domain to all processes
40    """
41
42    if not pypar_available: return domain # Bypass
43
44    # For some obscure reason this communication must happen prior to
45    # the more complex mesh distribution - Oh Well!
46    if myid == 0:
47        domain_name = domain.get_name()
48        domain_dir = domain.get_datadir()
49        # FIXME - what other attributes need to be transferred?
50
51        for p in range(1, numprocs):
52            send((domain_name, domain_dir), p)
53    else:
54        if verbose: print 'P%d: Receiving domain attributes' %(myid)
55
56        domain_name, domain_dir = receive(0)
57
58
59
60    # Distribute boundary conditions
61    # FIXME: This cannot handle e.g. Time_boundaries due to
62    # difficulties pickling functions
63    if myid == 0:
64        boundary_map = domain.boundary_map
65        for p in range(1, numprocs):
66            send(boundary_map, p)
67    else:
68        if verbose: print 'P%d: Receiving boundary map' %(myid)       
69
70        boundary_map = receive(0)
71       
72
73
74
75    if myid == 0:
76        # Partition and distribute mesh.
77        # Structures returned is in the
78        # correct form for the ANUGA data structure
79
80
81        points, vertices, boundary, quantities,\
82                ghost_recv_dict, full_send_dict,\
83                = distribute_mesh(domain)
84
85        if verbose: print 'Communication done'
86       
87    else:
88        # Read in the mesh partition that belongs to this
89        # processor
90        if verbose: print 'P%d: Receiving submeshes' %(myid)               
91        points, vertices, boundary, quantities,\
92                ghost_recv_dict, full_send_dict,\
93                = rec_submesh(0)
94
95
96    #------------------------------------------------------------------------
97    # Build the domain for this processor using partion structures
98    #------------------------------------------------------------------------
99    domain = Parallel_Domain(points, vertices, boundary,
100                             full_send_dict  = full_send_dict,
101                             ghost_recv_dict = ghost_recv_dict)
102
103    #------------------------------------------------------------------------
104    # Transfer initial conditions to each subdomain
105    #------------------------------------------------------------------------
106    for q in quantities:
107        domain.set_quantity(q, quantities[q]) 
108
109
110    #------------------------------------------------------------------------
111    # Transfer boundary conditions to each subdomain
112    #------------------------------------------------------------------------
113    boundary_map['ghost'] = None  # Add binding to ghost boundary
114    domain.set_boundary(boundary_map)
115
116
117    #------------------------------------------------------------------------
118    # Transfer other attributes to each subdomain
119    #------------------------------------------------------------------------
120    domain.set_name(domain_name)
121    domain.set_datadir(domain_dir)       
122
123    #------------------------------------------------------------------------
124    # Return parallel domain to all nodes
125    #------------------------------------------------------------------------
126    return domain   
127
128
129
130
131
132
133
134def distribute_mesh(domain):
135
136    numprocs = size()
137
138   
139    # Subdivide the mesh
140    print 'Subdivide mesh'
141    nodes, triangles, boundary, triangles_per_proc, quantities = \
142           pmesh_divide_metis(domain, numprocs)
143
144    # Build the mesh that should be assigned to each processor,
145    # this includes ghost nodes and the communicaiton pattern
146    print 'Build submeshes'   
147    submesh = build_submesh(nodes, triangles, boundary,\
148                            quantities, triangles_per_proc)
149
150    # Send the mesh partition to the appropriate processor
151    print 'Distribute submeshes'       
152    for p in range(1, numprocs):
153      send_submesh(submesh, triangles_per_proc, p)
154
155    # Build the local mesh for processor 0
156    points, vertices, boundary, quantities, ghost_recv_dict, full_send_dict = \
157              extract_hostmesh(submesh, triangles_per_proc)
158
159    # Return structures necessary for building the parallel domain
160    return points, vertices, boundary, quantities, \
161           ghost_recv_dict, full_send_dict
162   
163
164
165
Note: See TracBrowser for help on using the repository browser.