forked from yongwen/columbia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.cpp
1910 lines (1645 loc) · 58.3 KB
/
tasks.cpp
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
/* tasks.cpp : tasks implementation
$Revision: 30 $
Implements classes in tasks.h
Columbia Optimizer Framework
A Joint Research Project of Portland State University
and the Oregon Graduate Institute
Directed by Leonard Shapiro and David Maier
Supported by NSF Grants IRI-9610013 and IRI-9619977
*/
#include "stdafx.h"
#include "tasks.h"
#include "physop.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/* Function to compare the promise of rule applications */
int compare_moves (void const *x, void const *y)
{
// Cast arguments back to pointers to MOVEs
MOVE *a = (MOVE *) x;
MOVE *b = (MOVE *) y;
int result = 0;
if (a -> promise > b -> promise)
result = -1;
else if (a -> promise < b -> promise)
result = 1;
else
result = 0;
return result;
} // compare_moves
/* Function to compare the cost of mexprs */
int compare_afters (void const *x, void const *y)
{
// Cast arguments back to pointers to AFTER
AFTERS *a = (AFTERS *) x;
AFTERS *b = (AFTERS *) y;
int result = 0;
if ((*a -> cost) < (*b -> cost))
result = -1;
else if ((*a -> cost) > (*b -> cost))
result = 1;
else
result = 0;
return result;
} // compare_afters
// **************** TASKS *********************
// base class of OPT tasks
//##ModelId=3B0C085D00C1
TASK::TASK (int ContextID, int parentTaskNo)
:ContextID(ContextID)
{
ParentTaskNo = parentTaskNo; // for debug
}; //TASK::TASK
// **************** PTASKS *********************
// List of un-done tasks
//##ModelId=3B0C085D0143
PTASKS::PTASKS()
:first(NULL)
{ };//PTASKS::PTASKS
//##ModelId=3B0C085D014D
PTASKS::~PTASKS()
{
while (! empty ()) delete pop ();
}//PTASKS::~PTASKS
//##ModelId=3B0C085D0175
CString PTASKS::Dump()
{
CString os;
if (first == NULL) // task list is empty
os = "OPEN is empty\r\n";
else // task list has some tasks in it
{
os = "\t---- OPEN START ----\r\n";
// loop over all tasks to be done
int count=0;
CString temp;
for (TASK * task = first; task != NULL; task = task -> next)
{
temp.Format("\t%d -- %s\r\n", count++, task->Dump() );
os += temp;
}
os += "\t---- OPEN END ----";
}
return os;
} //PTASKS::Dump
//##ModelId=3B0C085D0157
bool PTASKS::empty ()
{
return (first == NULL);
} // TASK_LIST::empty
//##ModelId=3B0C085D0161
void PTASKS::push (TASK * task)
{
task -> next = first;
first = task; //Push Task
if(COVETrace)
{
CString os;
os.Format("PushTaskList {%s}\r\n", task ->Dump() );
OutputCOVE.Write(os, os.GetLength());
}
} //PTASKS::push
//##ModelId=3B0C085D016B
TASK * PTASKS::pop ()
{
if (empty ()) return ( NULL );
TASK * task = first;
first = task -> next;
if(COVETrace) //Pop a Task
{
CString os;
os.Format("PopTaskList\r\n");
OutputCOVE.Write(os, os.GetLength());
}
return ( task );
} //PTASKS::pop
// ************ O_GROUP ******************
// Task to optimize a group
//##ModelId=3B0C085D0215
O_GROUP::O_GROUP (GRP_ID grpID, int ContextID,int parentTaskNo, bool last, COST * bound)
:TASK(ContextID,parentTaskNo), GrpID(grpID), Last(last), EpsBound(bound)
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_O_GROUP].New();
// if INFBOUND flag is on, set the bound to be INF
#ifdef INFBOUND
COST *INFCost = new COST(-1);
CONT::vc[ContextID]->SetUpperBound(*INFCost);
#endif
} ;//O_GROUP::O_GROUP
/*
O_GROUP::perform
{
see search_circle in declaration of class GROUP, for notation
Call search_circle
If cases (1) or (2),
terminate this task. //circle is prepared
Cases (3) and (4) remain. More search.
IF (Group is not optimized)
assert (this is case 3, never searched for this property before)
if (property is ANY)
Push O_EXPR on first logical expression
add a winner for this context to the circle, with null plan
(i.e., initialize the winner's circle for this property.)
else
Push O_GROUP on this group with current context
Push O_GROUP on this group, with new context: ANY property and
cost = current context cost - appropriate enforcer cost, last task.
This is valid since the result of the ANY search will only be used with
the enforcer. If the cost is negative, then the enforcer cannot be used,
so the ANY winner should be null.
Since this is somewhat complex and prone to error, we will omit it for now
and use the original context's cost
else (Group is optimized)
if (property is ANY)
assert (this is case 4)
push O_INPUTS on all physical mexprs
else (property is not ANY)
Push O_INPUTS on all physical mexprs with current context, last one is last task
If case (3) [i.e. appropriate enforcer is not in group], Push APPLY_RULE on
enforcer rule, not the last task
add a winner for this context to the circle, with null plan.
(i.e., initialize the winner's circle for this property.)
*/
//##ModelId=3B0C085D0235
void O_GROUP::perform ()
{
SET_TRACE Trace(true);
PTRACE ("O_GROUP %d performing", GrpID);
#ifndef IRPROP
PTRACE2 ("Context ID: %d , %s", ContextID, CONT::vc[ContextID]->Dump() );
#endif
PTRACE ("Last flag is %d", Last);
GROUP * Group = Ssp->GetGroup(GrpID);
M_EXPR * FirstLogMExpr = Group->GetFirstLogMExpr();
if (FirstLogMExpr->GetOp()->is_const())
{
PTRACE("Group %d is const group", GrpID);
#ifndef IRPROP
M_EXPR * WPlan = new M_EXPR(*FirstLogMExpr);
Group->NewWinner(new PHYS_PROP(any), WPlan, new COST(0), true);
#endif
return;
}
bool moreSearch, SCReturn;
#ifdef IRPROP
PHYS_PROP * LocalReqdProp = M_WINNER::mc[GrpID] -> GetPhysProp(ContextID); //What prop is required of
SCReturn = Group -> search_circle(GrpID, LocalReqdProp, moreSearch);
if (!moreSearch)
{
// group is completely optimized
PTRACE("%s", "Winner's circle is prepared so terminate this task");
delete this;
return;
}
else // group is not optimized at all
{
// Get the first logical mexpr in the group
M_EXPR * LogMExpr = Group->GetFirstLogMExpr();
// push the enforcer rule before pushing the first logical MEXPR
PTRACE("%s", "Push APPLY_RULE on enforcer rule");
RULE * Rule = (*RuleSet)[R_SORT_RULE];
PTasks.push(new APPLY_RULE( Rule, FirstLogMExpr, false, 0, TaskNo, false));
// push the O_EXPR on first logical expression
PTasks.push( new O_EXPR( FirstLogMExpr, false, 0, TaskNo, true) );
}
#else
CONT * LocalCont = CONT::vc[ContextID];
PHYS_PROP * LocalReqdProp = LocalCont -> GetPhysProp(); //What prop is required
COST * LocalCost = LocalCont ->GetUpperBd();
SCReturn = Group -> search_circle(LocalCont, moreSearch);
//If case (2) or (1), terminate this task
if ( !moreSearch)
{
PTRACE("%s", "Winner's circle is prepared so terminate this task");
delete this;
return;
}
PTRACE("Group is %s optimized", Group->is_optimized()?"":"not");
if(!Group -> is_optimized())
{
assert(moreSearch && !SCReturn); //assert (this is case 3)
//if (property is ANY)
if(LocalReqdProp->GetOrder() == any)
{
PTRACE("%s", "add winner with null plan, push O_EXPR on 1st logical expression");
Group -> NewWinner(LocalReqdProp, NULL, new COST(*LocalCost), false);
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_EXPR( FirstLogMExpr, false, ContextID, TaskNo, true, eps_bound) );
}
else
PTasks.push( new O_EXPR( FirstLogMExpr, false, ContextID, TaskNo, true) );
}
else
{
PTRACE("%s","Push O_GROUP with current context, another with ANY context");
assert(LocalReqdProp -> GetOrder() == sorted); //temporary
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push(new O_GROUP (GrpID, ContextID, TaskNo, true, eps_bound));
}
else
PTasks.push(new O_GROUP (GrpID, ContextID, TaskNo, true));
COST *NewCost = new COST(*(LocalCont -> GetUpperBd()));
CONT * NewContext = new CONT(new PHYS_PROP(any), NewCost, false);
CONT::vc.Add(NewContext);
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push(new O_GROUP (GrpID, CONT::vc.GetSize()-1, TaskNo, true, eps_bound));
}
else
PTasks.push(new O_GROUP (GrpID, CONT::vc.GetSize()-1, TaskNo, true));
}
}
else //Group is optimized
{
// if (property is ANY)
// assert (this is case 4)
// push O_INPUTS on all physical mexprs
CArray <M_EXPR *, M_EXPR *> PhysMExprs;
int count = 0;
if(LocalReqdProp->GetOrder() == any)
{
PTRACE("%s","push O_INPUTS on all physical mexprs");
assert(moreSearch && SCReturn);
for( M_EXPR * PhysMExpr = Group->GetFirstPhysMExpr();
PhysMExpr ; PhysMExpr = PhysMExpr->GetNextMExpr() )
{
PhysMExprs.Add(PhysMExpr);
count++;
}
//push the last PhysMExpr
if (--count >= 0)
{
PTRACE("pushing O_INPUTS %s", PhysMExprs[count]->Dump());
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, true,
eps_bound) );
}
else
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, true) );
}
//push other PhysMExpr
while (--count >= 0)
{
PTRACE("pushing O_INPUTS %s", PhysMExprs[count]->Dump());
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, false,
eps_bound) );
}
else
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, false) );
}
}
else //property is not ANY)
{
assert(LocalReqdProp -> GetOrder() == sorted); //temporary
//Push O_INPUTS on all physical mexprs with current context, last one is last task
PTRACE("%s","Push O_INPUTS on all physical mexprs");
for( M_EXPR * PhysMExpr = Group->GetFirstPhysMExpr();
PhysMExpr ; PhysMExpr = PhysMExpr->GetNextMExpr() )
{
PhysMExprs.Add(PhysMExpr);
count++;
}
//push the last PhysMExpr
if (--count>=0)
{
PTRACE("pushing O_INPUTS %s", PhysMExprs[count]->Dump());
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, true,
eps_bound) );
}
else
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, true) );
}
//push other PhysMExpr
while (--count >= 0)
{
PTRACE("pushing O_INPUTS %s", PhysMExprs[count]->Dump());
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, false,
eps_bound) );
}
else
PTasks.push( new O_INPUTS( PhysMExprs[count], ContextID, TaskNo, false) );
}
//If case (3) [i.e. appropriate enforcer is not in group], Push APPLY_RULE on
// enforcer rule, not the last task
if (!SCReturn)
{
PTRACE("%s", "Push APPLY_RULE on enforcer rule");
if(LocalReqdProp -> GetOrder() == sorted)
{
RULE * Rule = (*RuleSet)[R_SORT_RULE];
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push(new APPLY_RULE( Rule, FirstLogMExpr,
false, ContextID, TaskNo));
}
else
PTasks.push(new APPLY_RULE( Rule, FirstLogMExpr,
false, ContextID, TaskNo, false));
}
else
{
assert(false);
}
}
// add a winner to the circle, with null plan.
//(i.e., initialize the winner's circle for this property.)
PTRACE("%s", "Init winner's circle for this property");
if(moreSearch && !SCReturn)
Group -> NewWinner(LocalReqdProp, NULL, new COST(*LocalCost), false);
}
}
#endif
delete this;
} //O_GROUP::perform
//##ModelId=3B0C085D023E
CString O_GROUP::Dump()
{
CString os;
CString temp;
os.Format("OPT_GROUP group %d,", GrpID);
temp.Format(" parent task %d,", ParentTaskNo);
os += temp;
temp.Format(" %s", CONT::vc[ContextID]->Dump());
os += temp;
return os;
} //O_GROUP::Dump
// ************ E_GROUP ******************
// Task to explore a group
//##ModelId=3B0C085D02E8
E_GROUP::E_GROUP (GRP_ID grpID, int ContextID,int parentTaskNo, bool last, COST * bound)
:TASK(ContextID,parentTaskNo),GrpID(grpID), Last(last), EpsBound(bound)
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_E_GROUP].New();
} ;//E_GROUP::E_GROUP
//##ModelId=3B0C085D0307
void E_GROUP::perform ()
{
SET_TRACE Trace(true);
PTRACE("E_GROUP %d performing", GrpID);
PTRACE2 ("Context ID: %d , %s", ContextID, CONT::vc[ContextID]->Dump() );
GROUP * Group = Ssp->GetGroup(GrpID);
if (Group -> is_optimized()) //See discussion in E_GROUP class declaration
{
delete this;
return;
}
else if (Group -> is_explored())
{
delete this;
return;
}
if(Group->is_exploring() ) assert(false);
else
{
// the group will be explored, let other tasks don't do it again
Group->set_exploring(true);
// mark the group not explored since we will begin exploration
Group->set_explored(false);
M_EXPR * LogMExpr = Group->GetFirstLogMExpr();
// only need to E_EXPR the first log expr,
// because it will generate all logical exprs by applying appropriate rules
// it won't generate dups because rule bit vector
PTRACE("pushing O_EXPR exploring %s", LogMExpr->Dump());
// this logical mexpr will be the last optimized one, mark it as the last task for this group
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_EXPR( LogMExpr, true, ContextID, TaskNo, true, eps_bound) );
}
else
PTasks.push( new O_EXPR( LogMExpr, true, ContextID, TaskNo, true) );
}
delete this;
}//E_GROUP::perform
//##ModelId=3B0C085D0310
CString E_GROUP::Dump()
{
CString os;
os.Format("E_GROUP group %d,", GrpID);
CString temp;
temp.Format(" parent task %d", ParentTaskNo);
os += temp;
return os;
}//E_GROUP::Dump
// ************ O_EXPR ******************
//##ModelId=3B0C085E0018
O_EXPR::O_EXPR (
M_EXPR * mexpr,
bool explore,
int ContextID,
int parent_task_no, bool last,
COST * bound)
: TASK(ContextID,parent_task_no),
MExpr(mexpr), explore(explore),
Last(last), EpsBound(bound)
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_O_EXPR].New();
} ;//O_EXPR::O_EXPR
//##ModelId=3B0C085E0041
void O_EXPR::perform()
{
PTRACE2 ("O_EXPR performing, %s mexpr: %s ", explore ? "exploring" : "optimizing", MExpr->Dump() );
#ifdef IRPROP
int GrpNo = MExpr->GetGrpID();
PTRACE2 ("ContextID: %d, %s", ContextID, (M_WINNER::mc[GrpNo]->GetPhysProp(ContextID))->Dump());
#else
PTRACE2 ("Context ID: %d , %s", ContextID, CONT::vc[ContextID]->Dump() );
#endif
PTRACE ("Last flag is %d", Last);
if(explore)
assert(MExpr->GetOp()->is_logical()); //explore is only for logical expression
if(MExpr->GetOp()->is_item())
{
PTRACE("%s", "expression is an item_op");
//push the O_INPUT for this item_expr
PTRACE("pushing O_INPUTS %s", MExpr->Dump());
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push( new O_INPUTS(MExpr, ContextID, TaskNo, true, eps_bound));
}
else
PTasks.push( new O_INPUTS(MExpr, ContextID, TaskNo, true));
delete this;
return;
}
// identify valid and promising rules
MOVE *Move = new MOVE[RuleSet->RuleCount]; // to collect valid, promising moves
int moves = 0; // # of moves already collected
for (int RuleNo = 0; RuleNo<RuleSet->RuleCount; RuleNo ++ )
{
RULE * Rule = (*RuleSet)[RuleNo];
if( Rule == NULL) continue; // some rules may be turned off
#ifdef UNIQ
if( !( MExpr -> can_fire(Rule -> get_index())) )
{
PTRACE("Rejected rule %d ",Rule -> get_index());
continue; // rule has already fired
}
#endif
if(explore && Rule->GetSubstitute()->GetOp()->is_physical() )
{
PTRACE("Rejected rule %d ",Rule -> get_index());
continue; // only fire transformation rule when exploring
}
int Promise = Rule -> promise(MExpr->GetOp(), ContextID);
// insert a valid and promising move into the array
if( Rule->top_match(MExpr->GetOp()) && Promise > 0)
{
Move [moves].promise = Promise;
Move [moves ++ ].rule = Rule;
#ifdef _DEBUG
TopMatch[RuleNo]++;
#endif
}
}
PTRACE ("%d promising moves", moves);
// order the valid and promising moves by their promise
qsort ((char *) Move, moves, sizeof (MOVE), compare_moves);
// optimize the rest rules in order of promise
while ( -- moves >= 0)
{
bool Flag=false;
if(Last)
// this's the last task in the group,pass it to the new task
{ Last = false; // turn off this, since it's no longer the last task
Flag = true;
}
// push future tasks in reverse order (due to LIFO stack)
RULE * Rule = Move[moves].rule;
PTRACE ("pushing rule `%s'", Rule->GetName() );
// apply the rule
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push(new APPLY_RULE (Rule, MExpr, explore, ContextID, TaskNo, Flag,
eps_bound) );
}
else
PTasks.push(new APPLY_RULE (Rule, MExpr, explore, ContextID, TaskNo, Flag) );
// for enforcer and expansion rules, don't explore patterns
EXPR *original = Rule->GetOriginal();
if( original->GetOp()->is_leaf() ) continue;
// earlier tasks: explore all inputs to match the original pattern
for (int input_no = original ->GetArity(); -- input_no >= 0; )
{
// only explore the input with arity > 0
if ( original->GetInput(input_no)->GetArity() )
{
// If not yet explored, schedule a task with new context
GRP_ID grp_no = (MExpr->GetInput(input_no));
if( !Ssp->GetGroup(grp_no)->is_exploring() )
{
//E_GROUP can not be the last task for the group
if (GlobepsPruning)
{
COST * eps_bound = new COST(*EpsBound);
PTasks.push ( new E_GROUP( grp_no, ContextID, TaskNo, false, eps_bound) );
}
else
PTasks.push ( new E_GROUP( grp_no, ContextID, TaskNo, false) );
}
}
} // earlier tasks: explore all inputs to match the original pattern
} // optimize in order of promise
delete [] Move;
delete this;
} //O_EXPR::perform
//##ModelId=3B0C085E0040
CString O_EXPR::Dump()
{
CString os;
os.Format("O_EXPR group %s,", MExpr->Dump());
CString temp;
temp.Format(" parent task %d", ParentTaskNo);
os += temp;
return os;
} //O_EXPR::Dump
/*********** O_INPUTS FUNCTIONS ***************/
//##ModelId=3B0C085E02B7
O_INPUTS::O_INPUTS (M_EXPR * MExpr, int ContextID, int ParentTaskNo, bool last, COST *bound, int ContNo)
:MExpr(MExpr), TASK(ContextID,ParentTaskNo),
InputNo(-1),Last(last), PrevInputNo(-1), EpsBound(bound), ContNo(ContNo)
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_O_INPUTS].New();
assert( MExpr -> GetOp() -> is_physical()|| MExpr->GetOp()->is_item());
//We can only calculate cost for physical operators
//Cache local properties
OP * Op = (PHYS_OP *)(MExpr -> GetOp()); // the op of the expr
arity = Op -> GetArity(); // cache arity of mexpr
// create the arrays of input costs and logical properties
if(arity)
{
InputCost = new COST* [arity];
InputLogProp = new LOG_PROP* [arity];
}
};
//##ModelId=3B0C085E02E9
O_INPUTS::~O_INPUTS ()
{
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_O_INPUTS].Delete();
// localcost was new by find_local_cost, so need to delete it
delete LocalCost;
if (EpsBound) delete EpsBound;
if(arity)
{
delete [] InputCost;
delete [] InputLogProp;
}
} // O_INPUTS::~O_INPUTS
/*
O_INPUTS::perform
NOTATION
InputCost[]: Contains actual (or lower bound) costs of optimal inputs to G.
CostSoFar: LocalCost + sum of all InputCost entries.
G: the group being optimized.
IG: various inputs to expressions in G.
SLB: (Special Lower Bound) The Lower Bound of G, derived with fetch and cucard
We use this term instead of Lower Bound since we will use other lower bounds.
There are still three flags: Pruning (also called Group Pruning), CuCardPruning and GlobepsPruning,
with new meanings. We plan to run benchmarks in four cases:
1. Starburst - generate all expressions [!Pruning && !CuCardPruning]
2. Group Pruning - aggressively check limits at all times [Pruning && !CuCardPruning]
aggressively check means if ( CostSoFar >= upper bound of context in G) then terminate.
3. Lower Bound Pruning - if there is no winner, then use IG's SLB in InputCost[]. [CuCardPruning].
This case assumes that the Pruning flag is on, i.e. the code forces Pruning to be true
if CuCardPruning is true. The SLB may involve cucard, fetch, copy, etc in the lower bound.
4. Global Epsilon Pruning [GlobepsPruning]. If a plan costs <= GLOBAL_EPS, it is a winner for G.
PSEUDOCODE
On the first (and no other) execution, the code must initialize some O_INPUTS members.
The idea here is to get a quick lower bound for the cost of the inputs.
The only nontrivial member is InputCost; here is how to initialize it:
//Initial values of InputCost are zero in the Starburst case
For each input group IG
If (Starburst case)
InputCost is zero
continue
Determine property required of search in IG
If no such property, terminate this task.
call search_circle on IG with that property, infinite cost.
If case (1), no possibility of satisfying the context
terminate this task
If search_circle returns a non-null Winner from IG, case (2)
InputCost[IG] = cost of that winner
else if (!CuCardPruning) //Group Pruning case (since Starburst not relevant here)
InputCost[IG] = 0
//remainder is Lower Bound Pruning case
else if there has been no previous search for ReqdProp
InputCost[IG] = SLB
else if there has been a previous search for ReqdProp
InputCost[IG] = max(cost of winner, IG's SLB) //This is a lower bound for IG
else
error - previous cost failed because of property
//The rest of the code should be executed on every execution of the task.
If (Pruning && CostSoFar >= upper bound) terminate.
if (arity==0 and required property can not be satisfied)
terminate this task
//Calculate cost of remaining inputs
For each remaining (from InputNo to arity) input group IG
Call search_circle()
If Starburst case and case (1)
error
else if case (1)
terminate this task
else If there is a non-null Winner in IG, case (2)
store its cost in InputCost
if (Pruning && CostSoFar exceeds G's context's upper bound) terminate task
else if (we did not just return from O_GROUP on IG)
//optimize this input; seek a winner for it
push this task
push O_GROUP for IG, using current context's cost minus CostSoFar plus InputCost[InputNo]
terminate this task
else // we just returned from O_GROUP on IG
Trace: This is an impossible plan
terminate this task
InputNo++;
endFor //calculate the cost of remaining inputs
//Now all inputs have been optimized
if (CostSoFar > G's context's upper bound)
terminate this task
//Now we know current expression satisfies current context.
if(GlobepsPruning && CostSoFar <= GLOBAL_EPS)
Make current mexpression a done winner for G
mark the current context as done
terminate this task
//Now we consider the possible states of the relevant winner in G
Search the winner's circle in G for the current task's physical property
If there is no such winner
error - the search should have initialized a winner
else If winner is done
error - we are in the midst of a search, not yet done
else If (winner is non-null and CostSoFar >= cost of this winner) || winner is null )
Replace existing winner with current mexpression and its cost, don't change done flag
Update the upper bound of the current context
*/
//##ModelId=3B0C085E0307
void O_INPUTS::perform ()
{
PTRACE2 ("O_INPUT performing Input %d, expr: %s", InputNo, MExpr->Dump() );
#ifdef IRPROP
int GrpNo = MExpr->GetGrpID();
PTRACE2 ("ContextID: %d, %s", ContextID, (M_WINNER::mc[GrpNo]->GetPhysProp(ContextID))->Dump());
#else
PTRACE2 ("Context ID: %d , %s", ContextID, CONT::vc[ContextID]->Dump() );
#endif
PTRACE ("Last flag is %d", Last);
//Cache local properties of G and the expression being optimized
OP * Op = MExpr ->GetOp(); //the op of the expr
assert(Op -> is_physical() );
GROUP * LocalGroup = Ssp -> GetGroup(MExpr -> GetGrpID()); //Group of the MExpr
#ifdef IRPROP
PHYS_PROP * LocalReqdProp = M_WINNER::mc[GrpNo]->GetPhysProp(ContextID);
COST * LocalUB = M_WINNER::mc[GrpNo]->GetUpperBd(LocalReqdProp);
PTRACE ("Bound (LocalUB) is %s", LocalUB->Dump());
#else
PHYS_PROP * LocalReqdProp = CONT::vc[ContextID] -> GetPhysProp(); //What prop is required
COST * LocalUB = CONT::vc[ContextID] -> GetUpperBd();
#endif
//if global eps pruning happened, terminate this task
if (GlobepsPruning && CONT::vc[ContextID] -> is_done())
{
PTRACE("%s", "Task terminated due to global eps pruning");
if (TraceOn && !ForGlobalEpsPruning) ClassStat[C_O_INPUTS].Delete();
if(Last)
// this's the last task for the group, so mark the group with completed optimizing
LocalGroup->set_optimized(true);
return;
}
//Declare locals
GRP_ID IGNo; //Input Group Number
GROUP * IG;
int input; //index over input groups
bool possible; // is it possible to satisfy the required property?
// COST * CostSoFar = new COST(0);
COST CostSoFar(0);
#ifndef IRPROP
WINNER * LocalWinner = LocalGroup -> GetWinner(LocalReqdProp); //Winner in G
#endif
COST Zero(0);
//On the first (and no other) execution, code must initialize some O_INPUTS members.
//The only nontrivial member is InputCost.
if( InputNo == -1 )
{
// init inputLogProp
for(input= 0; input< arity; input++)
{
GROUP * InputGroup = Ssp -> GetGroup(MExpr -> GetInput(input)); //Group of current input
InputLogProp[input] = InputGroup -> get_log_prop() ;
}
// get the localcost of the mexpr being optimized in G
LocalCost = Op->FindLocalCost ( LocalGroup->get_log_prop(), InputLogProp);
//For each input group IG
for(input= 0; input< arity; input++)
{
// Initial values of InputCost are zero in the Starburst (no Pruning) case
if(!Pruning)
{
if(!input)
PTRACE("%s","Not pruning so all InputCost elements are set to zero");
assert(!CuCardPruning);
InputCost[input] = &Zero;
continue;
}
IGNo = MExpr -> GetInput(input);
IG = Ssp -> GetGroup(IGNo); //Group of current input
// special case: the input is a const group - in item class
if(IG->GetFirstLogMExpr()->GetOp()->is_const())
{
PTRACE("Input %d is a const operator so its cost is zero", input);
InputCost[input] = ((CONST_OP *)IG->GetFirstLogMExpr()->GetOp())->get_cost();
continue;
}
PHYS_PROP * ReqProp;
if (Op->is_physical())
{
// Determine property required of that input
ReqProp = ((PHYS_OP *)Op)->InputReqdProp
(LocalReqdProp, InputLogProp[input], input, possible);
if( ! possible ) // if not possible, means no such input prop can satisfied
{
PTRACE ("Impossible search: Bad input %d", input);
delete ReqProp;
goto TerminateThisTask;
}
}
else ReqProp = new PHYS_PROP(any);
//call search_circle on IG with that property, infinite cost.
bool moreSearch, SCReturn;
COST * INFCost = new COST(-1);
#ifdef IRPROP
// the ReqProp will already be set up in multiwinner
SCReturn = IG -> search_circle(IGNo, ReqProp, moreSearch);
if (!moreSearch && !SCReturn) // input group is optimized
{
// if winner's cost >= INFCost then "impossible search, bad input"
PTRACE ("Impossible search: Bad input %d", input);
delete INFCost;
delete ReqProp;
goto TerminateThisTask;
}
else if (!moreSearch && SCReturn)
{
COST *WinCost = M_WINNER::mc[IGNo]->GetUpperBd(ReqProp);
InputCost[input] = WinCost;
}
else if (!CuCardPruning) // Group Pruning case
InputCost[input] = &Zero;
else // group is not optimized or CuCard Pruning case
InputCost[input] = IG -> GetLowerBd();
delete ReqProp;
delete INFCost;
#else
CONT * IGContext = new CONT(ReqProp, INFCost, false);
SCReturn = IG -> search_circle(IGContext, moreSearch);
PTRACE2("search_circle(): more search %s needed, return value is %s",
moreSearch?"":"not", SCReturn?"true":"false");
//If case (1), impossible, then terminate this task
if (!moreSearch && !SCReturn)
{
PTRACE ("Impossible search: Bad input %d", input);
delete IGContext;
goto TerminateThisTask;
}
//If search_circle returns a non-null Winner from InputGroup, case (2)
//InputCost[InputGroup] = cost of that winner
else if(!moreSearch && SCReturn )
{
InputCost[input] = IG -> GetWinner(ReqProp)->GetCost();
assert(IG->GetWinner(ReqProp)->GetDone());
}
//else if (!CuCardPruning) //Group Pruning case (since Starburst not relevant here)
//InputCost[IG] = 0
else if (!CuCardPruning)
InputCost[input] = &Zero;
//remainder applies only in CuCardPruning case
else
InputCost[input] = IG -> GetLowerBd();
delete IGContext;
#endif
} // initialize some O_INPUTS members
InputNo ++; // Ensure that previous code will not be executed again; begin with Input 0
}
//If Global Pruning and cost so far is greater than upper bound for this context, then terminate
CostSoFar.FinalCost(LocalCost, InputCost, arity);
if(Pruning && CostSoFar >= *LocalUB)
{
PTRACE2 ("Expr LowerBd %s, exceed Cond UpperBd %s,Pruning applied!",
CostSoFar.Dump(), LocalUB -> Dump() );
goto TerminateThisTask;
}
//Calculate the cost of remaining inputs
for(input = InputNo ; input < arity ; input++)
{
//set up local variables
IGNo = MExpr -> GetInput(input);
IG = Ssp -> GetGroup(IGNo); //Group of current input
// special case: the input is an const_op, continue
if(IG->GetFirstLogMExpr()->GetOp()->is_const())
{ // the cost of item group will not be refined, always equal to ConstGroupCost (0)
PTRACE("Input : %d is a const group", input);
continue;
}
//generate appropriate property for search of IG
PHYS_PROP * ReqProp;
if (Op->is_physical())
{
// Determine property required of that input
ReqProp = ((PHYS_OP *)Op)->InputReqdProp