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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
|
/*****************************************************************************
Copyright (C) 2010 University of Utah.
All Rights Reserved.
Purpose:
Register variables and functions into the global Lua addres space to
provide an environment for CHiLL scripts
Notes:
Contains Lua wrappers for the CHiLL Loop class and methods.
History:
01/2010 created by Gabe Rudy
03/2014 added support for CHiLL without Cuda (Derick Huth)
*****************************************************************************/
#define lua_c
#include <lua.hpp> //All lua includes wrapped in extern "C"
#include "loop.hh"
#include "chill_env.hh"
#ifdef CUDACHILL
#ifdef BUILD_ROSE
#include "loop_cuda_rose.hh"
#include "ir_rose.hh"
#include "ir_cudarose.hh"
#elif BUILD_SUIF
#include "loop_cuda.hh"
#include "ir_suif.hh"
#include "ir_cudasuif.hh"
#endif
#else
#include "chill_run_util.hh"
#include <omega.h>
#include "ir_code.hh"
#ifdef BUILD_ROSE
#include "ir_rose.hh"
#elif BUILD_SUIF
#include "ir_suif.hh"
#endif
#endif
using namespace omega;
#ifdef CUDACHILL
extern LoopCuda *myloop;
#else
extern Loop *myloop;
#endif
extern IR_Code *ir_code;
extern bool is_interactive;
extern bool repl_stop;
std::string procedure_name;
std::string source_filename;
extern std::vector<IR_Control *> ir_controls;
extern std::vector<int> loops;
//Macros for wrapping code to myloop-> that translates C++ exceptions to
//Lua stracktraced errors
#define REQUIRE_LOOP try{ if (myloop == NULL){ throw std::runtime_error("loop not initialized"); }
#define END_REQUIRE_LOOP }catch (const std::exception &e) { return luaL_error(L, e.what()); }
#ifdef CUDACHILL
void register_v1(lua_State *L);
void register_v2(lua_State *L);
#endif
//Extra param checking
static bool luaL_checkboolean(lua_State *L, int narg) {
if (!lua_isboolean(L,narg))
luaL_typerror(L, narg, "boolean");
return lua_toboolean(L, narg);
}
static bool luaL_optboolean(lua_State *L, int narg, bool def) {
return luaL_opt(L, luaL_checkboolean, narg, def);
}
static bool tointvector(lua_State *L, int narg, std::vector<int>& v) {
if (!lua_istable(L, narg))
return false;
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
v.push_back((int) luaL_checknumber(L, -1));
//printf("added: %d", v[v.size()-1]);
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
return true;
}
static bool tointset(lua_State* L, int narg, std::set<int>& s) {
if(!lua_istable(L, narg))
return false;
// iterate through array (lua table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
int val = (int)luaL_checknumber(L, -1);
//printf("added: %d\n", val);
s.insert(val);
lua_pop(L, 1);
}
}
static bool tostringvector(lua_State *L, int narg,
std::vector<std::string>& v) {
if (!lua_istable(L, narg))
return false;
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
v.push_back(luaL_checkstring(L,-1));
//printf("added: %d", v[v.size()-1]);
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
return true;
}
static bool tostringmap(lua_State *L, int narg,
std::map<std::string, std::string>& v) {
if (!lua_istable(L, narg))
return false;
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
v.insert(
std::make_pair(luaL_checkstring(L,-2), luaL_checkstring(L,-1)));
//printf("added: %d", v[v.size()-1]);
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
return true;
}
static bool tostringintmap(lua_State *L, int narg,
std::map<std::string, int>& v) {
if (!lua_istable(L, narg))
return false;
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
v.insert(
std::make_pair(luaL_checkstring(L,-2),
(int) luaL_checknumber(L, -1)));
//printf("added: %d", v[v.size()-1]);
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
return true;
}
static bool tostringintmapvector(lua_State *L, int narg, std::vector<std::map<std::string, int> >& v) {
if(!lua_istable(L, narg))
return false;
lua_pushnil(L);
// Iterate over table
while(lua_next(L, narg) != 0) {
std::map<std::string, int> map;
// use 'value' (at index -1), discard key
// try to parse table as a 'string to int' map.
if(!tostringintmap(L, -1, map))
return false;
v.push_back(map);
lua_pop(L, 1);
}
return true;
}
static bool tointmatrix(lua_State *L, int narg,
std::vector<std::vector<int> >& m) {
if (!lua_istable(L, narg))
return false;
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, narg) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
if (!lua_istable(L,-1)) {
lua_pop(L, 2);
return false;
}
m.push_back(std::vector<int>());
int i = m.size() - 1;
//Now iterate over the keys of the second level table
int l2 = lua_gettop(L); //Index of second level table
lua_pushnil(L); // first key
while (lua_next(L, l2) != 0) {
int k = (int) luaL_checknumber(L, -1);
m[i].push_back(k);
//printf("m[%d][%d] = %d\n", i,m[i].size()-1,k);
lua_pop(L, 1);
}
lua_pop(L, 1);
// removes 'value'; keeps 'key' for next iteration
}
return true;
}
static void strict_arg_num(lua_State *L, int num) {
int n = lua_gettop(L); //Number of arguments
if (n != num)
throw std::runtime_error("incorrect number of arguments");
}
// -------------------------------------------------------------------
// Initialization and finalization functions
// -------------------------------------------------------------------
#ifdef CUDACHILL
/* The function we'll call from the lua script */
static int init(lua_State *L) {
int n = lua_gettop(L); //Number of arguments
if (n > 0) {
//Expet one of the following forms
//l1 = init("mm4.sp2",0,0) --input file, procedure 0, loop 0
//or
//l1 = init("mm4.sp2","NameFromPragma")
const char* source_filename = luaL_optstring(L,1,0);
#ifdef BUILD_ROSE
if(lua_isstring(L,2)) {
const char* procedure_name = luaL_optstring(L, 2, 0);
#elif BUILD_SUIF
if (lua_isnumber(L, 2)) {
int procedure_number = luaL_optint(L, 2, 0);
#endif
int loop_num = luaL_optint(L, 3, 0);
lua_getglobal(L, "dest");
const char* dest_lang = lua_tostring(L,-1);
lua_pop(L, 1);
#ifdef BUILD_ROSE
ir_code = new IR_cudaroseCode(source_filename, procedure_name);
#elif BUILD_SUIF
//ir_code = new IR_cudasuifCode(source_filename, procedure_number, dest_lang);
ir_code = new IR_cudasuifCode(source_filename, procedure_number);
//myloop = new LoopCuda(ir_code->init_loop(loop_num), loop_num); //protonu--using the modified constructor
//protonu--here goes my initializations
//A lot of this code was lifted from Chun's parser.yy
//the plan is now to create the LoopCuda object directly
#endif
IR_Block *block = ir_code->GetCode();
ir_controls = ir_code->FindOneLevelControlStructure(block);
#ifdef BUILD_ROSE
int loop_count = 0;
for (int i = 0; i < ir_controls.size(); i++) {
if (ir_controls[i]->type() == IR_CONTROL_LOOP) {
loops.push_back(i);
loop_count++;
}
}
delete block;
std::vector<IR_Control *> parm;
for(int j = 0; j < loop_count; j++)
parm.push_back(ir_controls[loops[j]]);
block = ir_code->MergeNeighboringControlStructures(parm);
#elif BUILD_SUIF
for (int i = 0; i < ir_controls.size(); i++)
if (ir_controls[i]->type() == IR_CONTROL_LOOP)
loops.push_back(i);
delete block;
std::vector<IR_Control *> parm;
parm.push_back(ir_controls[loop_num]);
block = ir_code->MergeNeighboringControlStructures(parm);
#endif
myloop = new LoopCuda(block, loop_num);
delete block;
//end-protonu
} else {
//TODO: handle pragma lookup
}
//Also register a different set of global functions
myloop->original();
myloop->useIdxNames = true; //Use idxName in code_gen
register_v2(L);
//TODO: return a reference to the intial array if that makes sense
//still
return 0;
}
lua_getglobal(L, "source");
const char* source_filename = lua_tostring(L,-1);
lua_pop(L, 1);
lua_getglobal(L, "dest");
const char* dest_lang = lua_tostring(L,-1);
lua_pop(L, 1);
lua_getglobal(L, "procedure");
#ifdef BUILD_ROSE
const char* procedure_name = lua_tostring(L , -1);
#elif BUILD_SUIF
int procedure_number = lua_tointeger(L,-1);
#endif
lua_pop(L, 1);
lua_getglobal(L, "loop");
int loop_num = lua_tointeger(L, -1);
lua_pop(L, 1);
//ir_code = new IR_cudasuifCode(source_filename, procedure_number, dest_lang);
#ifdef BUILD_ROSE
ir_code = new IR_cudaroseCode(source_filename, procedure_name);
#elif BUILD_SUIF
ir_code = new IR_cudasuifCode(source_filename, procedure_number);
//myloop = new LoopCuda(ir_code->init_loop(loop_num), loop_num); //protonu--using the modified constructor
//protonu--here goes my initializations
//A lot of this code was lifted from Chun's parser.yy
//the plan is now to create the LoopCuda object directly
#endif
IR_Block *block = ir_code->GetCode();
ir_controls = ir_code->FindOneLevelControlStructure(block);
#ifdef BUILD_ROSE
int loop_count = 0;
for (int i = 0; i < ir_controls.size(); i++) {
if (ir_controls[i]->type() == IR_CONTROL_LOOP) {
loops.push_back(i);
loop_count++;
}
}
delete block;
std::vector<IR_Control *> parm;
for(int j = 0; j < loop_count; j++)
parm.push_back(ir_controls[loops[j]]);
block = ir_code->MergeNeighboringControlStructures(parm);
#elif BUILD_SUIF
for (int i = 0; i < ir_controls.size(); i++)
if (ir_controls[i]->type() == IR_CONTROL_LOOP)
loops.push_back(i);
delete block;
std::vector<IR_Control *> parm;
parm.push_back(ir_controls[loop_num]);
block = ir_code->MergeNeighboringControlStructures(parm);
#endif
myloop = new LoopCuda(block, loop_num);
delete block;
//register_v1(L);
register_v2 (L);
return 0;
}
#else
static void strict_arg_num(lua_State* L, int min, int max) {
int n = lua_gettop(L);
if(n < min || n > max)
throw std::runtime_error("incorrect number of arguments");
}
int get_loop_num_start(lua_State *L) {
lua_getglobal(L, "loop_start");
int loop_num_start = lua_tointeger(L, -1);
lua_pop(L, 1);
return loop_num_start;
}
int get_loop_num_end(lua_State* L) {
lua_getglobal(L, "loop_end");
int loop_num_end = lua_tointeger(L, -1);
lua_pop(L, 1);
return loop_num_end;
}
static int set_loop_num_start(lua_State *L, int start_num) {
lua_pushinteger(L, start_num);
lua_setglobal(L, "loop_start");
}
static int set_loop_num_end(lua_State *L, int end_num) {
lua_pushinteger(L, end_num);
lua_setglobal(L, "loop_end");
}
static int source(lua_State* L) {
if(!source_filename.empty()) {
fprintf(stderr, "only one file can be handled in a script");
if(!is_interactive)
exit(2);
}
source_filename = luaL_checkstring(L, 1);
return 0;
}
static int procedure(lua_State* L) {
if(!procedure_name.empty()) {
fprintf(stderr, "only one procedure can be handled in a script");
if(!is_interactive)
exit(2);
}
procedure_name = luaL_checkstring(L, 1);
return 0;
}
void finalize_loop(int loop_num_start, int loop_num_end) {
if (loop_num_start == loop_num_end) {
ir_code->ReplaceCode(ir_controls[loops[loop_num_start]], myloop->getCode());
ir_controls[loops[loop_num_start]] = NULL;
}
else {
std::vector<IR_Control *> parm;
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++)
parm.push_back(ir_controls[i]);
IR_Block *block = ir_code->MergeNeighboringControlStructures(parm);
ir_code->ReplaceCode(block, myloop->getCode());
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++) {
delete ir_controls[i];
ir_controls[i] = NULL;
}
}
delete myloop;
}
void finalize_loop(lua_State* L) {
int loop_num_start = get_loop_num_start(L);
int loop_num_end = get_loop_num_end(L);
finalize_loop(loop_num_start, loop_num_end);
}
static void init_loop(lua_State* L, int loop_num_start, int loop_num_end) {
if (source_filename.empty()) {
fprintf(stderr, "source file not set when initializing the loop");
if (!is_interactive)
exit(2);
}
else {
if (ir_code == NULL) {
#ifdef BUILD_ROSE
if (procedure_name.empty())
procedure_name = "main";
#elif BUILD_SUIF
if (procedure_number == -1)
procedure_number = 0;
#endif
#ifdef BUILD_ROSE
ir_code = new IR_roseCode(source_filename.c_str(), procedure_name.c_str());
#elif BUILD_SUIF
ir_code = new IR_suifCode(source_filename.c_str(), procedure_name.c_str());
#endif
IR_Block *block = ir_code->GetCode();
ir_controls = ir_code->FindOneLevelControlStructure(block);
for (int i = 0; i < ir_controls.size(); i++) {
if (ir_controls[i]->type() == IR_CONTROL_LOOP)
loops.push_back(i);
}
delete block;
}
if (myloop != NULL && myloop->isInitialized()) {
finalize_loop(L);
}
}
set_loop_num_start(L, loop_num_start);
set_loop_num_end(L, loop_num_end);
if (loop_num_end < loop_num_start) {
fprintf(stderr, "the last loop must be after the start loop");
if (!is_interactive)
exit(2);
}
if (loop_num_end >= loops.size()) {
fprintf(stderr, "loop %d does not exist", loop_num_end);
if (!is_interactive)
exit(2);
}
std::vector<IR_Control *> parm;
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++) {
if (ir_controls[i] == NULL) {
fprintf(stderr, "loop has already been processed");
if (!is_interactive)
exit(2);
}
parm.push_back(ir_controls[i]);
}
IR_Block *block = ir_code->MergeNeighboringControlStructures(parm);
myloop = new Loop(block);
delete block;
//if (is_interactive) printf("%s ", PROMPT_STRING);
}
static int loop(lua_State* L) {
// loop (n)
// loop (n:m)
int nargs = lua_gettop(L);
int start_num;
int end_num;
if(nargs == 1) {
start_num = luaL_optint(L, 1, 0);
end_num = start_num;
}
else if(nargs == 2) {
start_num = luaL_optint(L, 1, 0);
end_num = luaL_optint(L, 2, 0);
}
else {
fprintf(stderr, "loop takes one or two arguments");
if(!is_interactive)
exit(2);
}
init_loop(L, start_num, end_num);
return 0;
}
#endif
#ifdef CUDACHILL
static int exit(lua_State *L) {
strict_arg_num(L, 0);
repl_stop = true;
return 0;
}
static int print_code(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
myloop->printCode();
printf("\n");
END_REQUIRE_LOOP;
return 0;
}
static int print_ri(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
myloop->printRuntimeInfo();
printf("\n");
END_REQUIRE_LOOP;
return 0;
}
static int print_idx(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
myloop->printIndexes();
printf("\n");
END_REQUIRE_LOOP;
return 0;
}
static int print_dep(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
std::cout << myloop->dep;
END_REQUIRE_LOOP;
return 0;
}
static int print_space(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
for (int i = 0; i < myloop->stmt.size(); i++) {
printf("s%d: ", i + 1);
Relation r;
if (!myloop->stmt[i].xform.is_null())
r = Composition(copy(myloop->stmt[i].xform), copy(myloop->stmt[i].IS));
else
r = copy(myloop->stmt[i].IS);
r.simplify(2, 4);
r.print();
}END_REQUIRE_LOOP;
return 0;
}
static int num_statement(lua_State *L) {
REQUIRE_LOOP;
lua_pushinteger(L, myloop->stmt.size());
END_REQUIRE_LOOP;
return 1;
}
static int does_var_exists(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 1);
std::string symName = luaL_optstring(L,1,"");
lua_pushboolean(L, myloop->symbolExists(symName));
END_REQUIRE_LOOP;
return 1;
}
static int add_sync(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
int stmt = luaL_optint(L,1,0);
std::string idxName = luaL_optstring(L,2,"");
myloop->addSync(stmt, idxName);
END_REQUIRE_LOOP;
return 0;
}
static int rename_index(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
int stmt = luaL_optint(L,1,0);
std::string idxName = luaL_optstring(L,2,"");
std::string newName = luaL_optstring(L,3,"");
myloop->renameIndex(stmt, idxName, newName);
END_REQUIRE_LOOP;
return 0;
}
//basic on index names
static int permute_v2(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
int stmt = luaL_optint(L,1,0);
std::vector<std::string> order;
if (!tostringvector(L, 2, order)) {
throw std::runtime_error("second arg must be a string vector");
}
myloop->permute_cuda(stmt, order);
END_REQUIRE_LOOP;
return 0;
}
static int tile_v2(lua_State *L) {
REQUIRE_LOOP;
int n = lua_gettop(L); //Number of arguments
if (n != 3 && n != 7)
throw std::runtime_error("incorrect number of arguments");
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
if (n == 3) {
int outer_level = luaL_optint(L, 3, 1);
myloop->tile_cuda(stmt_num, level, outer_level);
} else {
int tile_size = luaL_optint(L, 3, 0);
int outer_level = luaL_optint(L, 4, 1);
std::string idxName = luaL_optstring(L,5,"");
std::string ctrlName = luaL_optstring(L,6,"");
TilingMethodType method = StridedTile;
if (n > 6) {
int imethod = luaL_optint(L, 7, 2);
if (imethod == 0)
method = StridedTile;
else if (imethod == 1)
method = CountedTile;
else {
throw std::runtime_error(
"7th argument must be either strided or counted");
}
}
myloop->tile_cuda(stmt_num, level, tile_size, outer_level, idxName,
ctrlName, method);
}END_REQUIRE_LOOP;
return 0;
}
static int cur_indices(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 1);
int stmt_num = luaL_optint(L, 1, 0);
//TODO: needs to be per stmt
lua_createtable(L, myloop->idxNames[stmt_num].size(), 0);
for (int i = 0; i < myloop->idxNames[stmt_num].size(); i++) {
lua_pushinteger(L, i + 1);
lua_pushstring(L, myloop->idxNames[stmt_num][i].c_str());
lua_settable(L, -3);
}END_REQUIRE_LOOP;
return 1;
}
static int block_indices(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
lua_newtable(L);
if (myloop->cu_bx > 1) {
lua_pushinteger(L, 1);
lua_pushstring(L, "bx");
lua_settable(L, -3);
}
if (myloop->cu_by > 1) {
lua_pushinteger(L, 2);
lua_pushstring(L, "by");
lua_settable(L, -3);
}END_REQUIRE_LOOP;
return 1;
}
static int thread_indices(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
lua_newtable(L);
if (myloop->cu_tx > 1) {
lua_pushinteger(L, 1);
lua_pushstring(L, "tx");
lua_settable(L, -3);
}
if (myloop->cu_ty > 1) {
lua_pushinteger(L, 2);
lua_pushstring(L, "ty");
lua_settable(L, -3);
}
if (myloop->cu_tz > 1) {
lua_pushinteger(L, 3);
lua_pushstring(L, "tz");
lua_settable(L, -3);
}END_REQUIRE_LOOP;
return 1;
}
static int block_dims(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
lua_pushinteger(L, myloop->cu_bx);
lua_pushinteger(L, myloop->cu_by);
END_REQUIRE_LOOP;
return 2;
}
static int thread_dims(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
lua_pushinteger(L, myloop->cu_tx);
lua_pushinteger(L, myloop->cu_ty);
lua_pushinteger(L, myloop->cu_tz);
END_REQUIRE_LOOP;
return 3;
}
static int hard_loop_bounds(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
int stmt = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int upper, lower;
myloop->extractCudaUB(stmt, level, upper, lower);
lua_pushinteger(L, lower);
lua_pushinteger(L, upper);
END_REQUIRE_LOOP;
return 2;
}
static int datacopy_v2(lua_State *L) {
REQUIRE_LOOP;
int n = lua_gettop(L); //Number of arguments
//overload 1
//examples:
// datacopy(0,4,a,false,0,1,-16)
// datacopy(0,3,2,1)
if (n < 4 || n > 9)
throw std::runtime_error("incorrect number of arguments");
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
const char* array_name = luaL_optstring(L, 3, 0);
std::vector<std::string> new_idxs;
if (!tostringvector(L, 4, new_idxs))
throw std::runtime_error("fourth argument must be an array of strings");
bool allow_extra_read = luaL_optboolean(L, 5, false);
int fastest_changing_dimension = luaL_optint(L, 6, -1);
int padding_stride = luaL_optint(L, 7, 1);
int padding_alignment = luaL_optint(L, 8, 1);
bool cuda_shared = luaL_optboolean(L, 9, false);
myloop->datacopy_cuda(stmt_num, level, array_name, new_idxs, allow_extra_read,
fastest_changing_dimension, padding_stride, padding_alignment,
cuda_shared);
END_REQUIRE_LOOP;
return 0;
}
static int datacopy_privatized_v2(lua_State *L) {
REQUIRE_LOOP;
int n = lua_gettop(L); //Number of arguments
//example:
//datacopy_privatized(0,3,"a",{4,5},false,-1,1,1)
if (n < 4 || n > 9)
throw std::runtime_error("incorrect number of arguments");
int stmt_num = luaL_optint(L, 1, 0);
std::string level_idx = luaL_optstring(L,2,"");
int level = myloop->findCurLevel(stmt_num, level_idx);
const char* array_name = luaL_optstring(L, 3, 0);
std::vector<std::string> privatized_idxs;
if (!tostringvector(L, 4, privatized_idxs))
throw std::runtime_error("4th argument must be an array of index strings");
std::vector<int> privatized_levels(privatized_idxs.size());
for (int i = 0; i < privatized_idxs.size(); i++)
privatized_levels[i] = myloop->findCurLevel(stmt_num, privatized_idxs[i]);
bool allow_extra_read = luaL_optboolean(L, 5, false);
int fastest_changing_dimension = luaL_optint(L, 6, -1);
int padding_stride = luaL_optint(L, 7, 1);
int padding_alignment = luaL_optint(L, 8, 1);
bool cuda_shared = luaL_optboolean(L, 9, false);
myloop->datacopy_privatized_cuda(stmt_num, level, array_name, privatized_levels,
allow_extra_read, fastest_changing_dimension, padding_stride,
padding_alignment, cuda_shared);
END_REQUIRE_LOOP;
return 0;
}
static int unroll_v2(lua_State *L) {
REQUIRE_LOOP;
//int n = lua_gettop(L); //Number of arguments
strict_arg_num(L, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level;
if (lua_isnumber(L, 2)) {
level = luaL_optint(L, 2, 0);
} else {
std::string level_idx = luaL_optstring(L,2,"");
level = myloop->findCurLevel(stmt_num, level_idx);
}
int unroll_amount = luaL_optint(L, 3, 0);
bool does_expand = myloop->unroll_cuda(stmt_num, level, unroll_amount);
lua_pushboolean(L, does_expand);
END_REQUIRE_LOOP;
return 1;
}
static int cudaize_v2(lua_State *L) {
REQUIRE_LOOP;
//int n = lua_gettop(L); //Number of arguments
strict_arg_num(L, 3);
std::string kernel_name = luaL_optstring(L, 1, 0);
std::vector<std::string> blockIdxs;
std::vector<std::string> threadIdxs;
std::map<std::string, int> array_sizes;
if (!tostringintmap(L, 2, array_sizes))
throw std::runtime_error("second argument must be a map[string->int]");
if (lua_istable(L, 3)) {
//Iterate through array (table)
lua_pushnil(L); // first key
while (lua_next(L, 3) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
if (strcmp(luaL_checkstring(L,-2), "block") == 0) {
if (!tostringvector(L, lua_gettop(L), blockIdxs))
throw std::runtime_error(
"third argument must have a string list for its 'block' key");
} else if (strcmp(luaL_checkstring(L,-2), "thread") == 0) {
if (!tostringvector(L, lua_gettop(L), threadIdxs))
throw std::runtime_error(
"third argument must have a string list for its 'thread' key");
} else {
goto v2NotTable;
}
lua_pop(L, 1);
}
} else {
v2NotTable: throw std::runtime_error(
"third argument must be a table with 'block' and 'thread' as potential keys and list of indexes as values");
}
myloop->cudaize_v2(kernel_name, array_sizes, blockIdxs, threadIdxs);
END_REQUIRE_LOOP;
return 0;
}
int get_loop_num(lua_State *L) {
lua_getglobal(L, "loop");
int loop_num = lua_tointeger(L, -1);
lua_pop(L, 1);
return loop_num;
}
static int copy_to_texture(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 1);
std::string array_name = luaL_optstring(L,1,0);
myloop->copy_to_texture(array_name.c_str());
END_REQUIRE_LOOP;
return 0;
}
/*static int copy_to_texture_2d(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
std::string array_name = luaL_optstring(L, 1, 0);
int width = luaL_optint(L, 2, 0);
int height = luaL_optint(L, 3, 0);
myloop->copy_to_texture_2d(array_name.c_str(), width, height);
END_REQUIRE_LOOP;
return 0;
}*/
//protonu-constant memory--place holder for now
static int copy_to_constant(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 1);
std::string array_name = luaL_optstring(L,1,0);
//call to loop->copy_to_texture goes here
myloop->copy_to_constant(array_name.c_str());
END_REQUIRE_LOOP;
return 0;
}
#else
static int print_code(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
myloop->printCode();
printf("\n");
END_REQUIRE_LOOP;
return 0;
}
static int print_dep(lua_State* L) {
REQUIRE_LOOP;
myloop->printDependenceGraph();
END_REQUIRE_LOOP;
return 0;
}
static int print_space(lua_State* L) {
REQUIRE_LOOP;
myloop->printIterationSpace();
END_REQUIRE_LOOP;
return 0;
}
static int exit(lua_State *L) {
strict_arg_num(L, 0);
repl_stop = true;
return 0;
}
static int known(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 1);
int num_dim = myloop->known.n_set();
// parse expression from string
std::vector<std::map<std::string, int> >* cond;
std::string cond_expr = luaL_optstring(L,1,0);
cond = parse_relation_vector(cond_expr.c_str());
Relation rel(num_dim);
F_And *f_root = rel.add_and();
for (int j = 0; j < cond->size(); j++) {
GEQ_Handle h = f_root->add_GEQ();
for (std::map<std::string, int>::iterator it = (*cond)[j].begin(); it != (*cond)[j].end(); it++) {
try {
int dim = from_string<int>(it->first);
if (dim == 0)
h.update_const(it->second);
else
throw std::invalid_argument("only symbolic variables are allowed in known condition");
}
catch (std::ios::failure e) {
Free_Var_Decl *g = NULL;
for (unsigned i = 0; i < myloop->freevar.size(); i++) {
std::string name = myloop->freevar[i]->base_name();
if (name == it->first) {
g = myloop->freevar[i];
break;
}
}
if (g == NULL)
throw std::invalid_argument("symbolic variable " + it->first + " not found");
else
h.update_coef(rel.get_local(g), it->second);
}
}
}
myloop->addKnown(rel);
END_REQUIRE_LOOP;
return 0;
}
static int remove_dep(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
int from = luaL_optint(L, 1, 0);
int to = luaL_optint(L, 2, 0);
myloop->removeDependence(from, to);
END_REQUIRE_LOOP;
return 0;
}
static int original(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 0);
myloop->original();
END_REQUIRE_LOOP;
return 0;
}
static int permute(lua_State *L) {
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 1) || (nargs > 3))
throw std::runtime_error("incorrect number of arguments in permute");
if(nargs == 1) {
// premute ( vector )
std::vector<int> pi;
if(!tointvector(L, 1, pi))
throw std::runtime_error("first arg in permute(pi) must be an int vector");
myloop->permute(pi);
}
else if (nargs == 2) {
// permute ( set, vector )
std::set<int> active;
std::vector<int> pi;
if(!tointset(L, 1, active))
throw std::runtime_error("the first argument in permute(active, pi) must be an int set");
if(!tointvector(L, 2, pi))
throw std::runtime_error("the second argument in permute(active, pi) must be an int vector");
myloop->permute(active, pi);
}
else if (nargs == 3) {
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
std::vector<int> pi;
if(!tointvector(L, 3, pi))
throw std::runtime_error("the third argument in permute(stmt_num, level, pi) must be an int vector");
myloop->permute(stmt_num, level, pi);
}
END_REQUIRE_LOOP;
return 0;
}
static int pragma(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
std::string pragmaText = luaL_optstring(L, 3, "");
myloop->pragma(stmt_num, level, pragmaText);
END_REQUIRE_LOOP;
return 0;
}
static int prefetch(lua_State *L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
std::string prefetchText = luaL_optstring(L, 3, "");
int hint = luaL_optint(L, 4, 0);
myloop->prefetch(stmt_num, level, prefetchText, hint);
END_REQUIRE_LOOP;
return 0;
}
static int tile(lua_State* L) {
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 3) || (nargs > 7))
throw std::runtime_error("incorrect number of arguments for tile");
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int tile_size = luaL_optint(L, 3, 0);
if(nargs == 3) {
myloop->tile(stmt_num, level, tile_size);
}
else if(nargs >= 4) {
int outer_level = luaL_optint(L, 4, 0);
if(nargs >= 5) {
TilingMethodType method = StridedTile;
int imethod = luaL_optint(L, 5, 2);
// check method input against expected values
if (imethod == 0)
method = StridedTile;
else if (imethod == 1)
method = CountedTile;
else
throw std::runtime_error("5th argument must be either strided or counted");
if(nargs >= 6) {
int alignment_offset = luaL_optint(L, 6, 0);
if(nargs == 7) {
int alignment_multiple = luaL_optint(L, 7, 1);
myloop->tile(stmt_num, level, tile_size, outer_level, method, alignment_offset, alignment_multiple);
}
if(nargs == 6)
myloop->tile(stmt_num, level, tile_size, outer_level, method, alignment_offset);
}
if(nargs == 5)
myloop->tile(stmt_num, level, tile_size, outer_level, method);
}
if(nargs == 4)
myloop->tile(stmt_num, level, tile_size, outer_level);
}
END_REQUIRE_LOOP;
return 0;
}
static int datacopy(lua_State* L) {
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 3) || (nargs > 7))
throw std::runtime_error("incorrect number of arguments for datacopy");
// Overload 1: bool datacopy(const std::vector<std::pair<int, std::vector<int> > > &array_ref_nums, int level, bool allow_extra_read = false, int fastest_changing_dimension = -1, int padding_stride = 1, int padding_alignment = 4, int memory_type = 0);
// Overload 2: bool datacopy(int stmt_num, int level, const std::string &array_name, bool allow_extra_read = false, int fastest_changing_dimension = -1, int padding_stride = 1, int padding_alignment = 4, int memory_type = 0);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
std::string array_name = std::string(luaL_optstring(L,3,0));
bool allow_extra_read = luaL_optboolean(L, 4, false);
int fastest_changing_dimension = luaL_optint(L, 5, -1);
int padding_stride = luaL_optint(L, 6, 1);
int padding_alignment = luaL_optint(L, 7, 4);
int memory_type = luaL_optint(L, 8, 0);
myloop->datacopy(stmt_num, level, array_name, allow_extra_read, fastest_changing_dimension, padding_stride, padding_alignment, memory_type);
END_REQUIRE_LOOP;
return 0;
}
static int datacopy_privatized(lua_State* L) {
// bool datacopy_privatized(int stmt_num, int level, const std::string &array_name, const std::vector<int> &privatized_levels, bool allow_extra_read = false, int fastest_changing_dimension = -1, int padding_stride = 1, int padding_alignment = 1, int memory_type = 0);
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 4) || (nargs > 9))
throw std::runtime_error("incorrect number of arguments for datacopy_privatized");
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
std::string array_name = std::string(luaL_optstring(L, 3, 0));
std::vector<int> privatized_levels;
tointvector(L, 4, privatized_levels);
bool allow_extra_read = luaL_optboolean(L, 5, false);
int fastest_changing_dimension = luaL_optint(L, 6, -1);
int padding_stride = luaL_optint(L, 7, 1);
int padding_alignment = luaL_optint(L, 8, 1);
int memory_type = luaL_optint(L, 9, 0);
myloop->datacopy_privatized(stmt_num, level, array_name, privatized_levels, allow_extra_read, fastest_changing_dimension, padding_stride, padding_alignment, memory_type);
END_REQUIRE_LOOP;
return 0;
}
static int unroll(lua_State* L) {
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 3) || (nargs > 4))
throw std::runtime_error("incorrect number of arguments for unroll");
//std::set<int> unroll(int stmt_num, int level, int unroll_amount, std::vector< std::vector<std::string> >idxNames= std::vector< std::vector<std::string> >(), int cleanup_split_level = 0);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int unroll_amount = luaL_optint(L, 3, 0);
std::vector< std::vector<std::string> > idxNames = std::vector< std::vector<std::string> >();
int cleanup_split_level = luaL_optint(L, 4, 0);
myloop->unroll(stmt_num, level, unroll_amount, idxNames, cleanup_split_level);
END_REQUIRE_LOOP;
return 0;
}
static int unroll_extra(lua_State* L) {
REQUIRE_LOOP;
int nargs = lua_gettop(L);
if((nargs < 3) || (nargs < 4))
throw std::runtime_error("incorrect number of arguments for unroll_extra");
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int unroll_amount = luaL_optint(L, 3, 0);
int cleanup_split_level = luaL_optint(L, 4, 0);
myloop->unroll_extra(stmt_num, level, unroll_amount, cleanup_split_level);
END_REQUIRE_LOOP;
return 0;
}
static int split(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int num_dim = myloop->stmt[stmt_num].xform.n_out();
// parse expression from string
std::vector<std::map<std::string, int> >* cond;
std::string cond_expr = luaL_optstring(L,3,0);
cond = parse_relation_vector(cond_expr.c_str());
Relation rel((num_dim-1)/2);
F_And *f_root = rel.add_and();
for (int j = 0; j < cond->size(); j++) {
GEQ_Handle h = f_root->add_GEQ();
for (std::map<std::string, int>::iterator it = (*cond)[j].begin(); it != (*cond)[j].end(); it++) {
try {
int dim = from_string<int>(it->first);
if (dim == 0)
h.update_const(it->second);
else {
if (dim > (num_dim-1)/2)
throw std::invalid_argument("invalid loop level " + to_string(dim) + " in split condition");
h.update_coef(rel.set_var(dim), it->second);
}
}
catch (std::ios::failure e) {
Free_Var_Decl *g = NULL;
for (unsigned i = 0; i < myloop->freevar.size(); i++) {
std::string name = myloop->freevar[i]->base_name();
if (name == it->first) {
g = myloop->freevar[i];
break;
}
}
if (g == NULL)
throw std::invalid_argument("unrecognized variable " + to_string(it->first.c_str()));
h.update_coef(rel.get_local(g), it->second);
}
}
}
myloop->split(stmt_num,level,rel);
END_REQUIRE_LOOP;
return 0;
}
static int nonsingular(lua_State* L) {
REQUIRE_LOOP;
std::vector< std::vector<int> > mat;
tointmatrix(L, 1, mat);
myloop->nonsingular(mat);
END_REQUIRE_LOOP;
return 0;
}
static int skew(lua_State* L) {
REQUIRE_LOOP;
std::set<int> stmt_nums;
std::vector<int> skew_amounts;
int level = luaL_optint(L, 2, 0);
tointset(L, 1, stmt_nums);
tointvector(L, 3, skew_amounts);
myloop->skew(stmt_nums, level, skew_amounts);
END_REQUIRE_LOOP;
return 0;
}
static int scale(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
std::set<int> stmt_nums;
int level = luaL_optint(L, 2, 0);
int scale_amount = luaL_optint(L, 3, 0);
tointset(L, 1, stmt_nums);
myloop->scale(stmt_nums, level, scale_amount);
END_REQUIRE_LOOP;
return 0;
}
static int reverse(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
std::set<int> stmt_nums;
int level = luaL_optint(L, 2, 0);
tointset(L, 1, stmt_nums);
myloop->reverse(stmt_nums, level);
END_REQUIRE_LOOP;
return 0;
}
static int shift(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
std::set<int> stmt_nums;
int level = luaL_optint(L, 2, 0);
int shift_amount = luaL_optint(L, 3, 0);
tointset(L, 1, stmt_nums);
myloop->shift(stmt_nums, level, shift_amount);
END_REQUIRE_LOOP;
return 0;
}
static int shift_to(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int absolute_pos = luaL_optint(L, 3, 0);
myloop->shift_to(stmt_num, level, absolute_pos);
END_REQUIRE_LOOP;
return 0;
}
static int peel(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 2, 3);
int stmt_num = luaL_optint(L, 1, 0);
int level = luaL_optint(L, 2, 0);
int amount = luaL_optint(L, 3, 1);
myloop->peel(stmt_num, level, amount);
END_REQUIRE_LOOP;
return 0;
}
static int fuse(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
std::set<int> stmt_nums;
int level = luaL_optint(L, 2, 0);
tointset(L, 1, stmt_nums);
myloop->fuse(stmt_nums, level);
END_REQUIRE_LOOP;
return 0;
}
static int distribute(lua_State* L) {
REQUIRE_LOOP;
strict_arg_num(L, 2);
std::set<int> stmts;
int level = luaL_optint(L, 1, 0);
tointset(L, 2, stmts);
myloop->distribute(stmts, level);
END_REQUIRE_LOOP;
return 0;
}
static int num_statements(lua_State *L) {
REQUIRE_LOOP;
lua_pushinteger(L, myloop->stmt.size());
END_REQUIRE_LOOP;
return 1;
}
#endif
/**
* Register global methods available to chill scripts
*/
void register_globals(lua_State *L) {
//---
//Preset globals
//---
lua_pushstring(L, CHILL_BUILD_VERSION);
lua_setglobal(L, "VERSION");
lua_pushstring(L, "C");
lua_setglobal(L, "dest");
lua_pushstring(L, "C");
lua_setglobal(L, "C");
//---
//Enums for functions
//---
//TileMethod
lua_pushinteger(L, 0);
lua_setglobal(L, "strided");
lua_pushinteger(L, 1);
lua_setglobal(L, "counted");
//MemoryMode
lua_pushinteger(L, 0);
lua_setglobal(L, "global");
lua_pushinteger(L, 1);
lua_setglobal(L, "shared");
lua_pushinteger(L, 2);
lua_setglobal(L, "texture");
//Bool flags
lua_pushboolean(L, 1);
lua_setglobal(L, "sync");
//...
}
#ifdef CUDACHILL
void register_functions(lua_State *L) {
lua_register(L, "init", init);
lua_register(L, "exit", exit);
lua_register(L, "print_code", print_code);
lua_register(L, "print_ri", print_ri);
lua_register(L, "print_idx", print_idx);
lua_register(L, "print_dep", print_dep);
lua_register(L, "print_space", print_space);
lua_register(L, "num_statement", num_statement);
}
void register_v2(lua_State *L) {
lua_register(L, "cudaize", cudaize_v2);
lua_register(L, "tile", tile_v2);
lua_register(L, "permute", permute_v2);
lua_register(L, "datacopy_privatized", datacopy_privatized_v2);
lua_register(L, "datacopy", datacopy_v2);
lua_register(L, "unroll", unroll_v2);
lua_register(L, "cur_indices", cur_indices);
lua_register(L, "block_indices", block_indices);
lua_register(L, "thread_indices", thread_indices);
lua_register(L, "block_dims", block_dims);
lua_register(L, "thread_dims", thread_dims);
lua_register(L, "hard_loop_bounds", hard_loop_bounds);
lua_register(L, "num_statements", num_statement);
lua_register(L, "does_exists", does_var_exists);
lua_register(L, "add_sync", add_sync);
lua_register(L, "rename_index", rename_index);
lua_register(L, "copy_to_texture", copy_to_texture);
lua_register(L, "copy_to_constant", copy_to_constant);
}
#else // CHiLL
void register_functions(lua_State* L) {
lua_register(L, "source", source);
lua_register(L, "procedure", procedure);
lua_register(L, "loop", loop);
lua_register(L, "print_code", print_code);
lua_register(L, "print_dep", print_dep);
lua_register(L, "print_space", print_space);
lua_register(L, "exit", exit);
lua_register(L, "known", known);
lua_register(L, "remove_dep", remove_dep);
lua_register(L, "original", original);
lua_register(L, "permute", permute);
lua_register(L, "pragma", pragma);
lua_register(L, "prefetch", prefetch);
lua_register(L, "tile", tile);
lua_register(L, "datacopy", datacopy);
lua_register(L, "datacopy_privatised", datacopy_privatized);
lua_register(L, "unroll", unroll);
lua_register(L, "unroll_extra", unroll_extra);
lua_register(L, "split", split);
lua_register(L, "nonsingular", nonsingular);
lua_register(L, "skew", skew);
lua_register(L, "scale", scale);
lua_register(L, "reverse", reverse);
lua_register(L, "shift", shift);
lua_register(L, "shift_to", shift_to);
lua_register(L, "peel", peel);
lua_register(L, "fuse", fuse);
lua_register(L, "distribute", distribute);
lua_register(L, "num_statements", num_statements);
}
#endif
|