source: inundation/parallel/parallel_shallow_water.py @ 3033

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

Further testing on time calculation in parallel computation

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