distributed_do.py 13.2 KB
Newer Older
1
2
3
4
import numpy as np
from .random import Random
from mpi4py import MPI

Martin Reinecke's avatar
Martin Reinecke committed
5
6
7
8
9
10
11
__all__ = ["ntask", "rank", "master", "local_shape", "data_object", "full",
           "empty", "zeros", "ones", "empty_like", "vdot", "abs", "exp",
           "log", "sqrt", "bincount", "from_object", "from_random",
           "local_data", "ibegin", "np_allreduce_sum", "distaxis",
           "from_local_data", "from_global_data", "to_global_data",
           "redistribute", "default_distaxis"]

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
12
13
14
_comm = MPI.COMM_WORLD
ntask = _comm.Get_size()
rank = _comm.Get_rank()
Martin Reinecke's avatar
Martin Reinecke committed
15
master = rank == 0
16
17


Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
18
def _shareSize(nwork, nshares, myshare):
19
    nbase = nwork//nshares
Martin Reinecke's avatar
Martin Reinecke committed
20
21
    return nbase if myshare >= nwork % nshares else nbase+1

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
22
23

def _shareRange(nwork, nshares, myshare):
Martin Reinecke's avatar
Martin Reinecke committed
24
25
    nbase = nwork//nshares
    additional = nwork % nshares
Martin Reinecke's avatar
Martin Reinecke committed
26
    lo = myshare*nbase + min(myshare, additional)
Martin Reinecke's avatar
Martin Reinecke committed
27
28
29
    hi = lo + nbase + (1 if myshare < additional else 0)
    return lo, hi

30

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
31
def local_shape(shape, distaxis):
Martin Reinecke's avatar
Martin Reinecke committed
32
    if len(shape) == 0:
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
33
        distaxis = -1
Martin Reinecke's avatar
Martin Reinecke committed
34
    if distaxis == -1:
35
        return shape
Martin Reinecke's avatar
Martin Reinecke committed
36
37
    shape2 = list(shape)
    shape2[distaxis] = _shareSize(shape[distaxis], ntask, rank)
38
39
    return tuple(shape2)

Martin Reinecke's avatar
Martin Reinecke committed
40

41
42
43
class data_object(object):
    def __init__(self, shape, data, distaxis):
        """Must not be called directly by users"""
Martin Reinecke's avatar
Martin Reinecke committed
44
        self._shape = tuple(shape)
Martin Reinecke's avatar
Martin Reinecke committed
45
        if len(self._shape) == 0:
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
46
            distaxis = -1
47
48
49
        self._distaxis = distaxis
        self._data = data

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
50
    def _sanity_checks(self):
51
        # check whether the distaxis is consistent
Martin Reinecke's avatar
Martin Reinecke committed
52
        if self._distaxis < -1 or self._distaxis >= len(self._shape):
53
            raise ValueError
Martin Reinecke's avatar
Martin Reinecke committed
54
55
56
57
        itmp = np.array(self._distaxis)
        otmp = np.empty(ntask, dtype=np.int)
        _comm.Allgather(itmp, otmp)
        if np.any(otmp != self._distaxis):
58
59
            raise ValueError
        # check whether the global shape is consistent
Martin Reinecke's avatar
Martin Reinecke committed
60
61
62
        itmp = np.array(self._shape)
        otmp = np.empty((ntask, len(self._shape)), dtype=np.int)
        _comm.Allgather(itmp, otmp)
63
        for i in range(ntask):
Martin Reinecke's avatar
Martin Reinecke committed
64
            if np.any(otmp[i, :] != self._shape):
65
66
                raise ValueError
        # check shape of local data
Martin Reinecke's avatar
Martin Reinecke committed
67
68
        if self._distaxis < 0:
            if self._data.shape != self._shape:
69
70
                raise ValueError
        else:
Martin Reinecke's avatar
Martin Reinecke committed
71
72
73
74
            itmp = np.array(self._shape)
            itmp[self._distaxis] = _shareSize(self._shape[self._distaxis],
                                              ntask, rank)
            if np.any(self._data.shape != itmp):
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
                raise ValueError

    @property
    def dtype(self):
        return self._data.dtype

    @property
    def shape(self):
        return self._shape

    @property
    def size(self):
        return np.prod(self._shape)

    @property
    def real(self):
Martin Reinecke's avatar
Martin Reinecke committed
91
        return data_object(self._shape, self._data.real, self._distaxis)
92
93
94

    @property
    def imag(self):
