-
Notifications
You must be signed in to change notification settings - Fork 6
/
chillmodule.cc
1998 lines (1692 loc) · 72 KB
/
chillmodule.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
// chill interface to python
#include "chill_io.hh"
#ifdef CUDACHILL
#include "rose.h" // ??
#include "loop_cuda_chill.hh"
#include "ir_rose.hh"
#include "ir_cudarose.hh"
#include <vector>
#else
#include "chill_run_util.hh"
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omega.h>
#include "loop.hh"
#include "ir_code.hh"
#ifdef FRONTEND_ROSE
#include "ir_rose.hh"
#endif
#endif
#include "chillmodule.hh"
// TODO
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <Python.h>
using namespace omega;
// -- Cuda CHiLL global variables --
#ifdef CUDACHILL
extern LoopCuda *myloop;
extern IR_Code *ir_code;
extern std::vector<IR_Control *> ir_controls;
extern std::vector<int> loops;
#else
extern Loop *myloop;
extern IR_Code *ir_code;
extern bool is_interactive;
extern bool repl_stop;
std::string procedure_name;
std::string source_filename;
std::string dest_filename;
int loop_start_num;
int loop_end_num;
extern std::vector<IR_Control *> ir_controls;
extern std::vector<int> loops;
#endif
// ----------------------- //
// CHiLL support functions //
// ----------------------- //
#ifndef CUDACHILL
// not sure yet if this actually needs to be exposed to the python interface
// these four functions are here to maintain similarity to the Lua interface
int get_loop_num_start() {
return loop_start_num;
}
int get_loop_num_end() {
return loop_end_num;
}
static void set_loop_num_start(int start_num) {
loop_start_num = start_num;
}
static void set_loop_num_end(int end_num) {
loop_end_num = end_num;
}
// TODO: finalize_loop(int,int) and init_loop(int,int) are identical to thier Lua counterparts.
// consider integrating them
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() {
int loop_num_start = get_loop_num_start();
int loop_num_end = get_loop_num_end();
finalize_loop(loop_num_start, loop_num_end);
}
static void init_loop(int loop_num_start, int loop_num_end) {
if (source_filename.empty()) {
debug_fprintf(stderr, "source file not set when initializing the loop");
if (!is_interactive)
exit(2);
}
else {
if (ir_code == NULL) {
#ifdef FRONTEND_ROSE
if (procedure_name.empty())
procedure_name = "main";
#endif
#ifdef FRONTEND_ROSE
if(dest_filename.empty()) {
ir_code = new IR_roseCode(source_filename.c_str(), procedure_name.c_str());
}
else {
ir_code = new IR_roseCode(source_filename.c_str(), procedure_name.c_str(), dest_filename.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();
}
}
set_loop_num_start(loop_num_start);
set_loop_num_end(loop_num_end);
if (loop_num_end < loop_num_start) {
debug_fprintf(stderr, "the last loop must be after the start loop");
if (!is_interactive)
exit(2);
}
if (loop_num_end >= loops.size()) {
debug_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) {
debug_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);
}
#endif
// ----------------------- //
// Python support funcions //
// ----------------------- //
// -- CHiLL support -- //
static void strict_arg_num(PyObject* args, int arg_num, const char* fname = NULL) {
int arg_given = PyTuple_Size(args);
char msg[128];
if(arg_num != arg_given) {
if(fname)
sprintf(msg, "%s: expected %i arguments, was given %i.", fname, arg_num, arg_given);
else
sprintf(msg, "Expected %i argumets, was given %i.", arg_num, arg_given);
throw std::runtime_error(msg);
}
}
static int strict_arg_range(PyObject* args, int arg_min, int arg_max, const char* fname = NULL) {
int arg_given = PyTuple_Size(args);
char msg[128];
if(arg_given < arg_min || arg_given > arg_max) {
if(fname)
sprintf(msg, "%s: expected %i to %i arguments, was given %i.", fname, arg_min, arg_max, arg_given);
else
sprintf(msg, "Expected %i to %i, argumets, was given %i.", arg_min, arg_max, arg_given);
throw std::runtime_error(msg);
}
return arg_given;
}
static int intArg(PyObject* args, int index, int dval = 0) {
if(PyTuple_Size(args) <= index)
return dval;
int ival;
PyObject *item = PyTuple_GetItem(args, index);
Py_INCREF(item);
if (PyInt_Check(item)) ival = PyInt_AsLong(item);
else {
debug_fprintf(stderr, "argument at index %i is not an int\n", index);
exit(-1);
}
return ival;
}
static std::string strArg(PyObject* args, int index, const char* dval = NULL) {
if(PyTuple_Size(args) <= index)
return dval;
std::string strval;
PyObject *item = PyTuple_GetItem(args, index);
Py_INCREF(item);
if (PyString_Check(item)) strval = strdup(PyString_AsString(item));
else {
debug_fprintf(stderr, "argument at index %i is not an string\n", index);
exit(-1);
}
return strval;
}
static bool boolArg(PyObject* args, int index, bool dval = false) {
if(PyTuple_Size(args) <= index)
return dval;
bool bval;
PyObject* item = PyTuple_GetItem(args, index);
Py_INCREF(item);
return (bool)PyObject_IsTrue(item);
}
static bool tostringintmapvector(PyObject* args, int index, std::vector<std::map<std::string,int> >& vec) {
if(PyTuple_Size(args) <= index)
return false;
PyObject* seq = PyTuple_GetItem(args, index);
//TODO: Typecheck
int seq_len = PyList_Size(seq);
for(int i = 0; i < seq_len; i++) {
std::map<std::string,int> map;
PyObject* dict = PyList_GetItem(seq, i);
PyObject* keys = PyDict_Keys(dict);
//TODO: Typecheck
int dict_len = PyList_Size(keys);
for(int j = 0; j < dict_len; j++) {
PyObject* key = PyList_GetItem(keys, j);
PyObject* value = PyDict_GetItem(dict, key);
std::string str_key = strdup(PyString_AsString(key));
int int_value = PyInt_AsLong(value);
map[str_key] = int_value;
}
vec.push_back(map);
}
return true;
}
static bool tointvector(PyObject* seq, std::vector<int>& vec) {
//TODO: Typecheck
int seq_len = PyList_Size(seq);
for(int i = 0; i < seq_len; i++) {
PyObject* item = PyList_GetItem(seq, i);
vec.push_back(PyInt_AsLong(item));
}
return true;
}
static bool tostringvector(PyObject* seq, std::vector<std::string>& vec) {
int seq_len = PyList_Size(seq);
for(int i = 0; i < seq_len; i++) {
PyObject* item = PyList_GetItem(seq, i);
vec.push_back(std::string(strdup(PyString_AsString(item))));
}
return true;
}
static bool tointvector(PyObject* args, int index, std::vector<int>& vec) {
if(PyTuple_Size(args) <= index)
return false;
PyObject* seq = PyTuple_GetItem(args, index);
return tointvector(seq, vec);
}
static bool tostringvector(PyObject* args, int index, std::vector<std::string>& vec) {
if(PyTuple_Size(args) <= index)
return false;
PyObject* seq = PyTuple_GetItem(args, index);
return tostringvector(seq, vec);
}
static bool tointset(PyObject* args, int index, std::set<int>& set) {
if(PyTuple_Size(args) <= index)
return false;
PyObject* seq = PyTuple_GetItem(args, index);
//TODO: Typecheck
int seq_len = PyList_Size(seq);
for(int i = 0; i < seq_len; i++) {
PyObject* item = PyList_GetItem(seq, i);
set.insert(PyInt_AsLong(item));
}
return true;
}
static bool tointmatrix(PyObject* args, int index, std::vector<std::vector<int> >& mat) {
if(PyTuple_Size(args) <= index)
return false;
PyObject* seq_one = PyTuple_GetItem(args, index);
int seq_one_len = PyList_Size(seq_one);
for(int i = 0; i < seq_one_len; i++) {
std::vector<int> vec;
PyObject* seq_two = PyList_GetItem(seq_one, i);
int seq_two_len = PyList_Size(seq_two);
for(int j = 0; j < seq_two_len; j++) {
PyObject* item = PyList_GetItem(seq_two, j);
vec.push_back(PyInt_AsLong(item));
}
mat.push_back(vec);
}
return true;
}
#ifdef CUDACHILL
// ------------------------------ //
// Cuda CHiLL interface functions //
// ------------------------------ //
static PyObject *
chill_print_code(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\nC print_code() PY\n");
((Loop*)myloop)->printCode();
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_print_ri(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\nC chill_print_ri() called from python\n");
myloop->printRuntimeInfo();
debug_fprintf(stderr, "\n");
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_print_idx(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\nC chill_print_idx() called from python\n");
myloop->printIndexes();
debug_fprintf(stderr, "\n");
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_print_dep(PyObject *self, PyObject *args)
{
debug_fprintf(stderr, "\nC chill_print_dep()\n");
std::cout << myloop->dep;
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_print_space(PyObject *self, PyObject *args)
{
debug_fprintf(stderr, "\nC chill_print_space()\n");
for (int i = 0; i < myloop->stmt.size(); i++) {
debug_fprintf(stderr, "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();
}
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_num_statements(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\nC chill_num_statements() called from python\n");
int num = myloop->stmt.size();
//DEBUG_PRINT("C num_statement() = %d\n", num);
return Py_BuildValue( "i", num ); // BEWARE "d" is DOUBLE, not int
}
static PyObject *
chill_does_var_exist( PyObject *self, PyObject *args)
{
debug_fprintf(stderr, "\nC chill_does_var_exist()\n");
int yesno = 0;
// TODO if (myloop->symbolExists(symName)) yesno = 1;
debug_fprintf(stderr, "*** chill_does_var_exist *** UNIMPLEMENTED\n");
return Py_BuildValue( "i", yesno); // there seems to be no boolean type
}
static PyObject *
chill_add_sync(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\nC chill_add_sync() *UNTESTED*\n");
int sstmt = -123;
// char index_name[180];
static char Buffer[1024];
static char *index_name = &Buffer[0];
if (!PyArg_ParseTuple(args, "is", &sstmt, &index_name)){
debug_fprintf(stderr, "chill_add_sync, can't parse statement number and name passed from python\n");
exit(-1);
}
debug_fprintf(stderr, "chill_add_sync, statement %d index_name '%s'\n",
sstmt, index_name);
std::string idxName( index_name); // ??
myloop->addSync(sstmt, idxName);
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_rename_index(PyObject *self, PyObject *args)
{
debug_fprintf(stderr, "\nC chill_rename_index() called from python\n");
int sstmt;
//char oldname[80], newname[80];
static char old[1024], newn[1024];
static char *oldname = &old[0], *newname=&newn[0];
if (!PyArg_ParseTuple(args, "iss", &sstmt, &oldname, &newname)){
debug_fprintf(stderr, "chill_rename_index, can't parse statement number and names passed from python\n");
exit(-1);
}
//DEBUG_PRINT("chill_rename_index, statement %d oldname '%s' newname '%s'\n",
//sstmt, oldname, newname);
std::string idxName(oldname);
std::string newName(newname);
//DEBUG_PRINT("calling myloop->renameIndex( %d, %s, %s )\n",
//sstmt, idxName.c_str(), newName.c_str());
myloop->renameIndex(sstmt, idxName, newName);
//DEBUG_PRINT("after myloop->renameIndex()\n");
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
//THIS NEEDS TO MOVE
static PyObject *
chill_permute_v2(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("C permute_v2()\n");
//int tot = sizeof(args);
//int things = tot / sizeof(PyObject *);
//DEBUG_PRINT("tot %d bytes, %d things\n", tot, things);
int sstmt = -123;
PyObject *pyObj;
//if (!PyArg_ParseTuple( args, "iO", &sstmt, &pyObj)) {
//if (!PyArg_ParseTuple( args, "i", &sstmt)) {
if (!PyArg_ParseTuple( args, "O", &pyObj)) { // everything on a single tuple
debug_fprintf(stderr, "failed to parse tuple\n");
exit(-1);
}
Py_XINCREF(pyObj);
// the ONLY arg is a tuple. figure out how big it is
int tupleSize = PyTuple_Size(pyObj);
//DEBUG_PRINT("%d things in order tuple\n", tupleSize);
// first has to be the statement number
PyObject *tupleItem = PyTuple_GetItem(pyObj, 0);
Py_XINCREF(tupleItem);
if (PyInt_Check( tupleItem )) sstmt = PyInt_AsLong( tupleItem );
else {
fflush(stdout);
debug_fprintf(stderr, "first tuple item in chill_permute_v2 is not an int?\n");
exit(-1);
}
//DEBUG_PRINT("stmt %d\n", sstmt);
char **strings;
std::vector<std::string> order;
std::string *cppstrptr;
std::string cppstr;
strings = (char **) malloc( sizeof(char *) * tupleSize ) ; // too big
for (int i=1; i<tupleSize; i++) {
tupleItem = PyTuple_GetItem(pyObj, i);
Py_XINCREF(tupleItem);
int im1 = i-1; // offset needed for the actual string vector
if (PyString_Check( tupleItem)) {
strings[im1] = strdup(PyString_AsString(tupleItem));
//DEBUG_PRINT("item %d = '%s'\n", i, strings[im1]);
//cppstrptr = new std::string( strings[im1] );
//order.push_back( &(new std::string( strings[im1] )));
//order.push_back( &cppstrptr );
cppstr = strings[im1];
order.push_back( cppstr );
}
else {
debug_fprintf(stderr, "later parameter was not a string?\n");
exit(-1);
}
}
myloop->permute_cuda(sstmt,order);
//DEBUG_PRINT("returned from permute_cuda()\n");
Py_RETURN_NONE; // return Py_BuildValue( "" );
}
static PyObject *
chill_tile_v2_3arg( PyObject *self, PyObject *args)
{
//DEBUG_PRINT("in chillmodule.cc, chill_tile_v2_3arg()\n");
int sstmt, level, tile_size, outer_level;
//char index_name[80], control_name[80];
static char *index_name, *control_name;
int tiling_method;
if (!PyArg_ParseTuple(args, "iii", &sstmt, &level, &outer_level)) {
debug_fprintf(stderr,"chill_tile_v2, can't parse parameters passed from python\n");
exit(-1);
}
// 3 parameter version
//DEBUG_PRINT("chill_tile_v2( %d %d %d) (3 parameter version) \n",
//sstmt,level,outer_level);
myloop->tile_cuda(sstmt,level,outer_level);
//DEBUG_PRINT("chill_tile_v2 3 parameter version returning normally\n");
Py_RETURN_NONE;
}
static PyObject *
chill_tile_v2_7arg( PyObject *self, PyObject *args)
{
//DEBUG_PRINT("in chillmodule.cc, chill_tile_v2_7arg()\n");
int sstmt, level, tile_size, outer_level;
//char index_name[80], control_name[80];
static char iname[1024], cname[1024];
static char *index_name = &iname[0], *control_name=&cname[0];
int tiling_method;
if (!PyArg_ParseTuple(args, "iiiissi",
&sstmt, &level, &tile_size, &outer_level,
&index_name, &control_name, &tiling_method)){
debug_fprintf(stderr, "chill_tile_v2_7arg, can't parse parameters passed from python\n");
exit(-1);
}
//DEBUG_PRINT("7 parameter version was called?\n");
// 7 parameter version was called
//DEBUG_PRINT("tile_v2( %d, %d, %d, %d ... )\n",
// sstmt, level, tile_size, outer_level);
//DEBUG_PRINT("tile_v2( %d, %d, %d, %d, %s, %s, %d)\n",
//sstmt,level,tile_size,outer_level,index_name, control_name, tiling_method);
TilingMethodType method = StridedTile;
if (tiling_method == 0) method = StridedTile;
else if (tiling_method == 1) method = CountedTile;
else debug_fprintf(stderr, "ERROR: tile_v2 illegal tiling method, using StridedTile\n");
//DEBUG_PRINT("outer level %d\n", outer_level);
//DEBUG_PRINT("calling myloop->tile_cuda( %d, %d, %d, %d, %s, %s, method)\n",
// sstmt, level, tile_size, outer_level, index_name, control_name);
// level+1?
myloop->tile_cuda(sstmt, level, tile_size, outer_level, index_name, control_name, method);
Py_RETURN_NONE;
}
static PyObject *
chill_cur_indices(PyObject *self, PyObject *args)
{
debug_fprintf(stderr, "cur_indices( %d )\n", stmt_num);
int stmt_num = -123;
if (!PyArg_ParseTuple(args, "i", &stmt_num)){
chill_fprintf(stderr, "chill_cur_indices, can't parse statement number passed from python\n");
exit(-1);
}
char formatstring[1024];
for (int i=0; i<1024; i++) formatstring[i] = '\0';
int num = myloop->idxNames[stmt_num].size();
for(int i=0; i<num; i++){
//DEBUG_PRINT("myloop->idxNames[%d] index %d = '%s'\n",
//stmt_num, i, myloop->idxNames[stmt_num][i].c_str());
// backwards, works because all entries are the same
//sprintf(formatstring, "i %s", formatstring);
strcat( formatstring, "s ");
// put this in a list or something to pass back to python
}
int l = strlen(formatstring);
if (l > 0) formatstring[l-1] = '\0';
//DEBUG_PRINT("%d current indices, format string '%s'\n\n",num,formatstring);
//DEBUG_PRINT("%d current indices\n\n", num);
//return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),myloop->idxNames[stmt_num][1].c_str() );
// I don't know a clean way to do this.
if (num == 2) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str());
if (num == 3) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str());
if (num == 4) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str());
if (num == 5) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str());
if (num == 6) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str());
if (num == 7) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str());
if (num == 8) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str());
if (num == 9) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str());
if (num == 10) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str());
if (num == 11) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str());
if (num == 12) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str());
if (num == 13) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str());
if (num == 14) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str(),
myloop->idxNames[stmt_num][13].c_str());
if (num == 15) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str(),
myloop->idxNames[stmt_num][13].c_str(),
myloop->idxNames[stmt_num][14].c_str());
if (num == 16) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str(),
myloop->idxNames[stmt_num][13].c_str(),
myloop->idxNames[stmt_num][14].c_str(),
myloop->idxNames[stmt_num][15].c_str());
if (num == 17) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str(),
myloop->idxNames[stmt_num][13].c_str(),
myloop->idxNames[stmt_num][14].c_str(),
myloop->idxNames[stmt_num][15].c_str(),
myloop->idxNames[stmt_num][16].c_str());
if (num == 18) return Py_BuildValue(formatstring, myloop->idxNames[stmt_num][0].c_str(),
myloop->idxNames[stmt_num][1].c_str(),
myloop->idxNames[stmt_num][2].c_str(),
myloop->idxNames[stmt_num][3].c_str(),
myloop->idxNames[stmt_num][4].c_str(),
myloop->idxNames[stmt_num][5].c_str(),
myloop->idxNames[stmt_num][6].c_str(),
myloop->idxNames[stmt_num][7].c_str(),
myloop->idxNames[stmt_num][8].c_str(),
myloop->idxNames[stmt_num][9].c_str(),
myloop->idxNames[stmt_num][10].c_str(),
myloop->idxNames[stmt_num][11].c_str(),
myloop->idxNames[stmt_num][12].c_str(),
myloop->idxNames[stmt_num][13].c_str(),
myloop->idxNames[stmt_num][14].c_str(),
myloop->idxNames[stmt_num][15].c_str(),
myloop->idxNames[stmt_num][16].c_str(),
myloop->idxNames[stmt_num][17].c_str());
debug_fprintf(stderr, "going to die horribly, num=%d\n", num);
}
static PyObject *
chill_block_indices(PyObject *self, PyObject *args) {
// I'm unsure what the legal states are here
// is it always "bx", or ("bx" and "by") ?
int howmany = 0;
char *loopnames[2];
if (myloop->cu_bx > 1) {
loopnames[howmany] = strdup("bx");
howmany++;
}
if (myloop->cu_by > 1) {
loopnames[howmany] = strdup("by");
howmany++;
}
if (howmany == 0) return Py_BuildValue("()");
if (howmany == 1) return Py_BuildValue("(s)", loopnames[0]);
if (howmany == 2) return Py_BuildValue("(ss)", loopnames[0], loopnames[1]);
debug_fprintf(stderr, "chill_block_indices(), gonna die, howmany == %d", howmany);
exit(666);
Py_RETURN_NONE;
}
static PyObject *
chill_thread_indices(PyObject *self, PyObject *args) {
// I'm unsure what the legal states are here
// is it always "tx", or ("tx" and "ty") or ("tx" and "ty" and "tz") ?
int howmany = 0;
char *loopnames[3];
if (myloop->cu_tx > 1) {
loopnames[howmany++] = strdup("tx");
}
if (myloop->cu_ty > 1) {
loopnames[howmany++] = strdup("ty");
}
if (myloop->cu_tz > 1) {
loopnames[howmany++] = strdup("tz");
}
if (howmany == 0) return Py_BuildValue("()");
if (howmany == 1) return Py_BuildValue("(s)",
loopnames[0]);
if (howmany == 2) return Py_BuildValue("(ss)",
loopnames[0],
loopnames[1]);
if (howmany == 3) return Py_BuildValue("(sss)",
loopnames[0],
loopnames[1],
loopnames[2]);
debug_fprintf(stderr, "chill_thread_indices(), gonna die, howmany == %d", howmany);
exit(999);
}
static PyObject *
block_dims(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("block_dims() returning %d %d\n", myloop->cu_bx, myloop->cu_by);
Py_BuildValue( "i i", myloop->cu_bx, myloop->cu_by);
}
static PyObject *
thread_dims(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("thread_dims() returning %d %d %d\n",
//myloop->cu_tx, myloop->cu_ty, myloop->cu_tz);
Py_BuildValue( "i i i", myloop->cu_tx, myloop->cu_ty, myloop->cu_tz);
}
static PyObject *
chill_hard_loop_bounds(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("hard_loop_bounds(");
int sstmt, level; // input parameters
int upper, lower; // output
if (!PyArg_ParseTuple(args, "ii", &sstmt, &level)){
debug_fprintf(stderr, "hard_loop_bounds, ");
debug_fprintf(stderr, "can't parse statement numbers passed from python\n");
exit(-1);
}
//DEBUG_PRINT(" %d, %d )\n", sstmt, level);
myloop->extractCudaUB(sstmt, level, upper, lower);
//DEBUG_PRINT("lower %d upper %d\n", lower, upper);
Py_BuildValue( "i i", lower, upper);
}
static PyObject *
chill_datacopy9(PyObject *self, PyObject *args)
{
//DEBUG_PRINT("\n\n\n***** datacopy_v2() 9ARGS\n");
int sstmt;
int level;
std::string cppstr;
std::string array_name;
std::vector<std::string> new_idxs;
bool allow_extra_read;
int fastest_changing_dimension;
int padding_stride;
int padding_alignment;
bool cuda_shared;
PyObject *pyObj;
if (!PyArg_ParseTuple( args, "O", &pyObj)) { // everything on a single tuple
debug_fprintf(stderr, "failed to parse tuple\n");
exit(-1);
}
Py_XINCREF( pyObj );
//if (PyList_Check(pyObj)) debug_fprintf(stderr, "it's a list\n");
//if (PyTuple_Check(pyObj)) debug_fprintf(stderr, "it's a tuple\n");
// the ONLY arg is a tuple. figure out how big it is
int tupleSize = PyTuple_Size(pyObj);
//DEBUG_PRINT("%d things in object tuple\n", tupleSize);
// first has to be the statement number
PyObject *tupleItem1 = PyTuple_GetItem(pyObj, 0);
Py_INCREF(tupleItem1);
if (PyInt_Check( tupleItem1)) sstmt = PyInt_AsLong( tupleItem1 );
else {
debug_fprintf(stderr, "second tuple item in chill_datacopy9 is not an int?\n");
exit(-1);
}
//DEBUG_PRINT("stmt %d\n", sstmt);
PyObject *tupleItem2 = PyTuple_GetItem(pyObj, 1); // second item is level
Py_INCREF(tupleItem2);
if (PyInt_Check( tupleItem2 )) level = PyInt_AsLong( tupleItem2);
else {
debug_fprintf(stderr, "second tuple item in chill_datacopy9 is not an int?\n");
exit(-1);
}
//DEBUG_PRINT("level %d\n", level );
// third item is array name
PyObject *tupleItem3 = PyTuple_GetItem(pyObj, 2);
Py_INCREF(tupleItem3);
array_name = strdup(PyString_AsString(tupleItem3));
//DEBUG_PRINT("array name '%s'\n", array_name.c_str());
// integer number of indices
PyObject *tupleItem4 = PyTuple_GetItem(pyObj, 3);
Py_INCREF(tupleItem4);
int numindex= PyInt_AsLong( tupleItem4 );
//DEBUG_PRINT("%d indices\n", numindex);
PyObject *tupleItemTEMP;
for (int i=0; i<numindex; i++) {
tupleItemTEMP = PyTuple_GetItem(pyObj, 4+i);
Py_INCREF(tupleItemTEMP);
cppstr = strdup(PyString_AsString(tupleItemTEMP));
new_idxs.push_back( cppstr );
//DEBUG_PRINT("%s\n", cppstr.c_str());
}
PyObject *tupleItem5 = PyTuple_GetItem(pyObj, 4+numindex);
Py_INCREF(tupleItem5);
allow_extra_read = PyInt_AsLong( tupleItem5 );
PyObject *tupleItem6 = PyTuple_GetItem(pyObj, 5+numindex);
Py_INCREF(tupleItem6);
fastest_changing_dimension = PyInt_AsLong( tupleItem6 );
PyObject *tupleItem7 = PyTuple_GetItem(pyObj, 6+numindex);
Py_INCREF(tupleItem7);
padding_stride = PyInt_AsLong( tupleItem7 );