distributed_do.py 6.89 KB
Newer Older
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
import numpy as np
from .random import Random
from mpi4py import MPI

comm = MPI.COMM_WORLD
ntask = comm.Get_size()
rank = comm.Get_rank()


def shareSize(nwork, nshares, myshare):
    nbase = nwork//nshares
    return nbase if myshare>=nwork%nshares else nbase+1

def get_locshape(shape, distaxis):
    if distaxis==-1:
        return shape
    shape2=list(shape)
    shape2[distaxis]=shareSize(shape[distaxis],ntask,rank)
    return tuple(shape2)

class data_object(object):
    def __init__(self, shape, data, distaxis):
        """Must not be called directly by users"""
        self._shape = shape
        self._distaxis = distaxis
        lshape = get_locshape(self._shape, self._distaxis)
        self._data = data

    def sanity_checks(self):
        # check whether the distaxis is consistent
        if self._distaxis<-1 or self._distaxis>=len(self._shape):
            raise ValueError
        itmp=np.array(self._distaxis)
        otmp=np.empty(ntask,dtype=np.int)
        comm.Allgather(itmp,otmp)
        if np.any(otmp!=self._distaxis):
            raise ValueError
        # check whether the global shape is consistent
        itmp=np.array(self._shape)
        otmp=np.empty((len(self._shape),ntask),dtype=np.int)
        comm.Allgather(itmp,otmp)
        for i in range(ntask):
            if (otmp[i,:]!=self._shape).any():
                raise ValueError
        # check shape of local data
        if self._distaxis<0:
            if self._data.shape!=self._shape:
                raise ValueError
        else:
            itmp=np.array(self._shape)
            itmp[self._distaxis] = get_local_length(self._shape[self._distaxis],ntask,rank)
            if self._data.shape!=itmp:
                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):
        return data_object(self._shape, self._data.real, self._dist_axis)

    @property
    def imag(self):
        return data_object(self._shape, self._data.imag, self._dist_axis)

    def _contraction_helper(self, op, axis):
        if axis is not None:
            if len(axis)==len(self._data.shape):
                axis = None
        if axis is None:
            res = getattr(self._data, op)()
            MPI.COMM_WORLD.Allreduce(res,res2,mpiop)

        if self._distaxis in axis:
            pass# reduce globally, redistribute the result along axis 0(?)
        else:
            pass# reduce locally

        # perform the contraction on the data
        data = getattr(self._data, op)(axis=axis)

        # check if the result is scalar or if a result_field must be constr.
        if np.isscalar(data):
            return data
        else:
            return data_object(data)

    def sum(self, axis=None):
        return self._contraction_helper("sum", MPI.SUM, axis)

    def _binary_helper(self, other, op):
        a = self._data
        if isinstance(other, data_object):
            b = other._data
            if a._shape != b._shape:
                raise ValueError("shapes are incompatible.")
            if a._distaxis != b._distaxis:
                raise ValueError("distributions are incompatible.")
        else:
            b = other

        tval = getattr(a, op)(b)
        return self if tval is a else data_object(self._shape, tval, self._distaxis)

    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__')

    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):
        return data_object(-self._data)

    def __abs__(self):
        return data_object(np.abs(self._data))

    def ravel(self):
        return data_object(self._data.ravel())

    def reshape(self, shape):
        return data_object(self._data.reshape(shape))

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

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


def full(shape, fill_value, dtype=None, dist_axis=0):
    return data_object(shape, np.full(shape, local_shape(shape, dist_axis), fill_value, dtype))


def empty(shape, dtype=np.float):
    return data_object(np.empty(shape, dtype))


def zeros(shape, dtype=np.float):
    return data_object(np.zeros(shape, dtype))


def ones(shape, dtype=np.float):
    return data_object(np.ones(shape, dtype))


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


def vdot(a, b):
    return np.vdot(a._data, b._data)


def _math_helper(x, function, out):
    if out is not None:
        function(x._data, out=out._data)
        return out
    else:
        return data_object(function(x._data))


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):
    return data_object(np.array(object._data, dtype=dtype, copy=copy))


def from_random(random_type, shape, dtype=np.float64, **kwargs):
    generator_function = getattr(Random, random_type)
    return data_object(generator_function(dtype=dtype, shape=shape, **kwargs))


def to_ndarray(arr):
    return arr._data


def from_ndarray(arr):
    return data_object(arr.shape,arr,-1)