-
Notifications
You must be signed in to change notification settings - Fork 5
/
loop_cuda_chill.cc
2459 lines (1974 loc) · 80.6 KB
/
loop_cuda_chill.cc
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
/*****************************************************************************
Copyright (C) 2009 University of Utah
All Rights Reserved.
Purpose:
Cudaize methods
Notes:
History:
1/7/10 Created by Gabe Rudy by migrating code from loop.cc
31/1/11 Modified by Protonu Basu
this is class LoopCuda, for creating CUDA output.
traditionally, it contained AST info from the front end compiler.
loop_cuda_rose.cc
loop_cuda_clang.cc etc
THIS version contains chillAST internally, so should not have to change
if you want to add a new front end parser.
*****************************************************************************/
#include <malloc.h>
#include "loop_cuda_chill.hh"
//#define TRANSFORMATION_FILE_INFO Sg_File_Info::generateDefaultFileInfoForTransformationNode()
#include <code_gen/CG_stringBuilder.h>
#include <omega/code_gen/include/codegen.h>
#include <code_gen/CG_utils.h>
#include <code_gen/CG_outputRepr.h>
#include "loop.hh"
#include <math.h>
#include "omegatools.hh"
#include "ir_cudachill.hh" // cudachill? TODO
//#include "ir_clang.hh" // includes all the "translate from clang to chill", so needs clang paths. bad.
#include "chill_error.hh"
#include <vector>
#include <strings.h>
//#define DEBUG
using namespace omega;
struct ConstCharStarComparator
{
bool operator()(const char *s1, const char *s2) const
{
return (strcmp(s1, s2) < 0);
}
};
typedef std::set<const char *, ConstCharStarComparator> what_t;
int charstarvectorindex( const char *str, std::vector< char * > vec ) {
for (int i=0; i<vec.size(); i++) if (!strcmp(str, vec[i])) return i;
return -1; // not found
}
extern char *k_cuda_texture_memory; //protonu--added to track texture memory type
extern char *k_ocg_comment;
extern bool cudaDebug;
std::string& upcase(std::string& s) {
for (int i = 0; i < s.size(); i++)
s[i] = toupper(s[i]);
return s;
}
void printVs(const std::vector<std::string>& curOrder) {
if (!cudaDebug) return;
for (int i = 0; i < curOrder.size(); i++) {
if (i > 0)
printf(",");
printf("%s", curOrder[i].c_str());
}
printf("\n");
fflush(stdout);
}
void printVS(const std::vector<std::string>& curOrder) {
if(!cudaDebug) return;
for (int i = 0; i < curOrder.size(); i++) {
if (i > 0)
printf(",");
printf("%s", curOrder[i].c_str());
}
printf("\n");
fflush(stdout);
}
LoopCuda::~LoopCuda() {
const int m = stmt.size();
for (int i = 0; i < m; i++)
stmt[i].code->clear();
}
bool LoopCuda::symbolExists(std::string s) {
debug_fprintf(stderr, "LoopCuda::symbolExists( %s ) TODO loop_cuda_chill.cc L89\nDIE\n", s.c_str());
exit(-1); // DFL
/*
if (body_symtab->find_variable(SgName(s.c_str())) commented OUT
|| parameter_symtab->find_variable(SgName(s.c_str())))
return true;
if (globals->lookup_variable_symbol(SgName(s.c_str())))
return true;
for (int i = 0; i < idxNames.size(); i++)
for (int j = 0; j < idxNames[i].size(); j++)
if (strcmp(idxNames[i][j].c_str(), s.c_str()) == 0)
return true;
*/
return false;
}
void LoopCuda::printSyncs() {
int numsyncs = syncs.size();
for (int i=0; i<numsyncs; i++) {
debug_fprintf(stderr, "%d %d %s\n", i, syncs[i].first, syncs[i].second.c_str());
}
}
void LoopCuda::addSync(int stmt_num, std::string idxName) {
debug_fprintf(stderr, "addsync\n");
//we store these and code-gen inserts sync to omega comments where stmt
//in loop that has idxName being generated
syncs.push_back(make_pair(stmt_num, idxName));
}
enum Type {
Int
};
// Helper function for wrapInIfFromMinBound
static chillAST_node* getInitBound(chillAST_BinaryOperator* assignOp) {
assert(assignOp->isAssignmentOp());
chillAST_node* condition = nullptr;
if(assignOp->lhs->isBinaryOperator() && assignOp->lhs->isRemOp()) {
auto rhs = assignOp->rhs->as<chillAST_node>();
auto remOp = assignOp->lhs->as<chillAST_BinaryOperator>();
// lower bound condition for x = a % b:
auto a = remOp->lhs->as<chillAST_node>();
auto b = remOp->rhs->as<chillAST_node>();
auto zero = new chillAST_IntegerLiteral(0);
condition = new chillAST_BinaryOperator(
new chillAST_BinaryOperator(
new chillAST_BinaryOperator(
assignOp->lhs,
"-",
a),
"%",
b),
"==",
zero);
}
assignOp->rhs = new chillAST_IntegerLiteral(0);
return condition;
}
chillAST_node* wrapInIfFromMinBound(
chillAST_node* then_part,
chillAST_ForStmt* loop,
chillAST_node *symtab,
chillAST_node* bound_sym) {
debug_fprintf(stderr, "wrapInIfFromMinBound()\n");
/*
SgBinaryOp* test_expr = isSgBinaryOp(loop->get_test_expr());
SgExpression* upperBound;
SgExpression* conditional;
upperBound = test_expr->get_rhs_operand();
CG_outputRepr *ifstmt;
SgCallExpression *call;
if (call = isSgCallExpression(upperBound))
if (isSgVarRefExp(call->get_function())->get_symbol()->get_name().getString()
== "__rose_lt") {
SgExprListExp* arg_list = call->get_args();
SgExpression *if_bound = *(arg_list->get_expressions().begin());
// This relies on the minimum expression being the rhs operand of
// the min instruction.
//
SgIfStmt *ifstmt = buildIfStmt(
buildLessOrEqualOp(buildVarRefExp(bound_sym), if_bound),
isSgStatement(then_part), NULL);
return isSgNode(ifstmt);
}
/* if (isSgConditionalExp(upperBound)) {
conditional = isSgConditionalExp(upperBound)->get_conditional_exp();
if (isSgBinaryOp(conditional)) {
SgBinaryOp* binop = isSgBinaryOp(conditional);
if (isSgLessThanOp(binop) || isSgLessOrEqualOp(binop)) {
SgIfStmt *ifstmt = buildIfStmt(
buildLessOrEqualOp(buildVarRefExp(bound_sym),
test_expr), isSgStatement(then_part), NULL);
return isSgNode(ifstmt);
}
}
}
*/
// Handle loop init stmts
chillAST_node* condition = nullptr;
auto for_init_stmt = loop->getInit();
if(for_init_stmt->isCompoundStmt()) {
//TODO: do we even support this ???
for(auto stmt: for_init_stmt->as<chillAST_CompoundStmt>()->getChildren()) {
condition = getInitBound(stmt->as<chillAST_BinaryOperator>());
}
}
else {
condition = getInitBound(for_init_stmt->as<chillAST_BinaryOperator>());
}
// Handle loop increment stmts
auto for_incr_expr = loop->getInc();
int stepsize = 1;
if(for_incr_expr->isBinaryOperator()) {
if(!strcmp(for_incr_expr->as<chillAST_BinaryOperator>()->op, "+=")) {
stepsize = for_incr_expr->as<chillAST_BinaryOperator>()->rhs->as<chillAST_IntegerLiteral>()->value;
if(stepsize > 1) {
auto ti = new chillAST_IfStmt(condition, loop->body, nullptr);
loop->body = ti;
then_part = ti;
}
}
}
// Handle loop test case stmts
auto for_test_expr = loop->getCond()->as<chillAST_BinaryOperator>();
auto upper_bound = for_test_expr->lhs->as<chillAST_node>();
if(upper_bound->isCallExpr()) {
auto upper_bound_call = upper_bound->as<chillAST_CallExpr>();
//TODO: if is call to __rose_lt...
}
return then_part;
}
/**
* This would be better if it was done by a CHiLL xformation instead of at codegen
*
* state:
* for(...)
* for(...)
* cur_body
* stmt1
*
* stm1 is in-between two loops that are going to be reduced. The
* solution is to put stmt1 at the end of cur_body but conditionally run
* in on the last step of the for loop.
*
* A CHiLL command that would work better:
*
* for(...)
* stmt0
* for(for i=0; i<n; i++)
* cur_body
* stmt1
* =>
* for(...)
* for(for i=0; i<n; i++)
* if(i==0) stmt0
* cur_body
* if(i==n-1) stmt1
*/
void findReplacePreferedIdxs(chillAST_node *newkernelcode,
chillAST_FunctionDecl *kernel ) {
debug_fprintf(stderr, "\nrecursiveFindReplacePreferedIdxs( sync 0 ) perhaps adding syncthreads\n");
chillAST_SymbolTable *symtab = kernel->getSymbolTable();
debug_fprintf(stderr, "this is the symbol table (things defined in the kernel)\n");
printSymbolTableMoreInfo( symtab);
//newkernelcode->findLoopIndexesToReplace( symtab, false );
kernel->getBody()->findLoopIndexesToReplace( symtab, false );
}
#define NOTRUNONGPU (ordered_cudaized_stmts[iter].second == -1)
#define RUNONGPU (ordered_cudaized_stmts[iter].second != -1)
// Anand's April 2015 version, with statement numbers and 1 more arg
bool LoopCuda::cudaize_v3(int stmt_num,
std::string kernel_name,
std::map<std::string, int> array_sizes,
std::vector<std::string> blockIdxs,
std::vector<std::string> threadIdxs,
std::vector<std::string> kernel_params) {
// set array sizes
for(auto dim_pair: array_sizes) {
this->array_sizes[dim_pair.first] = dim_pair.second;
}
cudaDebug = true;
debug_fprintf(stderr, "\n(chill) LoopCuda::cudaize_v3( stmt_num %d, kernel_name %s ) ANAND'S\n", stmt_num, kernel_name.c_str());
debug_fprintf(stderr, "blocks= ");
printVs(blockIdxs);
printf("thread= ");
printVs(threadIdxs);
printf("\n");
fflush(stdout);
debug_fprintf(stderr, "%d kernel_params:\n", kernel_params.size() );
for (int i = 0; i < kernel_params.size(); i++) {
debug_fprintf(stderr, "kernel_parameter: %s\n", kernel_params[i].c_str()); // input to this routine
// loop_cuda member, a set std::set<std::string> kernel_parameters;
kernel_parameters.insert(kernel_params[i]);
}
debug_fprintf(stderr, "\n");
CG_outputBuilder *ocg = ir->builder();
//this->array_dims.push_back(array_dims);
if (!validIndexes(stmt_num, blockIdxs)) {
throw std::runtime_error("One of the indexes in the block list was not "
"found in the current set of indexes.");
}
if (!validIndexes(stmt_num, threadIdxs)) {
throw std::runtime_error(
"One of the indexes in the thread list was not "
"found in the current set of indexes.");
}
if (blockIdxs.size() == 0) {
// throw std::runtime_error("Cudaize: Need at least one block dimension");
Vcu_bx.push_back(1);
Vcu_bx_repr.push_back(NULL);
VbxAst.push_back( new chillAST_IntegerLiteral( 1 ));
}
if (threadIdxs.size() == 0) {
// throw std::runtime_error("Cudaize: Need at least one block dimension");
Vcu_tx.push_back(1);
Vcu_tx_repr.push_back(NULL);
VtxAst.push_back( new chillAST_IntegerLiteral( 1 ));
}
int block_level = 0;
std::vector<int> thread_and_block_levels;
//Now, we will determine the actual size (if possible, otherwise
//complain) for the block dimensions and thread dimensions based on our
//indexes and the relations for our stmt;
debug_fprintf(stderr, "loopCuda::cudaize_vX() blockIdxs.size() %d\n", blockIdxs.size());
for (int i = 0; i < blockIdxs.size(); i++) {
debug_fprintf(stderr, "blockIdxs i %d\n", i);
int level = findCurLevel(stmt_num, blockIdxs[i]);
// integer (constant) and non-constant versions of upper bound
int ub, lb;
CG_outputRepr* ubrepr = NULL;
chillAST_node *ubcode = NULL; // chillAST version of non-constant upper bound
ub = -1;
lb = 9999;
debug_fprintf(stderr, "loopCuda::cudaize_vX() extractCudaUB( stmt %d, level %d, ub %d, lb %d)\n", stmt_num, level, ub, lb);
ubrepr = extractCudaUB(stmt_num, level, ub, lb);
if (lb != 0) {
//attempt to "normalize" the loop with an in-place tile and then re-check our bounds
if (cudaDebug)
printf(
"Cudaize: doing tile at level %d to try and normalize lower bounds\n",
level);
fflush(stdout);
tile(stmt_num, level, 1, level, CountedTile);
idxNames[stmt_num].insert(idxNames[stmt_num].begin() + (level), ""); //TODO: possibly handle this for all sibling stmts
ubrepr = extractCudaUB(stmt_num, level, ub, lb);
}
if (ubrepr) {
ubcode = ((CG_chillRepr *)ubrepr)->GetCode();
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound:\n");
ubcode->print(0, stderr); debug_fprintf(stderr, "\n");
}
if (lb != 0) {
char buf[1024];
sprintf(buf,
"Cudaize: Loop at level %d does not have 0 as its lower bound",
level);
throw std::runtime_error(buf);
}
// this now does nothing
if (ub < 0) {
char buf[1024];
sprintf(buf,
"Cudaize: Loop at level %d does not have a hard upper bound",
level);
//Anand: Commenting out error indication for lack of constant upper bound
//throw std::runtime_error(buf);
}
if (cudaDebug)
printf("block idx %s level %d lb: %d ub %d\n", blockIdxs[i].c_str(),
level, lb, ub);
fflush(stdout);
// bx
if (i == 0) { // bx
debug_fprintf(stderr, "blockIdxs i == 0, therefore bx?\n");
block_level = level;
if (ubrepr == NULL) {
Vcu_bx.push_back(ub + 1); // a constant int ub + 1
Vcu_bx_repr.push_back(NULL);
VbxAst.push_back( new chillAST_IntegerLiteral( ub + 1 ));
cu_bx = ub + 1;
} else {
Vcu_bx.push_back(0);
Vcu_bx_repr.push_back( // NON constant ub + 1
(omega::CG_chillRepr*)ocg->CreatePlus(ubrepr, ocg->CreateInt(1)));
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound bx :\n");
chillAST_BinaryOperator *UBP1; // ub + 1
UBP1 = new chillAST_BinaryOperator( ubcode, "+", new chillAST_IntegerLiteral(1) );
UBP1->print(0, stderr); debug_fprintf(stderr, "\n");
VbxAst.push_back( UBP1 ); //
}
debug_fprintf(stderr, "setting idxNames[%d][%d] to bx\n", stmt_num, level-1);
idxNames[stmt_num][level - 1] = "bx";
}
// by
else if (i == 1) { // by
debug_fprintf(stderr, "blockIdxs i == 1, therefore by?\n");
if (ubrepr == NULL) {
Vcu_by.push_back(ub + 1); // constant
Vcu_by_repr.push_back(NULL);
VbyAst.push_back( new chillAST_IntegerLiteral( ub + 1 ));
cu_by = ub + 1;
} else {
Vcu_by.push_back(0);
Vcu_by_repr.push_back( // NON constant ub + 1
(omega::CG_chillRepr*)ocg->CreatePlus(ubrepr, ocg->CreateInt(1)));
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound bx :\n");
chillAST_BinaryOperator *UBP1; // ub + 1
UBP1 = new chillAST_BinaryOperator( ubcode, "+", new chillAST_IntegerLiteral(1) );
UBP1->print(0, stderr); debug_fprintf(stderr, "\n");
VbxAst.push_back( UBP1 ); //
}
debug_fprintf(stderr, "setting idxNames[%d][%d] to by\n", stmt_num, level-1);
idxNames[stmt_num][level - 1] = "by";
}
thread_and_block_levels.push_back(level);
}
if (VbyAst.size() == 0) { // TODO probably wrong
block_level = 0; // ?? no by ??
debug_fprintf(stderr, "THERE IS NO BY (??) \n");
VbyAst.push_back( new chillAST_IntegerLiteral( 1 ));
}
// what is this ???
if (Vcu_by.size() == 0 && Vcu_by_repr.size() == 0) block_level = 0; // there are none ?? OR
else if (Vcu_by.size() > 0 && // there are some constant
!Vcu_by[Vcu_by.size() - 1] // the last one doesn't exist
&& Vcu_by_repr.size() > 0 // there are non-constant
&& !Vcu_by_repr[Vcu_by_repr.size() - 1]) // the last one doesn't exist
{
debug_fprintf(stderr, "I think this is impossible\n");
block_level = 0;
}
// ???
if (blockIdxs.size() < 2) {
Vcu_by_repr.push_back(NULL);
Vcu_by.push_back(0);
}
// must have at least 2 ???
int thread_level1 = 0;
int thread_level2 = 0;
for (int i = 0; i < threadIdxs.size(); i++) {
int level = findCurLevel(stmt_num, threadIdxs[i]);
int ub, lb;
CG_outputRepr* ubrepr = extractCudaUB(stmt_num, level, ub, lb);
chillAST_node *ubcode = NULL;
if (lb != 0) {
//attempt to "normalize" the loop with an in-place tile and then re-check our bounds
if (cudaDebug)
printf(
"Cudaize: doing tile at level %d to try and normalize lower bounds\n",
level);
fflush(stdout);
tile(stmt_num, level, 1, level, CountedTile);
idxNames[stmt_num].insert(idxNames[stmt_num].begin() + (level), "");
ubrepr = extractCudaUB(stmt_num, level, ub, lb);
}
if (ubrepr) {
ubcode = ((CG_chillRepr *)ubrepr)->GetCode();
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound:\n");
ubcode->print(0, stderr); debug_fprintf(stderr, "\n");
}
if (lb != 0) {
char buf[1024];
sprintf(buf,
"Cudaize: Loop at level %d does not have 0 as its lower bound",
level);
throw std::runtime_error(buf);
}
if (ub < 0) {
char buf[1024];
sprintf(buf,
"Cudaize: Loop at level %d does not have a hard upper bound",
level);
//Anand: Commenting out error indication for lack of constant upper bound
//throw std::runtime_error(buf);
}
if (cudaDebug) {
printf("thread idx %s level %d lb: %d ub %d\n",
threadIdxs[i].c_str(), level, lb, ub);
fflush(stdout);
}
if (i == 0) { // tx
thread_level1 = level;
if (ubrepr == NULL) {
Vcu_tx.push_back(ub + 1); // a constant int ub + 1
Vcu_tx_repr.push_back(NULL);
VtxAst.push_back( new chillAST_IntegerLiteral( ub + 1 ));
cu_tx = ub + 1;
} else {
Vcu_tx.push_back(0);
Vcu_tx_repr.push_back(// NON constant ub + 1
ocg->CreatePlus(ubrepr, ocg->CreateInt(1)));
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound tx :\n");
chillAST_BinaryOperator *UBP1; // ub + 1
UBP1 = new chillAST_BinaryOperator( ubcode, "+", new chillAST_IntegerLiteral(1) );
UBP1->print(0, stderr); debug_fprintf(stderr, "\n");
VtxAst.push_back( UBP1 ); //
}
debug_fprintf(stderr, "setting idxNames[%d][%d] to tx\n", stmt_num, level-1);
idxNames[stmt_num][level - 1] = "tx";
}
else if (i == 1) { // ty
thread_level2 = level;
if (ubrepr == NULL) {
Vcu_ty.push_back(ub + 1); // a constant int ub + 1
Vcu_ty_repr.push_back(NULL);
VtyAst.push_back( new chillAST_IntegerLiteral( ub + 1 ));
cu_ty = ub + 1;
} else { // NON constant ub + 1
Vcu_ty.push_back(0);
Vcu_ty_repr.push_back(
ocg->CreatePlus(ubrepr, ocg->CreateInt(1)));
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound ty :\n");
chillAST_BinaryOperator *UBP1; // ub + 1
UBP1 = new chillAST_BinaryOperator( ubcode, "+", new chillAST_IntegerLiteral(1) );
UBP1->print(0, stderr); debug_fprintf(stderr, "\n");
VtyAst.push_back( UBP1 ); //
}
debug_fprintf(stderr, "setting idxNames[%d][%d] to ty\n", stmt_num, level-1);
idxNames[stmt_num][level - 1] = "ty";
}
else if (i == 2) { // tz
if (ubrepr == NULL) {
Vcu_tz.push_back(ub + 1); // constant ub + 1
Vcu_tz_repr.push_back(NULL);
VtzAst.push_back( new chillAST_IntegerLiteral( ub + 1 ));
cu_tz = ub + 1;
} else {
Vcu_tz.push_back(0);
Vcu_tz_repr.push_back( // NON constant ub + 1
ocg->CreatePlus(ubrepr, ocg->CreateInt(1)));
debug_fprintf(stderr, "loop_cuda_chill.cc non-constant upper bound tz :\n");
chillAST_BinaryOperator *UBP1; // ub + 1
UBP1 = new chillAST_BinaryOperator( ubcode, "+", new chillAST_IntegerLiteral(1) );
UBP1->print(0, stderr); debug_fprintf(stderr, "\n");
VtzAst.push_back( UBP1 ); //
}
debug_fprintf(stderr, "setting idxNames[%d][%d] to tz\n", stmt_num, level-1);
idxNames[stmt_num][level - 1] = "tz";
}
thread_and_block_levels.push_back(level);
}
if (Vcu_ty.size() == 0 && Vcu_ty_repr.size() == 0)
thread_level1 = 0;
if (Vcu_tz.size() == 0 && Vcu_tz_repr.size() == 0)
thread_level2 = 0;
if (Vcu_ty.size() > 0 && !Vcu_ty[Vcu_ty.size() - 1] && Vcu_ty_repr.size() > 0
&& !Vcu_ty_repr[Vcu_ty_repr.size() - 1]) {
debug_fprintf(stderr, "I think this is impossible ty\n");
thread_level1 = 0;
}
if (Vcu_tz.size() > 0 && !Vcu_tz[Vcu_tz.size() - 1] && Vcu_tz_repr.size() > 0
&& !Vcu_tz_repr[Vcu_tz_repr.size() - 1]) {
debug_fprintf(stderr, "I think this is impossible tz\n");
thread_level2 = 0;
}
// ???
if (threadIdxs.size() < 2) {
Vcu_ty.push_back(0);
Vcu_ty_repr.push_back(NULL);
}
if (threadIdxs.size() < 3) {
Vcu_tz.push_back(0);
Vcu_tz_repr.push_back(NULL);
}
//Make changes to nonsplitlevels
std::vector<int> lex = getLexicalOrder(stmt_num);
//cudaized = getStatements(lex, 0);
debug_fprintf(stderr, "ADDING TO CUDAIZED\n");
cudaized.push_back(getStatements(lex, 0));
for (std::set<int>::iterator i = cudaized[cudaized.size() - 1].begin();
i != cudaized[cudaized.size() - 1].end(); i++) {
if (block_level) {
//stmt[i].nonSplitLevels.append((block_level)*2);
stmt_nonSplitLevels[*i].push_back((block_level) * 2);
}
if (thread_level1) {
//stmt[i].nonSplitLevels.append((thread_level1)*2);
stmt_nonSplitLevels[*i].push_back((thread_level1) * 2);
}
if (thread_level2) {
//stmt[i].nonSplitLevels.append((thread_level1)*2);
stmt_nonSplitLevels[*i].push_back((thread_level2) * 2);
}
idxNames[*i] = idxNames[stmt_num];
}
if (cudaDebug) {
printf("Codegen: current names: ");
printVS(idxNames[stmt_num]);
fflush(stdout);
}
//Set codegen flag
code_gen_flags |= GenCudaizeV2;
debug_fprintf(stderr, "\n(chill) ANAND'S LoopCuda::cudaize_v3() Set codegen flag\n");
//Save array dimension sizes
this->Varray_dims.push_back(array_sizes);
Vcu_kernel_name.push_back(kernel_name.c_str()); debug_fprintf(stderr, "Vcu_kernel_name.push_back(%s)\n", kernel_name.c_str());
block_and_thread_levels.insert(
std::pair<int, std::vector<int> >(stmt_num,
thread_and_block_levels));
cu_kernel_name = kernel_name.c_str();
debug_fprintf(stderr, "cu_kernel_name WILL BE %s\n", cu_kernel_name.c_str());
debug_fprintf(stderr, "\n(chill) ANAND'S LoopCuda::cudaize_v3( stmt_num %d ) DONE\n", stmt_num);
}
#include "cudaize_codegen_v2.cc"
// END "cudaize_codegen_v2.cc"
//Order taking out dummy variables
static std::vector<std::string> cleanOrder(std::vector<std::string> idxNames) {
std::vector<std::string> results;
for (int j = 0; j < idxNames.size(); j++) {
if (idxNames[j].length() != 0)
results.push_back(idxNames[j]);
}
return results;
}
void LoopCuda::permute_cuda(int stmt_num, const std::vector<std::string>& curOrder) {
debug_fprintf(stderr, "LoopCuda::permute_cuda()\n");
printf("curOrder: ");
printVs(curOrder);
printf("idxNames: ");
printVS(idxNames[stmt_num]);
std::vector<std::string> cIdxNames = cleanOrder(idxNames[stmt_num]);
bool same = true;
std::vector<int> pi;
for (int i = 0; i < curOrder.size(); i++) {
bool found = false;
for (int j = 0; j < cIdxNames.size(); j++) {
if (strcmp(cIdxNames[j].c_str(), curOrder[i].c_str()) == 0) {
debug_fprintf(stderr, "pushing pi for j+1=%d\n", j+1);
pi.push_back(j + 1);
found = true;
if (j != i)
same = false;
}
}
if (!found) {
throw std::runtime_error(
"One of the indexes in the permute order were not "
"found in the current set of indexes.");
}
}
for (int i = curOrder.size(); i < cIdxNames.size(); i++) {
debug_fprintf(stderr, "pushing pi for i=%d\n", i);
pi.push_back(i);
}
if (same)
return;
permute_cuda(stmt_num, pi);
//Set old indexe names as new
for (int i = 0; i < curOrder.size(); i++) {
idxNames[stmt_num][i] = curOrder[i].c_str(); //what about sibling stmts?
}
}
bool LoopCuda::permute_cuda(int stmt_num, const std::vector<int> &pi) {
// check for sanity of parameters
if (stmt_num >= stmt.size() || stmt_num < 0)
throw std::invalid_argument("invalid statement " + to_string(stmt_num));
const int n = stmt[stmt_num].xform.n_out();
if (pi.size() > (n - 1) / 2) {
debug_fprintf(stderr, "\n\nloop_cuda_CHILL.cc L 761, pi.size() %d > ((n=%d)-1)/2 = %d\n", pi.size(), n, (n-1)/2);
for (int i=0; i<pi.size(); i++) debug_fprintf(stderr, "pi[%d] = %d\n", i, pi[i]);
throw std::invalid_argument(
"iteration space dimensionality does not match permute dimensionality");
}
int first_level = 0;
int last_level = 0;
for (int i = 0; i < pi.size(); i++) {
if (pi[i] > (n - 1) / 2 || pi[i] <= 0)
throw std::invalid_argument(
"invalid loop level " + to_string(pi[i])
+ " in permuation");
if (pi[i] != i + 1) {
if (first_level == 0)
first_level = i + 1;
last_level = i + 1;
}
}
if (first_level == 0)
return true;
std::vector<int> lex = getLexicalOrder(stmt_num);
std::set<int> active = getStatements(lex, 2 * first_level - 2);
Loop::permute(active, pi);
}
//void LoopCuda::tile_cuda(int stmt, int level, int outer_level) { // 3 params
// debug_fprintf(stderr, "LoopCuda::tile_cuda(stmt %d, level %d, outer_level %d)\n",
// stmt, level, outer_level);
// tile_cuda(stmt, level, 1, outer_level, "", "", CountedTile);
//}
void LoopCuda::tile_cuda(int stmt_num, int level, int outer_level,// 3 numbers, and a method
TilingMethodType method /* defaults to CountedTile */ ) {
debug_fprintf(stderr, "LoopCuda::tile_cuda(stmt_num %d, level %d, outer_level %d, TilingMethodType method)\n", stmt_num, level, outer_level);
printsyms();
debug_fprintf(stderr, "before tile()\n");
tile_cuda(stmt_num, level, 1, outer_level, "", "", method); // calls version with 4 numbers, 2 strings, and a method
debug_fprintf(stderr, "after tile, before getLexicalOrder(stmt_num %d)\n", stmt_num);
printsyms();
debug_fprintf(stderr, "LoopCuda::tile_cuda() DONE\n");
}
void LoopCuda::tile_cuda(int level, int tile_size, int outer_level,
std::string idxName, std::string ctrlName, TilingMethodType method) {
debug_fprintf(stderr, "LoopCuda::tile_cuda( level %d, tile_size %d, outer_level %d, idxName %s, ctrlName %s, method)\n",
level, tile_size, outer_level, idxName.c_str(), ctrlName.c_str() );
printsyms();
tile_cuda(0, level, tile_size, outer_level, idxName, ctrlName, method);
}
void LoopCuda::tile_cuda(int stmt_num, int level, int tile_size, int outer_level,
std::string idxName, std::string ctrlName, TilingMethodType method) {
fflush(stdout);
debug_fprintf(stderr, "\nLoopCuda::tile_cuda 1234 name name method\n");
printsyms();
//debug_fprintf(stderr, "after printsyms\n");
//debug_fprintf(stderr, "\n%d statements\n", stmt.size());
//for (int i=0; i<stmt.size(); i++) {
// debug_fprintf(stderr, "%2d ", i);
// ((CG_chillRepr *)stmt[i].code)->Dump();
//}
//debug_fprintf(stderr, "\n");
//Do regular tile but then update the index and control loop variable
//names as well as the idxName to reflect the current state of things.
//printf("tile(%d,%d,%d,%d)\n", stmt_num, level, tile_size, outer_level);
//printf("idxNames before: ");
//printVS(idxNames[stmt_num]);
fflush(stdout);
debug_fprintf(stderr, "loop_cuda_chill.cc, tile_cuda(), before tile()\n");
tile(stmt_num, level, tile_size, outer_level, method);
debug_fprintf(stderr, "loop_cuda_chill.cc, tile_cuda(), after tile, before getLexicalOrder(stmt_num %d)\n", stmt_num);
debug_fprintf(stderr, "after tile, before getLexicalOrder(stmt_num %d)\n", stmt_num);
std::vector<int> lex = getLexicalOrder(stmt_num);
int dim = 2 * level - 1;
std::set<int> same_loop = getStatements(lex, dim - 1);
for (std::set<int>::iterator j = same_loop.begin(); j != same_loop.end();
j++) {
debug_fprintf(stderr, "*j = %d\n", *j);
if (idxName.size())
idxNames[*j][level - 1] = idxName.c_str();
if (tile_size == 1) {
//potentially rearrange loops
if (outer_level < level) {
std::string tmp = idxNames[*j][level - 1];
for (int i = level - 1; i > outer_level - 1; i--) {
if (i - 1 >= 0)
idxNames[*j][i] = idxNames[*j][i - 1];
}
idxNames[*j][outer_level - 1] = tmp;
}
//TODO: even with a tile size of one, you need a insert (of a dummy loop)
idxNames[*j].insert(idxNames[*j].begin() + (level), "");
} else {
if (!ctrlName.size())
throw std::runtime_error("No ctrl loop name for tile");
//insert
idxNames[*j].insert(idxNames[*j].begin() + (outer_level - 1),
ctrlName.c_str());
}
}
//printf("idxNames after: ");
//printVS(idxNames[stmt_num]);
printsyms();
debug_fprintf(stderr, "LoopCuda::tile_cuda 1234 name name method DONE\n\n\n");
}
bool LoopCuda::datacopy_privatized_cuda(int stmt_num, int level,
const std::string &array_name,
const std::vector<int> &privatized_levels, bool allow_extra_read,
int fastest_changing_dimension, int padding_stride,
int padding_alignment, bool cuda_shared) {
debug_fprintf(stderr, "LoopCuda::datacopy_privatized_cuda()\n");
int old_stmts = stmt.size();
printf("before datacopy_privatized:\n");
printIS(); fflush(stdout);
//datacopy_privatized(stmt_num, level, array_name, privatized_levels, allow_extra_read, fastest_changing_dimension, padding_stride, padding_alignment, cuda_shared);
if (cuda_shared)
datacopy_privatized(stmt_num, level, array_name, privatized_levels,
allow_extra_read, fastest_changing_dimension, padding_stride,
padding_alignment, 1);
else
datacopy_privatized(stmt_num, level, array_name, privatized_levels,
allow_extra_read, fastest_changing_dimension, padding_stride,
padding_alignment, 0);
printf("after datacopy_privatized:\n");
printIS(); fflush(stdout);
//Adjust idxNames to reflect updated state
std::vector<std::string> cIdxNames = cleanOrder(idxNames[stmt_num]);
int new_stmts = stmt.size();
for (int i = old_stmts; i < new_stmts; i++) {
//printf("fixing up statement %d\n", i);
std::vector<std::string> idxs;
//protonu-making sure the vector of nonSplitLevels grows along with
//the statement structure
stmt_nonSplitLevels.push_back(std::vector<int>());
//Indexes up to level will be the same
for (int j = 0; j < level - 1; j++)
idxs.push_back(cIdxNames[j]);
//Expect privatized_levels to match
for (int j = 0; j < privatized_levels.size(); j++)
idxs.push_back(cIdxNames[privatized_levels[j] - 1]);//level is one-based
//all further levels should match order they are in originally
if (privatized_levels.size()) {
int last_privatized = privatized_levels.back();
int top_level = last_privatized
+ (stmt[i].IS.n_set() - idxs.size());
//printf("last privatized_levels: %d top_level: %d\n", last_privatized, top_level);
for (int j = last_privatized; j < top_level; j++) {
idxs.push_back(cIdxNames[j]);
//printf("pushing back: %s\n", (const char*)cIdxNames[j]);
}
}
idxNames.push_back(idxs);
}
}
bool LoopCuda::datacopy_cuda(int stmt_num, int level,
const std::string &array_name,
const std::vector<std::string> new_idxs,
bool allow_extra_read, int fastest_changing_dimension,
int padding_stride, int padding_alignment, bool cuda_shared) {
int old_stmts = stmt.size();
//datacopy(stmt_num,level,array_name,allow_extra_read,fastest_changing_dimension,padding_stride,padding_alignment,cuda_shared);
printf("before datacopy:\n"); fflush(stdout);
printIS(); fflush(stdout);
if (cuda_shared)
datacopy(stmt_num, level, array_name, allow_extra_read,
fastest_changing_dimension, padding_stride, padding_alignment,
1);
else
datacopy(stmt_num, level, array_name, allow_extra_read,
fastest_changing_dimension, padding_stride, padding_alignment,
0);
printf("after datacopy:\n"); fflush(stdout);
printIS(); fflush(stdout);