File: RectPartitioner.py

package info (click to toggle)
ipyparallel 8.8.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,412 kB
  • sloc: python: 21,991; javascript: 267; makefile: 29; sh: 28
file content (520 lines) | stat: -rwxr-xr-x 19,533 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python
"""A rectangular domain partitioner and associated communication
functionality for solving PDEs in (1D,2D) using FDM
written in the python language

The global solution domain is assumed to be of rectangular shape,
where the number of cells in each direction is stored in nx, ny, nz

The numerical scheme is fully explicit

Authors
-------

 * Xing Cai
 * Min Ragan-Kelley

"""

from numpy import ascontiguousarray, frombuffer, zeros

try:
    from mpi4py import MPI
except ImportError:
    pass
else:
    mpi = MPI.COMM_WORLD


class RectPartitioner:
    """
    Responsible for a rectangular partitioning of a global domain,
    which is expressed as the numbers of cells in the different
    spatial directions. The partitioning info is expressed as an
    array of integers, each indicating the number of subdomains in
    one spatial direction.
    """

    def __init__(self, my_id=-1, num_procs=-1, global_num_cells=[], num_parts=[]):
        self.nsd = 0
        self.my_id = my_id
        self.num_procs = num_procs
        self.redim(global_num_cells, num_parts)

    def redim(self, global_num_cells, num_parts):
        nsd_ = len(global_num_cells)
        #        print("Inside the redim function, nsd=%d" %nsd_)

        if nsd_ < 1 | nsd_ > 3 | nsd_ != len(num_parts):
            print('The input global_num_cells is not ok!')
            return

        self.nsd = nsd_
        self.global_num_cells = global_num_cells
        self.num_parts = num_parts

    def prepare_communication(self):
        """
        Find the subdomain rank (tuple) for each processor and
        determine the neighbor info.
        """

        nsd_ = self.nsd
        if nsd_ < 1:
            print('Number of space dimensions is %d, nothing to do' % nsd_)
            return

        self.subd_rank = [-1, -1, -1]
        self.subd_lo_ix = [-1, -1, -1]
        self.subd_hi_ix = [-1, -1, -1]
        self.lower_neighbors = [-1, -1, -1]
        self.upper_neighbors = [-1, -1, -1]

        num_procs = self.num_procs
        my_id = self.my_id

        num_subds = 1
        for i in range(nsd_):
            num_subds = num_subds * self.num_parts[i]
        if my_id == 0:
            print("# subds=", num_subds)
            # should check num_subds againt num_procs

        offsets = [1, 0, 0]

        # find the subdomain rank
        self.subd_rank[0] = my_id % self.num_parts[0]
        if nsd_ >= 2:
            offsets[1] = self.num_parts[0]
            self.subd_rank[1] = my_id / offsets[1]
        if nsd_ == 3:
            offsets[1] = self.num_parts[0]
            offsets[2] = self.num_parts[0] * self.num_parts[1]
            self.subd_rank[1] = (my_id % offsets[2]) / self.num_parts[0]
            self.subd_rank[2] = my_id / offsets[2]

        print("my_id=%d, subd_rank: " % my_id, self.subd_rank)
        if my_id == 0:
            print("offsets=", offsets)

        # find the neighbor ids
        for i in range(nsd_):
            rank = self.subd_rank[i]
            if rank > 0:
                self.lower_neighbors[i] = my_id - offsets[i]
            if rank < self.num_parts[i] - 1:
                self.upper_neighbors[i] = my_id + offsets[i]

            k = self.global_num_cells[i] / self.num_parts[i]
            m = self.global_num_cells[i] % self.num_parts[i]

            ix = rank * k + max(0, rank + m - self.num_parts[i])
            self.subd_lo_ix[i] = ix

            ix = ix + k
            if rank >= (self.num_parts[i] - m):
                ix = ix + 1  # load balancing
            if rank < self.num_parts[i] - 1:
                ix = ix + 1  # one cell of overlap
            self.subd_hi_ix[i] = ix

        print(
            "subd_rank:",
            self.subd_rank,
            "lower_neig:",
            self.lower_neighbors,
            "upper_neig:",
            self.upper_neighbors,
        )
        print(
            "subd_rank:",
            self.subd_rank,
            "subd_lo_ix:",
            self.subd_lo_ix,
            "subd_hi_ix:",
            self.subd_hi_ix,
        )


