-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoGenerateIter.py
345 lines (328 loc) · 13.7 KB
/
AutoGenerateIter.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
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
import os
import sys
supportedTypes = [ 'int', 'float', 'double', 'long', 'pair', 'ppair', 'ipair', 'svec', 'ivec', 'fsvec', 'psvec' ];
class IterTypeVisitor:
def write(self, typ, fp):
for line in self.process(typ):
fp.write(line+'\n')
def process(self, typ):
if typ == 'ipair':
return self.processIPair()
elif typ == 'pair':
return self.processPair()
elif typ == 'ppair':
return self.processPPair()
elif typ == 'svec':
return self.processSvec()
elif typ == 'ivec':
return self.processIvec()
elif typ == 'fsvec':
return self.processFsvec()
elif typ == 'psvec':
return self.processPsvec()
else:
try:
return self.processPrimitive(typ)
except NotImplementedError:
if typ == 'int':
return self.processInt()
elif typ == 'float':
return self.processFloat()
elif typ == 'double':
return self.processDouble()
elif typ == 'long':
return self.processLong()
else:
raise RuntimeError("Unsupported type "+typ)
def processPrimitive(self, typ):
raise NotImplementedError("Unimplemented primitive processor")
def processInt(self):
raise NotImplementedError("Unimplemented int processor")
def processFloat(self):
raise NotImplementedError("Unimplemented float processor")
def processDouble(self):
raise NotImplementedError("Unimplemented double processor")
def processLong(self):
raise NotImplementedError("Unimplemented long processor")
def processPair(self):
raise NotImplementedError("Unimplemented pair processor")
def processPPair(self):
raise NotImplementedError("Unimplemented pair processor")
def processIPair(self):
raise NotImplementedError("Unimplemented ipair processor")
def processSvec(self):
raise NotImplementedError("Unimplemented svec processor")
def processPsvec(self):
raise NotImplementedError("Unimplemented psvec processor")
def processIvec(self):
raise NotImplementedError("Unimplemented ivec processor")
def processFsvec(self):
raise NotImplementedError("Unimplemented fsvec processor")
class NativeToJavaVisitor(IterTypeVisitor):
def processPrimitive(self, typ):
return typ.capitalize()
def processPair(self):
return 'Pair'
def processPPair(self):
return 'PPair'
def processIPair(self):
return 'UPair'
def processSvec(self):
return 'Svec'
def processPsvec(self):
return 'Psvec'
def processIvec(self):
return 'Ivec'
def processFsvec(self):
return 'Fsvec'
class ImportVisitor(IterTypeVisitor):
def processPrimitive(self, typ):
return ['' ]
def processPair(self):
return ['' ]
def processPPair(self):
return ['' ]
def processIPair(self):
return ['' ]
def processSvec(self):
return ['import java.util.HashMap;', 'import java.util.List;',
'import java.util.ArrayList;' ]
def processPsvec(self):
return ['import java.util.HashMap;', 'import java.util.List;',
'import java.util.ArrayList;' ]
def processIvec(self):
return [ 'import java.util.ArrayList;' ]
def processFsvec(self):
return ['import java.util.HashMap;', 'import java.util.List;',
'import java.util.ArrayList;' ]
class FieldDeclarationVisitor(IterTypeVisitor):
def processPrimitive(self, typ):
return [ ' private '+typ+'[] vals;', ' private int len;',
' private int currentIndex;' ]
def processPair(self):
return [ ' private double[] vals1;', ' private double[] vals2;',
' int len;', ' private int currentIndex;' ]
def processPPair(self):
return [ ' private int[] vals1;', ' private double[] vals2;',
' int len;', ' private int currentIndex;' ]
def processIPair(self):
return [ ' private int[] valIds;', ' private double[] vals1;',
' private double[] vals2;', ' int len;',
' private int currentIndex;' ]
def processSvec(self):
return [ ' private final List<int[]> indices;',
' private final List<double[]> vals;',
' private int currentIndex;', ' private int len;' ]
def processPsvec(self):
return [ ' private final double[] probs;',
' private final List<int[]> indices;',
' private final List<double[]> vals;',
' private int currentIndex;', ' private int len;' ]
def processIvec(self):
return [ ' private final List<int[]> vals;',
' private int currentIndex;', ' private int len;' ]
def processFsvec(self):
return [ ' private final List<int[]> indices;',
' private final List<float[]> vals;',
' private int currentIndex;', ' private int len;' ]
class ConstructorVisitor(IterTypeVisitor):
def processPrimitive(self, typ):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process(typ)+'ValueIterator('+typ+'[] setVals, int setLen) {')
lines.append(' this.vals = setVals; this.len = setLen;')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processPair(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('pair')+'ValueIterator(double[] setVals1, double[] setVals2, int setLen) {')
lines.append(' this.vals1 = setVals1; this.vals2 = setVals2; this.len = setLen;')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processPPair(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('ppair')+'ValueIterator(int[] setVals1, double[] setVals2, int setLen) {')
lines.append(' this.vals1 = setVals1; this.vals2 = setVals2; this.len = setLen;')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processIPair(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('ipair')+'ValueIterator(int[] setValIds, double[] setVals1, double[] setVals2, int setLen) {')
lines.append(' this.valIds = setValIds; this.vals1 = setVals1; this.vals2 = setVals2; this.len = setLen;')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processSvec(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('svec')+
"""ValueIterator(List<int[]> indices,
List<double[]> vals) {""")
lines.append(' this.indices = indices;')
lines.append(' this.vals = vals;')
lines.append(' this.len = indices.size();')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processPsvec(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('psvec')+
"""ValueIterator(double[] probs, List<int[]> indices,
List<double[]> vals) {""")
lines.append(' this.probs = probs;')
lines.append(' this.indices = indices;')
lines.append(' this.vals = vals;')
lines.append(' this.len = indices.size();')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processIvec(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('ivec')+
"""ValueIterator(List<int[]> vals) {""")
lines.append(' this.vals = vals;')
lines.append(' this.len = vals.size();')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
def processFsvec(self):
lines = [ ]
lines.append(' public HadoopCL'+NativeToJavaVisitor().process('fsvec')+
"""ValueIterator(List<int[]> indices,
List<float[]> vals) {""")
lines.append(' this.indices = indices;')
lines.append(' this.vals = vals;')
lines.append(' this.len = indices.size();')
lines.append(' this.currentIndex = 0;')
lines.append(' }')
return lines
class GetterVisitor(IterTypeVisitor):
def processPrimitive(self, typ):
lines = [ ]
lines.append(' public '+typ+' get() {')
lines.append(' return this.vals[this.currentIndex];')
lines.append(' }')
return lines
def processPair(self):
lines = [ ]
lines.append(' public double getVal1() {')
lines.append(' return this.vals1[this.currentIndex];')
lines.append(' }')
lines.append('')
lines.append(' public double getVal2() {')
lines.append(' return this.vals2[this.currentIndex];')
lines.append(' }')
return lines
def processPPair(self):
lines = [ ]
lines.append(' public int getVal1() {')
lines.append(' return this.vals1[this.currentIndex];')
lines.append(' }')
lines.append('')
lines.append(' public double getVal2() {')
lines.append(' return this.vals2[this.currentIndex];')
lines.append(' }')
return lines
def processIPair(self):
lines = [ ]
lines.append(' public int getValId() {')
lines.append(' return this.valIds[this.currentIndex];')
lines.append(' }')
lines.append('')
lines.append(' public double getVal1() {')
lines.append(' return this.vals1[this.currentIndex];')
lines.append(' }')
lines.append('')
lines.append(' public double getVal2() {')
lines.append(' return this.vals2[this.currentIndex];')
lines.append(' }')
return lines
def processSvec(self):
lines = [ ]
lines.append(' public int[] getValIndices() {')
lines.append(' return this.indices.get(this.currentIndex);')
lines.append(' }')
lines.append('')
lines.append(' public double[] getValVals() {')
lines.append(' return this.vals.get(this.currentIndex);')
lines.append(' }')
return lines
def processPsvec(self):
lines = [ ]
lines.append(' public double getProb() {')
lines.append(' return this.probs[this.currentIndex];')
lines.append(' }')
lines.append(' public int[] getValIndices() {')
lines.append(' return this.indices.get(this.currentIndex);')
lines.append(' }')
lines.append('')
lines.append(' public double[] getValVals() {')
lines.append(' return this.vals.get(this.currentIndex);')
lines.append(' }')
return lines
def processIvec(self):
lines = [ ]
lines.append(' public int[] getArray() {')
lines.append(' return this.vals.get(this.currentIndex);')
lines.append(' }')
return lines
def processFsvec(self):
lines = [ ]
lines.append(' public int[] getValIndices() {')
lines.append(' return this.indices.get(this.currentIndex);')
lines.append(' }')
lines.append('')
lines.append(' public float[] getValVals() {')
lines.append(' return this.vals.get(this.currentIndex);')
lines.append(' }')
return lines
if len(sys.argv) != 2:
print 'usage: python AutoGenerateIter.py type'
sys.exit(1)
typ = sys.argv[1]
if not typ in supportedTypes:
print 'Unsupported type '+typ
sys.exit(1)
fp = open('mapred/org/apache/hadoop/mapreduce/HadoopCL'+NativeToJavaVisitor().process(typ)+'ValueIterator.java', 'w')
fp.write('package org.apache.hadoop.mapreduce;\n')
fp.write('\n')
ImportVisitor().write(typ, fp)
fp.write('\n')
fp.write('public class HadoopCL'+NativeToJavaVisitor().process(typ)+'ValueIterator {\n')
FieldDeclarationVisitor().write(typ, fp)
fp.write('\n')
ConstructorVisitor().write(typ, fp)
fp.write('\n')
fp.write(' public boolean next() {\n')
fp.write(' if (this.currentIndex == this.len-1) return false;\n')
fp.write(' this.currentIndex = this.currentIndex + 1;\n')
fp.write(' return true;\n')
fp.write(' }\n')
fp.write('\n')
fp.write(' public boolean seekTo(int set) {\n')
fp.write(' if (set >= this.len) return false;\n')
fp.write(' this.currentIndex = set;\n')
fp.write(' return true;\n')
fp.write(' }\n')
fp.write('\n')
fp.write(' public int current() {\n')
fp.write(' return this.currentIndex;\n')
fp.write(' }\n')
fp.write('\n')
fp.write(' public int nValues() {\n')
fp.write(' return this.len;\n')
fp.write(' }\n')
fp.write('\n')
GetterVisitor().write(typ, fp)
fp.write('\n')
if typ == 'svec' or typ == 'ivec' or typ == 'fsvec' or typ == 'psvec':
fp.write(' public int vectorLength(int index) {\n')
fp.write(' return this.indices.get(index).length;\n')
fp.write(' }\n')
fp.write('\n')
fp.write(' public int currentVectorLength() {\n')
fp.write(' return vectorLength(this.currentIndex);\n')
fp.write(' }\n')
fp.write('}\n')
fp.close()