-
Notifications
You must be signed in to change notification settings - Fork 19
/
clang.py
652 lines (511 loc) · 21.1 KB
/
clang.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# TODO: To be refactored into shared memory lang,
# where you plugin in the sequential shared memory language specific codegen
from raco import algebra
from raco import expression
from raco.language import Language, clangcommon, Algebra
from raco import rules
from raco.pipelines import Pipelined
from raco.language.clangcommon import StagedTupleRef, ct
from raco.algebra import gensym
import logging
_LOG = logging.getLogger(__name__)
import itertools
import os.path
template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"c_templates")
def readtemplate(fname):
return file(os.path.join(template_path, fname)).read()
template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"c_templates")
base_template = readtemplate("base_query.template")
twopass_select_template = readtemplate("precount_select.template")
hashjoin_template = readtemplate("hashjoin.template")
filteringhashjoin_template = ""
filtering_nestedloop_join_chain_template = ""
# =readtemplate("filtering_nestedloop_join_chain.template")
ascii_scan_template = readtemplate("ascii_scan.template")
binary_scan_template = readtemplate("binary_scan.template")
class CStagedTupleRef(StagedTupleRef):
def __additionalDefinitionCode__(self):
constructor_template = """
public:
%(tupletypename)s (relationInfo * rel, int row) {
%(copies)s
}
"""
copytemplate = """_fields[%(fieldnum)s] = \
rel->relation[row*rel->fields + %(fieldnum)s];
"""
copies = ""
# TODO: actually list the trimmed schema offsets
for i in range(0, len(self.scheme)):
fieldnum = i
copies += copytemplate % locals()
tupletypename = self.getTupleTypename()
return constructor_template % locals()
class CC(Language):
@classmethod
def new_relation_assignment(cls, rvar, val):
return """
%s
%s
""" % (cls.relation_decl(rvar), cls.assignment(rvar, val))
@classmethod
def relation_decl(cls, rvar):
return "struct relationInfo *%s;" % rvar
@classmethod
def assignment(cls, x, y):
return "%s = %s;" % (x, y)
@staticmethod
def body(compileResult):
queryexec = compileResult.getExecutionCode()
initialized = compileResult.getInitCode()
declarations = compileResult.getDeclCode()
resultsym = "__result__"
return base_template % locals()
@staticmethod
def pipeline_wrap(ident, code, attrs):
# timing code
if True:
inner_code = code
timing_template = ct("""auto start_%(ident)s = walltime();
%(inner_code)s
auto end_%(ident)s = walltime();
auto runtime_%(ident)s = end_%(ident)s - start_%(ident)s;
std::cout << "pipeline %(ident)s: " << runtime_%(ident)s \
<< " s" \
<< std::endl;
std::cout << "timestamp %(ident)s start " \
<< std::setprecision(15) \
<< start_%(ident)s << std::endl;
std::cout << "timestamp %(ident)s end " \
<< std::setprecision(15) \
<< end_%(ident)s << std::endl;
""")
code = timing_template % locals()
return code
@staticmethod
def group_wrap(ident, grpcode, attrs):
pipeline_template = ct("""
auto start_%(ident)s = walltime();
%(grpcode)s
auto end_%(ident)s = walltime();
auto runtime_%(ident)s = end_%(ident)s - start_%(ident)s;
std::cout << "pipeline group %(ident)s: " \
<< runtime_%(ident)s \
<< " s" << std::endl;
""")
code = pipeline_template % locals()
return code
@staticmethod
def log(txt):
return """std::cout << "%s" << std::endl;
""" % txt
@staticmethod
def log_unquoted(code, level=0):
return """std::cout << %s << std::endl;
""" % code
@staticmethod
def log_file(code, level=0):
return """logfile << "%s" << "\\n";\n """ % code
@staticmethod
def log_file_unquoted(code, level=0):
return """logfile << %s << " ";\n """ % code
@staticmethod
def comment(txt):
return "// %s\n" % txt
nextstrid = 0
@classmethod
def newstringident(cls):
r = """str_%s""" % (cls.nextstrid)
cls.nextstrid += 1
return r
@classmethod
def compile_numericliteral(cls, value):
return '%s' % (value), [], []
@classmethod
def compile_stringliteral(cls, s):
sid = cls.newstringident()
lookup_init = """auto %s = string_index.string_lookup(%s);""" \
% (sid, s)
build_init = """
string_index = build_string_index("sp2bench_1m.index");
"""
return """(%s)""" % sid, [], [build_init, lookup_init]
# raise ValueError("String Literals not supported\
# in C language: %s" % s)
@classmethod
def negation(cls, input):
innerexpr, inits = input
return "(!%s)" % (innerexpr,), [], inits
@classmethod
def negative(cls, input):
innerexpr, decls, inits = input
return "(-%s)" % (innerexpr,), decls, inits
@classmethod
def expression_combine(cls, args, operator="&&"):
opstr = " %s " % operator
conjunc = opstr.join(["(%s)" % arg for arg, _, _ in args])
decls = reduce(lambda sofar, x: sofar + x, [d for _, d, _ in args])
inits = reduce(lambda sofar, x: sofar + x, [d for _, _, d in args])
_LOG.debug("conjunc: %s", conjunc)
return "( %s )" % conjunc, decls, inits
@classmethod
def compile_attribute(cls, expr):
if isinstance(expr, expression.NamedAttributeRef):
raise TypeError(
"Error compiling attribute reference %s. \
C compiler only support unnamed perspective. \
Use helper function unnamed." % expr)
if isinstance(expr, expression.UnnamedAttributeRef):
symbol = expr.tupleref.name
position = expr.position
assert position >= 0
return '%s.get(%s)' % (symbol, position), [], []
class CCOperator(Pipelined):
language = CC
def new_tuple_ref(self, sym, scheme):
return CStagedTupleRef(sym, scheme)
from raco.algebra import UnaryOperator
class CMemoryScan(algebra.UnaryOperator, CCOperator):
def produce(self, state):
self.input.produce(state)
# TODO: when have pipeline tree representation,
# TODO: will have a consumeMaterialized() method instead;
# TODO: for now we reuse the tuple-based consume
def consume(self, inputsym, src, state):
# now generate the scan from memory
# TODO: generate row variable to avoid naming conflict for nested scans
memory_scan_template = """for (uint64_t i : %(inputsym)s->range()) {
%(tuple_type)s %(tuple_name)s(%(inputsym)s, i);
%(inner_plan_compiled)s
} // end scan over %(inputsym)s
"""
stagedTuple = state.lookupTupleDef(inputsym)
tuple_type = stagedTuple.getTupleTypename()
tuple_name = stagedTuple.name
inner_plan_compiled = self.parent.consume(stagedTuple, self, state)
code = memory_scan_template % locals()
state.setPipelineProperty("type", "in_memory")
state.addPipeline(code)
return None
def num_tuples(self):
raise NotImplementedError("{}.num_tuples()".format(op=self.opname()))
def shortStr(self):
return "%s" % (self.opname())
def __eq__(self, other):
"""
For what we are using MemoryScan for, the only use
of __eq__ is in hashtable lookups for CSE optimization.
We omit self.schema because the relation_key determines
the level of equality needed.
@see FileScan.__eq__
"""
return UnaryOperator.__eq__(self, other)
class CGroupBy(algebra.GroupBy, CCOperator):
_i = 0
@classmethod
def __genHashName__(cls):
name = "group_hash_%03d" % cls._i
cls._i += 1
return name
def produce(self, state):
assert len(self.grouping_list) <= 2, \
"%s does not currently support groupings of \
more than 2 attributes" % self.__class__.__name__
assert len(self.aggregate_list) == 1, \
"""%s currently only supports aggregates of 1 attribute
(aggregate_list=%s)""" \
% (self.__class__.__name__, self.aggregate_list)
for agg_term in self.aggregate_list:
assert isinstance(agg_term,
expression.BuiltinAggregateExpression), \
"""%s only supports simple aggregate expressions.
A rule should create Apply[GroupBy]""" \
% self.__class__.__name__
self.useMap = len(self.grouping_list) > 0
if self.useMap:
if len(self.grouping_list) == 1:
declr_template = """std::unordered_map<int64_t, int64_t> \
%(hashname)s;
"""
elif len(self.grouping_list) == 2:
declr_template = """std::unordered_map<\
std::pair<int64_t, int64_t>, int64_t, pairhash> \
%(hashname)s;
"""
else:
declr_template = """int64_t %(hashname)s;
"""
self.hashname = self.__genHashName__()
hashname = self.hashname
hash_declr = declr_template % locals()
state.addDeclarations([hash_declr])
my_sch = self.scheme()
_LOG.debug("aggregates: %s", self.aggregate_list)
_LOG.debug("columns: %s", self.column_list())
_LOG.debug("groupings: %s", self.grouping_list)
_LOG.debug("groupby scheme: %s", my_sch)
_LOG.debug("groupby scheme[0] type: %s", type(my_sch[0]))
self.input.produce(state)
# now that everything is aggregated, produce the tuples
assert (not self.useMap) \
or isinstance(self.column_list()[0],
expression.AttributeRef), \
"assumes first column is the key and " \
"second is aggregate result: %s" % (self.column_list()[0])
if self.useMap:
if len(self.grouping_list) == 1:
produce_template = """for (auto it=%(hashname)s.begin(); \
it!=%(hashname)s.end(); it++) {
%(output_tuple_type)s %(output_tuple_name)s(\
{it->first, it->second});
%(inner_code)s
}
"""
elif len(self.grouping_list) == 2:
produce_template = """for (auto it=%(hashname)s.begin(); \
it!=%(hashname)s.end(); it++) {
%(output_tuple_type)s %(output_tuple_name)s(\
{it->first.first, it->first.second, it->second});
%(inner_code)s
}
"""
else:
produce_template = """{
%(output_tuple_type)s %(output_tuple_name)s({ %(hashname)s });
%(inner_code)s
}
"""
output_tuple = CStagedTupleRef(gensym(), my_sch)
output_tuple_name = output_tuple.name
output_tuple_type = output_tuple.getTupleTypename()
state.addDeclarations([output_tuple.generateDefinition()])
inner_code = self.parent.consume(output_tuple, self, state)
code = produce_template % locals()
state.setPipelineProperty("type", "in_memory")
state.addPipeline(code)
def consume(self, inputTuple, fromOp, state):
if self.useMap:
if len(self.grouping_list) == 1:
materialize_template = """%(op)s_insert(%(hashname)s, \
%(tuple_name)s, %(key1pos)s, %(valpos)s);
"""
elif len(self.grouping_list) == 2:
materialize_template = """%(op)s_insert(%(hashname)s, \
%(tuple_name)s, %(key1pos)s, %(key2pos)s, %(valpos)s);
"""
else:
materialize_template = """%(op)s_insert(%(hashname)s, \
%(tuple_name)s, %(valpos)s);
"""
hashname = self.hashname
tuple_name = inputTuple.name
# make key from grouped attributes
if self.useMap:
inp_sch = self.input.scheme()
key1pos = self.grouping_list[0].get_position(inp_sch)
if len(self.grouping_list) == 2:
key2pos = self.grouping_list[1].get_position(
inp_sch)
if isinstance(self.aggregate_list[0], expression.ZeroaryOperator):
# no value needed for Zero-input aggregate,
# but just provide the first column
valpos = 0
elif isinstance(self.aggregate_list[0], expression.UnaryOperator):
# get value positions from aggregated attributes
valpos = self.aggregate_list[0].input.get_position(self.scheme())
else:
assert False, "only support Unary or Zeroary aggregates"
op = self.aggregate_list[0].__class__.__name__
code = materialize_template % locals()
return code
class CHashJoin(algebra.Join, CCOperator):
_i = 0
@classmethod
def __genHashName__(cls):
name = "hash_%03d" % cls._i
cls._i += 1
return name
def produce(self, state):
if not isinstance(self.condition, expression.EQ):
msg = "The C compiler can only handle equi-join conditions of \
a single attribute: %s" % self.condition
raise ValueError(msg)
left_sch = self.left.scheme()
# find the attribute that corresponds to the right child
self.rightCondIsRightAttr = \
self.condition.right.position >= len(left_sch)
self.leftCondIsRightAttr = \
self.condition.left.position >= len(left_sch)
assert self.rightCondIsRightAttr ^ self.leftCondIsRightAttr
# find the attribute that corresponds to the right child
if self.rightCondIsRightAttr:
self.right_keypos = \
self.condition.right.position - len(left_sch)
else:
self.right_keypos = \
self.condition.left.position - len(left_sch)
# find the attribute that corresponds to the left child
if self.rightCondIsRightAttr:
self.left_keypos = self.condition.left.position
else:
self.left_keypos = self.condition.right.position
self.right.childtag = "right"
# common index is defined by same right side and same key
hashsym = state.lookupExpr((self.right, self.right_keypos))
if not hashsym:
# if right child never bound then store hashtable symbol and
# call right child produce
self._hashname = self.__genHashName__()
_LOG.debug("generate hashname %s for %s", self._hashname, self)
state.saveExpr((self.right, self.right_keypos), self._hashname)
self.right.produce(state)
else:
# if found a common subexpression on right child then
# use the same hashtable
self._hashname = hashsym
_LOG.debug("reuse hash %s for %s", self._hashname, self)
self.left.childtag = "left"
self.left.produce(state)
def consume(self, t, src, state):
if src.childtag == "right":
declr_template = """std::unordered_map\
<int64_t, std::vector<%(in_tuple_type)s>* > %(hashname)s;
"""
right_template = """insert(%(hashname)s, %(keyname)s, %(keypos)s);
"""
hashname = self._hashname
keyname = t.name
keypos = self.right_keypos
in_tuple_type = t.getTupleTypename()
# declaration of hash map
hashdeclr = declr_template % locals()
state.addDeclarations([hashdeclr])
# materialization point
code = right_template % locals()
return code
if src.childtag == "left":
left_template = """
for (auto %(right_tuple_name)s : \
lookup(%(hashname)s, %(keyname)s.get(%(keypos)s))) {
auto %(out_tuple_name)s = \
combine<%(out_tuple_type)s> (%(keyname)s, %(right_tuple_name)s);
%(inner_plan_compiled)s
}
"""
hashname = self._hashname
keyname = t.name
keytype = t.getTupleTypename()
keypos = self.left_keypos
right_tuple_name = gensym()
outTuple = CStagedTupleRef(gensym(), self.scheme())
out_tuple_type_def = outTuple.generateDefinition()
out_tuple_type = outTuple.getTupleTypename()
out_tuple_name = outTuple.name
state.addDeclarations([out_tuple_type_def])
inner_plan_compiled = self.parent.consume(outTuple, self, state)
code = left_template % locals()
return code
assert False, "src not equal to left or right"
def indentby(code, level):
indent = " " * ((level + 1) * 6)
return "\n".join([indent + line for line in code.split("\n")])
# iteration over table + insertion into hash table with filter
class CUnionAll(clangcommon.CUnionAll, CCOperator):
pass
class CApply(clangcommon.CApply, CCOperator):
pass
class CProject(clangcommon.CProject, CCOperator):
pass
class CSelect(clangcommon.CSelect, CCOperator):
pass
class CFileScan(clangcommon.CFileScan, CCOperator):
def __get_ascii_scan_template__(self):
return ascii_scan_template
def __get_binary_scan_template__(self):
return binary_scan_template
class CStore(clangcommon.BaseCStore, CCOperator):
def __file_code__(self, t, state):
code = ""
state.addPreCode('std::ofstream logfile;\n')
resultfile = str(self.relation_key).split(":")[2]
opentuple = 'logfile.open("%s");\n' % resultfile
schemafile = self.write_schema(self.scheme())
state.addPreCode(schemafile)
state.addPreCode(opentuple)
code += "int logi = 0;\n"
code += "for (logi = 0; logi < %s.numFields() - 1; logi++) {\n" \
% (t.name)
code += self.language.log_file_unquoted("%s.get(logi)" % t.name)
code += "}\n "
code += "logfile << %s.get(logi);\n" % (t.name)
code += "logfile << '\\n';"
state.addPostCode('logfile.close();')
return code
def write_schema(self, scheme):
schemafile = 'schema/' + str(self.relation_key).split(":")[2]
code = 'logfile.open("%s");\n' % schemafile
names = [x.encode('UTF8') for x in scheme.get_names()]
code += self.language.log_file("%s" % names)
code += self.language.log_file("%s" % scheme.get_types())
code += 'logfile.close();'
return code
class MemoryScanOfFileScan(rules.Rule):
"""A rewrite rule for making a scan into
materialization in memory then memory scan"""
def fire(self, expr):
if isinstance(expr, algebra.Scan) and not isinstance(expr, CFileScan):
return CMemoryScan(CFileScan(expr.relation_key, expr.scheme()))
return expr
def __str__(self):
return "Scan => MemoryScan[FileScan]"
def clangify(emit_print):
return [
rules.ProjectingJoinToProjectOfJoin(),
rules.OneToOne(algebra.Select, CSelect),
MemoryScanOfFileScan(),
rules.OneToOne(algebra.Apply, CApply),
rules.OneToOne(algebra.Join, CHashJoin),
rules.OneToOne(algebra.GroupBy, CGroupBy),
rules.OneToOne(algebra.Project, CProject),
rules.OneToOne(algebra.UnionAll, CUnionAll),
# TODO: obviously breaks semantics
rules.OneToOne(algebra.Union, CUnionAll),
clangcommon.StoreToBaseCStore(emit_print, CStore),
clangcommon.BreakHashJoinConjunction(CSelect, CHashJoin)
]
class CCAlgebra(Algebra):
language = CC
def __init__(self, emit_print=clangcommon.EMIT_CONSOLE):
""" To store results into a file or onto console """
self.emit_print = emit_print
def opt_rules(self, **kwargs):
# Sequence that works for datalog
# TODO: replace with below
# datalog_rules = [
# rules.CrossProduct2Join(),
# rules.SimpleGroupBy(),
# rules.OneToOne(algebra.Select, CSelect),
# MemoryScanOfFileScan(),
# rules.OneToOne(algebra.Apply, CApply),
# rules.OneToOne(algebra.Join, CHashJoin),
# rules.OneToOne(algebra.GroupBy, CGroupBy),
# rules.OneToOne(algebra.Project, CProject),
# TODO: obviously breaks semantics
# rules.OneToOne(algebra.Union, CUnionAll),
# rules.FreeMemory()
# ]
# sequence that works for myrial
rule_grps_sequence = [
rules.remove_trivial_sequences,
rules.simple_group_by,
clangcommon.clang_push_select,
[rules.ProjectToDistinctColumnSelect(),
rules.JoinToProjectingJoin()],
rules.push_apply,
clangify(self.emit_print)
]
if kwargs.get('SwapJoinSides'):
rule_grps_sequence.insert(0, [rules.SwapJoinSides()])
return list(itertools.chain(*rule_grps_sequence))