-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOperator.cpp
More file actions
1180 lines (1042 loc) · 39.7 KB
/
Operator.cpp
File metadata and controls
1180 lines (1042 loc) · 39.7 KB
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
/*
* Operator.cpp
* evaluate the expression graph
*
=======================================================*/
#include "Operator.hpp"
#include "utils/utils.hpp"
#include "utils/calcTime.hpp"
#include "Kernel.hpp"
#include "Jit_Driver.hpp"
#include "Grid.hpp"
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "Diagnosis.hpp"
#include <boost/format.hpp>
using namespace oa::kernel;
namespace oa {
namespace ops{
// needs to set all attributes to the new node
NodePtr new_node(const ArrayPtr &ap) {
NodePtr np = NodePool::global()->get();
np->set_type(TYPE_DATA);
np->set_data(ap);
np->set_data_type(ap->get_data_type());
np->set_shape(ap->shape());
np->set_scalar(ap->is_scalar());
np->set_seqs(ap->is_seqs());
np->set_pos(ap->get_pos());
np->set_bitset(ap->get_bitset());
np->set_pseudo(ap->is_pseudo());
np->set_data_list_size(1);
return np;
}
// only operator min_max & rep will call this function
// other binary operator will call new_node_type in modules
NodePtr new_node(NodeType type, NodePtr u, NodePtr v){
NodePtr np = NodePool::global()->get();
np->set_type(type);
np->add_input(0, u);
np->add_input(1, v);
np->set_lbound({{0, 0, 0}});
np->set_rbound({{0, 0, 0}});
np->set_update();
np->set_data_type(u->get_data_type());
if(u->get_pos() != -1)
np->set_pos(u->get_pos());
else if(v->get_pos() != -1)
np->set_pos(v->get_pos());
return np;
}
const NodeDesc& get_node_desc(NodeType type){
static bool has_init = false;
static OpDescList s;
// use NodeType.fypp to initialize the NodeDesc
if (!has_init) {
s.resize(NUM_NODE_TYPES);
///:mute
///:set i = 0
///:include "NodeType.fypp"
///:endmute
//intialize node descriptions.
///:set id = 0
///:for i in L
///:mute
///:set type = i[0]
///:set name = i[1]
///:set sy = i[2]
///:set ew = i[5]
///:if ew == 'F'
///:set ew = 'false'
///:else
///:set ew = 'true'
///:endif
///:set cl = i[6]
///:if cl == 'F'
///:set cl = 'false'
///:else
///:set cl = 'true'
///:endif
///:endmute
///:set ef = i[7]
///:set rt = i[8]
///:set kernel_name = 'kernel_' + i[1]
s[${type}$].type = ${type}$;
s[${type}$].name = "${name}$";
s[${type}$].sy = "${sy}$";
s[${type}$].ew = ${ew}$;
s[${type}$].cl = ${cl}$;
s[${type}$].expr = "${ef}$";
///!:if (('A' <= i[3] and i[3] <= 'F') or name == 'pow' or name == 'not')
///:if (i[1] == 'unknown' or i[2] == '')
s[${type}$].func = NULL;
///:else
s[${type}$].func = ${kernel_name}$;
///:endif
///!:else
///s[${type}$].func = NULL;
///!:endif
s[${type}$].rt = ${rt}$;
///:set id = id + 1
///:endfor
has_init = true;
}
return s.at(type);
}
// write the expression graph into filename.dot
void write_graph(const NodePtr& root, bool is_root,
const char *filename) {
if (MPI_RANK > 0) return ;
static std::ofstream ofs;
if (is_root) {
ofs.open(filename);
ofs<<"digraph G {"<<endl;
}
int id = root->get_id();
ofs<<id;
const NodeDesc & nd = get_node_desc(root->type());
//char buffer[500];
//sprintf(buffer, "[label=\"[%s]\\n id=%d \n (ref:%d) "
// "\n (lb:%d %d %d) \n (rb: %d %d %d) \n (pseudo: %d) \n (up: %d)\"];",
//nd.name, id, root.use_count(),
//root->get_lbound()[0], root->get_lbound()[1], root->get_lbound()[2],
//root->get_rbound()[0], root->get_rbound()[1], root->get_rbound()[2],
//root->is_pseudo(), root->need_update());
//ofs<<buffer<<endl;
ofs<<boost::format("[label=\"[%s]\\n id=%d \n (ref:%d) "
"\n (lb:%d %d %d) \n (rb: %d %d %d) \n (up: %d)\"];")
% nd.name % id % root.use_count()
% root->get_lbound()[0] % root->get_lbound()[1] % root->get_lbound()[2]
% root->get_rbound()[0] % root->get_rbound()[1] % root->get_rbound()[2]
% root->need_update() <<endl;
for (int i = 0; i < root->input_size(); i++) {
write_graph(root->input(i), false, filename);
ofs<<id<<"->"<<root->input(i)->get_id()<<";"<<endl;
}
if (is_root) {
ofs<<"}"<<endl;
ofs.close();
}
}
// force eval the expression graph, only use basic kernels
ArrayPtr force_eval(NodePtr A) {
if (A->has_data()) return A->get_data();
vector<ArrayPtr> ops_ap;
for (int i = 0; i < A->input_size(); i++) {
ops_ap.push_back(force_eval(A->input(i)));
}
const NodeDesc& nd = get_node_desc(A->type());
KernelPtr kernel_addr = nd.func;
ArrayPtr ap = kernel_addr(ops_ap);
ap->set_pseudo(A->is_pseudo());
ap->set_bitset(A->get_bitset());
ap->set_pos(A->get_pos());
return ap;
}
// based on specific NodeType to change the lbound
int3 change_lbound(NodeType type, int3 lb) {
switch (type) {
case TYPE_AXB:
case TYPE_DXB:
case TYPE_DXC:
lb[0] = 1;
break;
case TYPE_AYB:
case TYPE_DYB:
case TYPE_DYC:
lb[1] = 1;
break;
case TYPE_AZB:
case TYPE_DZB:
case TYPE_DZC:
lb[2] = 1;
break;
default:
break;
}
return lb;
}
// based on specific NodeType to change the rbound
int3 change_rbound(NodeType type, int3 rb) {
switch (type) {
case TYPE_AXF:
case TYPE_DXF:
case TYPE_DXC:
rb[0] = 1;
break;
case TYPE_AYF:
case TYPE_DYF:
case TYPE_DYC:
rb[1] = 1;
break;
case TYPE_AZF:
case TYPE_DZF:
case TYPE_DZC:
rb[2] = 1;
break;
default:
break;
}
return rb;
}
//
// =======================================================
// to evaluate expression like A = B + C + D
// we need to pass parameters to the fusion kernel
// like: the data pointer to ans A, parameters B, C & D
// the shape of ans A, parameters B, C & D etc.
// =======================================================
//
// NodePtr A : the root of (sub)expression graph
// list: the data list which used in fusion kernel
// update_list: the array list which needs to update boundary
// S: the shape of array in data list which used in fusion kernel
// ptr: the final Partition pointer of ans
// bt: the final bitset of ans
// lb_list: the lbound list of array in data list which used in fusion kernel
// rb_list: the rbound list of array in data list which used in fusion kernel
// lb_now: the lbound from the root to the current node
// rb_now: the rbound from the root to the current node
// data_list: the data list of different shape, to check whether data has to transfer or not
//
void get_kernel_parameter_with_op(NodePtr A, vector<void*> &list,
vector<ArrayPtr> &update_list, vector<int3> &S, PartitionPtr &ptr,
bitset<3> &bt, vector<int3> &lb_list, vector<int3> &rb_list,
int3 lb_now, int3 rb_now, vector<ArrayPtr> &data_list) {
bool find_in_data_list;
ArrayPtr ap;
// 1. the Node is a data node, put data into list
if (A->has_data()) {
ap = A->get_data();
// 1.1 to check whether the ap needs to transfer or not
if(!ap->is_scalar())
{
find_in_data_list = false;
for(int i = 0; i < data_list.size(); i++){
if(ap->shape() == data_list[i]->shape()){
PartitionPtr pp = ap->get_partition();
if(!(pp->equal(data_list[i]->get_partition()))){
// ap has the same shape with data_list[i], but the partition is not the same
ap = oa::funcs::transfer(ap, data_list[i]->get_partition());
}
find_in_data_list = true;
break;
}
}
// it's the first time the shape appears
if(!find_in_data_list) data_list.push_back(ap);
}
// 1.2 ap is a pseudo 3d, need to make_pseudo_3d
if (ap->get_bitset() != bt && !ap->is_seqs_scalar() && ap->is_pseudo()) {
if (ap->has_pseudo_3d() == false) {
ap->set_pseudo_3d(oa::funcs::make_psudo3d(ap));
}
ap = ap->get_pseudo_3d();
}
// 1.3 put the array's data into list
list.push_back(ap->get_buffer());
// 1.4 determine the answer's partition
if (ptr == NULL && ap->get_bitset() == bt) {
ptr = ap->get_partition();
}
// 1.5 put the buffer shape into S which needs in fusion kernel
// put the lb_now & rb_now into lb_list & rb_list which needs in update boundary
if (!A->is_seqs_scalar()) {
S.push_back(ap->buffer_shape());
update_list.push_back(ap);
lb_list.push_back(lb_now);
rb_list.push_back(rb_now);
}
return ;
}
// 2. Operator node is not element wise, or need update
const NodeDesc &nd = get_node_desc(A->type());
if (!nd.ew || A->need_update()) {
// need change need_update's state in order to evaluate recursively
bool flag = A->need_update();
A->set_update(false);
ArrayPtr ap = eval(A);
A->set_update(flag);
// 2.1 to check whether the ap needs to transfer or not
if(!ap->is_scalar())
{
find_in_data_list = false;
for(int i = 0; i < data_list.size(); i++){
if(ap->shape() == data_list[i]->shape()){
PartitionPtr pp = ap->get_partition();
if(!(pp->equal(data_list[i]->get_partition()))){
ap = oa::funcs::transfer(ap, data_list[i]->get_partition());
}
find_in_data_list = true;
break;
}
}
if(!find_in_data_list) data_list.push_back(ap);
}
// 2.2 ap is a pseudo 3d, need to make_pseudo_3d
if (ap->get_bitset() != bt && !ap->is_seqs_scalar() && ap->is_pseudo()) {
if (ap->has_pseudo_3d() == false) {
ap->set_pseudo_3d(oa::funcs::make_psudo3d(ap));
}
ap = ap->get_pseudo_3d();
}
// 2.3 put the array's data into list
list.push_back(ap->get_buffer());
// 2.4 determine the answer's partition
if (ptr == NULL && ap->get_bitset() == bt) {
ptr = ap->get_partition();
}
// 2.5 put the buffer shape into S which needs in fusion kernel
// put the lb_now & rb_now into lb_list & rb_list which needs in update boundary
if (!A->is_seqs_scalar()) {
S.push_back(ap->buffer_shape());
update_list.push_back(ap);
lb_list.push_back(lb_now);
rb_list.push_back(rb_now);
}
return ;
}
// 3. it's an operator node, get kernel parameters from it's child node
for (int i = 0; i < A->input_size(); i++) {
get_kernel_parameter_with_op(A->input(i), list, update_list, S, ptr, bt,
lb_list, rb_list, change_lbound(nd.type, lb_now), change_rbound(nd.type, rb_now), data_list);
}
// 4. if A is OPERATOR, need to bind grid if A.pos != -1
if (A->input_size() == 1 && A->get_pos() != -1) {
if (nd.type == TYPE_DXC ||
nd.type == TYPE_DYC ||
nd.type == TYPE_DZC ||
nd.type == TYPE_DXB ||
nd.type == TYPE_DXF ||
nd.type == TYPE_DYB ||
nd.type == TYPE_DYF ||
nd.type == TYPE_DZB ||
nd.type == TYPE_DZF) {
// 4.1 get grid ptr
ArrayPtr grid_ptr = Grid::global()->get_grid(A->get_pos(), nd.type);
// 4.2 get the grid's data into list
list.push_back(grid_ptr->get_buffer());
// 4.3 put the buffer shape into S
S.push_back(grid_ptr->buffer_shape());
//if (g_debug) grid_ptr->display("test grid");
}
}
}
// =======================================================
// evaluate the expression graph, which the root node is A
// treat operator as element wise
//
// case 1: if A has fusion kernel, use it to evaluate and return
// case 2: if A is a data node, just return it's data
// case 3: if A is not an element wise operator node
// or need to update, evaluate it's child first,
// after that, evaluate the A
// =======================================================
ArrayPtr eval(NodePtr A) {
// 1. Node has hash value, means may have a fusion kernel
if (A->hash()) {
// use A->hash() to get inside fusion kernel
FusionKernelPtr fkptr = Jit_Driver::global()->get(A->hash());
if (fkptr != NULL) {
// prepare parameters used in fusion kernel
vector<void*> list;
vector<int3> S;
vector<ArrayPtr> update_list;
PartitionPtr par_ptr;
bitset<3> bt = A->get_bitset();
vector<int3> lb_list;
vector<int3> rb_list;
int3 lb_now = {{0,0,0}};
int3 rb_now = {{0,0,0}};
vector<ArrayPtr> data_list;
get_kernel_parameter_with_op(A,
list, update_list, S, par_ptr, bt,
lb_list, rb_list, lb_now, rb_now,data_list);
int3 lb = A->get_lbound();
int3 rb = A->get_rbound();
int sb = lb[0] + lb[1] + lb[2] + rb[0] + rb[1] + rb[2];
int sz = update_list.size();
vector<MPI_Request> reqs_list;
// pthread_t tid;
// step 1: start of update boundary
if (sb) {
for (int i = 0; i < sz; i++){
oa::funcs::update_ghost_start(update_list[i], reqs_list, 4, lb_list[i], rb_list[i]);
}
oa::funcs::update_ghost_end(reqs_list);
}
// put the answer array's data and shape into list
ArrayPtr ap = ArrayPool::global()->get(par_ptr, A->get_data_type());
S.push_back(ap->buffer_shape());
S.push_back(A->get_lbound());
S.push_back(A->get_rbound());
S.push_back(ap->local_shape());
list.push_back(ap->get_buffer());
list.push_back((void*)S.data());
void** list_pointer = list.data();
// step 2: calc_inside
fkptr(list_pointer, ap->get_stencil_width());
if (sb) {
// step 3: end of update boundary
//oa::funcs::update_ghost_end(reqs_list);
//oa::MPI::wait_end(&tid);
// step 4: calc_outside
// use A->hash() + 1 to get outside fusion kernel
//FusionKernelPtr out_fkptr = Jit_Driver::global()->get(A->hash() + 1);
//if (out_fkptr) out_fkptr(list_pointer, ap->get_stencil_width());
// set the boundary to zeros based on lb & rb becased it used illegal data
//oa::funcs::set_boundary_zeros(ap, lb, rb);
}
oa::funcs::set_boundary_zeros(ap, lb, rb);
//cout<<"fusion-kernel called"<<endl;
ap->set_bitset(A->get_bitset());
ap->set_pos(A->get_pos());
return ap;
}
}
// 2. Node is a data node, just return the data
if (A->has_data()) return A->get_data();
// 3.1 Node is an operator node, and doesn't have fusion kernel
// first, evaluate it's child node recursively
vector<ArrayPtr> ops_ap;
for (int i = 0; i < A->input_size(); i++) {
ops_ap.push_back(eval(A->input(i)));
}
// 3.2 second, evaluate the node
ArrayPtr ap;
if(A->type() == TYPE_REF) {
ap = oa::funcs::subarray(ops_ap[0], A->get_ref());
} else {
const NodeDesc& nd = get_node_desc(A->type());
KernelPtr kernel_addr = nd.func;
ap = kernel_addr(ops_ap);
ap->set_bitset(A->get_bitset());
ap->set_pos(A->get_pos());
}
return ap;
}
// =======================================================
// Before evaluate the expression graph, we need to analyze the graph
// Here's how we generate the fusion kernel for each sub expression graph
//
// exp: A = AXF(A) + sub(A+B+C)
// there is two fusion kernels becasue of the sub operator
// kernel 1: ans = AXF(A) + tmp
// kernel 2: ans = A+B+C
// =======================================================
// NodePtr A : the root of (sub)expression graph
// is_root: we only have to generate fusion kernels of the root node
void gen_kernels_JIT_with_op(NodePtr A, bool is_root) {
// 1. if A is a data node, doesn't have to generate fusion kernel
if (A->has_data()) return ;
const NodeDesc &nd = get_node_desc(A->type());
// 2. if A is not element wise (like sum, rep, etc)
// need to generate it's children's fusion kernels recursively
if (!nd.ew) {
for (int i = 0; i < A->input_size(); i++) {
gen_kernels_JIT_with_op(A->input(i), true);
}
return ;
}
// 3. if A's need update state is true, should generate fusion kernel
if (A->need_update()) {
// should set update to false in order to generate kernels
A->set_update(false);
gen_kernels_JIT_with_op(A, true);
A->set_update(true);
return ;
}
// 4. is root && A->depth >= 2, generate fusion kernel
if (is_root && A->get_depth() >= 2) {
stringstream ss1;
stringstream code;
stringstream __code;
stringstream __point;
// generate hash code for tree
tree_to_string_stack(A, ss1);
std::hash<string> str_hash;
size_t hash = str_hash(ss1.str());
if (g_debug) cout<<ss1.str()<<endl;
if (g_debug) cout<<hash<<endl;
// if already have kernel function ptr, do nothing
if (Jit_Driver::global()->get(hash) != NULL) {
if (g_debug) cout<<hash<<endl;
A->set_hash(hash);
// return ; shouldn't return!!!!
}
else {
// else generate kernel function by JIT_Driver
int id = 0;
int S_id = 0;
vector<int> int_id, float_id, double_id;
tree_to_code_with_op(A, __code, __point, id, S_id, int_id, float_id, double_id);
// JIT source code add function signature
code_add_function_signature_with_op(code, hash);
// JIT source code add const parameters
code_add_const(code, int_id, float_id, double_id);
// JIT source code add calc_inside
code_add_calc_inside(code, __code, __point, A->get_data_type(), id, S_id);
// for debug
if (g_debug) cout<<code.str()<<endl;
// Add fusion kernel into JIT map
Jit_Driver::global()->insert(hash, code);
A->set_hash(hash);
int3 lb = A->get_lbound();
int3 rb = A->get_rbound();
int sb = lb[0] + lb[1] + lb[2] + rb[0] + rb[1] + rb[2];
// Add calc_outside
if (sb) {
//stringstream code_out;
//size_t hash_out = hash + 1;
//code_add_function_signature_with_op(code_out, hash_out);
//code_add_const(code_out, int_id, float_id, double_id);
//code_add_calc_outside(code_out, __code, A->get_data_type(), id, S_id);
//// cout<<code_out.str()<<endl;
//Jit_Driver::global()->insert(hash_out, code_out);
}
}
}
// 5. generate fusion kernels recursively
for (int i = 0; i < A->input_size(); i++) {
gen_kernels_JIT_with_op(A->input(i), false);
}
}
// example: (A1+S2)*A3
void tree_to_string(NodePtr A, stringstream &ss) {
const NodeDesc &nd = get_node_desc(A->type());
// only data or non-element-wise
if (A->has_data() || !nd.ew || A->need_update()) {
if (A->is_seqs_scalar()) ss<<"S";
else ss<<"A";
ss<<A->get_data_type();
return;
}
stringstream child[2];
for (int i = 0; i < A->input_size(); i++) {
tree_to_string(A->input(i), child[i]);
//child[i] = tree_to_string(A->input(i));
}
switch(A->input_size()) {
case 1:
if(nd.sy == "abs")
switch(A->get_data_type()) {
case DATA_INT:
ss<<"abs"<<"("<<child[0].str()<<")";
break;
case DATA_FLOAT:
ss<<"fabsf"<<"("<<child[0].str()<<")";
break;
case DATA_DOUBLE:
ss<<"fabs"<<"("<<child[0].str()<<")";
break;
default:
ss<<"fabs"<<"("<<child[0].str()<<")";
break;
}
else if(nd.sy == "sqrt")
switch(A->get_data_type()) {
case DATA_FLOAT:
ss<<"sqrtf"<<"("<<child[0].str()<<")";
break;
default:
ss<<"sqrt"<<"("<<child[0].str()<<")";
break;
}
else
ss<<nd.sy<<"("<<child[0].str()<<")";
break;
case 2:
if (nd.type == TYPE_POW) {
ss<<"pow("<<child[0].str()<<","<<child[1].str()<<")";
}
else {
ss<<"("<<child[0].str()<<")"<<nd.sy<<"("<<child[1].str()<<")";
}
break;
}
return;
}
void tree_to_code_with_op(NodePtr A, stringstream &ss, stringstream &__point, int &id, int& S_id,
vector<int>& int_id, vector<int>& float_id, vector<int>& double_id) {
const NodeDesc &nd = get_node_desc(A->type());
// data node
if (A->has_data() || !nd.ew || A->need_update()) {
// scalar
if (A->is_seqs_scalar()) {
switch(A->get_data_type()) {
case DATA_INT:
ss<<"I_";
ss<<int_id.size();
int_id.push_back(id);
break;
case DATA_FLOAT:
ss<<"F_";
ss<<float_id.size();
float_id.push_back(id);
break;
case DATA_DOUBLE:
ss<<"D_";
ss<<double_id.size();
double_id.push_back(id);
break;
}
// [i][j][k] based on node bitset
} else {
switch(A->get_data_type()) {
case DATA_INT:
__point<<" int *list_"<<id<<"; list_"<<id<<" = (int *) list["<<id<<"];\n";
break;
case DATA_FLOAT:
__point<<" float *list_"<<id<<"; list_"<<id<<" = (float *) list["<<id<<"];\n";
break;
case DATA_DOUBLE:
__point<<" double *list_"<<id<<"; list_"<<id<<" = (double *) list["<<id<<"];\n";
break;
}
/*
ss<<"(";
switch(A->get_data_type()) {
case DATA_INT:
ss<<"(int*)";
break;
case DATA_FLOAT:
ss<<"(float*)";
break;
case DATA_DOUBLE:
ss<<"(double*)";
break;
}
*/
ss<<"list_"<<id;
char pos_i[3] = "oi";
char pos_j[3] = "oj";
char pos_k[3] = "ok";
bitset<3> bit = A->get_bitset();
ss<<"[calc_id2("<<pos_i[bit[2]]<<",";
ss<<pos_j[bit[1]]<<",";
ss<<pos_k[bit[0]]<<",S"<<S_id<<"_0,S"<<S_id<<"_1)]";
S_id++;
}
id++;
return ;
}
stringstream child[2];
for (int i = 0; i < A->input_size(); i++) {
tree_to_code_with_op(A->input(i), child[i], __point, id, S_id,
int_id, float_id, double_id);
//child[i] = tree_to_string(A->input(i));
}
switch(A->input_size()) {
case 1:
if (nd.type == TYPE_UNKNOWN) {
string in = child[0].str();
// printf("in Operator, k = %d\n", A->get_slice());
string out = replace_string(in, "k", to_string(A->get_slice()));
ss<<out;
}
else change_string_with_op(ss, child[0].str(), nd);
// bind grid if A.pos != -1
if (A->get_pos() != -1) {
if (nd.type == TYPE_DXC ||
nd.type == TYPE_DYC ||
nd.type == TYPE_DZC ||
nd.type == TYPE_DXB ||
nd.type == TYPE_DXF ||
nd.type == TYPE_DYB ||
nd.type == TYPE_DYF ||
nd.type == TYPE_DZB ||
nd.type == TYPE_DZF) {
// get grid ptr
ArrayPtr grid_ptr = Grid::global()->get_grid(A->get_pos(), nd.type);
ss<<"/";
switch(A->get_data_type()) {
case DATA_INT:
__point<<" int *list_"<<id<<"; list_"<<id<<" = (int *) list["<<id<<"];\n";
break;
case DATA_FLOAT:
__point<<" float *list_"<<id<<"; list_"<<id<<" = (float *) list["<<id<<"];\n";
break;
case DATA_DOUBLE:
__point<<" double *list_"<<id<<"; list_"<<id<<" = (double *) list["<<id<<"];\n";
break;
}
/*
switch(grid_ptr->get_data_type()) {
case DATA_INT:
ss<<"(int*)";
break;
case DATA_FLOAT:
ss<<"(float*)";
break;
case DATA_DOUBLE:
ss<<"(double*)";
break;
}*/
ss<<"list_"<<id;
id++;
char pos_i[3] = "oi";
char pos_j[3] = "oj";
char pos_k[3] = "ok";
bitset<3> bit = grid_ptr->get_bitset();
ss<<"[calc_id2("<<pos_i[bit[2]]<<",";
ss<<pos_j[bit[1]]<<",";
ss<<pos_k[bit[0]]<<",S"<<S_id<<"_0,S"<<S_id<<"_1)]";
S_id++;
}
}
break;
case 2:
if (nd.type == TYPE_POW) {
ss<<"pow("<<child[0].str()<<","<<child[1].str()<<")";
}
else {
ss<<"("<<child[0].str()<<")"<<nd.sy<<"("<<child[1].str()<<")";
}
break;
}
return;
}
void change_string_with_op(stringstream& ss, string in, const NodeDesc &nd) {
string new_str1, new_str2, new_str;
switch(nd.type) {
// Central difference operator
case TYPE_DXC:
new_str1 = replace_string(in, "i,", "1+i,");
new_str2 = replace_string(in, "i,", "-1+i,");
ss<<"0.5*(("<<new_str1<<")-("<<new_str2<<"))";
break;
case TYPE_DYC:
new_str1 = replace_string(in, "j,", "1+j,");
new_str2 = replace_string(in, "j,", "-1+j,");
ss<<"0.5*(("<<new_str1<<")-("<<new_str2<<"))";
break;
case TYPE_DZC:
new_str1 = replace_string(in, "k,", "1+k,");
new_str2 = replace_string(in, "k,", "-1+k,");
ss<<"0.5*(("<<new_str1<<")-("<<new_str2<<"))";
break;
// average operator
case TYPE_AXB:
new_str = replace_string(in, "i,", "-1+i,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
case TYPE_AXF:
new_str = replace_string(in, "i,", "1+i,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
case TYPE_AYB:
new_str = replace_string(in, "j,", "-1+j,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
case TYPE_AYF:
new_str = replace_string(in, "j,", "1+j,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
case TYPE_AZB:
new_str = replace_string(in, "k,", "-1+k,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
case TYPE_AZF:
new_str = replace_string(in, "k,", "1+k,");
ss<<"0.5*(("<<in<<")+("<<new_str<<"))";
break;
// difference operator
case TYPE_DXB:
new_str = replace_string(in, "i,", "-1+i,");
ss<<"1.0*(("<<in<<")-("<<new_str<<"))";
break;
case TYPE_DXF:
new_str = replace_string(in, "i,", "1+i,");
ss<<"1.0*(("<<new_str<<")-("<<in<<"))";
break;
case TYPE_DYB:
new_str = replace_string(in, "j,", "-1+j,");
ss<<"1.0*(("<<in<<")-("<<new_str<<"))";
break;
case TYPE_DYF:
new_str = replace_string(in, "j,", "1+j,");
ss<<"1.0*(("<<new_str<<")-("<<in<<"))";
break;
case TYPE_DZB:
new_str = replace_string(in, "k,", "-1+k,");
ss<<"1.0*(("<<in<<")-("<<new_str<<"))";
break;
case TYPE_DZF:
new_str = replace_string(in, "k,", "1+k,");
ss<<"1.0*(("<<new_str<<")-("<<in<<"))";
break;
// abs operator
case TYPE_ABS:
ss<<"fabs"<<"("<<in<<")";
break;
// other default monocular operator
default:
ss<<nd.sy<<"("<<in<<")";
break;
}
}
// replace all old_str in string in by new_str
string replace_string(string& in, const string& old_str, const string& new_str) {
string out = in;
// use replace is not efficient, should be optimized later
for(string::size_type i = 0; (i = out.find(old_str, i)) != string::npos;) {
out.replace(i, old_str.length(), new_str);
i += new_str.length();
}
return out;
}
void tree_to_string_stack(NodePtr A, stringstream &ss) {
const NodeDesc &nd = get_node_desc(A->type());
if (A->has_data() || !nd.ew || A->need_update()) {
if (A->is_seqs_scalar()) ss<<"S";
else {
ss<<"A"<<A->get_bitset();
}
ss<<A->get_data_type();
// if (A->need_update()) ss<<nd.sy;
return ;
}
for (int i = 0; i < A->input_size(); i++) {
tree_to_string_stack(A->input(i), ss);
}
ss<<nd.sy;
return ;
}
void code_add_function_signature(stringstream& code, size_t& hash) {
code<<"extern \"C\" {\nvoid kernel_"<<hash;
code<<"(void** &list, int size) {\n";
}
void code_add_function_signature_with_op(stringstream& code, size_t& hash) {
// code<<"#include <array>\n\n";
// code<<"typedef std::array<int, 3> int3;\n\n";
code<<"#include \"math.h\"\n";
code<<"#include \"stdlib.h\"\n";
code<<"#include \"stdio.h\"\n";
code<<"typedef int int3[3];\n\n";
code<<"#define min(a,b) ((a)<(b))?(a):(b)\n";
code<<"#define BLOCK_NUM 32\n";
code<<"extern \"C\" {\n";
code<<"#define calc_id2(i,j,k,S0,S1) ((k)*(S0)*(S1)+(j)*(S0)+(i))\n";
code<<"void kernel_"<<hash;
code<<"(void** &list, int o) {\n";
}
void code_add_const(stringstream& code,
vector<int>& int_id, vector<int>& float_id, vector<int>& double_id) {
code<<"\n";
for (int i = 0; i < int_id.size(); i++) {
code<<" const int I_"<<i<<" = ((int*)list["<<int_id[i]<<"])[0];\n";
}
for (int i = 0; i < float_id.size(); i++) {
code<<" const float F_"<<i<<" = ((float*)list["<<float_id[i]<<"])[0];\n";
}
for (int i = 0; i < double_id.size(); i++) {
code<<" const double D_"<<i<<" = ((double*)list["<<double_id[i]<<"])[0];\n";
}
code<<"\n";
}
void code_add_function(stringstream& code,
stringstream& __code, DATA_TYPE dt, int& id) {
code<<" for (int i = 0; i < size; i++) {\n";
switch(dt) {
case DATA_INT:
code<<" ((int*)(list["<<id<<"]))[i] = ";
break;
case DATA_FLOAT:
code<<" ((float*)(list["<<id<<"]))[i] = ";
break;
case DATA_DOUBLE:
code<<" ((double*)(list["<<id<<"]))[i] = ";
break;
}
code<<__code.str()<<";\n }\n return ;\n}}";
}
/*
void code_add_calc_outside(stringstream& code,
stringstream& __code, DATA_TYPE dt, int& id, int& S_id) {
code<<" int3* int3_p = (int3*)(list["<<id + 1<<"]);\n";
for (int i = 0; i <= S_id; i++) {
code<<" const int3 &S"<<i<<" = int3_p["<<i<<"];\n";
code<<" const int S"<<i<<"_0 = int3_p["<<i<<"][0];\n";
code<<" const int S"<<i<<"_1 = int3_p["<<i<<"][1];\n";
}