class RectPartitioner1D(RectPartitioner):
    """
    Subclass of RectPartitioner, for 1D problems
    """

    def prepare_communication(self):
        """
        Prepare the buffers to be used for later communications
        """

        RectPartitioner.prepare_communication(self)

        if self.lower_neighbors[0] >= 0:
            self.in_lower_buffers = [zeros(1, float)]
            self.out_lower_buffers = [zeros(1, float)]
        if self.upper_neighbors[0] >= 0:
            self.in_upper_buffers = [zeros(1, float)]
            self.out_upper_buffers = [zeros(1, float)]

    def get_num_loc_cells(self):
        return [self.subd_hi_ix[0] - self.subd_lo_ix[0]]


class RectPartitioner2D(RectPartitioner):
    """
    Subclass of RectPartitioner, for 2D problems
    """

    def prepare_communication(self):
        """
        Prepare the buffers to be used for later communications
        """

        RectPartitioner.prepare_communication(self)

        self.in_lower_buffers = [[], []]
        self.out_lower_buffers = [[], []]
        self.in_upper_buffers = [[], []]
        self.out_upper_buffers = [[], []]

        size1 = self.subd_hi_ix[1] - self.subd_lo_ix[1] + 1
        if self.lower_neighbors[0] >= 0:
            self.in_lower_buffers[0] = zeros(size1, float)
            self.out_lower_buffers[0] = zeros(size1, float)
        if self.upper_neighbors[0] >= 0:
            self.in_upper_buffers[0] = zeros(size1, float)
            self.out_upper_buffers[0] = zeros(size1, float)

        size0 = self.subd_hi_ix[0] - self.subd_lo_ix[0] + 1
        if self.lower_neighbors[1] >= 0:
            self.in_lower_buffers[1] = zeros(size0, float)
            self.out_lower_buffers[1] = zeros(size0, float)
        if self.upper_neighbors[1] >= 0:
            self.in_upper_buffers[1] = zeros(size0, float)
            self.out_upper_buffers[1] = zeros(size0, float)

    def get_num_loc_cells(self):
        return [
            self.subd_hi_ix[0] - self.subd_lo_ix[0],
            self.subd_hi_ix[1] - self.subd_lo_ix[1],
        ]


class MPIRectPartitioner2D(RectPartitioner2D):
    """
    Subclass of RectPartitioner2D, which uses MPI via mpi4py for communication
    """

    def __init__(
        self, my_id=-1, num_procs=-1, global_num_cells=[], num_parts=[], slice_copy=True
    ):
        RectPartitioner.__init__(self, my_id, num_procs, global_num_cells, num_parts)
        self.slice_copy = slice_copy

    def update_internal_boundary(self, solution_array):
        nsd_ = self.nsd
        if nsd_ != len(self.in_lower_buffers) | nsd_ != len(self.out_lower_buffers):
            print("Buffers for communicating with lower neighbors not ready")
            return
        if nsd_ != len(self.in_upper_buffers) | nsd_ != len(self.out_upper_buffers):
            print("Buffers for communicating with upper neighbors not ready")
            return

        loc_nx = self.subd_hi_ix[0] - self.subd_lo_ix[0]
        loc_ny = self.subd_hi_ix[1] - self.subd_lo_ix[1]

        lower_x_neigh = self.lower_neighbors[0]
        upper_x_neigh = self.upper_neighbors[0]
        lower_y_neigh = self.lower_neighbors[1]
        upper_y_neigh = self.upper_neighbors[1]

        # communicate in the x-direction first
        if lower_x_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[0] = ascontiguousarray(solution_array[1, :])
            else:
                for i in range(0, loc_ny + 1):
                    self.out_lower_buffers[0][i] = solution_array[1, i]
            mpi.Isend(self.out_lower_buffers[0], lower_x_neigh)

        if upper_x_neigh > -1:
            mpi.Recv(self.in_upper_buffers[0], upper_x_neigh)
            if self.slice_copy:
                solution_array[loc_nx, :] = self.in_upper_buffers[0]
                self.out_upper_buffers[0] = ascontiguousarray(
                    solution_array[loc_nx - 1, :]
                )
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[loc_nx, i] = self.in_upper_buffers[0][i]
                    self.out_upper_buffers[0][i] = solution_array[loc_nx - 1, i]
            mpi.Isend(self.out_upper_buffers[0], upper_x_neigh)

        if lower_x_neigh > -1:
            mpi.Recv(self.in_lower_buffers[0], lower_x_neigh)
            if self.slice_copy:
                solution_array[0, :] = self.in_lower_buffers[0]
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[0, i] = self.in_lower_buffers[0][i]

        # communicate in the y-direction afterwards
        if lower_y_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[1] = ascontiguousarray(solution_array[:, 1])
            else:
                for i in range(0, loc_nx + 1):
                    self.out_lower_buffers[1][i] = solution_array[i, 1]
            mpi.Isend(self.out_lower_buffers[1], lower_y_neigh)

        if upper_y_neigh > -1:
            mpi.Recv(self.in_upper_buffers[1], upper_y_neigh)
            if self.slice_copy:
                solution_array[:, loc_ny] = self.in_upper_buffers[1]
                self.out_upper_buffers[1] = ascontiguousarray(
                    solution_array[:, loc_ny - 1]
                )
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, loc_ny] = self.in_upper_buffers[1][i]
                    self.out_upper_buffers[1][i] = solution_array[i, loc_ny - 1]
            mpi.Isend(self.out_upper_buffers[1], upper_y_neigh)

        if lower_y_neigh > -1:
            mpi.Recv(self.in_lower_buffers[1], lower_y_neigh)
            if self.slice_copy:
                solution_array[:, 0] = self.in_lower_buffers[1]
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, 0] = self.in_lower_buffers[1][i]


