source: inundation/parallel/parallel_shallow_water.py @ 2090

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

Removed pypar test files and commented pmesh_divide

File size: 8.4 KB
Line 
1import sys
2from os import sep
3sys.path.append('..'+sep+'pyvolution')
4
5"""Class Parallel_Shallow_Water_Domain -
62D triangular domains for finite-volume computations of
7the shallow water equation, with extra structures to allow
8communication between other Parallel_Domains and itself
9
10This module contains a specialisation of class Domain
11from module shallow_water.py
12
13Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
14Geoscience Australia, 2004-2005
15"""
16
17import logging, logging.config
18logger = logging.getLogger('parallel')
19logger.setLevel(logging.WARNING)
20
21try:
22    logging.config.fileConfig('log.ini')
23except:
24    pass
25
26from shallow_water import *
27from Numeric import zeros, Float, Int, ones, allclose, array
28import pypar
29
30
31class Parallel_Domain(Domain):
32
33    def __init__(self, coordinates, vertices, boundary = None,
34                 full_send_dict = None, ghost_recv_dict = None):
35
36        self.processor = pypar.rank()
37        self.numproc   = pypar.size()
38
39        Domain.__init__(self, coordinates, vertices, boundary)
40
41        N = self.number_of_elements
42
43        self.processor = pypar.rank()
44        self.numproc   = pypar.size()
45
46        # Setup Communication Buffers
47        self.nsys = 3
48        for key in full_send_dict:
49            buffer_shape = full_send_dict[key][0].shape[0]
50            full_send_dict[key].append(zeros( (buffer_shape,self.nsys) ,Float))
51
52
53        for key in ghost_recv_dict:
54            buffer_shape = ghost_recv_dict[key][0].shape[0]
55            ghost_recv_dict[key].append(zeros( (buffer_shape,self.nsys) ,Float))
56
57        self.full_send_dict  = full_send_dict
58        self.ghost_recv_dict = ghost_recv_dict
59
60        # Buffers for synchronisation of timesteps
61        self.local_timestep = zeros(1, Float)
62        self.global_timestep = zeros(1, Float)
63
64        self.local_timesteps = zeros(self.numproc, Float)
65
66
67        self.communication_time = 0.0
68        self.communication_reduce_time = 0.0
69        self.communication_broadcast_time = 0.0
70
71
72
73    def check_integrity(self):
74        Domain.check_integrity(self)
75
76        msg = 'Will need to check global and local numbering'
77        assert self.conserved_quantities[0] == 'stage', msg
78        assert self.conserved_quantities[1] == 'xmomentum', msg
79        assert self.conserved_quantities[2] == 'ymomentum', msg
80
81
82    def update_timestep_1(self, yieldstep, finaltime):
83        """Calculate local timestep using broadcasts
84        """
85
86
87        Domain.update_timestep(self, yieldstep, finaltime)
88
89        import time
90
91
92        t0 = time.time()
93
94        #Broadcast local timestep from every processor to every other
95        for pid in range(self.numproc):
96            #print 'P%d calling broadcast from %d' %(self.processor, pid)
97            self.local_timestep[0] = self.timestep
98            pypar.broadcast(self.local_timestep, pid, bypass=True)           
99            self.local_timesteps[pid] = self.local_timestep[0]
100
101        self.timestep = min(self.local_timesteps)
102
103        pypar.barrier()
104        self.communication_broadcast_time += time.time()-t0
105
106
107
108
109    def update_timestep(self, yieldstep, finaltime):
110        """Calculate local timestep
111        """
112
113
114        #Compute minimal timestep on local process
115        Domain.update_timestep(self, yieldstep, finaltime)
116
117        pypar.barrier()
118
119        import time
120        #Compute minimal timestep across all processes
121        self.local_timestep[0] = self.timestep
122        use_reduce_broadcast = True
123        if use_reduce_broadcast:
124            t0 = time.time()
125            pypar.reduce(self.local_timestep, pypar.MIN, 0,
126                         buffer=self.global_timestep,
127                         bypass=True)
128
129        else:
130            #Alternative: Try using straight send and receives
131            t0 = time.time()
132            self.global_timestep[0] = self.timestep
133
134            if self.processor == 0:
135                for i in range(1, self.numproc):
136                    pypar.receive(i,
137                                  buffer=self.local_timestep,
138                                  bypass=True)
139
140                    if self.local_timestep[0] < self.global_timestep[0]:
141                        self.global_timestep[0] = self.local_timestep[0]
142            else:
143                pypar.send(self.local_timestep, 0,
144                           use_buffer=True, bypass=True)
145
146
147
148
149        self.communication_reduce_time += time.time()-t0
150
151
152
153
154        #Broadcast minimal timestep to all
155        t0 = time.time()
156        pypar.broadcast(self.global_timestep, 0,
157                        bypass=True)
158
159        self.communication_broadcast_time += time.time()-t0
160
161
162        self.timestep = self.global_timestep[0]
163
164
165    #update_timestep = update_timestep_1
166
167    def update_ghosts(self):
168
169        # We must send the information from the full cells and
170        # receive the information for the ghost cells
171        # We have a dictionary of lists with ghosts expecting updates from
172        # the separate processors
173
174
175        from Numeric import take,put
176        import time
177        t0 = time.time()
178
179        # update of non-local ghost cells
180        for iproc in range(self.numproc):
181            if iproc == self.processor:
182                #Send data from iproc processor to other processors
183                for send_proc in self.full_send_dict:
184                    if send_proc != iproc:
185
186                        Idf  = self.full_send_dict[send_proc][0]
187                        Xout = self.full_send_dict[send_proc][2]
188
189                        for i, q in enumerate(self.conserved_quantities):
190                            #print 'Send',i,q
191                            Q_cv =  self.quantities[q].centroid_values
192                            Xout[:,i] = take(Q_cv,     Idf)
193
194                        pypar.send(Xout, send_proc,
195                                   use_buffer=True, bypass = True)
196
197
198            else:
199                #Receive data from the iproc processor
200                if  self.ghost_recv_dict.has_key(iproc):
201
202                    Idg = self.ghost_recv_dict[iproc][0]
203                    X = self.ghost_recv_dict[iproc][2]
204
205                    X = pypar.receive(iproc, buffer=X, bypass = True)
206
207                    for i, q in enumerate(self.conserved_quantities):
208                        #print 'Receive',i,q
209                        Q_cv =  self.quantities[q].centroid_values
210                        put(Q_cv,     Idg, X[:,i])
211
212        #local update of ghost cells
213        iproc = self.processor
214        if self.full_send_dict.has_key(iproc):
215
216            # LINDA:
217            # now store full as local id, global id, value
218            Idf  = self.full_send_dict[iproc][0]
219
220            # LINDA:
221            # now store ghost as local id, global id, value
222            Idg = self.ghost_recv_dict[iproc][0]
223
224            for i, q in enumerate(self.conserved_quantities):
225                #print 'LOCAL SEND RECEIVE',i,q
226                Q_cv =  self.quantities[q].centroid_values
227                put(Q_cv,     Idg, take(Q_cv,     Idf))
228
229        self.communication_time += time.time()-t0
230
231
232    def write_time(self):
233        if self.min_timestep == self.max_timestep:
234            print 'Processor %d, Time = %.4f, delta t = %.8f, steps=%d (%d)'\
235                  %(self.processor, self.time, self.min_timestep, self.number_of_steps,
236                    self.number_of_first_order_steps)
237        elif self.min_timestep > self.max_timestep:
238            print 'Processor %d, Time = %.4f, steps=%d (%d)'\
239                  %(self.processor, self.time, self.number_of_steps,
240                    self.number_of_first_order_steps)
241        else:
242            print 'Processor %d, Time = %.4f, delta t in [%.8f, %.8f], steps=%d (%d)'\
243                  %(self.processor, self.time, self.min_timestep,
244                    self.max_timestep, self.number_of_steps,
245                    self.number_of_first_order_steps)
246
247
248    def evolve(self, yieldstep = None, finaltime = None):
249        """Specialisation of basic evolve method from parent class
250        """
251
252        #Initialise real time viz if requested
253        if self.time == 0.0:
254            pass
255
256        #Call basic machinery from parent class
257        for t in Domain.evolve(self, yieldstep, finaltime):
258
259            #Pass control on to outer loop for more specific actions
260            yield(t)
Note: See TracBrowser for help on using the repository browser.