Martin Reinecke's avatar
Martin Reinecke committed
95
        return data_object(self._shape, self._data.imag, self._distaxis)
96

Martin Reinecke's avatar
Martin Reinecke committed
97
    def _contraction_helper(self, op, mpiop, axis):
98
        if axis is not None:
Martin Reinecke's avatar
Martin Reinecke committed
99
            if len(axis) == len(self._data.shape):
100
101
                axis = None
        if axis is None:
Martin Reinecke's avatar
Martin Reinecke committed
102
            res = np.array(getattr(self._data, op)())
Martin Reinecke's avatar
Martin Reinecke committed
103
            if (self._distaxis == -1):
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
104
                return res[()]
Martin Reinecke's avatar
Martin Reinecke committed
105
106
            res2 = np.empty((), dtype=res.dtype)
            _comm.Allreduce(res, res2, mpiop)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
107
            return res2[()]
108
109

        if self._distaxis in axis:
Martin Reinecke's avatar
Martin Reinecke committed
110
111
            res = getattr(self._data, op)(axis=axis)
            res2 = np.empty_like(res)
Martin Reinecke's avatar
Martin Reinecke committed
112
            _comm.Allreduce(res, res2, mpiop)
Martin Reinecke's avatar
Martin Reinecke committed
113
            return from_global_data(res2, distaxis=0)
114
        else:
Martin Reinecke's avatar
Martin Reinecke committed
115
            # perform the contraction on the local data
Martin Reinecke's avatar
Martin Reinecke committed
116
117
            res = getattr(self._data, op)(axis=axis)
            if self._distaxis == -1:
Martin Reinecke's avatar
Martin Reinecke committed
118
                return from_global_data(res, distaxis=0)
Martin Reinecke's avatar
Martin Reinecke committed
119
            shp = list(res.shape)
Martin Reinecke's avatar
Martin Reinecke committed
120
            shift = 0
Martin Reinecke's avatar
Martin Reinecke committed
121
            for ax in axis:
Martin Reinecke's avatar
Martin Reinecke committed
122
123
                if ax < self._distaxis:
                    shift += 1
Martin Reinecke's avatar
Martin Reinecke committed
124
125
            shp[self._distaxis-shift] = self.shape[self._distaxis]
            return from_local_data(shp, res, self._distaxis-shift)
126
127
128

    def sum(self, axis=None):
        return self._contraction_helper("sum", MPI.SUM, axis)
Martin Reinecke's avatar
Martin Reinecke committed
129

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
130
131
    def min(self, axis=None):
        return self._contraction_helper("min", MPI.MIN, axis)
Martin Reinecke's avatar
Martin Reinecke committed
132

Martin Reinecke's avatar
fixes    
Martin Reinecke committed
133
134
    def max(self, axis=None):
        return self._contraction_helper("max", MPI.MAX, axis)
135

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
136
137
138
    # FIXME: to be improved!
    def mean(self):
        return self.sum()/self.size
Martin Reinecke's avatar
Martin Reinecke committed
139

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
140
141
    def std(self):
        return np.sqrt(self.var())
Martin Reinecke's avatar
Martin Reinecke committed
142

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
143
144
145
    def var(self):
        return (abs(self-self.mean())**2).mean()

146
    def _binary_helper(self, other, op):
Martin Reinecke's avatar
Martin Reinecke committed
147
        a = self
148
        if isinstance(other, data_object):
Martin Reinecke's avatar
Martin Reinecke committed
149
            b = other
150
151
152
153
            if a._shape != b._shape:
                raise ValueError("shapes are incompatible.")
            if a._distaxis != b._distaxis:
                raise ValueError("distributions are incompatible.")
Martin Reinecke's avatar
Martin Reinecke committed
154
155
            a = a._data
            b = b._data
156
        else:
Martin Reinecke's avatar
Martin Reinecke committed
157
            a = a._data
158
159
160
            b = other

        tval = getattr(a, op)(b)
