-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathbdd.c
413 lines (341 loc) · 10 KB
/
bdd.c
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
#include <inttypes.h>
#include "vtr_assert.h"
#include "ace.h"
#include "misc/vec/vecPtr.h"
#include "bdd.h"
#include "cube.h"
#include "bdd/cudd/cuddInt.h"
//#include "vecPtr.h"
//#include "cudd.h"
int check_pi_status(Abc_Obj_t * obj);
void ace_bdd_count_paths(DdManager * mgr, DdNode * bdd, int * num_one_paths,
int * num_zero_paths);
double calc_cube_switch_prob(DdManager * mgr, DdNode * bdd, ace_cube_t * cube,
Vec_Ptr_t * inputs, int phase);
double calc_switch_prob_recur(DdManager * mgr, DdNode * bdd_next, DdNode * bdd,
ace_cube_t * cube, Vec_Ptr_t * inputs, double P1, int phase);
void ace_bdd_get_literals(Abc_Ntk_t * ntk, st__table ** lit_st_table,
Vec_Ptr_t ** literals) {
Abc_Obj_t * obj;
int i;
*literals = Vec_PtrAlloc(0);
*lit_st_table = st__init_table(st__ptrcmp, st__ptrhash);
Abc_NtkForEachObj(ntk, obj, i)
{
if (Abc_ObjIsCi(obj)) {
st__insert(*lit_st_table, (char*) obj,
(char*) Vec_PtrSize( *literals));
Vec_PtrPush(*literals, obj);
}
}
}
int check_pi_status(Abc_Obj_t * obj) {
int i;
Abc_Obj_t * fanin;
Abc_ObjForEachFanin(obj, fanin, i)
{
Ace_Obj_Info_t * info = Ace_ObjInfo(obj);
if (info->status == ACE_UNDEF) {
return 0;
}
}
return 1;
}
void ace_bdd_count_paths(DdManager * mgr, DdNode * bdd, int * num_one_paths,
int * num_zero_paths) {
DdNode * then_bdd;
DdNode * else_bdd;
if (bdd == Cudd_ReadLogicZero(mgr)) {
*num_zero_paths = *num_zero_paths + 1;
return;
} else if (bdd == Cudd_ReadOne(mgr)) {
*num_one_paths = *num_one_paths + 1;
return;
}
then_bdd = Cudd_T(bdd);
ace_bdd_count_paths(mgr, then_bdd, num_one_paths, num_zero_paths);
//bdd_free(then_bdd);
else_bdd = Cudd_E(bdd);
ace_bdd_count_paths(mgr, else_bdd, num_one_paths, num_zero_paths);
//bdd_free(else_bdd);
}
#if 0
int ace_bdd_build_network_bdds(
Abc_Ntk_t * ntk,
st_table * leaves,
Vec_Ptr_t * inputs,
int max_size,
double min_Prob)
{
Abc_Obj_t * obj;
double * P1s;
int i;
Vec_Ptr_t * nodes;
VTR_ASSERT(Vec_PtrSize(inputs) > 0);
nodes = Abc_NtkDfsSeq(ntk);
P1s = malloc(sizeof(double) * Vec_PtrSize(inputs));
Vec_PtrForEachEntry (Abc_Obj_t *, inputs, obj, i)
{
Ace_Obj_Info_t * info = Ace_ObjInfo(obj);
P1s[i] = info->static_prob;
info->prob0to1 = ACE_P0TO1(info->static_prob, info->switch_prob);
info->prob1to0 = ACE_P1TO0(info->static_prob, info->switch_prob);
}
Vec_PtrForEachEntry(Abc_Obj_t *, nodes, obj, i)
{
Ace_Obj_Info_t * info = Ace_ObjInfo(obj);
if (Abc_ObjIsCi(obj))
{
continue;
}
switch (info->status)
{
case ACE_SIM:
VTR_ASSERT (info->static_prob >= 0.0 && info->static_prob <= 1.0);
VTR_ASSERT (info->switch_prob >= 0.0 && info->switch_prob <= 1.0);
if (!st_lookup(leaves, (char*) obj, NULL))
{
st_insert(leaves, (char*) obj, (char*) Vec_PtrSize(inputs));
Vec_PtrPush(inputs, obj);
P1s = realloc(P1s, sizeof(double) * Vec_PtrSize(inputs));
P1s[Vec_PtrSize(inputs) - 1] = info->static_prob;
info->prob0to1 = ACE_P0TO1(info->static_prob, info->switch_prob);
info->prob1to0 = ACE_P1TO0(info->static_prob, info->switch_prob);
}
break;
case ACE_UNDEF:
VTR_ASSERT(0);
if (check_pi_status(obj))
{
while(1)
{
DdNode * bdd = obj->pData;
int n0;
int n1;
ace_bdd_count_paths(ntk->pManFunc, bdd, &n1, &n0);
if (n1 <= max_size && n0 <= max_size)
{
break;
}
//m == choose_root_fanin
//st_insert(leaves, (char*) )
}
}
break;
case ACE_DEF:
VTR_ASSERT(info->static_prob >= 0 && info->static_prob <= 1.0);
VTR_ASSERT(info->switch_prob >= 0 && info->switch_prob <= 1.0);
break;
case ACE_NEW:
case ACE_OLD:
default:
VTR_ASSERT(0);
}
}
free(P1s);
Vec_PtrFree(nodes);
return 0;
}
#endif
double calc_cube_switch_prob_recur(DdManager * mgr, DdNode * bdd,
ace_cube_t * cube, Vec_Ptr_t * inputs, st__table * visited, int phase) {
double * current_prob;
short i;
Abc_Obj_t * pi;
DdNode * bdd_if1, *bdd_if0;
double then_prob, else_prob;
if (bdd == Cudd_ReadLogicZero(mgr)) {
if (phase == 0)
return (1.0);
if (phase == 1)
return (0.0);
} else if (bdd == Cudd_ReadOne(mgr)) {
if (phase == 1)
return (1.0);
if (phase == 0)
return (0.0);
}
if (st__lookup(visited, (char *) bdd, (char **) ¤t_prob)) {
return (*current_prob);
}
/* Get literal index for this bdd node. */
//VTR_ASSERT(0);
i = Cudd_Regular(bdd)->index;
pi = (Abc_Obj_t*) Vec_PtrEntry((Vec_Ptr_t*) inputs, i);
current_prob = (double*) malloc(sizeof(double));
if (Cudd_IsComplement(bdd)) {
bdd_if1 = Cudd_E(bdd);
bdd_if0 = Cudd_T(bdd);
} else {
bdd_if1 = Cudd_T(bdd);
bdd_if0 = Cudd_E(bdd);
}
Ace_Obj_Info_t * fanin_info = Ace_ObjInfo(pi);
then_prob = calc_cube_switch_prob_recur(mgr, bdd_if1, cube, inputs, visited,
phase);
VTR_ASSERT(then_prob + EPSILON >= 0 && then_prob - EPSILON <= 1);
else_prob = calc_cube_switch_prob_recur(mgr, bdd_if0, cube, inputs, visited,
phase);
VTR_ASSERT(else_prob + EPSILON >= 0 && else_prob - EPSILON <= 1);
switch (node_get_literal (cube->cube, i)) {
case ZERO:
*current_prob = fanin_info->prob0to1 * then_prob
+ (1.0 - fanin_info->prob0to1) * else_prob;
break;
case ONE:
*current_prob = (1.0 - fanin_info->prob1to0) * then_prob
+ fanin_info->prob1to0 * else_prob;
break;
case TWO:
*current_prob = fanin_info->static_prob * then_prob
+ (1.0 - fanin_info->static_prob) * else_prob;
break;
default:
fail("Bad literal.");
}
st__insert(visited, (char *) bdd, (char *) current_prob);
VTR_ASSERT(*current_prob + EPSILON >= 0 && *current_prob - EPSILON < 1.0);
return (*current_prob);
}
double calc_cube_switch_prob(DdManager * mgr, DdNode * bdd, ace_cube_t * cube,
Vec_Ptr_t * inputs, int phase) {
double sp;
st__table * visited;
st__generator * gen;
const char* pKey;
char* pValue;
visited = st__init_table(st__ptrcmp, st__ptrhash);
sp = calc_cube_switch_prob_recur(mgr, bdd, cube, inputs, visited, phase);
st__foreach_item(visited, gen, (const char **)&pKey, (char **)&pValue) {
free(pValue);
}
st__free_table(visited);
VTR_ASSERT(sp + EPSILON >= 0. && sp - EPSILON <= 1.0);
return (sp);
}
double calc_switch_prob_recur(DdManager * mgr, DdNode * bdd_next, DdNode * bdd,
ace_cube_t * cube, Vec_Ptr_t * inputs, double P1, int phase) {
short i;
Abc_Obj_t * pi;
double switch_prob_t, switch_prob_e;
double prob;
DdNode * bdd_if1, *bdd_if0;
ace_cube_t * cube0, *cube1;
Ace_Obj_Info_t * info;
VTR_ASSERT(inputs != NULL);
VTR_ASSERT(Vec_PtrSize(inputs) > 0);
VTR_ASSERT(P1 >= 0);
if (bdd == Cudd_ReadLogicZero(mgr)) {
if (phase != 1)
return (0.0);
prob = calc_cube_switch_prob(mgr, bdd_next, cube, inputs, phase);
prob *= P1;
VTR_ASSERT(prob + EPSILON >= 0. && prob - EPSILON <= 1.);
return (prob * P1);
} else if (bdd == Cudd_ReadOne(mgr)) {
if (phase != 0)
return (0.0);
prob = calc_cube_switch_prob(mgr, bdd_next, cube, inputs, phase);
prob *= P1;
VTR_ASSERT(prob + EPSILON >= 0. && prob - EPSILON <= 1.);
return (prob * P1);
}
/* Get literal index for this bdd node. */
i = Cudd_Regular(bdd)->index;
pi = (Abc_Obj_t*) Vec_PtrEntry((Vec_Ptr_t*) inputs, i);
info = Ace_ObjInfo(pi);
if (Cudd_IsComplement(bdd)) {
bdd_if1 = Cudd_E(bdd);
bdd_if0 = Cudd_T(bdd);
} else {
bdd_if1 = Cudd_T(bdd);
bdd_if0 = Cudd_E(bdd);
}
/* Recursive call down the THEN branch */
cube1 = ace_cube_dup(cube);
set_remove(cube1->cube, 2 * i);
set_insert(cube1->cube, 2 * i + 1);
switch_prob_t = calc_switch_prob_recur(mgr, bdd_next, bdd_if1, cube1,
inputs, P1 * info->static_prob, phase);
ace_cube_free(cube1);
/* Recursive call down the ELSE branch */
cube0 = ace_cube_dup(cube);
set_insert(cube0->cube, 2 * i);
set_remove(cube0->cube, 2 * i + 1);
switch_prob_e = calc_switch_prob_recur(mgr, bdd_next, bdd_if0, cube0,
inputs, P1 * (1.0 - info->static_prob), phase);
ace_cube_free(cube0);
VTR_ASSERT(switch_prob_t + EPSILON >= 0. && switch_prob_t - EPSILON <= 1.);
VTR_ASSERT(switch_prob_e + EPSILON >= 0. && switch_prob_e - EPSILON <= 1.);
return (switch_prob_t + switch_prob_e);
}
double ace_bdd_calc_switch_act(DdManager * mgr, Abc_Obj_t * obj,
Vec_Ptr_t * fanins) {
int d;
int n0, n1;
Ace_Obj_Info_t * info = Ace_ObjInfo(obj);
Abc_Obj_t * fanin;
ace_cube_t * cube;
double switch_act;
int i;
DdNode * bdd;
d = info->depth;
VTR_ASSERT(d > 0);
d = (int) d * 0.4;
if (d < 1) {
d = 1;
}
if (Vec_PtrSize(fanins) < 1) {
return 0.5;
}
bdd = (DdNode*) obj->pData;
n0 = n1 = 0;
ace_bdd_count_paths(mgr, bdd, &n1, &n0);
Vec_PtrForEachEntry(Abc_Obj_t*, fanins, fanin, i)
//#define Vec_PtrForEachEntry( vVec, pEntry, i ) for ( i = 0; (i < Vec_PtrSize(vVec)) && (((pEntry) = Vec_PtrEntry(vVec, i)), 1); i++ )
//for ( i = 0; (i < Vec_PtrSize(fanins)) && (((fanin) = Vec_PtrEntry(fanins, i)), 1); i++ )
{
Ace_Obj_Info_t * fanin_info = Ace_ObjInfo(fanin);
fanin_info->prob0to1 =
ACE_P0TO1 (fanin_info->static_prob, fanin_info->switch_prob / (double) d);
fanin_info->prob1to0 =
ACE_P1TO0 (fanin_info->static_prob, fanin_info->switch_prob / (double) d);
prob_epsilon_fix(&fanin_info->prob0to1);
prob_epsilon_fix(&fanin_info->prob1to0);
VTR_ASSERT(
fanin_info->prob0to1 + EPSILON >= 0.
&& fanin_info->prob0to1 - EPSILON <= 1.0);
VTR_ASSERT(
fanin_info->prob1to0 + EPSILON >= 0.
&& fanin_info->prob1to0 - EPSILON <= 1.0);
}
cube = ace_cube_new_dc(Vec_PtrSize(fanins));
switch_act = 2.0
* calc_switch_prob_recur(mgr, bdd, bdd, cube, fanins, 1.0, n1 > n0)
* (double) d;
//switch_act = 2.0 * calc_switch_prob_recur (mgr, bdd, bdd, cube, fanins, 1.0, 1) * (double) d;
ace_cube_free(cube);
return switch_act;
}
int node_error(int code) {
switch (code) {
case 0:
fail("node_get_cube: node does not have a function");
/* NOTREACHED */
case 1:
fail("node_get_cube: cube index out of bounds");
/* NOTREACHED */
case 2:
fail("node_get_literal: bad cube");
/* NOTREACHED */
case 4:
fail("foreach_fanin: node changed during generation");
/* NOTREACHED */
case 5:
fail("foreach_fanout: node changed during generation");
/* NOTREACHED */
default:
fail("error code unused");
}
return 0;
}