forked from antonmks/Alenka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbison.y
1795 lines (1412 loc) · 47.3 KB
/
bison.y
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
%{
#include <stdlib.h>
#include <stack>
#include "lex.yy.c"
#include "cm.cu"
void clean_queues();
void order_inplace(CudaSet* a, stack<string> exe_type);
void yyerror(char *s, ...);
void emit(char *s, ...);
void emit_mul();
void emit_add();
void emit_minus();
void emit_div();
void emit_and();
void emit_eq();
void emit_or();
void emit_cmp(int val);
void emit_var(char *s, int c, char *f);
void emit_var_asc(char *s);
void emit_var_desc(char *s);
void emit_name(char *name);
void emit_count();
void emit_sum();
void emit_average();
void emit_min();
void emit_max();
void emit_string(char *str);
void emit_number(int_type val);
void emit_float(float_type val);
void emit_decimal(float_type val);
void emit_sel_name(char* name);
void emit_limit(int val);
void emit_union(char *s, char *f1, char *f2);
void emit_varchar(char *s, int c, char *f, int d);
void emit_load(char *s, char *f, int d, char* sep, bool stream);
void emit_load_binary(char *s, char *f, int d, bool stream);
void emit_store(char *s, char *f, char* sep);
void emit_store_binary(char *s, char *f, char* sep);
void emit_store_binary(char *s, char *f);
void emit_filter(char *s, char *f, int e);
void emit_order(char *s, char *f, int e, int ll = 0);
void emit_group(char *s, char *f, int e);
void emit_select(char *s, char *f, int ll);
void emit_join(char *s, char *j1);
void emit_join_tab(char *s);
void emit_distinct(char *s, char *f);
%}
%union {
int intval;
float floatval;
char *strval;
int subtok;
}
%token <strval> FILENAME
%token <strval> NAME
%token <strval> STRING
%token <intval> INTNUM
%token <intval> DECIMAL
%token <intval> BOOL
%token <floatval> APPROXNUM
/* user @abc names */
%token <strval> USERVAR
/* operators and precedence levels */
%right ASSIGN
%right EQUAL
%left OR
%left XOR
%left AND
%nonassoc IN IS LIKE REGEXP
%left NOT '!'
%left BETWEEN
%left <subtok> COMPARISON /* = <> < > <= >= <=> */
%left '|'
%left '&'
%left <subtok> SHIFT /* << >> */
%left '+' '-'
%left '*' '/' '%' MOD
%left '^'
%nonassoc UMINUS
%token AND
%token OR
%token LOAD
%token STREAM
%token FILTER
%token BY
%token JOIN
%token STORE
%token INTO
%token GROUP
%token FROM
%token SELECT
%token AS
%token ORDER
%token ASC
%token DESC
%token COUNT
%token USING
%token SUM
%token AVG
%token MIN
%token MAX
%token LIMIT
%token ON
%token BINARY
%type <intval> load_list opt_where opt_limit
%type <intval> val_list opt_val_list expr_list opt_group_list join_list
%start stmt_list
%%
/* Grammar rules and actions follow. */
stmt_list:
stmt ';'
| stmt_list stmt ';'
;
stmt:
select_stmt { emit("STMT"); }
;
select_stmt:
NAME ASSIGN SELECT expr_list FROM NAME opt_group_list
{ emit_select($1, $6, $7); } ;
| NAME ASSIGN LOAD FILENAME USING '(' FILENAME ')' AS '(' load_list ')'
{ emit_load($1, $4, $11, $7, 0); } ;
| NAME ASSIGN STREAM FILENAME USING '(' FILENAME ')' AS '(' load_list ')'
{ emit_load($1, $4, $11, $7, 1); } ;
| NAME ASSIGN STREAM FILENAME BINARY AS '(' load_list ')'
{ emit_load_binary($1, $4, $8, 1); } ;
| NAME ASSIGN LOAD FILENAME BINARY AS '(' load_list ')'
{ emit_load_binary($1, $4, $8, 0); } ;
| NAME ASSIGN FILTER NAME opt_where
{ emit_filter($1, $4, $5);}
| NAME ASSIGN ORDER NAME BY opt_val_list
{ emit_order($1, $4, $6);}
| NAME ASSIGN SELECT expr_list FROM NAME join_list
{ emit_join($1,$6); }
| STORE NAME INTO FILENAME USING '(' FILENAME ')' opt_limit
{ emit_store($2,$4,$7); }
| STORE NAME INTO FILENAME opt_limit BINARY
{ emit_store_binary($2,$4); }
;
expr:
NAME { emit_name($1); }
| NAME '.' NAME { emit("FIELDNAME %s.%s", $1, $3); }
| USERVAR { emit("USERVAR %s", $1); }
| STRING { emit_string($1); }
| INTNUM { emit_number($1); }
| APPROXNUM { emit_float($1); }
| DECIMAL { emit_decimal($1); }
| BOOL { emit("BOOL %d", $1); }
| NAME '{' INTNUM '}' ':' NAME '(' INTNUM ')' { emit_varchar($1, $3, $6, $8);}
| NAME '{' INTNUM '}' ':' NAME { emit_var($1, $3, $6);}
| NAME ASC { emit_var_asc($1);}
| NAME DESC { emit_var_desc($1);}
| COUNT '(' expr ')' { emit_count(); }
| SUM '(' expr ')' { emit_sum(); }
| AVG '(' expr ')' { emit_average(); }
| MIN '(' expr ')' { emit_min(); }
| MAX '(' expr ')' { emit_max(); }
;
expr:
expr '+' expr { emit_add(); }
| expr '-' expr { emit_minus(); }
| expr '*' expr { emit_mul(); }
| expr '/' expr { emit_div(); }
| expr '%' expr { emit("MOD"); }
| expr MOD expr { emit("MOD"); }
/*| '-' expr %prec UMINUS { emit("NEG"); }*/
| expr AND expr { emit_and(); }
| expr EQUAL expr { emit_eq(); }
| expr OR expr { emit_or(); }
| expr XOR expr { emit("XOR"); }
| expr SHIFT expr { emit("SHIFT %s", $2==1?"left":"right"); }
| NOT expr { emit("NOT"); }
| '!' expr { emit("NOT"); }
| expr COMPARISON expr { emit_cmp($2); }
/* recursive selects and comparisons thereto */
| expr COMPARISON '(' select_stmt ')' { emit("CMPSELECT %d", $2); }
| '(' expr ')' {emit("EXPR");}
;
expr:
expr IS BOOL { emit("ISBOOL %d", $3); }
| expr IS NOT BOOL { emit("ISBOOL %d", $4); emit("NOT"); }
;
opt_group_list: { /* nil */
$$ = 0;
}
| GROUP BY val_list { $$ = $3}
expr_list:
expr AS NAME { $$ = 1; emit_sel_name($3);}
| expr_list ',' expr AS NAME { $$ = $1 + 1; emit_sel_name($5);}
;
load_list:
expr { $$ = 1; }
| load_list ',' expr {$$ = $1 + 1; }
;
val_list:
expr { $$ = 1; }
| expr ',' val_list { $$ = 1 + $3; }
;
opt_val_list: { /* nil */
$$ = 0
} | val_list;
opt_where:
BY expr { emit("FILTER BY"); };
join_list:
JOIN NAME ON expr { $$ = 1; emit_join_tab($2);}
| JOIN NAME ON expr join_list { $$ = 1; emit_join_tab($2); };
opt_limit: { /* nil */
$$ = 0
}
| LIMIT INTNUM { emit_limit($2); };
%%
struct float_to_decimal
{
__host__ __device__
float_type operator()(const float_type x)
{
return (int_type)(x*100);
}
};
#include <iostream>
#include <queue>
#include <string>
#include <map>
#include <set>
#include "join.cu"
#include "filter.cu"
#include "select.cu"
#include "merge.cu"
#include "zone_map.cu"
#ifdef _WIN64
#else
#define _FILE_OFFSET_BITS 64
#endif
#ifdef _WIN64
#define fseeko _fseeki64
#define ftello _ftelli64
#else
#define fseeko fseek
#define ftello ftell
typedef long off_t;
#endif
bool fact_file_exists = 0;
FILE *file_pointer;
string fact_file_name = "NULL";
long long int stream_pos = 0;
unsigned int lc = 0;
queue<string> namevars;
queue<string> typevars;
queue<int> sizevars;
queue<int> cols;
queue<string> op_type;
queue<string> op_value;
queue<int_type> op_nums;
queue<float_type> op_nums_f;
queue<unsigned int> j_col_count;
unsigned int sel_count = 0;
unsigned int join_cnt = 0;
int join_col_cnt = 0;
unsigned int eqq = 0;
stack<string> op_join;
// STL map to manage CudaSet variables
map<string,CudaSet*> varNames;
unsigned int orig_recCount;
unsigned int statement_count = 0;
map<string,unsigned int> stat;
bool scan_state = 0;
void emit_name(char *name)
{
op_type.push("NAME");
op_value.push(name);
}
void emit_limit(int val)
{
op_nums.push(val);
}
void emit_string(char *str)
{ // remove the float_type quotes
string sss(str,1, strlen(str)-2);
op_type.push("STRING");
op_value.push(sss);
}
void emit_number(int_type val)
{
op_type.push("NUMBER");
op_nums.push(val);
}
void emit_float(float_type val)
{
op_type.push("FLOAT");
op_nums_f.push(val);
}
void emit_decimal(float_type val)
{
op_type.push("DECIMAL");
op_nums_f.push(val);
}
void emit_mul()
{
op_type.push("MUL");
}
void emit_add()
{
op_type.push("ADD");
}
void emit_div()
{
op_type.push("DIV");
}
void emit_and()
{
op_type.push("AND");
if (join_col_cnt == -1)
join_col_cnt++;
join_col_cnt++;
eqq = 0;
}
void emit_eq()
{
//op_type.push("JOIN");
eqq++;
join_cnt++;
if(eqq == join_col_cnt+1) {
j_col_count.push(join_col_cnt+1);
join_col_cnt = -1;
}
else
if (join_col_cnt == -1 )
j_col_count.push(1);
}
void emit_or()
{
op_type.push("OR");
}
void emit_minus()
{
op_type.push("MINUS");
}
void emit_cmp(int val)
{
op_type.push("CMP");
op_nums.push(val);
}
void emit(char *s, ...)
{
}
void emit_var(char *s, int c, char *f)
{
namevars.push(s);
typevars.push(f);
sizevars.push(0);
cols.push(c);
}
void emit_var_asc(char *s)
{
op_type.push(s);
op_value.push("ASC");
}
void emit_var_desc(char *s)
{
op_type.push(s);
op_value.push("DESC");
}
void emit_varchar(char *s, int c, char *f, int d)
{
namevars.push(s);
typevars.push(f);
sizevars.push(d);
cols.push(c);
}
void emit_sel_name(char *s)
{
op_type.push("emit sel_name");
op_value.push(s);
sel_count++;
}
void emit_count()
{
op_type.push("COUNT");
}
void emit_sum()
{
op_type.push("SUM");
}
void emit_average()
{
op_type.push("AVG");
}
void emit_min()
{
op_type.push("MIN");
}
void emit_max()
{
op_type.push("MAX");
}
void emit_join_tab(char *s)
{
op_join.push(s);
};
thrust::device_ptr<unsigned int> order_inplace(CudaSet* a, stack<string> exe_type, set<string> field_names)
{
thrust::device_ptr<unsigned int> permutation = thrust::device_malloc<unsigned int>(a->mRecCount);
thrust::sequence(permutation, permutation+(a->mRecCount));
unsigned int* raw_ptr = thrust::raw_pointer_cast(permutation);
void* temp;
CUDA_SAFE_CALL(cudaMalloc((void **) &temp, a->mRecCount*float_size));
for(int i=0; !exe_type.empty(); ++i, exe_type.pop()) {
int colInd = (a->columnNames).find(exe_type.top())->second;
if ((a->type)[colInd] == 0)
update_permutation((int_type*)(a->d_columns)[colInd], raw_ptr, a->mRecCount, "ASC", (int_type*)temp);
else if ((a->type)[colInd] == 1)
update_permutation((float_type*)(a->d_columns)[colInd], raw_ptr, a->mRecCount,"ASC", (float_type*)temp);
else {
CudaChar* c = (CudaChar*)(a->h_columns)[colInd];
for(int j=(c->mColumnCount)-1; j>=0 ; j--)
update_permutation_char((c->d_columns)[j], raw_ptr, a->mRecCount, (char*)temp, "ASC");
};
};
for (set<string>::iterator it=field_names.begin(); it!=field_names.end(); ++it) {
int i = a->columnNames[*it];
if ((a->type)[i] == 0)
apply_permutation((int_type*)(a->d_columns)[i], raw_ptr, a->mRecCount, (int_type*)temp);
else if ((a->type)[i] == 1)
apply_permutation((float_type*)(a->d_columns)[i], raw_ptr, a->mRecCount, (float_type*)temp);
else {
CudaChar* c = (CudaChar*)(a->h_columns)[i];
for(int j=(c->mColumnCount)-1; j>=0 ; j--)
apply_permutation_char((c->d_columns)[j], raw_ptr, a->mRecCount, (char*)temp);
};
};
cudaFree(temp);
return permutation;
}
void emit_join(char *s, char *j1)
{
string j2 = op_join.top();
op_join.pop();
statement_count++;
if (scan_state == 0) {
if (stat.find(j1) == stat.end()) {
cout << "Join : couldn't find variable " << j1 << endl;
exit(1);
};
if (stat.find(j2) == stat.end()) {
cout << "Join : couldn't find variable " << j2 << endl;
exit(1);
};
stat[s] = statement_count;
stat[j1] = statement_count;
stat[j2] = statement_count;
return;
};
if(varNames.find(j1) == varNames.end() || varNames.find(j2) == varNames.end()) {
clean_queues();
return;
};
// while(!op_join.empty()) {
// cout << "JOIN TBL " << op_join.top() << endl;
// op_join.pop();
// };
//cout << "join cnt " << join_cnt << endl;
//while(!op_value.empty()) {
// cout << "JOIN COL " << op_value.front() << endl;;
// op_value.pop();
//};
//while(!j_col_count.empty()) {
// cout << "JOIN AND " << j_col_count.front() << endl;;
// j_col_count.pop();
//};
CudaSet* left = varNames.find(j1)->second;
CudaSet* right = varNames.find(j2)->second;
if(left->readyToProcess == 0 || right->readyToProcess == 0)
return;
if (left->fact_table == 0 && right->fact_table == 0 && lc > 1)
return;
queue<string> op_sel;
queue<string> op_sel_as;
for(int i=0; i < sel_count; i++) {
op_sel.push(op_value.front());
op_value.pop();
op_sel_as.push(op_value.front());
op_value.pop();
};
string f1 = op_value.front();
op_value.pop();
string f2 = op_value.front();
op_value.pop();
printf("emit join: %s %s \n", s, j1);
std::clock_t start1 = std::clock();
CudaSet* c;
if (left->mRecCount == 0 || right->mRecCount == 0) {
c = new CudaSet(left,right,0, op_sel, op_sel_as);
c->readyToProcess = 1;
if (left->fact_table == 1 || right->fact_table == 1)
c->fact_table = 1;
varNames[s] = c;
clean_queues();
return;
};
unsigned int colInd1 = (left->columnNames).find(f1)->second;
unsigned int colInd2 = (right->columnNames).find(f2)->second;
bool left_in_gpu = 0;
bool right_in_gpu = 0;
if(left->d_columns[0] != 0)
left_in_gpu = 1;
if(right->d_columns[0] != 0)
right_in_gpu = 1;
//cout << "in gpu " << left_in_gpu << " " << right_in_gpu << endl;
//cout << "facts " << left->fact_table << " " << right->fact_table << endl;
set<string> field_names;
stack<string> exe_type;
exe_type.push(f2);
field_names.insert(f2);
// check if already sorted
//cout << "alloced " << left->maxRecs << " " << right->mRecCount << endl;
//cout << "seg counts " << left->segCount << " " << right->segCount << endl;
//right->Store("f1.txt","|",1000000,0);
if(!right_in_gpu) {
right->allocColumnOnDevice(colInd2, right->mRecCount);
right->CopyColumnToGpu(colInd2);
};
thrust::device_ptr<int_type> r((int_type*)(right->d_columns[colInd2]));
if(lc == 1 || right->fact_table) {
thrust::device_ptr<int_type> r((int_type*)(right->d_columns[colInd2]));
if(!thrust::is_sorted(r, r+right->mRecCount)) {
thrust::device_ptr<unsigned int> perm = order_inplace(right, exe_type, field_names);
unsigned int* raw_ptr = thrust::raw_pointer_cast(perm);
void* temp;
cudaMalloc((void **) &temp, right->mRecCount*float_size);
for(int i = 0; i < right->mColumnCount; i++) {
if(i != colInd2 && !right_in_gpu) {
right->allocColumnOnDevice(i, right->mRecCount);
right->CopyColumnToGpu(i);
};
if ((right->type)[i] == 0 && i != colInd2)
apply_permutation((int_type*)(right->d_columns)[i], raw_ptr, right->mRecCount, (int_type*)temp);
else if ((right->type)[i] == 1 && i != colInd2)
apply_permutation((float_type*)(right->d_columns)[i], raw_ptr, right->mRecCount, (float_type*)temp);
else if (i != colInd2) {
CudaChar* c = (CudaChar*)(right->h_columns)[i];
for(int j=(c->mColumnCount)-1; j>=0 ; j--)
apply_permutation_char((c->d_columns)[j], raw_ptr, right->mRecCount, (char*)temp);
};
if(i != colInd2 && !right_in_gpu) {
right->CopyColumnToHost(i);
right->deAllocColumnOnDevice(i);
};
};
thrust::device_free(perm);
cudaFree(temp);
};
};
if (!left_in_gpu)
left->allocColumnOnDevice(colInd1, left->maxRecs);
//cout << "alloc max segment " << left->maxRecs << endl;
thrust::device_vector<unsigned int> d_res1;
thrust::device_vector<unsigned int> d_res2;
for(int i = 0; i < left->segCount; i ++) { // Main piece cycle
// cout << "cycle start " << getFreeMem() << endl;
if (!left_in_gpu )
left->CopyColumnToGpu(colInd1, i);
std::clock_t start2 = std::clock();
cout << "right:left " << right->mRecCount << " " << left->mRecCount << endl;
//cout << "right col is unique : " << right->isUnique(colInd2) << endl;
if ((left->type)[colInd1] == 0 && (right->type)[colInd2] == 0) {
join((int_type*)(right->d_columns)[colInd2], (int_type*)(left->d_columns)[colInd1],
d_res1, d_res2, left->mRecCount, right->mRecCount, right->isUnique(colInd2));
}
else if ((left->type)[colInd1] == 2 && (right->type)[colInd2] == 2)
join((CudaChar*)(right->h_columns)[colInd2], (CudaChar*)(left->h_columns)[colInd1], d_res1, d_res2);
else if ((left->type)[colInd1] == 1 && (right->type)[colInd2] == 1) {
thrust::device_ptr<float_type> dev_ptr_left((float_type*)(left->d_columns)[colInd1]);
thrust::device_ptr<float_type> dev_ptr_right((float_type*)(right->d_columns)[colInd2]);
thrust::device_ptr<int_type> dev_ptr_int_left = thrust::device_malloc<int_type>(left->mRecCount);
thrust::device_ptr<int_type> dev_ptr_int_right = thrust::device_malloc<int_type>(right->mRecCount);
thrust::transform(dev_ptr_left, dev_ptr_left + left->mRecCount, dev_ptr_int_left, float_to_decimal());
thrust::transform(dev_ptr_right, dev_ptr_right + right->mRecCount, dev_ptr_int_right, float_to_decimal());
join(thrust::raw_pointer_cast(dev_ptr_int_right), thrust::raw_pointer_cast(dev_ptr_int_left),
d_res1, d_res2, left->mRecCount, right->mRecCount, 0);
thrust::device_free(dev_ptr_int_left);
thrust::device_free(dev_ptr_int_right);
};
// Here we need to add possible joins on other columns
queue<string> op_value1(op_value);
while(!op_value1.empty()) {
f1 = op_value1.front();
op_value1.pop();
f2 = op_value1.front();
op_value1.pop();
colInd1 = (left->columnNames).find(f1)->second;
colInd2 = (right->columnNames).find(f2)->second;
if(left->type[colInd1] != right->type[colInd2]) {
cout << "Cannot do join on columns of different types : " << f1 << " " << f2 << endl;
exit(1);
};
if (!right_in_gpu) {
right->allocColumnOnDevice(colInd2, right->mRecCount);
right->CopyColumnToGpu(colInd2);
};
if (!left_in_gpu) {
left->allocColumnOnDevice(colInd1, left->maxRecs);
left->CopyColumnToGpu(colInd1, i);
};
void* d1;
void* d2;
if (right->type[colInd2] == 0 ) {
thrust::device_ptr<int_type> src1((int_type*)(right->d_columns)[colInd2]);
CUDA_SAFE_CALL(cudaMalloc((void **) &d1, d_res2.size()*int_size));
thrust::device_ptr<int_type> dest1((int_type*)d1);
thrust::gather(d_res2.begin(), d_res2.end(), src1, dest1);
thrust::device_ptr<int_type> src2((int_type*)(left->d_columns)[colInd1]);
CUDA_SAFE_CALL(cudaMalloc((void **) &d2, d_res2.size()*int_size));
thrust::device_ptr<int_type> dest2((int_type*)d2);
thrust::gather(d_res1.begin(), d_res1.end(), src2, dest2);
thrust::transform(dest1, dest1+d_res2.size(), dest2, dest2, thrust::equal_to<int_type>());
int sz = thrust::reduce(dest2, dest2 + d_res2.size());
thrust::copy_if(d_res1.begin(), d_res1.end(), dest2, dest1, nz<int_type>());
d_res1.resize(sz);
thrust::copy(dest1, dest1+sz, d_res1.begin());
thrust::copy_if(d_res2.begin(), d_res2.end(), dest2, dest1, nz<int_type>());
d_res2.resize(sz);
thrust::copy(dest1, dest1+sz, d_res2.begin());
thrust::device_free(dest1);
thrust::device_free(dest2);
}
else if (right->type[colInd2] == 1 ) {
thrust::device_ptr<float_type> src1((float_type*)(right->d_columns)[colInd2]);
CUDA_SAFE_CALL(cudaMalloc((void **) &d1, d_res2.size()*float_size));
thrust::device_ptr<float_type> dest1((float_type*)d1);
thrust::gather(d_res2.begin(), d_res2.end(), src1, dest1);
thrust::device_ptr<float_type> src2((float_type*)(left->d_columns)[colInd1]);
CUDA_SAFE_CALL(cudaMalloc((void **) &d2, d_res2.size()*float_size));
thrust::device_ptr<float_type> dest2((float_type*)d2);
thrust::gather(d_res1.begin(), d_res1.end(), src2, dest2);
thrust::device_ptr<int_type> d3 = thrust::device_malloc<int_type>(d_res2.size());
thrust::device_ptr<int_type> d4 = thrust::device_malloc<int_type>(d_res2.size());
thrust::transform(dest1, dest1+d_res2.size(), dest2, d3, f_equal_to());
int sz = thrust::reduce(d3, d3 + d_res2.size());
thrust::copy_if(d_res1.begin(), d_res1.end(), d3, d4, nz<int_type>());
d_res1.resize(sz);
thrust::copy(d4, d4+sz, d_res1.begin());
thrust::copy_if(d_res2.begin(), d_res2.end(), d3, d4, nz<int_type>());
d_res2.resize(sz);
thrust::copy(d4, d4+sz, d_res2.begin());
thrust::device_free(d3);
thrust::device_free(d4);
thrust::device_free(dest1);
thrust::device_free(dest2);
}
else { //CudaChar
CudaChar *s1 = (CudaChar*)(right->h_columns)[colInd2];
CUDA_SAFE_CALL(cudaMalloc((void **) &d1, d_res2.size()));
thrust::device_ptr<char> dest1((char*)d1);
CudaChar *s2 = (CudaChar*)(left->h_columns)[colInd1];
CUDA_SAFE_CALL(cudaMalloc((void **) &d2, d_res2.size()));
thrust::device_ptr<char> dest2((char*)d2);
int colCnt;
if (s1->mColumnCount > s2->mColumnCount)
colCnt = s2->mColumnCount;
else
colCnt = s1->mColumnCount;
thrust::device_ptr<int_type> d3 = thrust::device_malloc<int_type>(d_res2.size());
thrust::device_ptr<int_type> d4 = thrust::device_malloc<int_type>(d_res2.size());
for(unsigned int j=0; j < colCnt; j++) {
thrust::device_ptr<char> src1(s1->d_columns[j]);
thrust::device_ptr<char> src2(s2->d_columns[j]);
thrust::gather(d_res2.begin(), d_res2.end(), src1, dest1);
thrust::gather(d_res2.begin(), d_res2.end(), src2, dest2);
thrust::transform(dest1, dest1+d_res2.size(), dest2, d3, thrust::equal_to<char>());
int sz = thrust::reduce(d3, d3 + d_res2.size());
thrust::copy_if(d_res1.begin(), d_res1.end(), d3, d4, nz<int_type>());
d_res1.resize(sz);
thrust::copy(d4, d4+sz, d_res1.begin());
thrust::copy_if(d_res2.begin(), d_res2.end(), d3, d4, nz<int_type>());
d_res2.resize(sz);
thrust::copy(d4, d4+sz, d_res2.begin());
};
thrust::device_free(d3);
thrust::device_free(d4);
thrust::device_free(dest1);
thrust::device_free(dest2);
// here we will have to add some code if joined varchar columns are not of the same size
};
if (!right_in_gpu)
right->deAllocColumnOnDevice(colInd2);
if (!left_in_gpu)
left->deAllocColumnOnDevice(colInd1);
}
//cout << "join final end " << d_res1.size() << " " << getFreeMem() << endl;
//std::cout<< "join1 time " << ( ( std::clock() - start1 ) / (double)CLOCKS_PER_SEC ) <<'\n';
if(i == 0)
c = new CudaSet(right,left,d_res1.size(),op_sel, op_sel_as);
if (d_res1.size() != 0)
c->gather(right,left,d_res2,d_res1, i, op_sel);
//cout << " ctotal " << c->mRecCount << endl;
}; // end of join piece cycle
// if a and b are not fact tables then lets copy to host all result columns
if(!c->fact_table) {
c->CopyToHost(0,c->mRecCount);
c->deAllocOnDevice();
};
if (left->fact_table == 0 && right->fact_table == 0) {
left->deAllocOnDevice();
right->deAllocOnDevice();
};
if (left->fact_table == 0 && right->fact_table == 1) {
right->deAllocOnDevice();
};
if (right->fact_table == 0 && left->fact_table == 1) {
left->deAllocOnDevice();
};
varNames[s] = c;
c->maxRecs = c->mRecCount;
c->segCount = 1;
clean_queues();
if(stat[s] == statement_count) {
c->free();
varNames.erase(s);
};
if(stat[j1] == statement_count && fact_file_loaded == 1) {
left->free();
varNames.erase(j1);
};
if(stat[j2] == statement_count && (strcmp(j1,j2.c_str()) != 0) && fact_file_loaded == 1) {
right->free();
varNames.erase(j2);
};
if(stat[j1] == statement_count && fact_file_loaded == 0 && left->fact_table == 0 && right->fact_table == 0) {
left->free();
varNames.erase(j1);
};
if(stat[j2] == statement_count && fact_file_loaded == 0 && left->fact_table == 0 && right->fact_table == 0) {
right->free();
varNames.erase(j2);
};
if(stat[j2] == statement_count && left->fact_table == 0 && right->fact_table == 0 && varNames.find(j2) != varNames.end()) {
right->free();
varNames.erase(j2);
};
if(stat[j1] == statement_count && right->fact_table == 0 && left->fact_table == 0 && varNames.find(j1) != varNames.end()) {
left->free();
varNames.erase(j1);
};
if(stat[j2] == statement_count && right->fact_table == 1 && !right->keep && fact_file_loaded == 0) {
right->free();
varNames.erase(j2);
};
if(stat[j1] == statement_count && left->fact_table == 1 && !left->keep && fact_file_loaded == 0) {
left->free();
varNames.erase(j1);
};
std::cout<< "join time " << ( ( std::clock() - start1 ) / (double)CLOCKS_PER_SEC ) <<'\n';
}
void emit_order(char *s, char *f, int e, int ll)
{
if(ll == 0)
statement_count++;
if (scan_state == 0 && ll == 0) {
if (stat.find(f) == stat.end()) {
cout << "Order : couldn't find variable " << f << endl;
exit(1);
};
stat[s] = statement_count;
stat[f] = statement_count;
return;
};
if(varNames.find(f) == varNames.end() ) {
clean_queues();
return;
};
CudaSet* a = varNames.find(f)->second;
if(a->readyToProcess == 0)
return;
if(a->fact_table == 0 && lc > 1)
return;
if (a->mRecCount == 0) {
if(varNames.find(s) == varNames.end())
varNames[s] = new CudaSet(0,1);
else {
CudaSet* c = varNames.find(s)->second;
c->mRecCount = 0;
};
return;
};
stack<string> exe_type, exe_value;
printf("emit order: %s %s \n", s, f);