class ZMQRectPartitioner2D(RectPartitioner2D):
    """
    Subclass of RectPartitioner2D, which uses 0MQ via pyzmq for communication
    The first two arguments must be `comm`, an EngineCommunicator object,
    and `addrs`, a dict of connection information for other EngineCommunicator
    objects.
    """

    def __init__(
        self,
        comm,
        addrs,
        my_id=-1,
        num_procs=-1,
        global_num_cells=[],
        num_parts=[],
        slice_copy=True,
    ):
        RectPartitioner.__init__(self, my_id, num_procs, global_num_cells, num_parts)
        self.slice_copy = slice_copy
        self.comm = comm  # an Engine
        self.addrs = addrs

    def prepare_communication(self):
        RectPartitioner2D.prepare_communication(self)
        # connect west/south to east/north
        west_id, south_id = self.lower_neighbors[:2]
        west = self.addrs.get(west_id, None)
        south = self.addrs.get(south_id, None)
        self.comm.connect(south, west)

    def update_internal_boundary_x_y(self, solution_array):
        """update the inner boundary with the same send/recv pattern as the MPIPartitioner"""
        nsd_ = self.nsd
        dtype = solution_array.dtype
        if nsd_ != len(self.in_lower_buffers) | nsd_ != len(self.out_lower_buffers):
            print("Buffers for communicating with lower neighbors not ready")
            return
        if nsd_ != len(self.in_upper_buffers) | nsd_ != len(self.out_upper_buffers):
            print("Buffers for communicating with upper neighbors not ready")
            return

        loc_nx = self.subd_hi_ix[0] - self.subd_lo_ix[0]
        loc_ny = self.subd_hi_ix[1] - self.subd_lo_ix[1]

        lower_x_neigh = self.lower_neighbors[0]
        upper_x_neigh = self.upper_neighbors[0]
        lower_y_neigh = self.lower_neighbors[1]
        upper_y_neigh = self.upper_neighbors[1]
        trackers = []
        flags = dict(copy=False, track=False)
        # communicate in the x-direction first
        if lower_x_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[0] = ascontiguousarray(solution_array[1, :])
            else:
                for i in range(0, loc_ny + 1):
                    self.out_lower_buffers[0][i] = solution_array[1, i]
            t = self.comm.west.send(self.out_lower_buffers[0], **flags)
            trackers.append(t)

        if upper_x_neigh > -1:
            msg = self.comm.east.recv(copy=False)
            self.in_upper_buffers[0] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[loc_nx, :] = self.in_upper_buffers[0]
                self.out_upper_buffers[0] = ascontiguousarray(
                    solution_array[loc_nx - 1, :]
                )
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[loc_nx, i] = self.in_upper_buffers[0][i]
                    self.out_upper_buffers[0][i] = solution_array[loc_nx - 1, i]
            t = self.comm.east.send(self.out_upper_buffers[0], **flags)
            trackers.append(t)

        if lower_x_neigh > -1:
            msg = self.comm.west.recv(copy=False)
            self.in_lower_buffers[0] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[0, :] = self.in_lower_buffers[0]
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[0, i] = self.in_lower_buffers[0][i]

        # communicate in the y-direction afterwards
        if lower_y_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[1] = ascontiguousarray(solution_array[:, 1])
            else:
                for i in range(0, loc_nx + 1):
                    self.out_lower_buffers[1][i] = solution_array[i, 1]
            t = self.comm.south.send(self.out_lower_buffers[1], **flags)
            trackers.append(t)

        if upper_y_neigh > -1:
            msg = self.comm.north.recv(copy=False)
            self.in_upper_buffers[1] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[:, loc_ny] = self.in_upper_buffers[1]
                self.out_upper_buffers[1] = ascontiguousarray(
                    solution_array[:, loc_ny - 1]
                )
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, loc_ny] = self.in_upper_buffers[1][i]
                    self.out_upper_buffers[1][i] = solution_array[i, loc_ny - 1]
            t = self.comm.north.send(self.out_upper_buffers[1], **flags)
            trackers.append(t)

        if lower_y_neigh > -1:
            msg = self.comm.south.recv(copy=False)
            self.in_lower_buffers[1] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[:, 0] = self.in_lower_buffers[1]
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, 0] = self.in_lower_buffers[1][i]

        # wait for sends to complete:
        if flags['track']:
            for t in trackers:
                t.wait()

    def update_internal_boundary_send_recv(self, solution_array):
        """update the inner boundary, sending first, then receiving"""
        nsd_ = self.nsd
        dtype = solution_array.dtype
        if nsd_ != len(self.in_lower_buffers) | nsd_ != len(self.out_lower_buffers):
            print("Buffers for communicating with lower neighbors not ready")
            return
        if nsd_ != len(self.in_upper_buffers) | nsd_ != len(self.out_upper_buffers):
            print("Buffers for communicating with upper neighbors not ready")
            return

        loc_nx = self.subd_hi_ix[0] - self.subd_lo_ix[0]
        loc_ny = self.subd_hi_ix[1] - self.subd_lo_ix[1]

        lower_x_neigh = self.lower_neighbors[0]
        upper_x_neigh = self.upper_neighbors[0]
        lower_y_neigh = self.lower_neighbors[1]
        upper_y_neigh = self.upper_neighbors[1]
        trackers = []
        flags = dict(copy=False, track=False)

        # send in all directions first
        if lower_x_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[0] = ascontiguousarray(solution_array[1, :])
            else:
                for i in range(0, loc_ny + 1):
                    self.out_lower_buffers[0][i] = solution_array[1, i]
            t = self.comm.west.send(self.out_lower_buffers[0], **flags)
            trackers.append(t)

        if lower_y_neigh > -1:
            if self.slice_copy:
                self.out_lower_buffers[1] = ascontiguousarray(solution_array[:, 1])
            else:
                for i in range(0, loc_nx + 1):
                    self.out_lower_buffers[1][i] = solution_array[i, 1]
            t = self.comm.south.send(self.out_lower_buffers[1], **flags)
            trackers.append(t)

        if upper_x_neigh > -1:
            if self.slice_copy:
                self.out_upper_buffers[0] = ascontiguousarray(
                    solution_array[loc_nx - 1, :]
                )
            else:
                for i in range(0, loc_ny + 1):
                    self.out_upper_buffers[0][i] = solution_array[loc_nx - 1, i]
            t = self.comm.east.send(self.out_upper_buffers[0], **flags)
            trackers.append(t)

        if upper_y_neigh > -1:
            if self.slice_copy:
                self.out_upper_buffers[1] = ascontiguousarray(
                    solution_array[:, loc_ny - 1]
                )
            else:
                for i in range(0, loc_nx + 1):
                    self.out_upper_buffers[1][i] = solution_array[i, loc_ny - 1]
            t = self.comm.north.send(self.out_upper_buffers[1], **flags)
            trackers.append(t)

        # now start receiving
        if upper_x_neigh > -1:
            msg = self.comm.east.recv(copy=False)
            self.in_upper_buffers[0] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[loc_nx, :] = self.in_upper_buffers[0]
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[loc_nx, i] = self.in_upper_buffers[0][i]

        if lower_x_neigh > -1:
            msg = self.comm.west.recv(copy=False)
            self.in_lower_buffers[0] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[0, :] = self.in_lower_buffers[0]
            else:
                for i in range(0, loc_ny + 1):
                    solution_array[0, i] = self.in_lower_buffers[0][i]

        if upper_y_neigh > -1:
            msg = self.comm.north.recv(copy=False)
            self.in_upper_buffers[1] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[:, loc_ny] = self.in_upper_buffers[1]
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, loc_ny] = self.in_upper_buffers[1][i]

        if lower_y_neigh > -1:
            msg = self.comm.south.recv(copy=False)
            self.in_lower_buffers[1] = frombuffer(msg, dtype=dtype)
            if self.slice_copy:
                solution_array[:, 0] = self.in_lower_buffers[1]
            else:
                for i in range(0, loc_nx + 1):
                    solution_array[i, 0] = self.in_lower_buffers[1][i]

        # wait for sends to complete:
        if flags['track']:
            for t in trackers:
                t.wait()

    # use send/recv pattern instead of x/y sweeps
    update_internal_boundary = update_internal_boundary_send_recv