Martin Reinecke's avatar
Martin Reinecke committed
161
162
163
164
        if tval is a:
            return self
        else:
            return data_object(self._shape, tval, self._distaxis)
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

    def __add__(self, other):
        return self._binary_helper(other, op='__add__')

    def __radd__(self, other):
        return self._binary_helper(other, op='__radd__')

    def __iadd__(self, other):
        return self._binary_helper(other, op='__iadd__')

    def __sub__(self, other):
        return self._binary_helper(other, op='__sub__')

    def __rsub__(self, other):
        return self._binary_helper(other, op='__rsub__')

    def __isub__(self, other):
        return self._binary_helper(other, op='__isub__')

    def __mul__(self, other):
        return self._binary_helper(other, op='__mul__')

    def __rmul__(self, other):
        return self._binary_helper(other, op='__rmul__')

    def __imul__(self, other):
        return self._binary_helper(other, op='__imul__')

    def __div__(self, other):
        return self._binary_helper(other, op='__div__')

    def __rdiv__(self, other):
        return self._binary_helper(other, op='__rdiv__')

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
199
200
201
    def __idiv__(self, other):
        return self._binary_helper(other, op='__idiv__')

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    def __truediv__(self, other):
        return self._binary_helper(other, op='__truediv__')

    def __rtruediv__(self, other):
        return self._binary_helper(other, op='__rtruediv__')

    def __pow__(self, other):
        return self._binary_helper(other, op='__pow__')

    def __rpow__(self, other):
        return self._binary_helper(other, op='__rpow__')

    def __ipow__(self, other):
        return self._binary_helper(other, op='__ipow__')

    def __eq__(self, other):
        return self._binary_helper(other, op='__eq__')

    def __ne__(self, other):
        return self._binary_helper(other, op='__ne__')

    def __neg__(self):
Martin Reinecke's avatar
Martin Reinecke committed
224
        return data_object(self._shape, -self._data, self._distaxis)
225
226

    def __abs__(self):
Martin Reinecke's avatar
Martin Reinecke committed
227
        return data_object(self._shape, np.abs(self._data), self._distaxis)
228
229
230
231
232
233
234
235

    def all(self):
        return self._data.all()

    def any(self):
        return self._data.any()


Martin Reinecke's avatar
Martin Reinecke committed
236
def full(shape, fill_value, dtype=None, distaxis=0):
Martin Reinecke's avatar
Martin Reinecke committed
237
238
    return data_object(shape, np.full(local_shape(shape, distaxis),
                                      fill_value, dtype), distaxis)
239
240


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
241
def empty(shape, dtype=None, distaxis=0):
Martin Reinecke's avatar
Martin Reinecke committed
242
243
    return data_object(shape, np.empty(local_shape(shape, distaxis),
                                       dtype), distaxis)
244
245


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
246
def zeros(shape, dtype=None, distaxis=0):
Martin Reinecke's avatar
Martin Reinecke committed
247
248
    return data_object(shape, np.zeros(local_shape(shape, distaxis), dtype),
                       distaxis)
249
250


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
251
def ones(shape, dtype=None, distaxis=0):
Martin Reinecke's avatar
Martin Reinecke committed
252
253
    return data_object(shape, np.ones(local_shape(shape, distaxis), dtype),
                       distaxis)
254
255
256
257
258
259
260


def empty_like(a, dtype=None):
    return data_object(np.empty_like(a._data, dtype))


def vdot(a, b):
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
261
    tmp = np.array(np.vdot(a._data, b._data))
Martin Reinecke's avatar
Martin Reinecke committed
262
263
    res = np.empty((), dtype=tmp.dtype)
    _comm.Allreduce(tmp, res, MPI.SUM)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
264
    return res[()]
265
266
267
268
269
270
271


def _math_helper(x, function, out):
    if out is not None:
        function(x._data, out=out._data)
        return out
    else:
Martin Reinecke's avatar
Martin Reinecke committed
272
        return data_object(x.shape, function(x._data), x._distaxis)
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


def abs(a, out=None):
    return _math_helper(a, np.abs, out)


def exp(a, out=None):
    return _math_helper(a, np.exp, out)


def log(a, out=None):
    return _math_helper(a, np.log, out)


def sqrt(a, out=None):
    return _math_helper(a, np.sqrt, out)


def bincount(x, weights=None, minlength=None):
    if weights is not None:
        weights = weights._data
    res = np.bincount(x._data, weights, minlength)
    return data_object(res)


def from_object(object, dtype=None, copy=True):
Martin Reinecke's avatar
Martin Reinecke committed
299
300
301
    return data_object(object._shape, np.array(object._data, dtype=dtype,
                                               copy=copy),
                       distaxis=object._distaxis)
302
303


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
304
def from_random(random_type, shape, dtype=np.float64, distaxis=0, **kwargs):
305
    generator_function = getattr(Random, random_type)
