-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexdc_factor_new_v2_ave_bak.cc
1385 lines (1288 loc) · 40.3 KB
/
exdc_factor_new_v2_ave_bak.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <climits>
#include <vector>
#include <map>
#include <cmath>
#include <cassert>
#include <ctime>
#include <sys/timeb.h>
#include "head/queue.h"
#include "head/stack.h"
#include "head/basics.h"
#include "head/helper.h"
#include "head/read_file.h"
#include "head/write_func.h"
#include "head/exdc_helper.h"
#include "head/btree.h"
#include "head/simu_ckt.h"
#include "head/sim_new_abc.h"
#include "cudd/bnet.h"
#include "head/loc_sim_main.h"
#include "/home/wuyi/usr/CUDD/cudd-2.5.0/cudd/cudd.h"
#include "/home/wuyi/usr/CUDD/cudd-2.5.0/cudd/cuddInt.h"
#include "cudd/cudd_build_v2.h"
//#include "cudd/ntr_cudd.h"
using namespace std;
extern int numPI_ini;
extern double total_deviation;
extern int num_deviation;
extern int sample_num;
extern double ini_threshold_em;
extern int num_one_po_equal, num_one_po_unequal;
extern map<string, set<char*> > po_tfi;
extern map<string, set<string> > po_inputs;
extern map<string, vector<string> > po_cone_string;
int sample_size = 1;
int conf_prob = 0.95; //confidence level
#define mode 2 ////mode = 2 represents maximum error magnitude; mode = 1 represents average error magnitude
#define inacc_ratio 0
#define exp_ratio 0.005
#define em_ratio 1
#define em_ratio_single 2
#define score_mode 2
#define ignore_flag 1
#define ignore_po_size_one 1
#define lit_weight 0.1
#define er_weight 0.6
#define em_weight 0.3
/*
functions in this file:
*/
//Global variables and external variables
int exdc_cubes_real_er(BnetNetwork *net, DdManager **dd, vector<string> &dont_care, vector<string> &local_dc_set, vector<char*> &cutnodes, set<string> &all_exdc_minterms, map<set<string>, double> &pattern_er, double &this_real_er, double threshold)
{
struct timeb st, et;
set<string>::iterator itrs, itrs1;
map<string, double>::iterator itrm_sd;
map<set<string>, double>::iterator itrm_vd;
BnetNode *auxnd;
cout << "In exdc_cubes_real_er, all_exdc_minterms: " << endl;
for(itrs = all_exdc_minterms.begin(); itrs != all_exdc_minterms.end(); itrs++)
cout << *itrs << endl;
itrm_vd = pattern_er.find(all_exdc_minterms);
if(itrm_vd != pattern_er.end())
{
this_real_er = itrm_vd->second;
cout << "found match in pattern_er!" << endl;
return 0;
}
ftime(&st);
ofstream fout;
fout.open("./pla_files/exdc_cubes.pla", ios::out);
itrs = all_exdc_minterms.begin();
string str = *itrs;
int numin = str.size();
int numout = 1;
fout << ".i " << numin << endl;
fout << ".o " << numout << endl;
fout << ".ilb ";
for(int i = 0; i < numin; i++)
fout << "n" << i << " ";
fout << endl;
fout << ".ob out" << endl;
// cout << "non-dc exdc_cubes: " << endl;
for(itrs = all_exdc_minterms.begin(); itrs != all_exdc_minterms.end(); itrs++)
{
string minterm = *itrs;
if(isIncludeVec(dont_care, minterm) || isIncludeVec(local_dc_set, minterm))
{
cout << "minterm " << minterm << " is dc." << endl;
continue;
}
fout << *itrs << " 1" << endl;
}
fout << ".e" << endl;
fout.close();
char com[100];
sprintf(com, "sis -t none -f ./script/sim_pla_cube.rug > ./pla_files/pla.txt");
system(com);
vector<string> exdc_cubes_sim;
ifstream fin;
fin.open("./pla_files/exdc_cubes_sim.pla", ios::in);
string s;
// cout << "exdc_cubes_sim: " << endl;
while(getline(fin, str))
{
istringstream ss(str);
ss >> s;
if(s[0] != '.')
{
exdc_cubes_sim.push_back(s);
// cout << s << endl;
}
}
fin.close();
ftime(&et);
double rt_sim_pla = ((et.time - st.time)*1000 + (et.millitm - st.millitm))/1000.0;
// cout << "@runtime for sim_pla: " << rt_sim_pla << endl;
/*Build bdds for don't cares in dc_include*/
int num_exdc_cubes = exdc_cubes_sim.size();
ftime(&st);
DdNode *tmp;
DdNode *func, *prod, *var, *var_tmp;
func = Cudd_ReadLogicZero(*dd);
Cudd_Ref(func);
cout << "exdc_cubes_sim: " << endl;
for(int i = 0; i < exdc_cubes_sim.size(); i++)
{
prod = Cudd_ReadOne(*dd);
Cudd_Ref(prod);
string dc = exdc_cubes_sim[i];
cout << dc << endl;
for(int i = 0; i < dc.size(); i++)
{
if(dc[i] == '-')
continue;
char *cnode = cutnodes[i];
if(!st_lookup((net)->hash, cnode, &auxnd))
{
cout << "current node doesn't exixt in st_table!" << endl;
exit(1);
}
if (dc[i] == '1')
var = auxnd->dd;
else
var = Cudd_Not(auxnd->dd);
tmp = Cudd_bddAnd(*dd, prod, var);
if (tmp == NULL)
{
cout << "tmp is NULL!" << endl;
exit(1);
}
Cudd_Ref(tmp);
Cudd_IterDerefBdd(*dd, prod);
prod = tmp;
}
tmp = Cudd_bddOr(*dd, func, prod);
Cudd_Ref(tmp);
Cudd_IterDerefBdd(*dd, func);
func = tmp;
}
double num_minterm = Cudd_CountMinterm(*dd, func, numPI_ini);
Cudd_IterDerefBdd(*dd, func);
this_real_er = num_minterm/pow(2.0, numPI_ini);
pattern_er.insert(make_pair(all_exdc_minterms, this_real_er));
ftime(&et);
double rt_comp_er = ((et.time - st.time)*1000 + (et.millitm - st.millitm))/1000.0;
// cout << "@runtime for comp_er: " << rt_comp_er << endl;
return 0;
}
/*exdc_real_er_v2()*/
double exdc_real_er_v2(BnetNetwork *net, DdManager **dd, vector<char*> &cutnodes, string &sexdc, int num_digit)
{
double this_real_er;
BnetNode *nd, *auxnd;
DdNode *func;
if(num_digit == 1)
{
char *cnode = cutnodes[0];
st_lookup(net->hash, cnode, &nd);
if(sexdc[0] == '1')
this_real_er = nd->rp;
else if(sexdc[0] == '0')
this_real_er = 1 - nd->rp;
return this_real_er;
}
else
{
DdNode *prod = Cudd_ReadOne(*dd);
Cudd_Ref(prod);
// cout << "sexdc = " << sexdc << endl;
for(int i = 0; i < sexdc.size(); i++)
{
if(sexdc[i] == '-')
continue;
char *cnode = cutnodes[i];
// cout << "cnode: " << cnode << endl;
if(!st_lookup((net)->hash, cnode, &auxnd))
{
cout << "current node doesn't exixt in st_table!" << endl;
exit(1);
}
if(auxnd->dd == NULL)
{
cout << "auxnd->dd is NULL!" << endl;
exit(1);
}
DdNode *var;
if (sexdc[i] == '1')
var = auxnd->dd;
else
var = Cudd_Not(auxnd->dd);
if(var == NULL)
{
cout << "var is NULL!" << endl;
exit(1);
}
DdNode *tmp = Cudd_bddAnd((*dd), prod, var);
if (tmp == NULL)
{
cout << "tmp is NULL!" << endl;
exit(1);
}
Cudd_Ref(tmp);
Cudd_IterDerefBdd(*dd, prod);
// Cudd_IterDerefBdd(*dd, var);
prod = tmp;
}
double num_minterm = Cudd_CountMinterm(*dd, prod, numPI_ini);
// cout << "num_minterm = " << num_minterm << endl;
Cudd_IterDerefBdd(*dd, prod);
double this_real_er = num_minterm/pow(2.0, numPI_ini);
return this_real_er;
}
}
void build_tree_from_exp(string &str, btNode **root)
{
*root = CreateInfixTree(str);
// int numPT = 0;
// cout << "compute numPT: " << endl;
// compNumPT(root, numPT);
// cout << "inorder print: " << endl;
InOrderPrintTree(*root);
// cout << endl;
// cout << "number of cubes: " << numPT << endl;
}
void add_space_star(string &str, string &new_str)
{
string s;
//add space around '(' and ')'
string::size_type n = 0;
string leftp = "( ";
string rightp = " )";
while((n = str.find('(', n)) != string::npos)
{
str.replace(n, 1, leftp);
n += leftp.size();
}
n = 0;
while((n = str.find(')', n)) != string::npos)
{
str.replace(n, 1, rightp);
n += rightp.size();
}
//add '*' in the right place
// cout << "current str: " << str << endl;
string preStr("*");
istringstream ss(str);
string newStr;
while(ss >> s)
{
if(s == "+" || s == ")")
{
newStr.append(s);
newStr.append(" ");
}
else
{
if(preStr != "*" && preStr != "+" && preStr != "(")
newStr.append("* ");
newStr.append(s);
newStr.append(" ");
}
preStr = s;
}
new_str = newStr;
}
void build_btree(vector<string> &lit_unit, vector<string> &sop_set, vector<string> &factor_set, map<string, btNode*> &factor_exp_trees)
{
map<string, btNode*>::iterator itrm_sb1;
string str, s;
for(int i = 0; i < lit_unit.size(); i++)
{
str = lit_unit[i];
string newStr;
add_space_star(str, newStr);
// cout << "new str: " << newStr << endl;
if(newStr.find('(') != string::npos || newStr.find(')') != string::npos)
factor_set.push_back(newStr);
else
sop_set.push_back(newStr);
}
//build a binary tree for each factor expression
// cout << "factor_set: " << endl;
for(int i = 0; i < factor_set.size(); i++)
{
// cout << factor_set[i] << endl;
btNode *root;
build_tree_from_exp(factor_set[i], &root);
factor_exp_trees.insert(pair<string, btNode*>(factor_set[i], root));
}
}
int check_ignore_case(BnetNetwork *net, istringstream &ss1, string &cstr)
{
string s;
BnetNode *tmp;
int num_node = 0;
while(ss1 >> s)
{
if(s == "*" || s == "(" || s == ")")
continue;
num_node++;
}
if(num_node == 1)
{
string ncstr;
string::size_type n = 0;
if((n = cstr.find('\'', n)) != string::npos)
ncstr = cstr.substr(0, n);
else
ncstr = cstr;
char *cname = new char[50];
strcpy(cname, ncstr.c_str());
st_lookup(net->hash, cname, &tmp);
if(tmp->type == BNET_INPUT_NODE)
{
cout << "the only node in this sop is an input node, ignore!" << endl;
delete []cname;
return 1;
}
else
delete []cname;
}
return 0;
}
void get_true_name(string &s, string &ncstr, int &sign)
{
//remove trailing spaces
int pos_space = -1;
for(int j = 0; j < s.size(); j++)
if(s[j] == ' ')
{
pos_space = j;
break;
}
if(pos_space != -1)
s = s.substr(0, pos_space);
//remove last '\'' if exists
string::size_type n = 0;
sign = 1; //positive literal
if((n = s.find('\'', n)) != string::npos)
{
ncstr = s.substr(0, n);
sign = 0; //positive literal
}
else
ncstr = s;
}
void retrieve_cube(istringstream &ss, map<string, int> &name_pos, int num_input, string &ccube)
{
string s;
map<string, int>::iterator itrm_si;
map<int, int> pos_sign;
map<int, int>::iterator itrmi;
while(ss >> s)
{
if(s == "*" || s == "(" || s == ")")
continue;
string ncstr;
int sign;
get_true_name(s, ncstr, sign);
itrm_si = name_pos.find(ncstr);
pos_sign.insert(pair<int, int>(itrm_si->second, sign));
}
for(int i = 0; i < num_input; i++)
{
itrmi = pos_sign.find(i);
if(itrmi == pos_sign.end())
ccube.append(1, '-');
else
{
int sign = itrmi->second;
if(sign == 1)
ccube.append(1, '1');
else
ccube.append(1, '0');
}
}
}
void update_index_inv_pla(btNode *root, map<int, btNode*> &leaf_set, map<string, int> &name_pos, int num_input, map<int, vector<string> > &index_inv_pla, multimap<string, int> &node_index, set<string> ¤t_inv_pla)
{
// cout << "in update_index_inv_pla: " << endl;
map<int, btNode*>::iterator itrm_ib;
for(itrm_ib = leaf_set.begin(); itrm_ib != leaf_set.end(); itrm_ib++)
{
int index = itrm_ib->first;
btNode *leaf = itrm_ib->second;
node_index.insert(make_pair(leaf->data, index));
// cout << "leaf node: " << leaf->data << endl;
//obtain index_inv_pla
vector<string> inv_cubes;
get_involve_cubes(root, leaf, inv_cubes);
vector<string> inv_pla;
// cout << "inv_cubes: " << endl;
for(int j = 0; j < inv_cubes.size(); j++)
{
// cout << inv_cubes[j] << endl;
string this_inv_pla;
get_involve_pla(leaf_set, name_pos, num_input, inv_cubes[j], this_inv_pla);
current_inv_pla.insert(this_inv_pla);
inv_pla.push_back(this_inv_pla);
// cout << this_inv_pla << endl;
}
index_inv_pla.insert(make_pair(index, inv_pla));
}
}
//exdc_factor
void exdc_factor_new_v2(BnetNetwork *net, DdManager **dd, char *cnode, vector<char*> &unsort_cutnodes, map<string, struct score_pla> &sim_record, vector<string> &org_pla, vector<string> &dont_care, vector<string> &local_dc_set, set<char*> &po_set, map<string, vector<string> > &po_dont_care_map, map<string, int> &internal_index, vector<struct index_flag> &input_index, map<string, struct wi_pair> &sim_output_wi, struct po_index_set &pis, vector<string> &rand, vector<string> &simu_res, int num_output, map<string, map<string, double> > &node_pattern_rate, struct score_pla &max_sp, double threshold_er, double threshold_em, double real_em, int &min_modified_po, int &max_modified_po, int iIndex)
{
//iterators
struct timeb st, et, st1, et1, st2, et2;
multimap<double, string>::iterator itrmm_ds;
map<string, double>::iterator itrm_sd, itrm_sd1;
map<string, struct score_pla>::iterator itrm_ss;
multimap<double, vector<string> >::iterator itrm_dv;
set<int>::iterator itrs0, itrs1, itrs2;
set<string>::iterator itrss, itrss0, itrss1;
set<char*>::iterator itrs_char;
map<string, set<char*> >::iterator itrm_sc;
map<string, set<string> >::iterator itrm_sss;
map<string, vector<string> >::iterator itrm_sv;
map<string, struct wi_pair>::iterator itrm_sw;
map<int, int>::iterator itrmi;
//variables
char com[100];
string str, s;
BnetNode *nd, *tmp, *auxnd;
max_sp.score = -(1e+9);
max_sp.lit_save = 0;
max_sp.real_er = 1;
max_sp.ave_em = 1e+9;
max_sp.status = 0;
double max_score = -(1e+9);
int lit_save, num_lines_org_pla = 0, flag_within = 0, cur_em_ratio;
double score, this_real_er, max_error_mag = 1;
vector<string> final_pla;
map<set<string>, double> pattern_er;
//print out current node's info
cout << endl << "$current node: " << cnode << endl;
int num_input = unsort_cutnodes.size();
st_lookup(net->hash, cnode, &nd);
cout << "org_pla: " << endl;
for(int i = 0; i < org_pla.size(); i++)
{
cout << org_pla[i] << endl;
num_lines_org_pla++;
}
cout << endl;
//get the factored form of cnode
cout << endl << "$factor form of bignode: " << endl;
write_bignode_pla(net, cnode);
sprintf(com, "sis -t none -f ./script/print_factor_org.rug > factor.txt");
system(com);
//get name_pos
cout << "unsort_cutnodes: " << endl;
set<string> insig;
map<string, int> name_pos;
map<string, int>::iterator itrm_si;
for(int i = 0; i < unsort_cutnodes.size(); i++)
{
cout << unsort_cutnodes[i] << " ";
string name(unsort_cutnodes[i]);
insig.insert(name);
name_pos.insert(pair<string, int>(name, i));
}
cout << endl;
//if current node is PO itself and mode = 2 (maximum error magnitude), then check whether its weight is larger than weight_limit
int weight_limit = log(threshold_em)/log(2);
//if current node has only one affected po, then first get its weight and compute its maximum error magnitude
int flag_po = 0;
int min_weight = 1000, max_weight = -1;
int only_weight = -1;
if (po_set.size() == 1)
{
cout << "Current node has one affected po!" << endl;
itrs_char = po_set.begin();
string po(*itrs_char);
if(iIndex > 0) po.append("sim");
itrm_sw = sim_output_wi.find(po);
int weight = itrm_sw->second.weight;
min_weight = max_weight = only_weight = weight;
max_error_mag = pow(2.0, weight);
if (mode == 2)
{
st_lookup(net->hash, cnode, &nd);
if (nd->nfo == 1)
{
char *fanout = nd->fanouts[0];
st_lookup(net->hash, fanout, &tmp);
if (tmp->type == BNET_OUTPUT_NODE)
{
cout << "weight = " << weight << ", weight_limit = " << weight_limit << endl;
if (weight > weight_limit)
{
cout << "cnode is PO itself and its weight is larger than weight_limit!" << endl;
return;
}
else
}
}
}
}
else
{
cout << "Current node has multiple affected pos!" << endl;
for(itrs_char = po_set.begin(); itrs_char != po_set.end(); itrs_char++)
{
string po(*itrs_char);
if(iIndex > 0) po.append("sim");
itrm_sw = sim_output_wi.find(po);
int weight = itrm_sw->second.weight;
if (weight < min_weight) min_weight = weight;
if (weight > max_weight) max_weight = weight;
}
max_error_mag = pow(2.0, min_weight);
}
// cout << "max_error_mag = " << max_error_mag << endl;
int pre_min_modified_po = min_modified_po;
int pre_max_modified_po = max_modified_po;
cout << "min_modified_po = " << min_modified_po << ", max_modified_po = " << max_modified_po << endl;
cout << "min_weight = " << min_weight << ", max_weight = " << max_weight << endl;
int this_min_modified_po = (min_weight < min_modified_po)? min_weight: min_modified_po;
int this_max_modified_po = (max_weight > max_modified_po)? max_weight: max_modified_po;
min_modified_po = this_min_modified_po;
max_modified_po = this_max_modified_po;
cout << "this_min_modified_po = " << this_min_modified_po << ", this_max_modified_po = " << this_max_modified_po << endl;
if (this_min_modified_po > weight_limit) return;
//read sub_abs.blif and its POs
ifstream fin;
fin.open("./files/sub_abs/sub_abs_mtp.blif", ios::in);
string newline;
int start = 0;
vector<string> sub_abs_ckt;
while(getline(fin, str))
{
istringstream ss(str);
ss >> s;
if(s == ".end")
break;
if(s == ".names" && start == 0) start = 1;
newline.clear();
if(start)
{
if (s == ".names")
{
newline.append(s);
newline.append(" ");
while (ss >> s)
{
s.append("_sub ");
newline.append(s);
}
}
else
newline = str;
}
sub_abs_ckt.push_back(newline);
}
vector<string> sub_abs_pi, sub_abs_po;
FILE *fp = fopen("./files/sub_abs/sub_abs_mtp.blif", "r");
BnetNetwork *net_sub = Bnet_ReadNetwork(fp);
for (int i = 0; i < net_sub->npis; i++)
{
char *pi = net_sub->inputs[i];
string pi_str(pi);
pi_str.append("_sub");
sub_abs_pi.push_back(pi_str);
}
for (int i = 0; i < net_sub->npos; i++)
{
char *po = net_sub->outputs[i];
string po_str(po);
po_str.append("_sub");
sub_abs_po.push_back(po_str);
}
fclose(fp);
#ifdef use_record
if(!sim_record.empty())
{
string cnode_str(cnode);
itrm_ss = sim_record.find(cnode_str);
if(itrm_ss != sim_record.end())
{
struct score_pla this_score_pla = itrm_ss->second;
cout << "found match in sim_record: " << endl;
set<string> old_dc = this_score_pla.dc;
set<string> new_dc;
for(int i = 0; i < dont_care.size(); i++)
new_dc.insert(dont_care[i]);
for(int i = 0; i < local_dc_set.size(); i++)
new_dc.insert(local_dc_set[i]);
set<string> old_insig = this_score_pla.insig;
final_pla = this_score_pla.pla;
string cdc(num_input, '-');
if(old_dc != new_dc || old_insig != insig)
cout << "don't-cares are changed or input nodes are changed!" << endl;
else if(final_pla.empty() || final_pla[0] == cdc)
cout << "this node is simplified to be 0 or 1!" << endl;
else
{
cout << "don't-cares and input nodes are unchanged!" << endl;
cout << "this_final_pla: " << endl;
for(int i = 0; i < final_pla.size(); i++)
cout << final_pla[i] << endl;
max_sp = this_score_pla;
return;
}
}
}
#endif
//obtain pattern_rate for cnode
map<string, map<string, double> >::iterator itrm_ssd;
string sname(cnode);
itrm_ssd = node_pattern_rate.find(sname);
map<string, double> pattern_rate = itrm_ssd->second;
// cout << "patterns with error rate <= threshold_er: " << endl;
//If all input patterns have probability larger than threshold_er, than directly return.
int num_useful = 0;
if (dont_care.empty())
{
for(itrm_sd = pattern_rate.begin(); itrm_sd != pattern_rate.end(); itrm_sd++)
{
double er = itrm_sd->second;
if((er-threshold_er)/threshold_er <= inacc_ratio)
{
num_useful++;
break;
}
}
if(!num_useful)
{
max_sp.score = -1;
max_sp.real_er = 1;
cout << "no useful patterns!" << endl;
return;
}
}
//read the factored form of cnode from file factor.txt
string ffe;
read_factor_v2(ffe);
//build a binary tree for the factored-form-expression
btNode *ini_root;
string ffe_space;
add_space_star(ffe, ffe_space);
build_tree_from_exp(ffe_space, &ini_root);
cout << "ffe_space = " << ffe_space << endl;
//pick exdcs for ffe(factor-form-expression)
cout << endl << "**************************************" << endl;
cout << "$pick exdcs for ffe(factor-form-expression): " << endl;
cout << "current factor: " << ffe << endl;
btNode *root;
//find all leaf nodes and set their indexes: leaf_set
map<int, btNode*> ini_leaf_set, leaf_set;
map<int, btNode*>::iterator itrm_ib;
int start_ind = 0;
visitleaf(ini_root, ini_leaf_set, start_ind);
leaf_set = ini_leaf_set;
int num_lit = ini_leaf_set.size();
cout << "num_lit = " << num_lit << endl;
comp_exp(&ini_root); //compute expression at each node
//obtain ini_node_inv_pla & ini_current_inv_pla
map<int, vector<string> > ini_index_inv_pla, index_inv_pla;
map<int, vector<string> >::iterator itrm_iv;
multimap<string, int> ini_node_index, node_index;
multimap<string, int>::iterator itrmm_si;
set<string> ini_current_inv_pla, current_inv_pla;
update_index_inv_pla(ini_root, ini_leaf_set, name_pos, num_input, ini_index_inv_pla, ini_node_index, ini_current_inv_pla);
index_inv_pla = ini_index_inv_pla;
current_inv_pla = ini_current_inv_pla;
node_index = ini_node_index;
//start check every possible simplification
set<int> over_er_index;
set<string> exdc_cubes, all_exdc_minterms;
vector<string> sim_org_pla, sim_org_pla_tmp, final_org_pla;
double er_devi;
int num_lit_org = num_lit;
if(num_lit > 5) num_lit = 5; //set the limit for literal save
//ASPDAC modification: ignore the case where lit_save = 1
for(int i = 2; i <= num_lit; i++)
{
if(i == num_lit)
{
cout << endl << "##lit_save = " << i << endl;
cout << "case 1. removing this whole factor: " << endl;
// cout << "ini_current_inv_pla: " << endl;
exdc_cubes.clear();
for(itrss = ini_current_inv_pla.begin(); itrss != ini_current_inv_pla.end(); itrss++)
{
// cout << *itrs << endl;
exdc_cubes.insert(*itrss);
}
all_exdc_minterms.clear();
for(itrss = exdc_cubes.begin(); itrss != exdc_cubes.end(); itrss++)
{
string cube = *itrss;
exp_cube_set(cube, all_exdc_minterms);
}
double this_simu_er = 0;
//status = 1 means that there are non-dc minterms that will cause errors
int status = simu_real_er(all_exdc_minterms, dont_care, local_dc_set, pattern_rate, num_deviation, this_simu_er);
this_real_er = this_simu_er;
cout << "this_simu_er = " << this_real_er << ", threshold_er = " << threshold_er << endl;
er_devi = (this_simu_er - threshold_er)/threshold_er;
if(er_devi > inacc_ratio)
cout << "!error rate is beyond threshold_er" << endl;
else
{
cout << "!error rate is within threshold_er" << endl;
//compute the averge error magnitude
if (ignore_flag && this_real_er * max_error_mag > em_ratio_single * threshold_em)
cout << "out of threshold_em!" << endl;
else
{
sim_org_pla.clear();
for(int k = 0; k < org_pla.size(); k++)
{
itrss = ini_current_inv_pla.find(org_pla[k]);
if(itrss == ini_current_inv_pla.end())
sim_org_pla.push_back(org_pla[k]);
}
final_org_pla.clear();
lit_save = get_save_new(net, cnode, 0, sim_org_pla, final_org_pla, iIndex);
if (lit_save == 1)
{
cout << "lit_save = 1, ignore this benefit." << endl;
continue;
}
double cur_error_mag;
if(this_simu_er == 0)
{
cur_error_mag = real_em;
cout << "po_set: " << endl;
for(itrs_char = po_set.begin(); itrs_char != po_set.end(); itrs_char++)
{
cout << *itrs_char << " ";
if(status)
{
string po(*itrs_char);
if(iIndex > 0) po.append("sim");
itrm_sw = sim_output_wi.find(po);
int weight = itrm_sw->second.weight;
cur_error_mag += pow(2.0, weight) * exp_ratio;
}
}
cout << endl;
}
else
cur_error_mag = estimate_error_mag_bdd(net, cnode, sub_abs_ckt, sub_abs_pi, sub_abs_po, this_min_modified_po, this_max_modified_po, final_org_pla, threshold_em, weight_limit, mode);
cout << "cur_error_mag = " << cur_error_mag << endl;
cout << "threshold_em = " << threshold_em << endl;
//if this node itself is a PO, then em_ratio = 1
cur_em_ratio = em_ratio;
if (flag_po == 1) cur_em_ratio = 1;
if(cur_error_mag <= cur_em_ratio * threshold_em)
{
cout << "within threshold_em!" << endl;
if (lit_save > 1)
{
if (flag_within == 0) flag_within = 1;
if (score_mode == 1)
{
if(this_real_er == 0 && cur_error_mag == 0)
score = lit_save * 100;
else
score = lit_weight*lit_save/num_lit_org + er_weight*(threshold_er - this_real_er)/threshold_er + em_weight*(threshold_em - cur_error_mag)/threshold_em;
}
else if (score_mode == 2)
{
if (this_real_er == 0)
{
if (cur_error_mag == 0) score = lit_save * (1e+10);
else score = lit_save / (exp_ratio * cur_error_mag);
}
else score = lit_save / (this_real_er*cur_error_mag);
}
cout << "lit_save = " << lit_save << endl;
cout << "score = " << score << endl;
if(score > max_score)
{
max_sp.score = score;
max_sp.lit_save = lit_save;
max_sp.real_er = this_real_er;
max_sp.ave_em = cur_error_mag;
max_sp.pla = final_org_pla;
if (status && this_real_er == 0)
max_sp.status = 1;
if (!status && this_real_er == 0)
{
if (pre_min_modified_po == 1000 && pre_max_modified_po == -1)
{
min_modified_po = 1000;
max_modified_po = -1;
}
}
vector<string> all_dc = dont_care;
all_dc.insert(all_dc.end(), local_dc_set.begin(), local_dc_set.end());
set<string> dc_set;
for(int k = 0; k < all_dc.size(); k++)
dc_set.insert(all_dc[k]);
max_sp.dc = dc_set;
max_sp.insig = insig;
max_score = score;
}
}
}
}
}
cout << "case 2. make this whole factor become 1: " << endl;
string cdc(num_input, '-');
vector<string> org_cubes;
org_cubes.push_back(cdc);
all_exdc_minterms.clear();
minus_cubes(org_cubes, org_pla, all_exdc_minterms);
this_simu_er = 0;
status = simu_real_er(all_exdc_minterms, dont_care, local_dc_set, pattern_rate, num_deviation, this_simu_er);
this_real_er = this_simu_er;
cout << "this_simu_er = " << this_real_er << ", threshold_er = " << threshold_er << endl;
er_devi = (this_simu_er - threshold_er)/threshold_er;
if(er_devi > inacc_ratio)
cout << "!error rate is beyond threshold_er" << endl;
else
{
cout << "!error rate is within threshold_er" << endl;
//compute the averge error magnitude
if (ignore_flag && this_real_er * max_error_mag > em_ratio_single * threshold_em)
cout << "out of threshold_em!" << endl;
else
{
sim_org_pla.clear();
final_org_pla.clear();
lit_save = get_save_new(net, cnode, 1, sim_org_pla, final_org_pla, iIndex);
if (lit_save == 1)
{
cout << "lit_save = 1, ignore this benefit." << endl;
continue;
}
double cur_error_mag;
if(this_simu_er == 0)
{
cur_error_mag = real_em;
cout << "po_set: " << endl;
for(itrs_char = po_set.begin(); itrs_char != po_set.end(); itrs_char++)
{
cout << *itrs_char << " ";
if(status)
{
string po(*itrs_char);
if(iIndex > 0) po.append("sim");
itrm_sw = sim_output_wi.find(po);
int weight = itrm_sw->second.weight;
cur_error_mag += pow(2.0, weight) * exp_ratio;
}
}
cout << endl;
}
else
cur_error_mag = estimate_error_mag_bdd(net, cnode, sub_abs_ckt, sub_abs_pi, sub_abs_po, this_min_modified_po, this_max_modified_po, final_org_pla, threshold_em, weight_limit, mode);
cout << "cur_error_mag = " << cur_error_mag << endl;
cout << "threshold_em = " << threshold_em << endl;
//if this node itself is a PO, then em_ratio = 1
cur_em_ratio = em_ratio;
if (flag_po == 1) cur_em_ratio = 1;
if(cur_error_mag <= cur_em_ratio * threshold_em)
{
cout << "within threshold_em!" << endl;
if (lit_save > 1)
{
if (flag_within == 0) flag_within = 1;
if (score_mode == 1)
{
if(this_real_er == 0 && cur_error_mag == 0)
score = lit_save * 100;
else
score = lit_weight*lit_save/num_lit_org + er_weight*(threshold_er - this_real_er)/threshold_er + em_weight*(threshold_em - cur_error_mag)/threshold_em;
}
else if (score_mode == 2)
{
if (this_real_er == 0)
{
if (cur_error_mag == 0) score = lit_save * (1e+10);
else score = lit_save/(exp_ratio*cur_error_mag);
}
else score = lit_save/(this_real_er*cur_error_mag);
}
cout << "lit_save = " << lit_save << endl;
cout << "score = " << score << endl;
if(score > max_score)
{
max_sp.score = score;
max_sp.lit_save = lit_save;
max_sp.real_er = this_real_er;
max_sp.ave_em = cur_error_mag;
max_sp.pla = final_org_pla;
cout << "status = " << status << ", this_real_er = " << this_real_er << endl;
if (status && this_real_er == 0)
max_sp.status = 1;
if (!status && this_real_er == 0)
{
if (pre_min_modified_po == 1000 && pre_max_modified_po == -1)
{