/
mapreduce.py
284 lines (233 loc) · 7.45 KB
/
mapreduce.py
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
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2013, Luis Pedro Coelho <luis@luispedro.org>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
# License : MIT
'''
mapreduce: Build tasks that follow a map-reduce pattern.
'''
from .jug import Task
from .utils import identity
from .hash import hash_one
from itertools import chain
import operator
__all__ = [
'mapreduce',
'map',
'reduce',
]
def _get_function(f):
if getattr(f, '_jug_is_task_generator', False):
hvalue = hash_one(f)
f = f.f
f.__jug_hash__ = lambda: hvalue
return f
return f
def _jug_map_reduce(reducer, mapper, inputs):
from six.moves import builtins
reducer = _get_function(reducer)
mapper = _get_function(mapper)
return builtins.reduce(reducer, builtins.map(mapper, inputs))
def _jug_reduce(reducer, inputs):
from six.moves import builtins
reducer = _get_function(reducer)
return builtins.reduce(reducer, chain(inputs))
def _break_up(lst, step):
start = 0
next = step
while start < len(lst):
yield lst[start:next]
start = next
next += step
def _jug_map(mapper, es):
from six.moves import builtins
return builtins.map(mapper, es)
def _jug_map_curry(mapper, es):
return [mapper(*e) for e in es]
def mapreduce(reducer, mapper, inputs, map_step=4, reduce_step=8):
'''
task = mapreduce(reducer, mapper, inputs, map_step=4, reduce_step=8)
Create a task that does roughly the following::
reduce(reducer, map(mapper, inputs))
Roughly because the order of operations might be different. In particular,
`reducer` should be a true `reducer` functions (i.e., commutative and
associative).
Parameters
----------
reducer : associative, commutative function
This should map
Y_0,Y_1 -> Y'
mapper : function from X -> Y
inputs : list of X
map_step : integer, optional
Number of mapping operations to do in one go.
This is what defines an inner task. (default: 4)
reduce_step : integer, optional
Number of reduce operations to do in one go.
(default: 8)
Returns
-------
task : jug.Task object
'''
reducers = [Task(_jug_map_reduce, reducer, mapper, input_i) for input_i in _break_up(inputs, map_step)]
while len(reducers) > 1:
reducers = [Task(_jug_reduce, reducer, reduce_i) for reduce_i in _break_up(reducers, reduce_step)]
if len(reducers) == 0:
return identity([])
elif len(reducers) == 1:
return reducers[0]
else:
assert False, 'This is a bug'
class block_access_slice(object):
__slots__ = ('base', 'start', 'stop', 'stride', '_hvalue')
def __init__(self, access, orig):
self.base = access
self.start,self.stop,self.stride = orig
self._hvalue = None
def __getitem__(self, p):
if isinstance(p, slice):
start,stop,stride = p.indices(len(self))
return block_access_slice(self.base, (self.start + start, self.stop - (len(self)-stop), self.stride * stride))
elif isinstance(p, int):
p *= self.stride
p += self.start
if p >= self.stop:
raise IndexError
return self.base[p]
else:
raise TypeError
def __len__(self):
return self.stop - self.start
def __jug_hash__(self):
if self._hvalue is not None:
return self._hvalue
self._hvalue = hash_one({
'type': 'map-access-slice',
'base': self.base,
'start': self.start,
'stop': self.stop,
'stride': self.stride,
})
return self._hvalue
def __jug_value__(self):
from .task import value
return [value(self[i]) for i in xrange(len(self))]
class block_access(object):
__slots__ = ('blocks','block_size', 'len','_hvalue')
def __init__(self, blocks, block_size, len):
self.blocks = blocks
self.block_size = block_size
self.len = len
self._hvalue = None
def __getitem__(self, p):
if isinstance(p, slice):
return block_access_slice(self, p.indices(self.len))
elif isinstance(p, int):
if not (0 <= p < self.len):
raise IndexError
b = p//self.block_size
bi = p % self.block_size
return self.blocks[b][bi]
else:
raise TypeError
def __len__(self):
return self.len
def __jug_hash__(self):
if self._hvalue is not None:
return self._hvalue
value = hash_one({
'type': 'map-access',
'len': self.len,
'blocks': self.blocks,
'block_size': self.block_size,
})
self._hvalue = value
return value
def __jug_value__(self):
from .task import value
return [value(self[i]) for i in xrange(len(self))]
def map(mapper, sequence, map_step=4):
'''
sequence' = map(mapper, sequence, map_step=4)
Roughly equivalent to::
sequence' = [Task(mapper,s) for s in sequence]
except that the tasks are grouped in groups of `map_step`
Parameters
----------
mapper : function
function from A -> B
sequence : list of A
map_step : integer, optional
nr of elements to process per task. This should be set so that each
task takes the right amount of time.
Returns
-------
sequence' : list of B
sequence'[i] = mapper(sequence[i])
See Also
--------
mapreduce
currymap: function
Curried version of this function
'''
if map_step == 1:
return [Task(mapper, s) for s in sequence]
blocks = []
n = 0
for ss in _break_up(sequence, map_step):
blocks.append(
Task(_jug_map, _get_function(mapper), ss)
)
n += len(ss)
return block_access(blocks, map_step, n)
def currymap(mapper, sequence, map_step=4):
'''
sequence' = currymap(mapper, sequence, map_step=4)
Roughly equivalent to::
sequence' = [Task(mapper,*s) for s in sequence]
except that the tasks are grouped in groups of `map_step`
Parameters
----------
mapper : function
function from A1 -> A2 ... -> An -> B
sequence : list of (A1,A2,...,An)
map_step : integer, optional
nr of elements to process per task. This should be set so that each
task takes the right amount of time.
Returns
-------
sequence' : list of B
sequence'[i] = mapper(*sequence[i])
See Also
--------
mapreduce: function
map: function
Uncurried version of this function
'''
if map_step == 1:
return [Task(mapper, *s) for s in sequence]
result = []
for ss in _break_up(sequence, map_step):
t = Task(_jug_map_curry, _get_function(mapper), ss)
for i,_ in enumerate(ss):
result.append(t[i])
return result
def reduce(reducer, inputs, reduce_step=8):
'''
task = reduce(reducer, inputs, reduce_step=8)
Parameters
----------
reducer : associative, commutative function
This should map
Y_0,Y_1 -> Y'
inputs : list of X
reduce_step : integer, optional
Number of reduce operations to do in one go.
(default: 8)
Returns
-------
task : jug.Task object
See Also
--------
mapreduce
'''
return mapreduce(reducer, None, inputs, reduce_step=reduce_step)