Martin Reinecke's avatar
Martin Reinecke committed
306
307
    # lshape = local_shape(shape, distaxis)
    # return data_object(shape, generator_function(dtype=dtype, shape=lshape, **kwargs), distaxis=distaxis)
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
308
    return from_global_data(generator_function(dtype=dtype, shape=shape, **kwargs), distaxis=distaxis)
309

Martin Reinecke's avatar
Martin Reinecke committed
310

Martin Reinecke's avatar
Martin Reinecke committed
311
312
313
314
def local_data(arr):
    return arr._data


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
315
316
def ibegin(arr):
    res = [0] * arr._data.ndim
Martin Reinecke's avatar
Martin Reinecke committed
317
    res[arr._distaxis] = _shareRange(arr._shape[arr._distaxis], ntask, rank)[0]
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
318
    return tuple(res)
Martin Reinecke's avatar
Martin Reinecke committed
319
320


Martin Reinecke's avatar
fixes    
Martin Reinecke committed
321
322
def np_allreduce_sum(arr):
    res = np.empty_like(arr)
Martin Reinecke's avatar
Martin Reinecke committed
323
    _comm.Allreduce(arr, res, MPI.SUM)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
324
    return res
Martin Reinecke's avatar
Martin Reinecke committed
325
326
327
328
329
330


def distaxis(arr):
    return arr._distaxis


Martin Reinecke's avatar
Martin Reinecke committed
331
def from_local_data(shape, arr, distaxis):
Martin Reinecke's avatar
Martin Reinecke committed
332
333
334
    return data_object(shape, arr, distaxis)


Martin Reinecke's avatar
Martin Reinecke committed
335
336
def from_global_data(arr, distaxis=0):
    if distaxis == -1:
Martin Reinecke's avatar
Martin Reinecke committed
337
        return data_object(arr.shape, arr, distaxis)
Martin Reinecke's avatar
Martin Reinecke committed
338
    lo, hi = _shareRange(arr.shape[distaxis], ntask, rank)
Martin Reinecke's avatar
Martin Reinecke committed
339
    sl = [slice(None)]*len(arr.shape)
Martin Reinecke's avatar
Martin Reinecke committed
340
    sl[distaxis] = slice(lo, hi)
Martin Reinecke's avatar
Martin Reinecke committed
341
342
343
    return data_object(arr.shape, arr[sl], distaxis)


Martin Reinecke's avatar
Martin Reinecke committed
344
345
def to_global_data(arr):
    if arr._distaxis == -1:
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
346
347
348
349
350
        return arr._data
    tmp = redistribute(arr, dist=-1)
    return tmp._data


Martin Reinecke's avatar
Martin Reinecke committed
351
def redistribute(arr, dist=None, nodist=None):
Martin Reinecke's avatar
Martin Reinecke committed
352
353
354
    if dist is not None:
        if nodist is not None:
            raise ValueError
Martin Reinecke's avatar
Martin Reinecke committed
355
        if dist == arr._distaxis:
Martin Reinecke's avatar
Martin Reinecke committed
356
357
358
359
360
361
            return arr
    else:
        if nodist is None:
            raise ValueError
        if arr._distaxis not in nodist:
            return arr
Martin Reinecke's avatar
Martin Reinecke committed
362
        dist = -1
Martin Reinecke's avatar
Martin Reinecke committed
363
364
        for i in range(len(arr.shape)):
            if i not in nodist:
Martin Reinecke's avatar
Martin Reinecke committed
365
                dist = i
Martin Reinecke's avatar
Martin Reinecke committed
366
                break
Martin Reinecke's avatar
Martin Reinecke committed
367

Martin Reinecke's avatar
Martin Reinecke committed
368
    if arr._distaxis == -1:  # just pick the proper subset
Martin Reinecke's avatar
Martin Reinecke committed
369
        return from_global_data(arr._data, dist)
Martin Reinecke's avatar
Martin Reinecke committed
370
    if dist == -1:  # gather data
Martin Reinecke's avatar
Martin Reinecke committed
371
        tmp = np.moveaxis(arr._data, arr._distaxis, 0)
Martin Reinecke's avatar
Martin Reinecke committed
372
373
        slabsize = np.prod(tmp.shape[1:])*tmp.itemsize
        sz = np.empty(ntask, dtype=np.int)
Martin Reinecke's avatar
Martin Reinecke committed
374
        for i in range(ntask):
Martin Reinecke's avatar
Martin Reinecke committed
375
376
377
378
379
380
381
            sz[i] = slabsize*_shareSize(arr.shape[arr._distaxis], ntask, i)
        disp = np.empty(ntask, dtype=np.int)
        disp[0] = 0
        disp[1:] = np.cumsum(sz[:-1])
        tmp = tmp.flatten()
        out = np.empty(arr.size, dtype=arr.dtype)
        _comm.Allgatherv(tmp, [out, sz, disp, MPI.BYTE])
Martin Reinecke's avatar
Martin Reinecke committed
382
383
384
385
        shp = np.array(arr._shape)
        shp[1:arr._distaxis+1] = shp[0:arr._distaxis]
        shp[0] = arr.shape[arr._distaxis]
        out = out.reshape(shp)
Martin Reinecke's avatar
Martin Reinecke committed
386
        out = np.moveaxis(out, 0, arr._distaxis)
Martin Reinecke's avatar
Martin Reinecke committed
387
        return from_global_data(out, distaxis=-1)
Martin Reinecke's avatar
Martin Reinecke committed
388
    # real redistribution via Alltoallv
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
389
    # temporary slow, but simple solution for comparison purposes:
Martin Reinecke's avatar
Martin Reinecke committed
390
    # return redistribute(redistribute(arr,dist=-1),dist=dist)
Martin Reinecke's avatar
Martin Reinecke committed
391

Martin Reinecke's avatar
Martin Reinecke committed
392
393
    tmp = np.moveaxis(arr._data, (dist, arr._distaxis), (0, 1))
    tshape = tmp.shape
Martin Reinecke's avatar
Martin Reinecke committed
394
395
396
    slabsize = np.prod(tmp.shape[2:])*tmp.itemsize
    ssz = np.empty(ntask, dtype=np.int)
    rsz = np.empty(ntask, dtype=np.int)
Martin Reinecke's avatar
Martin Reinecke committed
397
    for i in range(ntask):
Martin Reinecke's avatar
Martin Reinecke committed
398
399
400
401
402
403
404
405
406
407
408
409
        ssz[i] = _shareSize(arr.shape[dist], ntask, i)*tmp.shape[1]*slabsize
        rsz[i] = _shareSize(arr.shape[dist], ntask, rank) * \
            _shareSize(arr.shape[arr._distaxis], ntask, i) * \
            slabsize
    sdisp = np.empty(ntask, dtype=np.int)
    rdisp = np.empty(ntask, dtype=np.int)
    sdisp[0] = 0
    rdisp[0] = 0
    sdisp[1:] = np.cumsum(ssz[:-1])
    rdisp[1:] = np.cumsum(rsz[:-1])
    tmp = tmp.flatten()
    out = np.empty(np.prod(local_shape(arr.shape, dist)), dtype=arr.dtype)
Martin Reinecke's avatar
Martin Reinecke committed
410
411
    s_msg = [tmp, (ssz, sdisp), MPI.BYTE]
    r_msg = [out, (rsz, rdisp), MPI.BYTE]
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
412
    _comm.Alltoallv(s_msg, r_msg)
Martin Reinecke's avatar
Martin Reinecke committed
413
414
415
416
    out2 = np.empty([_shareSize(arr.shape[dist], ntask, rank),
                     arr.shape[arr._distaxis]] + list(tshape[2:]),
                    dtype=arr.dtype)
    ofs = 0
417
418
    for i in range(ntask):
        lsize = rsz[i]//tmp.itemsize
Martin Reinecke's avatar
Martin Reinecke committed
419
420
421
        lo, hi = _shareRange(arr.shape[arr._distaxis], ntask, i)
        out2[slice(None), slice(lo, hi)] = \
            out[ofs:ofs+lsize].reshape([_shareSize(arr.shape[dist], ntask, rank),_shareSize(arr.shape[arr._distaxis],ntask,i)]+list(tshape[2:]))
422
        ofs += lsize
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
423
    new_shape = [_shareSize(arr.shape[dist],ntask,rank), arr.shape[arr._distaxis]] +list(tshape[2:])
Martin Reinecke's avatar
Martin Reinecke committed
424
    out2 = out2.reshape(new_shape)
425
    out2 = np.moveaxis(out2, (0, 1), (dist, arr._distaxis))
Martin Reinecke's avatar
Martin Reinecke committed
426
    return from_local_data(arr.shape, out2, dist)
Martin Reinecke's avatar
Martin Reinecke committed
427
428
429
430


def default_distaxis():
    return 0