par.py 272 KB
Newer Older
1 2 3 4 5 6 7 8
from pyabc import *
import pyabc_split
import redirect
import sys
import os
import time
import math
import main
.  
Baruch Sterin committed
9 10
import filecmp

11 12

global G_C,G_T,latches_before_abs,latches_before_pba,n_pos_before,x_factor,methods,last_winner
.  
Baruch Sterin committed
13
global last_cex,JV,JP, cex_list,max_bmc, last_cx, pord_on, trim_allowed, temp_dec, abs_ratio, ifbip
14 15
global if_no_bip, gabs, gla, sec_options,last_gasp_time, abs_ref_time, bmcs1, total_spec_refine_time
global last_gap
16 17 18 19 20 21 22 23 24 25 26 27 28

"""
The functions that are currently available from module _abc are:

int n_ands();
int n_pis();
int n_pos();
int n_latches();
int n_bmc_frames();
int prob_status(); 1 = unsat, 0 = sat, -1 = unsolved
int cex_get()
int cex_put()
int run_command(char* cmd);
.  
Baruch Sterin committed
29 30
int n_nodes();
int n_levels();
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

bool has_comb_model();
bool has_seq_model();
bool is_true_cex();
bool is_valid_cex();
  return 1 if the number of PIs in the current network and in the current counter-example are equal
int  n_cex_pis();
  return the number of PIs in the current counter-example
int  n_cex_regs();
  return the number of flops in the current counter-example
int  cex_po();
  returns the zero-based output PO number that is SAT by cex
int  cex_frame();
  return the zero-based frame number where the outputs is SAT
The last four APIs return -1, if the counter-example is not defined. 
""" 
#global variables
#________________________________________________
stackno_gabs = stackno_gore = stackno_greg= 0
STATUS_UNKNOWN = -1
STATUS_SAT = 0
STATUS_UNSAT = 1
53
RESULT = ('SAT', 'SAT', 'UNSAT', 'UNDECIDED', 'UNDECIDED', 'ERROR')
54 55 56 57 58 59 60 61 62 63 64 65 66
Sat = Sat_reg = 0
Sat_true = 1
Unsat = 2
Undecided = Undecided_reduction = 3
Undecided_no_reduction = 4
Error = 5
Restart = 6
xfi = x_factor = 1  #set this to higher for larger problems or if you want to try harder during abstraction
max_bmc = -1
last_time = 0
j_last = 0
seed = 113
init_simp = 1
.  
Baruch Sterin committed
67 68
temp_dec = True
ifpord1 = 1
69 70 71 72 73 74 75 76 77 78
K_backup = init_time = 0
last_verify_time = 20
last_cex = last_winner = 'None'
last_cx = 0
trim_allowed = True
pord_on = False
sec_sw = False
sec_options = ''
cex_list = []
TERM = 'USL'
79 80
last_gasp_time = 10000
last_gasp_time = 500
81
last_gasp_time = 900 #set to conform to hwmcc12
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
use_pms = True

#gabs = False #use the gate refinement method after vta
#abs_time = 100

####################################
#default abstraction methods
gabs = False #False = use gla refinement, True = use reg refinement.
gla = True #use gla_abs instead of vta_abs
##abs_time = 10000 #number of sec before initial abstraction terminates.
abs_time = 150
abs_time = 5000
abs_time = 500
abs_time = 100
abs_ref_time = 50 #number of sec. allowed for abstraction refinement.
total_spec_refine_time = 150
ifbip = 0 # sets the abtraction method to vta or gla, If = 1 then uses ,abs
if_no_bip = False #True sets it up so it can't use bip and reachx commands.
abs_ratio = .5 #this controls when abstraction is too big and gives up
#####################################

def abstr_a(t1=200,t2=200,absr=0):
    global abs_time, abs_ref_time, abs_ratio
    if not absr == 0:
        abs_ratio_old = abs_ratio
        abs_ratio = absr
    abs_time = t1
    abs_ref_time = t2
    abstracta(False)
    if not absr == 0:
        abs_ratio = abs_ratio_old

114
t_init = 2 #initial time for poor man's concurrency.
115 116 117 118 119 120 121 122 123 124

def set_global(s=''):
    global G_C,G_T,latches_before_abs,latches_before_pba,n_pos_before,x_factor,methods,last_winner
    global last_cex,JV,JP, cex_list,max_bmc, last_cx, pord_on, trim_allowed, temp_dec, abs_ratio, ifbip
    global if_no_bip, gabs, gla, sec_options,last_gasp_time,abs_ref_time, abs_time,use_pms
    exec(s)


methods = ['PDR', 'INTRP', 'BMC', 'SIM', 'REACHX',
           'PRE_SIMP', 'simple', 'PDRM', 'REACHM', 'BMC3','Min_Retime',
.  
Baruch Sterin committed
125 126
           'For_Retime','REACHP','REACHN','PDR_sd','prove_part_2',
           'prove_part_3','verify','sleep','PDRM_sd','prove_part_1',
127
           'run_parallel','INTRPb', 'INTRPm', 'REACHY', 'REACHYc','RareSim','simplify', 'speculate',
128
           'quick_sec', 'BMC_J', 'BMC2', 'extract -a', 'extract', 'PDRa', 'par_scorr', 'dsat', 'iprove']
129
#'0.PDR', '1.INTERPOLATION', '2.BMC', '3.SIMULATION',
130
#'4.REACHX', '5.PRE_SIMP', '6.simple', '7.PDRM', '8.REACHM', 9.BMC3'
131 132 133
# 10. Min_ret, 11. For_ret, 12. REACHP, 13. REACHN 14. PDRseed 15.prove_part_2,
#16.prove_part_3, 17.verify, 18.sleep, 19.PDRMm, 20.prove_part_1,
#21.run_parallel, 22.INTRP_bwd, 23. Interp_m 24. REACHY 25. REACHYc 26. Rarity Sim 27. simplify
.  
Baruch Sterin committed
134
#28. speculate, 29. quick_sec, 30 bmc3 -S, 31. BMC2 32. extract -a 33. extract 34. pdr_abstract
135
#35 par_scorr, 36. dsat, 37. iprove
136
win_list = [(0,.1),(1,.1),(2,.1),(3,.1),(4,.1),(5,-1),(6,-1),(7,.1)]
137 138 139 140 141 142 143
FUNCS = ["(pyabc_split.defer(pdr)(t))",
##         "(pyabc_split.defer(abc)('&get;,pdr -vt=%f'%t))",
         "(pyabc_split.defer(intrp)(t))",
##         "(pyabc_split.defer(abc)('&get;,imc -vt=%f'%(t)))",
##         "(pyabc_split.defer(abc)('&get;,imc-sofa -vt=%f'%(t)))",
         "(pyabc_split.defer(bmc)(t))",
##         "(pyabc_split.defer(abc)('&get;,bmc -vt=%f'%t))",
144
         "(pyabc_split.defer(simulate)(t))",
145 146
         "(pyabc_split.defer(reachx)(t))",
##         "(pyabc_split.defer(abc)('reachx -t %d'%t))",
147
         "(pyabc_split.defer(pre_simp)())",
.  
Baruch Sterin committed
148
##         "(pyabc_split.defer(super_prove)(2))",
149
         "(pyabc_split.defer(simple)(t))",
150 151
         "(pyabc_split.defer(pdrm)(t))",
         "(pyabc_split.defer(abc)('&get;&reachm -vcs -T %d'%t))",
152 153 154 155
         "(pyabc_split.defer(bmc3)(t))",
##         "(pyabc_split.defer(abc)('bmc3 -C 1000000 -T %f'%t))",
         "(pyabc_split.defer(abc)('dretime;&get;&lcorr;&dc2;&scorr;&put;dretime'))",
         "(pyabc_split.defer(abc)('dretime -m;&get;&lcorr;&dc2;&scorr;&put;dretime'))",
156 157
         "(pyabc_split.defer(abc)('&get;&reachp -vr -T %d'%t))",
         "(pyabc_split.defer(abc)('&get;&reachn -vr -T %d'%t))",
.  
Baruch Sterin committed
158 159 160 161
##         "(pyabc_split.defer(abc)('&get;,pdr -vt=%f -seed=521'%t))",
         "(pyabc_split.defer(pdrseed)(t))",
         "(pyabc_split.defer(prove_part_2)())",
         "(pyabc_split.defer(prove_part_3)())",
162 163 164
         "(pyabc_split.defer(verify)(JV,t))",
         "(pyabc_split.defer(sleep)(t))",
         "(pyabc_split.defer(pdrmm)(t))",
.  
Baruch Sterin committed
165
         "(pyabc_split.defer(prove_part_1)())",
166 167
         "(pyabc_split.defer(run_parallel)(JP,t,'TERM'))",
         "(pyabc_split.defer(abc)('&get;,imc -bwd -vt=%f'%t))",
.  
Baruch Sterin committed
168
##         "(pyabc_split.defer(abc)('int -C 1000000 -F 10000 -K 2 -T %f'%t))",
169 170 171 172
         "(pyabc_split.defer(intrpm)(t))",
##         "(pyabc_split.defer(abc)('int -C 1000000 -F 10000 -K 1 -T %f'%t))",
         "(pyabc_split.defer(reachy)(t))",
##         "(pyabc_split.defer(abc)('&get;&reachy -v -T %d'%t))",
173 174 175 176 177
         "(pyabc_split.defer(abc)('&get;&reachy -cv -T %d'%t))",
         "(pyabc_split.defer(simulate2)(t))",
         "(pyabc_split.defer(simplify)())",
         "(pyabc_split.defer(speculate)())",
         "(pyabc_split.defer(quick_sec)(t))",
.  
Baruch Sterin committed
178
         "(pyabc_split.defer(bmc_j)(t))",
179 180
##         "(pyabc_split.defer(abc)('bmc2 -C 1000000 -T %f'%t))",
         "(pyabc_split.defer(bmc2)(t))",
.  
Baruch Sterin committed
181 182 183
         "(pyabc_split.defer(extractax)('a'))",
         "(pyabc_split.defer(extractax)())",
         "(pyabc_split.defer(pdra)(t))",
184 185 186
         "(pyabc_split.defer(pscorr)(t))",
         "(pyabc_split.defer(dsat)(t))",
         "(pyabc_split.defer(iprove)(t))"
187 188 189
         ]
##         "(pyabc_split.defer(abc)('bmc3 -C 1000000 -T %f -S %d'%(t,int(1.5*max_bmc))))"
#note: interp given 1/2 the time.
190 191

## Similar engines below listed in the order of priority, high to low.
192
allreachs = [4,8,12,13,24,25]
193
allreachs = [24,4]
194
reachs = [24]
.  
Baruch Sterin committed
195
##allpdrs = [14,7,34,19,0]
196 197 198 199 200
allpdrs = [34,7,14,19,0]
allpdrs2 = [34,7,14,19,0]
pdrs = [34,7,14,0]
allbmcs = [9,30,2,31]
exbmcs = [2,9,31]
201
bmcs = [9,30]
202 203
bmcs1 = [9]
allintrps = [23,1,22]
204
bestintrps = [23]
205 206 207
##intrps = [23,1]
intrps = [23,1] #putting ,imc-sofa first for now to test
allsims = [26,3]
.  
Baruch Sterin committed
208
sims = [26] 
209 210
allslps = [18]
slps = [18]
.  
Baruch Sterin committed
211
imc1 = [1]
212 213
pre = [5]
combs = [36,37]
214

.  
Baruch Sterin committed
215
JVprove = [7,23,4,24]
216 217 218 219 220 221 222 223 224 225 226 227 228 229
JV = pdrs+intrps+bmcs+sims #sets what is run in parallel '17. verify' above
JP = JV + [27] # sets what is run in  '21. run_parallel' above 27 simplify should be last because it can't time out.
#_____________________________________________________________


# Function definitions:
# simple functions: ________________________________________________________________________
# set_globals, abc, q, x, has_any_model, is_sat, is_unsat, push, pop

# ALIASES

def initialize():
    global xfi, max_bmc, last_time,j_last, seed, init_simp, K_backup, last_verify_time
    global init_time, last_cex, last_winner, trim_allowed, t_init, sec_options, sec_sw
230 231 232
    global n_pos_before, n_pos_proved, last_cx, pord_on, temp_dec, abs_time, gabs, gla,m_trace
    global smp_trace,hist,init_initial_f_name, skip_spec, t_iter_start,last_simp, final_all, scorr_T_done
    global last_gap
233 234 235 236 237 238
    xfi = x_factor = 1  #set this to higher for larger problems or if you want to try harder during abstraction
    max_bmc = -1
    last_time = 0
    j_last = 0
    seed = 113
    init_simp = 1
.  
Baruch Sterin committed
239
    temp_dec = True
240 241 242 243 244 245 246 247 248
    K_backup = init_time = 0
    last_verify_time = 20
    last_cex = last_winner = 'None'
    last_cx = 0
    trim_allowed = True
    pord_on = False
    t_init = 2 #this will start sweep time in find_cex_par to 2*t_init here
    sec_sw = False
    sec_options = ''
249
    smp_trace = m_trace = []
250 251 252
    cex_list = []
    n_pos_before = n_pos()
    n_pos_proved = 0
253
    abs_time = 150
254
    abs_ref_time = 50 #number of sec. allowed for abstraction refinement.
255 256
    total_spec_refine_time = 150
    abs_ratio = .5
257 258 259 260 261 262 263 264 265 266 267 268 269 270
    hist = []
    skip_spec = False
    t_iter_start = 0
    inf = 10000000
    last_simp = [inf,inf,inf,inf]
    final_all = 1
    scorr_T_done = 0
    last_gap = 0
##    abs_time = 100
##    gabs = False
##    abs_time = 500
##    gabs = True

    
271

.  
Baruch Sterin committed
272 273 274 275
def set_abs_method():
    """ controls the way we do abstraction, 0 = no bip, 1 = old way, 2 use new bip and -dwr
    see absab()
    """
276 277 278
    global ifbip, abs_time,gabs,gla,if_no_bip
    print 'current values ifbip = %d, abs_time = %d'%(ifbip,abs_time)
    print 'Set method of abstraction: \n0 = vta for 500 and gla refin., \n1 = old way, \n2 = ,abs and -dwr, \n3 = vta for 100 followed by gla refine.,\n4 = vta for 500 then gla refine. but no bip methods gla refine., \n5 = gla and gla refine.'
.  
Baruch Sterin committed
279 280
    s = raw_input()
    s = remove_spaces(s)
281
    if s == '1': #use the old way with ,abs but no dwr
.  
Baruch Sterin committed
282
        ifbip = 1 #old way
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
        abs_time = 100
        if_no_bip = False
        gabs = True
        gla = False
    elif s == '0':#use vta and gla refinement
        ifbip = 0 
        abs_time = 500
        if_no_bip = False
        gabs = False
        gla = False
    elif s == '2':  #use ,abc -dwr
        ifbip = 2 
        abs_time = 100
        if_no_bip = False
        gabs = True #use register refinement
        gla = False
    elif s == '3': #use vta and gla refinement
        ifbip = 0
        abs_time = 100
        if_no_bip = False
        gabs = False
        gla = False
    elif s == '4': #use vta, gla refine. and no bip
        ifbip = 0
        abs_time = 100
        if_no_bip = True
        gabs = True
        gla = False
    elif s == '5': #use gla and gla_refinement
        ifbip = 0
        abs_time = 100
        if_no_bip = False
        gabs = False
        gla = True
    #should make any of the methods able to us no bip
    print 'ifbip = %d, abs_time = %d, gabs = %d, if_no_bip = %d, gla = %d'%(ifbip,abs_time,gabs,if_no_bip,gla)
.  
Baruch Sterin committed
319
    
320 321 322
def ps():
    print_circuit_stats()

323 324 325 326 327 328
def iprove(t=100):
    abc('iprove')

def dsat(t=100):
    abc('dsat')

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
def n_real_inputs():
    """This gives the number of 'real' inputs. This is determined by trimming away inputs that
    have no connection to the logic. This is done by the ABC alias 'trm', which changes the current
    circuit. In some applications we do not want to change the circuit, but just to know how may inputs
    would go away if we did this. So the current circuit is saved and then restored afterwards."""
##    abc('w %s_savetempreal.aig; logic; trim; st ;addpi'%f_name)
    abc('w %s_savetempreal.aig'%f_name)
    with redirect.redirect( redirect.null_file, sys.stdout ):
##        with redirect.redirect( redirect.null_file, sys.stderr ):
        reparam()
    n = n_pis()
    abc('r %s_savetempreal.aig'%f_name)
    return n

def timer(t):
    btime = time.clock()
    time.sleep(t)
    print t
    return time.clock() - btime

def sleep(t):
350
##    print 'Sleep time = %d'%t
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    time.sleep(t)
    return Undecided
        
def abc(cmd):
    abc_redirect_all(cmd)

def abc_redirect( cmd, dst = redirect.null_file, src = sys.stdout ):
    """This is our main way of calling an ABC function. Redirect, means that we suppress any output from ABC"""
    with redirect.redirect( dst, src ):
        return run_command( cmd )

def abc_redirect_all( cmd ):
    """This is our main way of calling an ABC function. Redirect, means that we suppress any output from ABC, including error printouts"""
    with redirect.redirect( redirect.null_file, sys.stdout ):
        with redirect.redirect( redirect.null_file, sys.stderr ):
            return run_command( cmd )

368 369 370
##def convert(t):
##    t = int(t*100)
##    return str(float(t)/100)
371 372 373

def set_engines(N=0):
    """
.  
Baruch Sterin committed
374
    Called only when read_file is called.
375
    Sets the MC engines that are used in verification according to
.  
Baruch Sterin committed
376
    if there are 4 or 8 processors. if if_no_bip = 1, we will not use any bip and reachx engines
377
    """
378 379
    global reachs,pdrs,sims,intrps,bmcs,n_proc,abs_ratio,ifbip,bmcs1, if_no_bip, allpdrs,allbmcs
    bmcs1 = [9] #BMC3
380 381
    #for HWMCC we want to set N = 8
    N = 8
382
    if N == 0:
383 384 385 386 387 388
        N = n_proc = 1+os.sysconf(os.sysconf_names["SC_NPROCESSORS_ONLN"])
##        N = n_proc = 8 ### simulate 4 processors for HWMCC - turn this off a hwmcc.
    else:
        n_proc = N
##    print 'n_proc = %d'%n_proc
    if N <= 1:
389 390 391 392 393 394 395
        reachs = [24]
        pdrs = [7]
##        bmcs = [30]
        bmcs = [9]
        intrps = []
        sims = []
        slps = [18]
396
    elif N <= 2:
397 398 399 400 401 402
        reachs = [24]
        pdrs = [7]
        bmcs = [30]
        intrps = []
        sims = []
        slps = [18]
403 404 405
    elif N <= 4:
        reachs = [24] #reachy
        pdrs = [7,34] #prdm pdr_abstract
.  
Baruch Sterin committed
406
        if if_no_bip:
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
            allpdrs = pdrs = [7,19] #pdrm pdrmm
        bmcs = [9,30] #bmc3 bmc3 -S
        intrps = [23] #unterp_m
        sims = [26] #Rarity_sim
        slps = [18] #sleep
# 0.PDR, 1.INTERPOLATION, 2.BMC, 3.SIMULATION,
# 4.REACHX, 5.PRE_SIMP, 6.simple, 7.PDRM, 8.REACHM, 9.BMC3
# 10.Min_ret, 11.For_ret, 12.REACHP, 13.REACHN 14.PDRseed 15.prove_part_2,
# 16.prove_part_3, 17.verify, 18.sleep, 19.PDRMm, 20.prove_part_1,
# 21.run_parallel, 22.INTRP_bwd, 23.Interp_m 24.REACHY 25.REACHYc 26.Rarity Sim 27.simplify
# 28.speculate, 29.quick_sec, 30.bmc3 -S, 31.BMC2 32.extract -a 33.extract 34.pdr_abstract
# 35.par_scorr, 36.dsat, 37.iprove

# BIPS = 0.PDR, 1.INTERPOLATION, 2.BMC, 14.PDRseed, 22.INTRP_bwd, 34.pdr_abstract
#       also  reparam which uses ,reparam 

    elif N <= 8: #used for HWMCC
        reachs = [24] #REACHY
        allpdrs = pdrs = [7,34,14] #PDRM pdr_abstract PDR_seed
        intrps = [23,1] #Interp_m
        allbmcs = bmcs = [9,30,31] #BMC3 bmc3 -S 
.  
Baruch Sterin committed
428
        if if_no_bip:
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
            allpdrs = pdrs = [7,19] #PDRM PDRMm
            intrps = allintrps = [23] #Interp_m
            bmcs = allbmcs = [2]
        sims = [26] #Rarity_Sim
        slps = [18] #sleep
    else:
        reachs = [24,4] #REACHY REACHX
        pdrs = [7,34,14,19,0] #PDRM pdr_abstract PDR_seed PDRMm PDR
        intrps = [23,1] #Interp_m INTERPOLATION
        bmcs = allbmcs
        if if_no_bip:
            allpdrs = pdrs = [7,19] #PDRM PDRMm
            intrps = allintrps = [23] #Interp_m
            reachs = [24] #REACHY
            bmcs = [9,30] #BMC3 bmc3 -S 
        sims = [26] #Rarity_Sim
        slps = [18] #sleep
        
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
def set_globals():
    """This sets global parameters that are used to limit the resources used by all the operations
    bmc, interpolation BDDs, abstract etc. There is a global factor 'x_factor' that can
    control all of the various resource limiting parameters"""
    global G_C,G_T,x_factor
    nl=n_latches()
    na=n_ands()
    np = n_pis()
    #G_C = min(500000,(3*na+500*(nl+np)))
    G_C = x_factor * min(100000,(3*na+500*(nl+np)))
    #G_T = min(250,G_C/2000)
    G_T = x_factor * min(75,G_C/2000)
    G_T = max(1,G_T)
    #print('Global values: BMC conflicts = %d, Max time = %d sec.'%(G_C,G_T))
    
def a():
    """this puts the system into direct abc input mode"""
    print "Entering ABC direct-input mode. Type q to quit ABC-mode"
    n = 0
    while True:
        print '     abc %d> '%n,
        n = n+1
        s = raw_input()
        if s == "q":
            break
        run_command(s)

def remove_spaces(s):
    y = ''
    for t in s:
        if not t == ' ':
            y = y + t
    return y

.  
Baruch Sterin committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
def seq_name(f):
    names = []
    f = f + '_'
    names = []
    while len(f)>0:
        j = f.find('_')
        if j == -1:
            break
        names = names + [f[:j]]
##        print names
        f = f[j+1:]
##        print f
    return names

def revert(f,n):
    l = seq_name(f)
    for j in range(n):
        if len(l)>0:
            l.pop()
    name = construct(l)
    return name

503 504 505 506 507
def n_eff_pos():
    N=n_pos()
    l=len(list_0_pos())
    return N-l

.  
Baruch Sterin committed
508 509 510 511 512 513 514 515 516 517
def construct(l):
    ll = l
    name = ''
    while len(l)>0:
        name = '_'+ll.pop()+name
    return name[1:]

def process_sat():
    l = seq_name(f_name)

518 519 520 521 522 523 524 525 526 527 528
def add_trace(s):
    global m_trace
    m_trace = m_trace + [s] 

def read_file_quiet_i(fname=None):
    """ this preserves t_inter_start and is called internally by some functons."""
    global t_iter_start
    ts = t_iter_start
    read_file_quiet(fname)
    t_iter_start = ts

529 530 531 532 533 534 535
def read_file_quiet(fname=None):
    """This is the main program used for reading in a new circuit. The global file name is stored (f_name)
    Sometimes we want to know the initial starting name. The file name can have the .aig extension left off
    and it will assume that the .aig extension is implied. This should not be used for .blif files.
    Any time we want to process a new circuit, we should use this since otherwise we would not have the
    correct f_name."""
    global max_bmc,  f_name, d_name, initial_f_name, x_factor, init_initial_f_name, win_list,seed, sec_options
536
    global win_list, init_simp, po_map, aigs, hist, init_initial_f_name
.  
Baruch Sterin committed
537 538
    abc('fraig_restore') #clear out any residual fraig_store
    set_engines() #temporary
539 540 541 542 543 544 545 546 547 548 549
    init_simp = 1
    win_list = [(0,.1),(1,.1),(2,.1),(3,.1),(4,.1),(5,-1),(6,-1),(7,.1)] #initialize winning engine list
    po_map = range(n_pos())
    initialize()
##    x_factor = 1
##    seed = 223
##    max_bmc = -1
    if fname is None:
        print 'Type in the name of the aig file to be read in'
        s = raw_input()
        s = remove_spaces(s)
.  
Baruch Sterin committed
550
##        print s
551 552 553 554
    else:
        s = fname
    if s[-4:] == '.aig':
        f_name = s[:-4]
.  
Baruch Sterin committed
555 556
    elif s[-5:] == '.blif':
        f_name = s[:-5]
557 558 559 560
    else:
        f_name = s
        s = s+'.aig'
##    run_command(s)
.  
Baruch Sterin committed
561 562
##    print s
    if s[-4:] == '.aig':
563 564 565
##        run_command('&r %s;&put'%s) #warning: changes names to generic ones.
        run_command('r %s'%s)
        run_command('zero')
.  
Baruch Sterin committed
566 567 568 569
    else: #this is a blif file
        run_command('r %s'%s)
        abc('st;&get;&put') #changes names to generic ones for doing cec later.
        run_command('zero;w %s.aig'%f_name)
570
    set_globals()
571
    hist = []
.  
Baruch Sterin committed
572
    init_initial_f_name = initial_f_name = f_name
573 574 575 576
    run_command('fold') #only does something if some of the outputs are constraints.
    aigs_pp('push','initial')
    #aigs = create push/pop history of aigs
    #aigs.push() put the initial aig on the aig list.
.  
Baruch Sterin committed
577
    print 'Initial f_name = %s'%f_name
578
    abc('addpi') #only does something if there are no PIs
.  
Baruch Sterin committed
579 580 581 582
    #check_pos() #this removes constant outputs with a warning -
    #needed when using iso. Need another fix for using iso.
    ps()
    return
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

def aigs_pp(op='push', typ='reparam'):
    global hist,init_initial_f_name
##    print hist
    if op == 'push':
        hist.append(typ)
        abc('w %s_aigs_%d.aig'%(init_initial_f_name,len(hist)))
    if op == 'pop':
        abc('cexsave') #protect current cex from a read
        abc('r %s_aigs_%d.aig'%(init_initial_f_name,len(hist)))
        abc('cexload')
        typ = hist.pop()
##    print hist
    return typ

def scl():
    abc('&get;&scl;&put')
    ps()

def cex_trim_g(F_init=0,tail=0,m=''):
    abc('w %s_cex.aig'%f_name)
    N=cex_frame()
    G = N - tail
    F = F_init
    abc('cexsave')
    while True:
        print 'F = %d, G = %d'%(F,G)
        abc('r %s_cex.aig'%f_name)
        abc('cexload')
        if m == '':
            abc('cexcut -F %d -G %d'%(F,G))
        else:
            abc('cexcut -m -F %d -G %d'%(F,G))
##        abc('drw')
##        ps()
        res = run_parallel(slps+bmcs,20)
##        run_command('bmc2 -v -T 20')
##        if is_sat(): #got a shortening of cex
        if not res == Undecided:
            Nb = cex_frame() #size of shortcut
            abc('cexmerge -F %d -G %d'%(F,G))
            abc('r %s_cex.aig'%f_name)
            abc('cexload')
            abc('testcex -a')
            if cex_po() <0:
                return 'ERROR2'
            Nt=cex_frame() #current cex length
            print 'Cex length reduced from %d to %d'%(N,Nt)
            return
        F = F + (G-F)/2
##        G = N - i*delta
        if F >= G:
           return

def cex_trim(factor=1):
    t_begin = time.time()
    abc('w %s_cex.aig'%f_name)
    N=cex_frame()
    inc = min(N/10,100)
    F = 0
    G = inc
    abc('cexsave')
    abc('cexcut -n -F %d -G %d'%(F,G))
    run_command('bmc2 -v -F %d -T 5'%(.9*inc))
    inc = max(int(factor*n_bmc_frames()),2)
    F = N - inc
    G = N
    print 'inc = %d'%inc
    while True:
        abc('r %s_cex.aig'%f_name)
        abc('cexload')
        abc('cexcut -n -F %d -G %d'%(F,G))
##        abc('drw')
##        ps()
##        run_command('bmc2 -v -F %d -T 20'%(.9*inc))
        run_parallel(slps+bmcs,10)
        if not is_sat():
            abc('cex_load') #leave current cex in buffer
            Nb = inc
        else:
            Nb = cex_frame() #size of shortcut
            abc('cexmerge -F %d -G %d'%(F,G))
        abc('r %s_cex.aig'%f_name)
        abc('cexload')
        abc('testcex -a')
        if cex_po() <0:
            return 'ERROR2'
##        abc('cexload')
        Nt=cex_frame() #current cex length
        print 'Cex length = %d'%Nt
        G=F
        F = max(0,F - inc)
        print 'F = %d, G = %d'%(F,G)
        if G <= 2:
            abc('cexload')
            print 'Time: %0.2f'%(time.time() - t_begin) 
            return
680
    
681
        
682 683 684 685 686 687 688 689 690 691 692
def read_file():
    global win_list, init_simp, po_map
    read_file_quiet()
##    ps()
##    init_simp = 1
##    win_list = [(0,.1),(1,.1),(2,.1),(3,.1),(4,.1),(5,-1),(6,-1),(7,.1)] #initialize winning engine list
##    po_map = range(n_pos())

def rf():
##    set_engines(4) #temporary
    read_file()
693
    abc('zero')
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

def write_file(s):
    """this is the main method for writing the current circuit to an AIG file on disk.
    It manages the name of the file, by giving an extension (s). The file name 'f_name'
    keeps increasing as more extensions are written. A typical sequence is
    name, name_smp, name_smp_abs, name_smp_abs_spec, name_smp_abs_spec_final"""
    global f_name
    """Writes out the current file as an aig file using f_name appended with argument"""
    f_name = '%s_%s'%(f_name,s)
    ss = '%s.aig'%(f_name)
    print 'WRITING %s: '%ss,
    ps()
    abc('w '+ss)

def bmc_depth():
    """ Finds the number of BMC frames that the latest operation has used. The operation could be BMC, reachability
    interpolation, abstract, speculate. max_bmc is continually increased. It reflects the maximum depth of any version of the circuit
    including g ones, for which it is known that there is not cex out to that depth."""
    global max_bmc
    c = cex_frame()
    if c > 0:
        b = c-1
    else:
        b = n_bmc_frames()
718 719 720
    if b > max_bmc:
        max_bmc = b
        report_bmc_depth(max_bmc)
721 722
    return max_bmc

.  
Baruch Sterin committed
723 724 725 726
def null_status():
    """ resets the status to the default values but note that the &space is changed"""
    abc('&get;&put')

727 728 729 730
def set_max_bmc(b):
    """ Keeps increasing max_bmc which is the maximum number of time frames for
    which the current circuit is known to be UNSAT for"""
    global max_bmc
731 732 733 734 735 736
    if b > max_bmc:
        max_bmc = b
        report_bmc_depth(max_bmc)

def report_bmc_depth(m):
    print 'u%d'%m
737 738 739 740 741 742 743 744

def print_circuit_stats():
    """Stardard way of outputting statistice about the current circuit"""
    global max_bmc
    i = n_pis()
    o = n_pos()
    l = n_latches()
    a = n_ands()
.  
Baruch Sterin committed
745 746 747 748
    s='ANDs'
    if a == -1:
        a = n_nodes()
        s = 'Nodes'
749 750
##    b = max(max_bmc,bmc_depth()) # don't want to do this because bmc_depth can change max_bmc
    b = max_bmc
751 752 753
    c = cex_frame()
    if b>= 0:
        if c>=0:
.  
Baruch Sterin committed
754
            print 'PIs=%d,POs=%d,FF=%d,%s=%d,max depth=%d,CEX depth=%d'%(i,o,l,s,a,b,c)
755
        elif is_unsat():
.  
Baruch Sterin committed
756
            print 'PIs=%d,POs=%d,FF=%d,%s=%d,max depth = infinity'%(i,o,l,s,a)
757
        else:
.  
Baruch Sterin committed
758
            print 'PIs=%d,POs=%d,FF=%d,%s=%d,max depth=%d'%(i,o,l,s,a,b)            
759 760
    else:
        if c>=0:
.  
Baruch Sterin committed
761
            print 'PIs=%d,POs=%d,FF=%d,%s=%d,CEX depth=%d'%(i,o,l,s,a,c)
762
        else:
.  
Baruch Sterin committed
763
            print 'PIs=%d,POs=%d,FF=%d,%s=%d'%(i,o,l,s,a)
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

def is_unsat():
    if prob_status() == 1:
        return True
    else:
        return False

def is_sat():
    if prob_status() == 0:
        return True
    else:
        return False

def wc(file):
    """writes <file> so that costraints are preserved explicitly"""
    abc('&get;&w %s'%file)

def rc(file):
    """reads <file> so that if constraints are explicit, it will preserve them"""
783
    abc('&r -s %s;&put'%file)                         
784 785 786 787 788 789 790

#more complex functions: ________________________________________________________
#, abstract, pba, speculate, final_verify, dprove3

def timer(s):
    btime = time.clock()
    abc(s)
791
    print 'time = %0.2f'%(time.clock() - btime)
792 793 794

def med_simp():
    x = time.time()
795
    abc("&get;&scl;&dc2;&lcorr;&dc2;&scorr;&fraig;&dc2;&put;dretime")
796 797
    #abc("dc2rs")
    ps()
798
    print 'time = %0.2f'%(time.time() - x)
799

800
def simplify_old(M=0):
801 802 803 804 805 806 807 808
    """Our standard simplification of logic routine. What it does depende on the problem size.
    For large problems, we use the &methods which use a simple circuit based SAT solver. Also problem
    size dictates the level of k-step induction done in 'scorr' The stongest simplification is done if
    n_ands < 20000. Then it used the clause based solver and k-step induction where |k| depends
    on the problem size """
    set_globals()
    abc('&get;&scl;&lcorr;&put')
    p_40 = False
.  
Baruch Sterin committed
809
    n =n_ands()
810 811 812 813
    if n >= 70000 and not '_smp' in f_name:
##        abc('&get;&scorr -C 0;&put')
        scorr_T(30)
        ps()
.  
Baruch Sterin committed
814 815 816
    n =n_ands()
    if n >= 100000:
        abc('&get;&scorr -k;&put')
817 818 819
        ps()
    if (70000 < n and n < 150000):
##        print '1'
820
        p_40 = True
821 822 823
        abc("&get;&dc2;&put;dretime;&get;&lcorr;&dc2;&put;dretime;&get;&scorr;&fraig;&dc2;&put;dretime")
##        print 2'
        ps()
824
        n = n_ands()
.  
Baruch Sterin committed
825 826
##        if n<60000:
        if n < 80000:
827
            abc("&get;&scorr -F 2;&put;dc2rs")
828
            ps()
829 830
        else: # n between 60K and 100K
            abc("dc2rs")
831
            ps()
832
    n = n_ands()
.  
Baruch Sterin committed
833 834
##    if (30000 < n  and n <= 40000):
    if (60000 < n  and n <= 70000):
835
        if not p_40:
836
            abc("&get;&dc2;&put;dretime;&get;&lcorr;&dc2;&put;dretime;&get;&scorr;&fraig;&dc2;&put;dretime")
837
            abc("&get;&scorr -F 2;&put;dc2rs")
838
            ps()
839
        else:
840 841
            abc("dc2rs")
            ps()
842
    n = n_ands()
.  
Baruch Sterin committed
843 844
##    if n <= 60000:
    if n <= 70000:
845 846
        abc('scl -m;drw;dretime;lcorr;drw;dretime')
        ps()
847
        nn = max(1,n)
.  
Baruch Sterin committed
848
        m = int(min( 70000/nn, 16))
849 850
        if M > 0:
            m = M
851 852 853 854 855 856 857 858
        if m >= 1:
            j = 1
            while j <= m:
                set_size()
                if j<8:
                    abc('dc2')
                else:
                    abc('dc2rs')
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
                abc('scorr -C 1000 -F %d'%j) #was 5000 temporarily 1000
                if check_size():
                    break
                j = 2*j
                print 'ANDs=%d,'%n_ands(),
                if n_ands() >= .98 * nands:
                     break
                continue
            if not check_size():
                print '\n'
    return get_status()

def simplify(M=0,N=0):
    """Our standard simplification of logic routine. What it does depende on the problem size.
    For large problems, we use the &methods which use a simple circuit based SAT solver. Also problem
    size dictates the level of k-step induction done in 'scorr' The stongest simplification is done if
    n_ands < 20000. Then it used the clause based solver and k-step induction where |k| depends
    on the problem size
    Does not change #PIs.
    """
    global smp_trace
    set_globals()
    smp_trace = smp_trace + ['&scl;&lcorr']
    abc('&get;&scl;&lcorr;&put')
    p_40 = False
    n =n_ands()
    if N == 0 and n >= 70000 and not '_smp' in f_name:
##        abc('&get;&scorr -C 0;&put')
##        print 'Trying scorr_T'
        scorr_T(30)
        ps()
    n =n_ands()
    if n >= 100000:
        smp_trace = smp_trace + ['&scorr']
        abc('&get;&scorr -k;&put')
        ps()
    if (70000 < n and n < 150000):
        p_40 = True
        smp_trace = smp_trace + ['&dc2;dretime;&lcorr;&dc2;dretime;&scorr;&fraig;&dc2;dretime']
        abc("&get;&dc2;&put;dretime;&get;&lcorr;&dc2;&put;dretime;&get;&scorr;&fraig;&dc2;&put;dretime")
        ps()
    n = n_ands()
##    if (30000 < n  and n <= 40000):
    if (60000 < n  and n <= 70000):
        if not p_40:
            smp_trace = smp_trace + ['&dc2;dretime;&lcorr;&dc2;dretime;&scorr;&fraig;&dc2;dretime']
            abc("&get;&dc2;&put;dretime;&get;&lcorr;&dc2;&put;dretime;&get;&scorr;&fraig;&dc2;&put;dretime")
            smp_trace = smp_trace + ['&scorr -F 2;dc2rs']
            abc("&get;&scorr -F 2;&put;dc2rs")
            ps()
        else:
            abc("dc2rs")
            smp_trace = smp_trace + ['dc2rs']
            ps()
    n = n_ands()
##    if n <= 60000:
    if n <= 70000:
        smp_trace = smp_trace + ['scl -m;drw;dretime;lcorr;drw;dretime']
        abc('scl -m;drw;dretime;lcorr;drw;dretime')
        ps()
        nn = max(1,n)
        m = int(min( 70000/nn, 16))
        if M > 0:
            m = M
        if N == 0 and m >= 1:
            j = 1
            while j <= m:
                set_size()
                if j<8:
                    abc('dc2')
                else:
                    abc('dc2rs')
                smp_trace = smp_trace + ['scorr -F %d'%j]
                abc('scorr -C 1000 -F %d'%j) #was 5000 temporarily 1000
933 934 935
                if check_size():
                    break
                j = 2*j
.  
Baruch Sterin committed
936 937 938
                print 'ANDs=%d,'%n_ands(),
                if n_ands() >= .98 * nands:
                     break
939
                continue
.  
Baruch Sterin committed
940 941
            if not check_size():
                print '\n'
942 943
    return get_status()
            
944
def simulate2(t=900):
945 946 947 948 949 950
    """Does rarity simulation. Simulation is restricted by the amount
    of memory it might use. At first wide but shallow simulation is done, followed by
    successively more narrow but deeper simulation. 
    seed is globally initiallized to 113 when a new design is read in"""
    global x_factor, f_name, tme, seed
    btime = time.clock()
951
    tt = time.time()
952 953
    diff = 0
    while True:
954 955
        f = 20
        w = 64
.  
Baruch Sterin committed
956
        b = 16
957
        r = 700
958
        for k in range(9): #this controls how deep we go
.  
Baruch Sterin committed
959
            f = min(f*2, 3500)
960
            w = max(((w+1)/2)-1,1)
.  
Baruch Sterin committed
961
            abc('sim3 -F %d -W %d -N %d -R %d -B %d'%(f,w,seed,r,b))
962 963
            seed = seed+23
            if is_sat():
964
##                print 'RareSim time = %0.2f at frame %d'%((time.time() - tt),cex_frame())
965 966 967 968
                return 'SAT'
            if ((time.clock()-btime) > t):
                return 'UNDECIDED'

969
def simulate(t=900):
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
    abc('&get')
    result = eq_simulate(t)
    return result

def eq_simulate(t):
    """Simulation is restricted by the amount
    of memory it might use. At first wide but shallow simulation is done, followed by
    successively more narrow but deeper simulation. The aig to be simulated must be in the & space
    If there are equivalences, it will refine them. Otherwise it is a normal similation
    seed is globally initiallized to 113 when a new design is read in"""
    global x_factor, f_name, tme, seed
    btime = time.clock()
    diff = 0
    while True:
        f = 5
        w = 255
        for k in range(9):
            f = min(f *2, 3500)
            r = f/20
            w = max(((w+1)/2)-1,1)
##            abc('&sim3 -R %d -W %d -N %d'%(r,w,seed))
991
            abc('&sim -F %d -W %d -N %d'%(f,w,seed))
992 993 994 995 996 997 998
            seed = seed+23
            if is_sat():
                return 'SAT'
            if ((time.clock()-btime) > t):
                return 'UNDECIDED'

def generate_abs(n):
999 1000
    """generates an abstracted  model (gabs) from the greg file or gla. The gabs file is automatically
    generated in the & space by &abs_derive or gla_derive. We store it away using the f_name of the problem
1001 1002 1003
    being solved at the moment. The f_name keeps changing with an extension given by the latest
    operation done - e.g. smp, abs, spec, final, group. """
    global f_name
1004 1005 1006 1007 1008 1009 1010 1011
    #we have a cex and we use this generate a new gabs (gla) file
    if gabs: #use the register refinement method
        abc('&r -s %s_greg.aig; &abs_derive; &put; w %s_gabs.aig'%(f_name,f_name)) # do we still need the gabs file
    else: #use the gate refinement method
        run_command('&r -s %s_gla.aig; &gla_derive; &put'%f_name)
        if n_ands() < 2000:
            run_command('scl;scorr;dretime')
        run_command('w %s_gabs.aig'%f_name)
1012 1013 1014 1015 1016 1017
    if n == 1:
        #print 'New abstraction: ',
        ps()
    return   

def refine_with_cex():
1018 1019 1020
    """Refines the greg or gla file (which contains the original problem with the set of FF's or gates
    that have been abstracted).
    This uses the current cex to modify the greg or gla file to reflect which regs(gates) are in the
1021 1022
    new current abstraction"""
    global f_name
1023 1024 1025 1026 1027 1028
    if gabs:
        abc('&r -s %s_greg.aig;&w %s_greg_before.aig'%(f_name,f_name))
        run_command('&abs_refine -s; &w %s_greg.aig'%f_name)
    else:
        run_command('&r -s %s_gla.aig;&w %s_gla_before.aig'%(f_name,f_name))
        run_command('&gla_refine; &w %s_gla.aig'%f_name)
1029 1030
    return

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
def refine_with_cex_suffix():
    """Refines the greg or gla file (which contains the original problem with the set of FF's or gates
    that have been abstracted).
    This uses the current cex to modify the greg or gla file to reflect which regs(gates) are in the
    new current abstraction"""
    global f_name
    return Undecided_no_reduction
    t = 5
    cexf = cex_frame()
    suf = .9*cexf
    run_command('write_status %s_temp.status'%f_name)
    ub = int(cexf -min(10, .02*cexf))
    lb = int(min(10,.02*cexf))
    suf = int(.5*(ub-lb))
    if_last = 0
    N = 0
.  
Baruch Sterin committed
1047
    while True:
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
        N = N+1
        tt = time.time()
        run_command('read_status %s_temp.status'%f_name)
        print 'Refining using suffix %d with time = %d'%(suf,t)
        run_command('&r -s %s_gla.aig;&w %s_gla_before.aig'%(f_name,f_name))
        F = create_funcs([18],t) #create a timer function with timeout = t
        F = F + [eval('(pyabc_split.defer(abc)("&gla_refine -F %d; &w %s_gla.aig"))'%(suf,f_name))]
        for i,res in pyabc_split.abc_split_all(F): #need to do a binary search
            if i == 0:  #timeout
                lb = int(suf)
                dec = 'increasing'
                break
            elif same_abs(): #suffix did not refine - need to decrease suf
                ub = int(suf)
                dec = 'decreasing'
                break
            else: #refinement happened
                print 'refinement happened.'
                return
        print 'ub = %.2f, lb = %.2f, suf = %.2f'%(ub,lb,suf)
        suf = int(lb+.5*(ub-lb))
        if (ub-lb)< (max(1.1,min(10,.02*cexf))) or if_last or N >=4: # not refining in time allowed, give up
            print '(ub-lb) = %0.2f'%(ub-lb)
            print 'could not refine in resources allowed'
            return Undecided_no_reduction
    
def same_abs():
    run_command('r %s_gabs.aig'%f_name)
    set_size()
##    ps()
    run_command('&r -s %s_gla.aig; &gla_derive; &put'%f_name)
    if n_ands() < 2000:
        run_command('scl;scorr;dretime')
##    ps()
    return check_size()
.  
Baruch Sterin committed
1083 1084

def abstraction_refinement(latches_before,NBF,ratio=.75):
1085
    """Subroutine of 'abstract' which does the refinement of the abstracted model,
1086
    using counterexamples found by BMC, BDD reachability, etc"""
1087
    global x_factor, f_name, last_verify_time, x, win_list, last_winner, last_cex, t_init, j_last, sweep_time
1088
    global cex_list, last_cx, abs_ref_time
1089
    sweep_time = 2
1090
    T1 = time.time()
1091 1092 1093 1094 1095
    if NBF == -1:
        F = 2000
    else:
        F = 2*NBF
    print '\nIterating abstraction refinement'
1096
    add_trace('abstraction refinement')
1097
    J = slps+intrps+pdrs+bmcs+sims
1098
    J=modify_methods(J)
1099 1100
    print sublist(methods,J)
    last_verify_time = t = x_factor*max(50,max(1,2.5*G_T))
1101
##    t = 1000 #temporary
.  
Baruch Sterin committed
1102
    t = abs_time
1103 1104 1105 1106
    initial_verify_time = last_verify_time = t
    reg_verify = True
    print 'Verify time set to %d'%last_verify_time
    while True: #cex based refinement
1107
        generate_abs(1) #generate new gabs file from refined greg or gla file
1108 1109
        set_globals()
        latches_after = n_latches()
.  
Baruch Sterin committed
1110
        if small_abs(ratio):
1111 1112 1113 1114
            print 'abstraction too large'
            return Undecided_no_reduction
        if (time.time() - T1)> abs_ref_time:
            print 'abstraction time ran out'
1115 1116 1117 1118 1119
            break
        t = last_verify_time
        yy = time.time()
        abc('w %s_beforerpm.aig'%f_name)
        rep_change = reparam() #new - must do reconcile after to make cex compatible
1120 1121
##        if rep_change:
##            add_trace('reparam')
1122
        abc('w %s_afterrpm.aig'%f_name)
.  
Baruch Sterin committed
1123 1124
##        if reg_verify:
        status = verify(J,t)
1125 1126
        print 'status = ',
        print status
.  
Baruch Sterin committed
1127 1128
##        else:
##            status = pord_1_2(t)
1129
###############
1130
        if status[0] == Sat_true:
1131
            print 'Found true cex'
1132 1133
            reconcile_a(rep_change)
##            add_trace('SAT by %s'%status[1])
1134
            return Sat_true
1135 1136 1137 1138 1139
        if status[0] == Unsat:
##            add_trace('UNSAT by %s'%status[1])      
            return Unsat
        if status[0] == Sat:
##            add_trace('SAT by %s'%status[1])
.  
Baruch Sterin committed
1140
            abc('write_status %s_after.status'%f_name)
1141
            reconcile_a(rep_change) # makes the cex compatible with original before reparam and puts original in work space
1142
            abc('write_status %s_before.status'%f_name)
1143 1144 1145 1146 1147 1148 1149 1150 1151
            if gabs: #global variable
                refine_with_cex()
            else:
                result = refine_with_cex_suffix()
                if result == Sat:
                    return Sat
##                result = refine_with_cex()
                if result == Undecided_no_reduction:
                    return result
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
            if is_sat(): # if cex can't refine, status is set to Sat_true
                print 'Found true cex in output %d'%cex_po()
                return Sat_true
            else:
                continue
        else:
            break
    print '**** Latches reduced from %d to %d'%(latches_before, n_latches())
    return Undecided_reduction

.  
Baruch Sterin committed
1162 1163
def small_abs(ratio=.75):
    """ tests is the abstraction is too large"""
1164 1165 1166
##    return ((rel_cost_t([pis_before_abs,latches_before_abs, ands_before_abs])> -.1)
##           or (n_latches() >= ratio*latches_before_abs))
    return (n_latches() >= ratio*latches_before_abs)
.  
Baruch Sterin committed
1167 1168 1169 1170 1171 1172 1173 1174 1175

##def abstract(if_bip=True):
##    global ratio
##    if if_bip:
##        return abstractb(True) #old method using abstraction refinement
##    else:
##        return abstractb(False) #not using bip and reachx

def abstractb():
1176
    """ abstracts using N Een's method 3 - cex/proof based abstraction. The result is further refined using
.  
Baruch Sterin committed
1177
    simulation, BMC or BDD reachability. abs_ratio is the the limit for accepting an abstraction"""
1178
    global G_C, G_T, latches_before_abs, x_factor, last_verify_time, x, win_list, j_last, sims
.  
Baruch Sterin committed
1179 1180 1181 1182
    global latches_before_abs, ands_before_abs, pis_before_abs, abs_ratio
    if ifbip < 1:
        print 'using ,abs in old way'
    tt = time.time()
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
    j_last = 0
    set_globals()
    #win_list = []
    latches_before_abs = n_latches()
    ands_before_abs = n_ands()
    pis_before_abs = n_real_inputs()
    abc('w %s_before_abs.aig'%f_name)
    print 'Start: ',
    ps()
    funcs = [eval('(pyabc_split.defer(initial_abstract)())')]
    # fork off BMC3 and PDRm along with initial abstraction
    t = 10000 #want to run as long as initial abstract takes.
##    J = sims+pdrs+bmcs+intrps
.  
Baruch Sterin committed
1196 1197 1198 1199
    J = slps+pdrs+bmcs+intrps
    J = modify_methods(J,1)
##    if n_latches() < 80:
##        J = J + [4]
1200 1201
    funcs = create_funcs(J,t) + funcs
    mtds = sublist(methods,J) + ['initial_abstract'] #important that initial_abstract goes last
.  
Baruch Sterin committed
1202
    m,result = fork_last(funcs,mtds)
1203 1204 1205 1206 1207
    if is_sat():
        print 'Found true counterexample in frame %d'%cex_frame()
        return Sat_true
    if is_unsat():
        return Unsat
.  
Baruch Sterin committed
1208
##    set_max_bmc(NBF)
1209 1210 1211
    NBF = bmc_depth()
    print 'Abstraction good to %d frames'%max_bmc
    #note when things are done in parallel, the &aig is not restored!!!
1212
    abc('&r -s %s_greg.aig; &w initial_greg.aig; &abs_derive; &put; w initial_gabs.aig; w %s_gabs.aig'%(f_name,f_name))
1213 1214 1215 1216 1217
    set_max_bmc(NBF)
    print 'Initial abstraction: ',
    ps()
    abc('w %s_init_abs.aig'%f_name)
    latches_after = n_latches()
.  
Baruch Sterin committed
1218 1219 1220 1221
##    if latches_after >= .90*latches_before_abs: #the following should match similar statement
##    if ((rel_cost_t([pis_before_abs, latches_before_abs, ands_before_abs])> -.1) or
##        (latches_after >= .75*latches_before_abs)):
    if small_abs(abs_ratio):
1222
        abc('r %s_before_abs.aig'%f_name)
1223
        print "Too little reduction!"
.  
Baruch Sterin committed
1224
        print 'Abstract time wasted = %0.2f'%(time.time()-tt)
1225 1226 1227
        return Undecided_no_reduction
    sims_old = sims
    sims=sims[:1] #make it so that rarity sim is not used since it can't find a cex
.  
Baruch Sterin committed
1228
    result = abstraction_refinement(latches_before_abs, NBF,abs_ratio)
1229 1230 1231 1232
    sims = sims_old
    if result <= Unsat:
        return result
##    if n_latches() >= .90*latches_before_abs:
.  
Baruch Sterin committed
1233
##    if ((rel_cost_t([pis_before_abs, latches_before_abs, ands_before_abs])> -.1) or (latches_after >= .90*latches_before_abs)):
1234
##    if rel_cost_t([pis_before_abs,latches_before_abs, ands_before_abs])> -.1:
1235
    if small_abs(abs_ratio) or result == Undecided_no_reduction: #r is ratio of final to initial latches in abstraction. If greater then True
1236
        abc('r %s_before_abs.aig'%f_name) #restore original file before abstract.
1237
        print "Too little reduction!  ",
.  
Baruch Sterin committed
1238 1239 1240 1241 1242 1243 1244
        print 'Abstract time wasted = %0.2f'%(time.time()-tt)
        result = Undecided_no_reduction
        return result
    #new
    else:
        write_file('abs') #this is only written if it was not solved and some change happened.
    print 'Abstract time = %0.2f'%(time.time()-tt)
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    return result

def initial_abstract_old():
    global G_C, G_T, latches_before_abs, x_factor, last_verify_time, x, win_list
    set_globals()
    time = max(1,.1*G_T)
    abc('&get;,bmc -vt=%f'%time)
    set_max_bmc(bmc_depth())
    c = 2*G_C
    f = max(2*max_bmc,20)
    b = min(max(10,max_bmc),200)
    t = x_factor*max(1,2*G_T)
    s = min(max(3,c/30000),10) # stability between 3 and 10 
    cmd = '&get;,abs -bob=%d -stable=%d -timeout=%d -vt=%d -depth=%d'%(b,s,t,t,f)
##    print cmd
    print 'Running initial_abstract with bob=%d,stable=%d,time=%d,depth=%d'%(b,s,t,f)
    abc(cmd)
    abc('&w %s_greg.aig'%f_name)
##    ps()

.  
Baruch Sterin committed
1265 1266
def initial_abstract(t=100):
    global G_C, G_T, latches_before_abs, x_factor, last_verify_time, x, win_list, max_bmc, ifbip
1267 1268
    set_globals()
    time = max(1,.1*G_T)
.  
Baruch Sterin committed
1269
    time = min(time,t)
1270 1271 1272 1273 1274
    abc('&get;,bmc -vt=%f'%time)
    set_max_bmc(bmc_depth())
    c = 2*G_C
    f = max(2*max_bmc,20)
    b = min(max(10,max_bmc),200)
.  
Baruch Sterin committed
1275 1276 1277
    t1 = x_factor*max(1,2*G_T)
    t = max(t1,t)
    s = min(max(3,c/30000),10) # stability between 3 and 10
1278
    cmd = '&get;,abs -bob=%d -stable=%d -timeout=%d -vt=%d -depth=%d'%(b,s,t,t,f)
.  
Baruch Sterin committed
1279 1280 1281
    if ifbip == 2:
        cmd = '&get;,abs -bob=%d -stable=%d -timeout=%d -vt=%d -depth=%d -dwr=%s_vabs'%(b,s,t,t,f,f_name)
        print 'Using -dwr=%s_vabs'%f_name
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
##    print cmd
    print 'Running initial_abstract with bob=%d,stable=%d,time=%d,depth=%d'%(b,s,t,f)
    abc(cmd)
    bmc_depth()
##    pba_loop(max_bmc+1)
    abc('&w %s_greg.aig'%f_name)
    return max_bmc

def abs_m():
    set_globals()
    y = time.time()
    nl = n_abs_latches() #initial set of latches
    c = 2*G_C
    t = x_factor*max(1,2*G_T) #total time
    bmd = bmc_depth()
    if bmd < 0:
        abc('bmc3 -T 2') #get initial depth estimate
        bmd = bmc_depth()
    f = bmd
    abc('&get')
    y = time.time()
    cmd = '&abs_cba -v -C %d -T %0.2f -F %d'%(c,.8*t,bmd) #initial absraction
##    print '\n%s'%cmd
    abc(cmd)
    b_old = b = n_bmc_frames()
    f = min(2*bmd,max(bmd,1.6*b))
    print 'cba: latches = %d, depth = %d'%(n_abs_latches(),b)
##    print n_bmc_frames()
    while True:
        if (time.time() - y) > .9*t:
            break
        nal = n_abs_latches()
        cmd = '&abs_cba -v -C %d -T %0.2f -F %d'%(c,.8*t,f) #f is 2*bmd and is the maximum number of frames allowed
##        print '\n%s'%cmd
        abc(cmd)
##        print n_bmc_frames()
        b_old = b
        b = n_bmc_frames() 
        nal_old = nal 
        nal = n_abs_latches() #nal - nal_old is the number of latches added by cba
        #b - b_old is the additional time frames added by cba
        f = min(2*bmd,max(bmd,1.6*b))   #may be this should just be bmd
        f = max(f,1.5*bmd)
        print 'cba: latches = %d, depth = %d'%(nal,b)
        if ((nal == nal_old) and (b >= 1.5*b_old) and b >= 1.5*bmd):
            """
            Went at least bmd depth and saw too many frames without a cex
            (ideally should know how many frames without a cex)
            """
            print 'Too many frames without cex'
            break
        if b > b_old: #if increased depth
            continue
        if nal > .9*nl: # try to minimize latches
##            cmd = '&abs_pba -v -S %d -F %d -T %0.2f'%(b,b+2,.2*t)
            cmd = '&abs_pba -v -F %d -T %0.2f'%(b+2,.2*t)
##            print '\n%s'%cmd
            abc(cmd)
            b = n_bmc_frames()
            nal_old = nal
            nal = n_abs_latches()
            print 'pba: latches = %d, depth = %d'%(nal,b)
##            print n_bmc_frames()
            if nal_old < nal: #if latches increased there was a cex
                continue
            if nal > .9*nl: # if still too big 
                return
        continue 
##    b = n_bmc_frames()
    cmd = '&abs_pba -v -F %d -T %0.2f'%(b+2,.2*t)
##    print '\n%s'%cmd
    abc(cmd)
    b = n_bmc_frames()
    print 'pba: latches = %d, depth = %d'%(n_abs_latches(),b)
##    print n_bmc_frames()
    print 'Total time = %0.2f'%(time.time()-y)

def n_abs_latches():
    abc('&w pba_temp.aig') #save the &space
    abc('&abs_derive;&put')
1362
    abc('&r -s pba_temp.aig')
1363 1364 1365 1366 1367
    return n_latches()
        
def pba_loop(F):
    n = n_abs_latches()
    while True:
.  
Baruch Sterin committed
1368
        run_command('&abs_pba -v -C 0 -F %d'%F)
1369 1370
        abc('&w pba_temp.aig')
        abc('&abs_derive;&put')
1371
        abc('&r -s pba_temp.aig')
1372 1373 1374 1375 1376 1377 1378 1379
        N = n_latches()
##        if n == N or n == N+1:
##            break
##        elif N > n:
        if N > n:
            print 'cex found'
        break

.  
Baruch Sterin committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
def ssm(options=''):
    """ Now this should be the same as super_prove(1) """
    y = time.time()
    result = prove_part_1() # simplify first
    if result == 'UNDECIDED':
        result = ss(options)
    print 'Total time taken on file %s by function ssm(%s) = %d sec.'%(initial_f_name,options,(time.time() - y))
    return result

def ssmg():
    return ssm('g')
def ssmf():
    return ssm('f')


def ss(options=''):
    """
    Alias for super_sec
    This is the preferred command if the problem (miter) is suspected to be a SEC problem
    """
1400 1401
    global max_bmc, init_initial_f_name, initial_f_name,win_list, last_verify_time, sec_options
    sec_options = options
.  
Baruch Sterin committed
1402 1403 1404
    print '\n*************Executing speculate************'
    y = time.time()
    abc('scl')
1405
    result = speculate()
.  
Baruch Sterin committed
1406 1407 1408 1409
    # if result is 1 then it is a real SAT since we did not do anything before
    if result > 2: #save the result and read in with /rf so that files are initialized correctly
        if not '_spec' in f_name:
            write_file('spec') #make sure we do not overwrite original file
1410
        read_file_quiet_i('%s'%f_name) #this resets f_name and initial_f_name etc.
.  
Baruch Sterin committed
1411 1412 1413
        print '\n*************Executing super_prove ************'
        print 'New f_name = %s'%f_name
        result = sp()
1414
        if result[0] == 'SAT':
.  
Baruch Sterin committed
1415
            result = 'UNDECIDED' #because speculation was done initially.
1416
    elif result[0] == 1:
.  
Baruch Sterin committed
1417 1418 1419 1420
        result = 'SAT'
    else:
        result = RESULT[result]
    print 'Total time taken on file %s by function ss(%s) = %d sec.'%(initial_f_name,options,(time.time() - y))
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    return result

def quick_sec(t):
##    fb_name = f_name[:-3]+'New'
##    abc('&get;&miter -s %s.aig;&put'%fb_name)
##    abc('w %s.%s_miter.aig'%(f_name,fb_name))
    quick_simp()
    verify(slps+ pdrs+bmcs+intrps,t)
    if is_unsat():
        return 'UNSAT'
    if is_sat():
        return 'SAT'
    else:
        return'UNDECIDED'

.  
Baruch Sterin committed
1436 1437 1438 1439 1440 1441 1442
def pre_sec():
    """ put files to be compared into Old and New aigs. Simplify, but
    turn off reparameterization so that PIs in Old and New match after simplification.
    """
    global trim_allowed
##    trim_allowed = False
##    print 'trim allowed = ',trim_allowed
1443
    print 'First file: ',
1444
    read_file_quiet_i() #note - reads into & space and then does &put
.  
Baruch Sterin committed
1445 1446
    ps()
    prs(False)
1447
    ps()
.  
Baruch Sterin committed
1448
    abc('&w Old.aig')
1449
    print 'Second file: ',
1450
    read_file_quiet_i()
1451
    ps()
.  
Baruch Sterin committed
1452 1453 1454
    prs(False)
    ps()
    abc('&w New.aig')
1455
        
.  
Baruch Sterin committed
1456 1457 1458 1459 1460 1461 1462
def cec():
    print 'Type in the name of the aig file to be compared against'
    s = raw_input()
    s = remove_spaces(s)
    if not 'aig' in s:
        s = s+'.aig'
    run_command("&get;&cec -v %s"%s)
1463 1464 1465

def sec(B_part,options):
    """
.  
Baruch Sterin committed
1466 1467
    Use this for AB filtering and not sup_sec
    Use pp_sec to make easy names for A and B, namely Old and New.
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    This assumes that the original aig (renamed A_name below) is already read into the working space.
    Then we form a miter using &miter between two circuits, A_name, and B_name.
    We then do speculate immediately. Optionally we could simplify A and B
    and then form the miter and start from there. The only difference in speculate
    is that &srm2 is used, which only looks at equivalences where one comes from A and
    one from B. Options are -a and -b which says use only flops in A or in B or both. The
    switch sec_sw controls what speculate does when it generates the SRM.
    """
    global f_name,sec_sw, A_name, B_name, sec_options
    yy = time.time()
    A_name = f_name # Just makes it so that we can refer to A_name later in &srm2
    B_name = B_part
    run_command('&get; &miter -s %s.aig; &put'%B_name)
##    abc('orpos')
.  
Baruch Sterin committed
1482
    f_name = A_name+'_'+B_name+'_miter' # reflect that we are working on a miter.
1483 1484 1485 1486 1487 1488 1489
    abc('w %s.aig'%f_name)
    print 'Miter = ',
    ps()
    sec_options = options
    if sec_options == 'ab':
        sec_options = 'l' #it will be changed to 'ab' after &equiv
    sec_sw = True 
.  
Baruch Sterin committed
1490
    result = speculate() 
1491 1492 1493 1494 1495 1496
    sec_options = ''
    sec_sw = False
    if result <= Unsat:
        result = RESULT[result]
    else:
        result = sp()
1497
        if result[0] == 'SAT':
.  
Baruch Sterin committed
1498
            result = 'UNDECIDED'
1499 1500 1501 1502 1503
    print 'Total time = %d'%(time.time() - yy)
    return result

def filter(opts):
    global A_name,B_name
.  
Baruch Sterin committed
1504
##    print 'Filtering with options = %s'%opts
1505 1506 1507
    """ This is for filter which effectively only recognizes options -f -g"""
    if (opts == '' or opts == 'l'): #if 'l' this is used only for initial &equiv2 to get initial equiv creation
        return
1508
    print 'filter = %s  '%opts,
1509
    if opts == 'ab':
.  
Baruch Sterin committed
1510 1511
        print A_name ,
        print B_name
1512
##        run_command('&ps')
1513 1514
        run_command('&filter -f %s.aig %s.aig'%(A_name,B_name))
        return
1515 1516 1517
####    if not opts == 'f':
####        opts = 'g'
##    print 'filter = %
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
    run_command('&filter -%s'%opts)

def check_if_spec_first():
    global sec_sw, A_name, B_name, sec_options, po_map
    set_globals()
    t = max(1,.5*G_T)
    r = max(1,int(t))
    abc('w check_save.aig')
    abc('&w check_and.aig')
    abc("&get; &equiv3 -v -F 20 -T %f -R %d"%(t,5*r))
    filter('g')
1529
    abc("&srm -A %s_gsrm.aig; r %s_gsrm.aig"%(f_name,f_name))
1530 1531 1532
    print 'Estimated # POs = %d for initial speculation'%n_pos()
    result = n_pos() > max(50,.25*n_latches())
    abc('r check_save.aig')
1533
    abc('&r -s check_and.aig')
1534 1535
    return result

.  
Baruch Sterin committed
1536
def initial_speculate(sec_opt=''):
1537 1538
    global sec_sw, A_name, B_name, sec_options, po_map
    set_globals()
.  
Baruch Sterin committed
1539 1540
    if sec_options == '':
        sec_options = sec_opt
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
    # 1000 - 15, 5000 - 25, 10000 - 30, 50000 - 50
    na = n_ands()
##    t = max(1,G_T)
    if na < 1000:
        t =20
    elif na < 5000:
        t = 20 + ((na-1000)/4000)*20
    elif na < 10000:
        t = 40 + ((na-5000)/5000)*20
    elif na < 50000:
        t = 60 + ((na-40000)/40000)*15
    else:
        t = 75
1554
    r = max(1,int(t))
1555
    rounds = 30*r
.  
Baruch Sterin committed
1556
    print 'Initial sec_options = %s'%sec_options
1557 1558 1559 1560 1561
##    if sec_options == 'l':
##        cmd = "&get; &equiv3 -lv -F 20 -T %f -R %d -S %d"%(3*t,rounds,rounds/20)
##    else:
##        cmd = "&get; &equiv3 -v -F 20 -T %f -R %d -S %d"%(3*t,rounds,rounds/20)
    cmd = "&get; &equiv3 -v -F 20 -T %d -R %d -S %d"%(int(t),0,0) #####XXX
.  
Baruch Sterin committed
1562
    print cmd
1563 1564
    abc(cmd)
##    print 'AND space after &equiv3: ',
1565
##    run_command('&ps')
1566 1567 1568 1569 1570
    if (sec_options == 'l'):
        if sec_sw:
            sec_options = 'ab'
        else:
            sec_options = 'f'
.  
Baruch Sterin committed
1571 1572 1573 1574
##    print 'A_name: ',
##    run_command('r %s.aig;ps'%A_name)
##    print 'B_name: ',
##    run_command('r %s.aig;ps'%B_name)
1575
    print 'filtering'
1576 1577 1578 1579
    filter(sec_options)
    abc('&w initial_gore.aig')
##    print 'Running &srm'
    if sec_sw:
.  
Baruch Sterin committed
1580 1581 1582 1583 1584 1585
        print 'miter: ',
        run_command('&ps')
        print 'A_name: ',
        run_command('r %s.aig;ps'%A_name)
        print 'B_name: ',
        run_command('r %s.aig;ps'%B_name)
1586 1587 1588
        cmd = "&srm2 -%s %s.aig %s.aig; r gsrm.aig; w %s_gsrm.aig; &w %s_gore.aig"%(sec_options,A_name,B_name,f_name,f_name)
        abc(cmd)
        po_map = range(n_pos())
1589
        return sec_options
1590
    else:
.  
Baruch Sterin committed
1591
##        abc('&r %s_gore.aig; &srm ; r gsrm.aig; w %s_gsrm.aig'%(f_name,f_name))
1592 1593
        cmd = "&srm -A %s_gsrm.aig; r %s_gsrm.aig; &w %s_gore.aig"%(f_name,f_name,f_name)
        print 'Running %s'%cmd
1594
        abc(cmd)
.  
Baruch Sterin committed
1595
        print 'done with &srm'
1596
        po_map = range(n_pos())
1597 1598 1599
        if sec_options == '' or sec_options == 'g':
##            if n_pos() > 10000:###temp
            if n_eff_pos() > 1000: ##### Temporary
.  
Baruch Sterin committed
1600 1601
                sec_options = 'g'
                print 'sec_options set to %s'%'g'
1602
                abc('&r -s %s_gore.aig'%f_name)
.  
Baruch Sterin committed
1603
                filter(sec_options)
1604 1605 1606
##                print 'Running &srm'
                cmd = "&srm -A %s_gsrm.aig; r %s_gsrm.aig; &w %s_gore.aig"%(f_name,f_name,f_name)
##                print 'Running %s'%cmd
.  
Baruch Sterin committed
1607 1608
                abc(cmd)
                po_map = range(n_pos())
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
                if n_eff_pos() > 500:
##                if n_pos() > 20000:####temp
                    sec_options = 'f'
                    print 'sec_options set to %s'%'f'
                    abc('&r -s %s_gore.aig'%f_name)
                    filter(sec_options)
                    print 'Running &srm'
                    cmd = "&srm -A %s_gsrm.aig; r %s_gsrm.aig; &w %s_gore.aig"%(f_name,f_name,f_name)
                    print 'Running %s'%cmd
                    abc(cmd)
                    po_map = range(n_pos())
    return sec_options
                
##                    if n_pos() > 2000:
##                        return sec_options
                        
.  
Baruch Sterin committed
1625
        
1626 1627
def test_against_original():
    '''tests whether we have a cex hitting an original PO'''
.  
Baruch Sterin committed
1628
    abc('&w %s_save.aig'%f_name) #we preserve whatever was in the & space
1629 1630
    abc('&r -s %s_gore.aig'%f_name) #This is the original
    abc('testcex') #test the cex against the &space
1631
    PO = cex_po()
.  
Baruch Sterin committed
1632
##    print 'test_against original gives PO = %d'%PO 
1633
    abc('&r -s %s_save.aig'%f_name)
1634 1635 1636 1637
    if PO > -1:
##        print 'cex fails an original PO'
        return True
    else:
.  
Baruch Sterin committed
1638
        abc('write_status %s_status.status'%f_name)
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
        return False

def set_cex_po(n=0):
    """
    if cex falsifies a non-real PO return that PO first,
    else see if cex_po is one of the original, then take it next
    else return -1 which means that the cex is not valid and hence an error.
    parameter n = 1 means test the &-space
    """
    global n_pos_before, n_pos_proved #these refer to real POs
    if n == 0:
.  
Baruch Sterin committed
1650
        abc('testcex -a -O %d'%(n_pos_before-n_pos_proved)) #test regular AIG space
1651
    else:
.  
Baruch Sterin committed
1652
        abc('testcex -O %d'%(n_pos_before-n_pos_proved)) #test the &-AIG
1653
    PO = cex_po()
.  
Baruch Sterin committed
1654
##    print 'cex_po = %d, n_pos_before = %d, n_pos_proved = %d'%(PO, n_pos_before, n_pos_proved)
1655
    if PO >= (n_pos_before - n_pos_proved): #cex_po is not an original
.  
Baruch Sterin committed
1656
##        print '1. cex PO = %d'%PO
1657 1658
        return PO # after original so take it.
    if n == 0:
.  
Baruch Sterin committed
1659
        abc('testcex -a') #test regular
1660
    else:
.  
Baruch Sterin committed
1661
        abc('testcex')  #test &space
1662
    PO = cex_po()
1663
    print '2. cex PO = %d'%PO
1664 1665 1666 1667
    cx = cex_get()
    if PO > -1:
        if test_against_original(): #this double checks that it is really an original PO
            cex_put(cx)
1668
            print 'test_against_original was valid'
1669 1670
            return PO
        else:
1671
            print '1. PO is not valid'
1672
            return -1 #error
.  
Baruch Sterin committed
1673
    if PO < 0 or PO >= (n_pos_before - n_pos_proved): # not a valid cex because already tested outside original.
1674
##        print 'cex_po = %d, n_pos_before = %d, n_pos_proved = %d'%(PO, n_pos_before, n_pos_proved)
.  
Baruch Sterin committed
1675 1676 1677
        print '2. PO is not valid'
        PO = -1 #error
##    print '3. cex PO = %d'%PO
1678 1679
    return PO

1680 1681 1682 1683
def cex_stats():
    print 'cex_pis = %d, cex_regs = %d, cex_po = %d, cex_frame = %d'%(n_cex_pis(),n_cex_regs(),cex_po(),cex_frame())

def speculate(t=0):
1684 1685 1686 1687
    """Main speculative reduction routine. Finds candidate sequential equivalences and refines them by simulation, BMC, or reachability
    using any cex found. """    
    global G_C,G_T,n_pos_before, x_factor, n_latches_before, last_verify_time, trim_allowed, n_pos_before
    global t_init, j_last, sec_sw, A_name, B_name, sec_options, po_map, sweep_time, sims, cex_list, n_pos_proved,ifpord1
1688 1689 1690 1691 1692 1693 1694 1695 1696
    global last_cx, total_spec_refine_time, skip_spec
##    print 'sec_options = %s'%sec_options
    if skip_spec:
        return Undecided_no_reduction
    add_trace('speculate')
    if t > 0:
        total_spec_refine_time = t
    abc('scl') #make sure no dangling flops
    abc('orpos')
1697 1698
    last_cx = 0
    ifpord1 = 1
.  
Baruch Sterin committed
1699 1700
    initial_po_size = last_srm_po_size = n_pos()
    initial_sizes = [n_pis(),n_pos(),n_latches(),n_ands()]
1701 1702
    if sec_sw:
        print 'A_name = %s, B_name = %s, f_name = %s, sec_options = %s'%(A_name, B_name, f_name, sec_options)
.  
Baruch Sterin committed
1703
    elif n_ands()> 36000 and sec_options == '':
1704
##        add_trace('sec options g')
1705 1706
        sec_options = 'g'
        print 'sec_options set to "g"'
1707
##        add_trace('sec_options ="g"')
1708 1709 1710 1711
        
    def refine_with_cex():
        """Refines the gore file to reflect equivalences that go away because of cex"""
        global f_name
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
        abc('write_status %s_before_refine.status'%f_name)
        abc('&r -s %s_gore.aig; &resim -m'%f_name)
##        run_command('&ps')
##        cex_stats()
        filter(sec_options)
        run_command('&w %s_gore.aig'%f_name)
        return

    def refine_without_cex(L=[]):
        """removes the POs in the current SRM in the list L. Alters the equivalence classes in the
            gore file accordingly.
        """
        global f_name
        if L == []:
            return
        print 'Entered refine_without_cex'
        abc('write_status %s_before_refine.status'%f_name)
        create_abc_array(L)
        print 'wrote array'
        abc('&r -s %s_gore.aig; &equiv_filter'%f_name)
        print 'filtered gore using L'
1733
        filter(sec_options)
1734
        print 'filtered with %s'%sec_options
1735 1736
        run_command('&w %s_gore.aig'%f_name)
        return
1737

1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
    
    def set_cex(lst):
        """ assumes only one in lst """
        for j in range(len(lst)):
            cx = lst[j]
            if cx == None:
                continue
            else:
                cex_put(cx)
                break
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
    def retry(t):
        add_trace('retrying')
        print 'retrying winner cex which did not refine'
        abc('r %s_gsrm_before.aig'%f_name) #restore previous gsrm
        abc('w %s_beforerpm.aig'%f_name)
        rep_change = reparam() #must be paired with reconcile below if cex
        if rep_change:
            add_trace('reparam')
        abc('w %s_afterrpm.aig'%f_name)
        if last_winner == 'RareSim':
            simulate2(t)
        elif last_winner == 'PDR':
            pdr(t)
        elif last_winner == 'BMC':
            bmc(t)
        elif last_winner == 'INTRP':
            intrp(t)
        elif last_winner == 'PDRM':
            pdrm(t)
        elif last_winner == 'BMC3':
            bmc3(t)
        elif last_winner == 'PDR_sd':
            pdrseed(t)
        elif last_winner == 'PDRM_sd':
            pdrmm(t)
        elif last_winner == 'INTRPm':
            intrpm(t)
        elif last_winner == 'REACHY':
            reachy(t)
        elif last_winner == 'BMC_J':
            bmc_j(t)
        elif last_winner == 'PDRa':
            pdra(t)
        else:
            reconcile(rep_change)
            return False
        reconcile(rep_change)
        if not is_sat():
            return False
        abc('&r -s %s_gore_before.aig ;&w %s_gore.aig'%(f_name,f_name)) #restore old gore file
        return True
    
1790 1791 1792 1793 1794 1795
    def generate_srm():
        """generates a speculated reduced model (srm) from the gore file"""
        global f_name, po_map, sec_sw, A_name, B_name, sec_options, n_pos_proved
##        print 'Generating'
        pos = n_pos()
        ab = n_ands()
.  
Baruch Sterin committed
1796
        abc('w %s_oldsrm.aig'%f_name) #save for later purposes
1797
        if sec_sw:
1798
            run_command('&r -s %s_gore.aig; &srm2 -%s %s.aig %s.aig; r gsrm.aig; w %s_gsrm.aig'%(f_name,sec_options,A_name,B_name,f_name))
1799
        else:
1800
            abc('&r -s %s_gore.aig; &srm -A %s_gsrm.aig ; r %s_gsrm.aig'%(f_name,f_name,f_name)) #do we still need to write the gsrm file
1801 1802 1803 1804 1805
##        ps()
        po_map = range(n_pos())
        ps()
        n_pos_proved = 0
        return 'OK'
1806

1807 1808 1809 1810
    n_pos_before = n_pos()
    n_pos_proved = 0
    n_latches_before = n_latches()    
    set_globals()
.  
Baruch Sterin committed
1811 1812 1813
##    t = max(1,.5*G_T)#irrelevant
##    r = max(1,int(t))
    t = 1000
1814
    j_last = 0
.  
Baruch Sterin committed
1815 1816
    J = slps+sims+pdrs+bmcs+intrps
    J = modify_methods(J,1)
1817 1818
    print 'sec_options = %s'%sec_options 
    funcs = [eval('(pyabc_split.defer(initial_speculate)("%s"))'%sec_options)]
1819 1820
    funcs = create_funcs(J,10000)+funcs #want other functins to run until initial speculate stops
    mtds = sublist(methods,J) + ['initial_speculate'] #important that initial_speculate goes last
1821 1822 1823 1824 1825 1826 1827 1828
    print mtds
    res = fork_last(funcs,mtds)
    print 'init_spec return = ',
    print res
    if res[1] in ['f','g','']:
        sec_options = res[1]
    add_trace('sec_options = %s'%sec_options)
    add_trace('Number of POs: %d'%n_pos())
1829 1830 1831 1832 1833 1834 1835
##    ps()
    if is_unsat():
        return Unsat
    if is_sat():
        return Sat_true
    if n_pos_before == n_pos():
        print 'No new outputs. Quitting speculate'
1836
        add_trace('de_speculate')
1837
        return Undecided_no_reduction # return result is unknown
1838 1839 1840 1841
    if n_eff_pos() > 1999000:
        print 'Too many POs'
        add_trace('de_speculate')
        return Undecided_no_reduction
1842 1843
    print 'Initial speculation: ',
    ps()
1844 1845 1846 1847 1848 1849 1850 1851 1852
    abc('w %s_initial_gsrm.aig'%f_name)
    if n_pos() > 1000:
        print 'Too many new outputs. Quitting speculate'
        add_trace('de_speculate')
        return Undecided_no_reduction # return result is unknown
    if n_pos() <= n_pos_before + 2:
        print 'Too few new outputs. Quitting speculate'
        add_trace('de_speculate')
        return Undecided_no_reduction # return result is unknown
1853 1854
    if n_latches() == 0:
        return check_sat()
1855 1856 1857 1858 1859 1860 1861 1862
    if use_pms:
        p,q,r=par_multi_sat(0)
        q = indices(r,1)
        print sumsize(r)
        if count_less(r,1) < .25*len(r):
            print 'too many POs are already SAT'
            add_trace('de_speculate')
            return Undecided_no_reduction
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    if sec_options == 'l' and sec_sw:
        sec_options = 'ab' #finished with initial speculate with the 'l' option
        print "sec_options set to 'ab'"
    elif sec_options == 'l':
        sec_options = 'f'
        print "sec_options set to 'f'"
    po_map = range(n_pos()) #we need this because the initial_speculate is done in parallel and po_map is not passed back.
    npi = n_pis()
    set_globals()
    if is_sat():
        return Sat_true
    simp_sw = init = True
1875
    add_trace('speculative refinement')
1876 1877
    print '\nIterating speculation refinement'
    sims_old = sims
.  
Baruch Sterin committed
1878
    sims = sims[:1] 
1879
    J = slps+sims+pdrs+intrps+bmcs
.  
Baruch Sterin committed
1880 1881
    J = modify_methods(J)
##    print sublist(methods,J)
1882 1883
    t = max(50,max(1,2*G_T))
    last_verify_time = t
1884 1885 1886
    ### temp
    last_verify_time = total_spec_refine_time
    ###
1887 1888 1889 1890 1891
    print 'Verify time set to %d'%last_verify_time
    reg_verify = True
    ref_time = time.time()
    sweep_time = 2
    ifpord1=1
1892 1893 1894
    par_verify = re_try = False
##    total_spec_refine_time = 150
    while True: ##################### refinement loop
1895 1896
        set_globals()
        yy = time.time()
1897 1898 1899 1900 1901 1902
        time_used = (yy-ref_time)
        print 'Time_used = %0.2f'%time_used
        if time_used > total_spec_refine_time:
            print 'Allotted speculation refinement time is exceeded'
            add_trace('de_speculate')
            return Undecided_no_reduction
1903 1904 1905 1906 1907
        if not init:
            abc('r %s_gsrm.aig'%f_name) #this is done only to set the size of the previous gsrm.
            abc('w %s_gsrm_before.aig'%f_name)
            set_size()
            result = generate_srm()
1908 1909 1910 1911 1912 1913 1914
            if n_pos() <= n_pos_before + 1: #heuristic that if only have one equivalence, then not worth it
                abc('r %s.aig'%f_name) #revert to previous aig
                sims = sims_old
                print 'UNDECIDED'
                print 'Refinement time = %0.2f'%(time.time() - ref_time)
                add_trace('de_speculate')
                return Undecided_no_reduction 
.  
Baruch Sterin committed
1915
            last_srm_po_size = n_pos()
1916 1917 1918 1919 1920 1921 1922
            yy = time.time()
            # if the size of the gsrm did not change after generating a new gsrm
            # and if the cex is valid for the gsrm, then the only way this can happen is if
            # the cex_po is an original one.
            if check_size(): #same size before and after
                if check_cex(): #valid cex failed to refine possibly
                    if 0 <= cex_po() and cex_po() < (n_pos_before - n_pos_proved): #original PO
1923 1924
                        print 'Found cex in original output number = %d'%cex_po()
                        print 'Refinement time = %0.2f'%(time.time() - ref_time)
1925 1926 1927
                        return Sat_true
                    elif check_same_gsrm(f_name): #if two gsrms are same, then failed to refine
                        print 'CEX failed to refine'
1928
                        add_trace('de_speculate')
1929 1930 1931
                        return Error
                else:
                    print 'not a valid cex'
1932 1933 1934 1935 1936 1937 1938 1939
                    print 'Last winner = %s'%last_winner
                    print 're_try = %d'%re_try
                    if re_try:
                        add_trace('de_speculate')
                        return Error #abort speculation
                    re_try = True
            else:
                re_try = False # just got a valid refinement so reset.
1940
            if n_latches() == 0:
1941 1942 1943 1944 1945 1946
                print 'Number of latches reduced to 0'
                print 'CEX refined incorrectly'
                abc('r %s.aig'%f_name) #revert to previous aig
                sims = sims_old
                add_trace('de_speculate')
                return Error
1947 1948 1949 1950 1951 1952 1953 1954 1955
        init = False # make it so that next time it is not the first time through
        if not t == last_verify_time: # heuristic that if increased last verify time,
                                      # then try pord_all 
            t = last_verify_time
            if reg_verify:
                t_init = (time.time() - yy)/2 #start poor man's concurrency at last cex fime found
                t_init = min(10,t_init)
                t = last_verify_time
                print 'Verify time set to %d'%t
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
        if not re_try:
##            abc('w %s_beforerpm.aig'%f_name)
##            rep_change = reparam() #must be paired with reconcile below if cex
####            if rep_change:
####                add_trace('reparam')
##            abc('w %s_afterrpm.aig'%f_name)
            rep_change = False #TEMP
            if reg_verify:
                if par_verify:
                    S,L_sat_POs,s = par_multi_sat(120)
                    L_sat_POs = indices(s,1)
##                    L_sat_POs = L[1]
                    L=[]
                    for j in range(len(L_sat_POs)): #eliminate any of the original POs
                        if L_sat_POs[j] >= (n_pos_before-n_pos_proved):
                            L=L+[L_sat_POs[j]]
                    L_sat_POs = L
                    print L
                    if not L_sat_POs == []:
                        ress = [1,[['multi_sat']]]
                        add_trace(['multi_sat'])
                    else:
                        reg_verify = False
                        ress = pord_1_2(t)
                        add_trace(ress[1])
                else:
                    ttt = time.time() #find time it takes to find a cex
                    ress = verify(J,t)
                    t_last_verify = time.time() - ttt
            else:
                ress = pord_1_2(t)
##                print ress
                add_trace(ress[1])
            result = ress[0]
##            add_trace(ress[1])
1991
        else:
1992 1993 1994 1995 1996
            if not retry(100):
                add_trace('de_speculate')
                return Error
            result = get_status()
##        print result
1997
        if result == Unsat:
1998
            add_trace('UNSAT by %s'%ress[1])
1999
            print 'UNSAT'
2000
            print 'Refinement time = %0.2f'%(time.time() - ref_time)
2001 2002
            return Unsat
        if result < Unsat:
2003 2004 2005 2006 2007
            abc('&r -s %s_gore.aig;&w %s_gore_before.aig'%(f_name,f_name)) #we are making sure that none of the original POs fail
            if par_verify:
                refine_without_cex(L_sat_POs)
                print 'refining without cex done'
                continue
2008 2009
            if not reg_verify:
                set_cex(cex_list)
2010 2011 2012 2013 2014 2015 2016
##            if not re_try:
####                rec = reconcile(rep_change) #end of pairing with reparam()TEMP
####                if rec == 'error':
####                    add_trace('de_speculate')
####                    return Error
##                assert (npi == n_cex_pis()),'ERROR: #pi = %d, #cex_pi = %d'%(npi,n_cex_pis())
            abc('&r -s %s_gore.aig;&w %s_gore_before.aig'%(f_name,f_name)) #we are making sure that none of the original POs fail
.  
Baruch Sterin committed
2017 2018 2019
            if reg_verify:
                PO = set_cex_po(0) #testing the regular space
            else:
2020
                abc('&r -s %s_gsrm.aig'%f_name)
.  
Baruch Sterin committed
2021
                PO = set_cex_po(1) # test against the &space.
2022
            print 'cex_PO is %d,  '%PO,
2023 2024
            if (-1 < PO and PO < (n_pos_before-n_pos_proved)):
                print 'Found cex in original output = %d'%cex_po()
2025
                print 'Refinement time = %0.2f'%(time.time() - ref_time)
2026 2027
                return Sat_true
            if PO == -1:
2028
                add_trace('de_speculate')
2029 2030
                return Error
            refine_with_cex()    #change the number of equivalences
2031 2032
            if not par_verify and t_last_verify > 2500:
                par_verify = True #switch to finding many POs at a time
2033 2034 2035
            continue
        elif (is_unsat() or n_pos() == 0):
            print 'UNSAT'
2036
            print 'Refinement time = %0.2f'%(time.time() - ref_time)
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
            return Unsat
        else: #if undecided, record last verification time
            print 'Refinement returned undecided in %d sec.'%t
            last_verify_time = t
            #########################added
            if reg_verify: #try one last time with parallel POs cex detection (find_cex_par) if not already tried
                abc('r %s_beforerpm.aig'%f_name) # to continue refinement, need to restore original
                t_init = min(last_verify_time,(time.time() - yy)/2) #start poor man's concurrency at last cex fime found
                t_init = min(10,t_init)
                reg_verify = False
                t = last_verify_time # = 2*last_verify_time
                abc('w %s_beforerpm.aig'%f_name)
                rep_change = reparam() #must be paired with reconcile()below
                abc('w %s_afterrpm.aig'%f_name)
2051 2052 2053 2054
                ress = pord_1_2(t) #main call to verification
                print ress
                result = ress[0]
                add_trace(ress[1])
2055 2056
                if result == Unsat:
                    print 'UNSAT'
2057
                    print 'Refinement time = %0.2f'%(time.time() - ref_time)
2058
                    return Unsat
2059 2060 2061
                if is_sat() or result == Sat:
##                    assert result == get_status(),'result: %d, status: %d'%(result,get_status())
                    print 'result: %d, status: %d'%(result,get_status())
2062
                    set_cex(cex_list)
2063 2064 2065 2066 2067
                    rec = reconcile(rep_change)
                    if rec == 'error':
                        add_trace('de_speculate')
                        return Error
                    abc('&r -s %s_gsrm.aig'%f_name)
.  
Baruch Sterin committed
2068
                    PO = set_cex_po(1) #testing the & space
2069 2070
                    if (-1 < PO and PO < (n_pos_before-n_pos_proved)):
                        print 'Found cex in original output = %d'%cex_po()
2071
                        print 'Refinement time = %0.2f'%(time.time() - ref_time)
2072 2073
                        return Sat_true
                    if PO == -1:
2074
                        add_trace('de_speculate')
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
                        return Error
                    refine_with_cex()    #change the number of equivalences
                    continue
                else: #if undecided, record last verification time
                    last_verify_time = t
                    print 'UNDECIDED'
                    break
            ################### added
            else:
                break
    sims = sims_old
    print 'UNDECIDED'
2087
    print 'Refinement time = %0.2f'%(time.time() - ref_time)
.  
Baruch Sterin committed
2088 2089
##    if last_srm_po_size == initial_po_size: #essentially nothing happened. last_srm_po_size will be # POs in last srm.
    if initial_sizes == [n_pis(),n_pos(),n_latches(),n_ands()]:
2090 2091
        abc('r %s.aig'%f_name)
        add_trace('de_speculate')
.  
Baruch Sterin committed
2092 2093 2094
        return Undecided_no_reduction #thus do not write spec file
    else: #file was changed, so some speculation happened. If we find a cex later, need to know this.
        write_file('spec')
2095 2096
        return Undecided_reduction

2097
def simple_sat(t=900):
2098 2099 2100
    """
    aimed at trying harder to prove SAT
    """
.  
Baruch Sterin committed
2101
    y = time.time()
2102 2103 2104 2105 2106
    bmcs2 = [9,31]
    bmcs2 = [9,30]
    J = allbmcs+pdrs+sims+[5]
##    J = modify_methods(J)
##    J = [14,2,7,9,30,31,26,5] #5 is pre_simp
.  
Baruch Sterin committed
2107 2108
    funcs = create_funcs(J,t)
    mtds =sublist(methods,J)
2109
    print mtds
.  
Baruch Sterin committed
2110 2111 2112 2113
    fork_last(funcs,mtds)
    result = get_status()
    if result > Unsat:
        write_file('smp')
2114
        result = verify(slps+allbmcs+pdrs+sims,t)
.  
Baruch Sterin committed
2115
    print 'Time for simple_sat = %0.2f'%(time.time()-y)
2116 2117
    report_bmc_depth(max(max_bmc,n_bmc_frames()))
    return [RESULT[result[0]]] + [result[1]]
.  
Baruch Sterin committed
2118

2119
def simple(t=10000,no_simp=0):
.  
Baruch Sterin committed
2120
    y = time.time()
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
##    pre_simp()
    if not no_simp:
        prove_part_1()
        if is_sat():
            return ['SAT']+['pre_simp']
        if is_unsat():
            return ['UNSAT']+['pre_simp']
        if n_latches() == 0:
            return [RESULT[check_sat()]]+['pre_simp']
##    J = slps+sims+bmcs+pdrs+intrps+pre
    J = slps+sims+allbmcs+allpdrs+intrps
.  
Baruch Sterin committed
2132 2133
    J = modify_methods(J)
    result = verify(J,t)
2134 2135
##    add_pord('%s by %s'%(result[0],result[1])
    return [RESULT[result[0]]] + [result[1]]
.  
Baruch Sterin committed
2136

2137 2138
def simple_bip(t=1000):
    y = time.time()
2139
    J = [0,14,1,2,30,5] #5 is pre_simp
2140 2141 2142 2143 2144 2145
    funcs = create_funcs(J,t)
    mtds =sublist(methods,J)
    fork_last(funcs,mtds)
    result = get_status()
    if result > Unsat:
        write_file('smp')
2146
        result = verify(slps+[0,14,1,2,30],t)
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
    print 'Time for simple_bip = %0.2f'%(time.time()-y)
    return RESULT[result] 

def check_same_gsrm(f):
##    return False #disable the temporarily until can figure out why this is there
    """checks gsrm miters before and after refinement and if equal there is an error"""
    global f_name
    abc('r %s_gsrm.aig'%f)
##    ps()
    run_command('miter -c %s_gsrm_before.aig'%f)
##    ps()
    abc('&get; ,bmc -timeout=5')
    result = True #if the same
    if is_sat(): #if different
        result = False
    abc('r %s_gsrm.aig'%f)
##    ps()
    return result

def check_cex():
    """ check if the last cex still asserts one of the outputs.
    If it does then we have an error"""
    global f_name
2170 2171
    abc('read_status %s_before_refine.status'%f_name)
    abc('&r -s %s_gsrm_before.aig'%f_name)
2172
##    abc('&r %s_gsrm.aig'%f_name)
2173 2174
    run_command('testcex') #test the cex against the &-space aig.
##    print 'cex po = %d'%cex_po()
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
    return cex_po() >=0

def set_size():
    """Stores  the problem size of the current design.
    Size is defined as (PIs, POs, ANDS, FF)""" 
    global npi, npo, nands, nff, nmd
    npi = n_pis()
    npo = n_pos()
    nands = n_ands()
    nff = n_latches()
    nmd = max_bmc
    #print npi,npo,nands,nff

def check_size():
    """Assumes the problem size has been set by set_size before some operation.
    This checks if the size was changed
    Size is defined as (PIs, POs, ANDS, FF, max_bmc)
    Returns TRUE is size is the same""" 
    global npi, npo, nands, nff, nmd
    #print n_pis(),n_pos(),n_ands(),n_latches()
    result = ((npi == n_pis()) and (npo == n_pos()) and (nands == n_ands()) and (nff == n_latches()) )
    return result

def inferior_size():
    """Assumes the problem size has been set by set_size beore some operation.
    This checks if the new size is inferior (larger) to the old one 
    Size is defined as (PIs, POs, ANDS, FF)""" 
    global npi, npo, nands, nff
    result = ((npi < n_pis()) or (npo < n_pos()) or (nands < n_ands()) )
    return result

2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
##def quick_verify(n):
##    """Low resource version of final_verify n = 1 means to do an initial
##    simplification first. Also more time is allocated if n =1"""
##    global last_verify_time
##    trim()
##    if n == 1:
##        simplify()
##        if n_latches == 0:
##            return check_sat()
##        trim()
##        if is_sat():
##            return Sat_true
##    #print 'After trimming: ',
##    #ps()
##    set_globals()
##    last_verify_time = t = max(1,.4*G_T)
##    if n == 1:
##        last_verify_time = t = max(1,2*G_T)
##    print 'Verify time set to %d '%last_verify_time
##    J = [18] + intrps+bmcs+pdrs+sims
##    status = verify(J,t)
##    return status
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249

def process_status(status):
    """ if there are no FF, the problem is combinational and we still have to check if UNSAT"""
    if n_latches() == 0:
        return check_sat()
    return status
    
def get_status():
    """this simply translates the problem status encoding done by ABC
    (-1,0,1)=(undecided,SAT,UNSAT) into the status code used by our
    python code. -1,0,1 => 3,0,2
    """
    if n_latches() == 0:
        return check_sat()
    status = prob_status() #interrogates ABC for the current status of the problem.
    # 0 = SAT i.e. Sat_reg = 0 so does not have to be changed.
    if status == 1:
        status = Unsat
    if status == -1: #undecided
        status = Undecided
    return status

2250 2251 2252 2253 2254 2255 2256
def two_temp(t=20):
    tt = time.time()
    abc('tempor;scl;drw;&get;&rpm;&put;tempor;scl;drw;&get;&rpm;&put;scorr')
    print 'Time for two_temp = %.2f'%(time.time()-tt)
    return get_status()

def reparam_m():
2257
    """eliminates PIs which if used in abstraction or speculation must be restored by
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
    reconcile and the cex made compatible with file beforerpm
    Uses the &-space
    """
    abc('w %s_temp.aig'%f_name)
    n = n_pis()
    t1 = time.time()
##    abc('&get;,reparam -aig=%s_rpm.aig; r %s_rpm.aig'%(f_name,f_name))
    abc('&get;&rpm;&put')
    tm = (time.time() - t1)
    if n_pis() == 0:
        print 'Number of PIs reduced to 0. Added a dummy PI'
        abc('addpi')
    nn = n_pis()
    if nn < n:
        print 'Reparam_m: PIs %d => %d, time = %.2f'%(n,nn,tm)
        rep_change = True
    else:
        abc('r %s_temp.aig'%f_name)
        rep_change = False
    return rep_change

def reparam_e():
    """eliminates PIs which if used in abstraction or speculation must be restored by
    reconcile and the cex made compatible with file beforerpm
    Uses the &-space
    """
    abc('w %s_temp.aig'%f_name)
2285
    n = n_pis()
2286
    t1 = time.time()
2287
    abc('&get;,reparam -aig=%s_rpm.aig; r %s_rpm.aig'%(f_name,f_name))
2288 2289
##    abc('&get;&rpm;&put')
    tm =(time.time() - t1)
2290 2291 2292 2293 2294
    if n_pis() == 0:
        print 'Number of PIs reduced to 0. Added a dummy PI'
        abc('addpi')
    nn = n_pis()
    if nn < n:
2295
        print 'Reparam_e: PIs %d => %d, time = %.2f'%(n,nn,tm)
2296
        rep_change = True
2297 2298 2299
    else:
        abc('r %s_temp.aig'%f_name)
        rep_change = False
2300 2301
    return rep_change

2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
def reparam():
##    abc('w %s_temp.aig'%f_name)
##    res = reparam_e()
##    res = reparam_m()
    res = reparam_e()
    return res

##def try_and_rpm():
##    abc('w %s_temp.aig'%f_name)
##    n = n_pis()
##    t1 = time.time()
##    abc('&get;&rpm;&put')
##    print 'time &rpm = %.2f'%(time.time() - t1)
##    if n_pis() == 0:
##        print '&rpm: Number of PIs reduced to 0. Added a dummy PI'
##        abc('addpi')
##    nn = n_pis()
##    if nn < n:
##        print '&rpm: Reparam: PIs %d => %d'%(n,nn)
####        rep_change = True
##    abc('r %s_temp.aig'%f_name)
####    else:
####        abc('r %s_temp.aig'%f_name)
####        return False

2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
def reconcile(rep_change):
    """used to make current cex compatible with file before reparam() was done.
    However, the cex may have come
    from extracting a single output and verifying this.
    Then the cex_po is 0 but the PO it fails could be anything.
    So testcex rectifies this."""
    global n_pos_before, n_pos_proved
##    print 'rep_change = %s'%rep_change
    if rep_change == False:
        return
2337
    abc('&r -s %s_beforerpm.aig; &w tt_before.aig'%f_name)
2338
    abc('write_status %s_after.status;write_status tt_after.status'%f_name)
2339
    abc('&r -s %s_afterrpm.aig;&w tt_after.aig'%f_name)
2340 2341 2342 2343
    POa = set_cex_po(1)   #this should set cex_po() to correct PO. A 1 here means it uses &space to check
    abc('reconcile %s_beforerpm.aig %s_afterrpm.aig'%(f_name,f_name))
    # reconcile modifies cex and restores work AIG to beforerpm
    abc('write_status %s_before.status;write_status tt_before.status'%f_name)
2344
    POb = set_cex_po()#does not make sense if we are in absstraction refinement
2345
    if POa != POb:
2346 2347
        abc('&r -s %s_beforerpm.aig; &w tt_before.aig'%f_name)
        abc('&r -s %s_afterrpm.aig; &w tt_after.aig'%f_name)
.  
Baruch Sterin committed
2348
        print 'cex PO afterrpm = %d not = cex PO beforerpm = %d'%(POa,POb)
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
    if POa < 0: #'cex did not assert any output'
        return 'error'

def reconcile_a(rep_change):
    """ This is the reconcile used in abstraction refinement
    used to make current cex compatible with file before reparam() was done.
    However, the cex may have come
    from extracting a single output and verifying this.
    Then the cex_po is 0 but the PO it fails could be anything.
    So testcex rectifies this."""
    global n_pos_before, n_pos_proved
##    print 'rep_change = %s'%rep_change
    if rep_change == False:
        return
    abc('&r -s %s_beforerpm.aig; &w tt_before.aig'%f_name)
    abc('write_status %s_after.status;write_status tt_after.status'%f_name)
    abc('&r -s %s_afterrpm.aig;&w tt_after.aig'%f_name)
    POa = set_cex_po(1)   #this should set cex_po() to correct PO. A 1 here means it uses &space to check
    abc('reconcile %s_beforerpm.aig %s_afterrpm.aig'%(f_name,f_name))
    # reconcile modifies cex and restores work AIG to beforerpm
    abc('write_status %s_before.status;write_status tt_before.status'%f_name)
    if POa < 0: #'cex did not assert any output'
        return 'error'
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388

def reconcile_all(lst, rep_change):
    """reconciles the list of cex's"""
    global f_name, n_pos_before, n_pos_proved
    if rep_change == False:
        return lst
    list = []
    for j in range(len(lst)):
        cx = lst[j]
        if cx == None:
            continue
        cex_put(cx)
        reconcile(rep_change)
        list = list + [cex_get()]
    return list
    

2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
##def try_rpm():
##    """rpm is a cheap way of doing reparameterization and is an abstraction method, so may introduce false cex's.
##    It finds a minimum cut between the PIs and the main sequential logic and replaces this cut by free inputs.
##    A quick BMC is then done, and if no cex is found, we assume the abstraction is valid. Otherwise we revert back
##    to the original problem before rpm was tried."""
##    global x_factor
##    if n_ands() > 30000:
##        return
##    set_globals()
##    pis_before = n_pis()
##    abc('w %s_savetemp.aig'%f_name)
##    abc('rpm')
##    result = 0
##    if n_pis() < .5*pis_before:
##        bmc_before = bmc_depth()
##        #print 'running quick bmc to see if rpm is OK'
##        t = max(1,.1*G_T)
##        #abc('bmc3 -C %d, -T %f'%(.1*G_C, t))
##        abc('&get;,bmc -vt=%f'%t)
##        if is_sat(): #rpm made it sat by bmc test, so undo rpm
##            abc('r %s_savetemp.aig'%f_name)
##        else:
##            trim()
##            print 'WARNING: rpm reduced PIs to %d. May make SAT.'%n_pis()
##            result = 1
##    else:
##        abc('r %s_savetemp.aig'%f_name)
##    return result
2417 2418 2419 2420 2421 2422 2423 2424
            
def verify(J,t):
    """This method is used for finding a cex during refinement, but can also
    be used for proving the property. t is the maximum time to be used by
    each engine J is the list of methods to run in parallel. See FUNCS for list"""
    global x_factor, final_verify_time, last_verify_time, methods
    set_globals()
    t = int(max(1,t))
.  
Baruch Sterin committed
2425
    J = modify_methods(J)
2426 2427 2428 2429
    mtds = sublist(methods,J)
    print mtds
    #print J,t
    F = create_funcs(J,t)
.  
Baruch Sterin committed
2430
    (m,result) = fork_break(F,mtds,'US') #FORK here
2431 2432 2433
##    result = fork_break(F,mtds,'US') #FORK here
    print result
##    assert result[0] == get_status(),'result: %d, status: %d'%(result[0],get_status())
2434 2435
    return result

.  
Baruch Sterin committed
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
def dsat_all(t=100,c=100000):
    print 't=%d,c=%d'%(t,c)
    N = n_pos()
    abc('&get')
    J = range(N)
    ttt = time.time()
    J.reverse()
    abc('w %s_temp.aig'%f_name)
    for j in J:
        tt = time.time()
2446
        abc('r %s.aig'%f_name)
.  
Baruch Sterin committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457
        run_command('cone -O %d; dc2; dsat -C %d'%(j,c))
        if is_unsat():
            print 'Output %d is %s'%(j,RESULT[2]),
        else:
            print 'Output %d is %s'%(j,RESULT[3]),
        T = time.time() -tt
        print 'time = %0.2f'%T
        if time.time() - tt > t:
            break
    print 'Total time = %0.2f'%(time.time() - ttt)
            
2458
def check_sat(t=900):
2459 2460
    """This is called if all the FF have disappeared, but there is still some logic left. In this case,
    the remaining logic may be UNSAT, which is usually the case, but this has to be proved. The ABC command 'dsat' is used fro combinational problems"""
2461
    global smp_trace
2462 2463 2464 2465 2466
    if not n_latches() == 0:
        print 'circuit is not combinational'
        return Undecided
##    print 'Circuit is combinational - checking with dsat'
    abc('&get') #save the current circuit
2467 2468 2469 2470 2471 2472 2473 2474
    abc('orpos')
    J = combs+slps
    mtds = sublist(methods,J)
##    print mtds
    F = create_funcs(J,t)
    (m,result) = fork_last(F,mtds) #FORK here
##    print '%s: '%mtds[m],
##    smp_trace = smp_trace + ['%s'%mtds[m]]
2475
    if is_sat():
.  
Baruch Sterin committed
2476 2477 2478 2479 2480
        abc('&put')
        if n_pos() == 1:
            return Sat_true
        else:
            return Undecided_no_reduction #some POs could be unsat.
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    elif is_unsat():
        return Unsat
    else:
        abc('&put') #restore
        return Undecided_no_reduction

def try_era(s):
    """era is explicit state enumeration that ABC has. It only works if the number of PIs is small,
    but there are cases where it works and nothing else does"""
    if n_pis() > 12:
        return
    cmd = '&get;&era -mv -S %d;&put'%s
    print 'Running %s'%cmd
    run_command(cmd)

def try_induction(C):
    """Sometimes proving the property directly using induction works but not very often.
    For 'ind' to work, it must have only 1 output, so all outputs are or'ed together temporarily"""
    return Undecided_reduction
    print '\n***Running induction'
    abc('w %s_temp.aig'%f_name)
    abc('orpos; ind -uv -C %d -F 10'%C)
    abc('r %s_savetemp.aig'%f_name)
    status = prob_status()
    if not status == 1:
        return Undecided_reduction
    print 'Induction succeeded'
    return Unsat
        
def smp():
    abc('smp')
    write_file('smp')

def dprove():
    abc('dprove -cbjupr')

def trim():
2518
    global trim_allowed,smp_trace
2519
    if not trim_allowed:
2520 2521 2522
        return False
    result = reparam()
    return result
2523

.  
Baruch Sterin committed
2524
def prs(x=True):
2525
    global trim_allowed, smp_trace
.  
Baruch Sterin committed
2526
    """ If x is set to False, no reparameterization will be done in pre_simp"""
2527 2528
    global smp_trace
    smp_trace = []
.  
Baruch Sterin committed
2529 2530
    trim_allowed = x
    print 'trim_allowed = ',trim_allowed
2531 2532 2533
    y = time.time()
    result = pre_simp()
    print 'Time = %0.2f'%(time.time() - y)
2534
    write_file('smp')
2535
    return RESULT[result[0]]
2536

2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559
def check_push():
    """save the current aig if it has a different number of latches from last aig on lst"""
    result = False
    n = n_latches()
##    ps()
    abc('&get;cexsave') #save the current aig
##    typ = hist[-1:]
##    print hist
    run_command('r %s_aigs_%d.aig'%(init_initial_f_name,len(hist)))
##    typ = aigs_pp('pop')
##    aigs.pop() #check latches in last aig.
    nn = n_latches()
##    ps()
##    aigs.push() # put back last aig.
##    aigs_pp('push',typ)
    abc('&put;cexload') # restore current aig
##    print 'check_push: current n=%d, previous nn=%d'%(n,nn)
    if not n == nn: #if number of latches changes need to push the current aig so that reconcile can work.
##        aigs.push()
##        print 'n /= nn'
        aigs_pp('push','reparam0') #default is push,reparam
        result = True
    return result
.  
Baruch Sterin committed
2560

2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
def dump():
    """ get rid of the last aig on the list"""
    abc('&get')
##    aigs.pop()
    aigs_pp('pop')
    abc('&put')

def test_no_simp():
    global last_simp
    ri = float(n_pis())/float(last_simp[0])
    ro = float(n_pos())/float(last_simp[1])
    rl = float(n_latches())/float(last_simp[2])
    ra = float(n_ands())/float(last_simp[3])
    val = min(ri,ro,rl,ra)
    if val < .95:
        print 'simplification worthwhile'
        return False
    print 'simplification not worthwhile'
    return True
        
def pre_simp(n=0,N=0):
.  
Baruch Sterin committed
2582 2583
    """This uses a set of simplification algorithms which preprocesses a design.
    Includes forward retiming, quick simp, signal correspondence with constraints, trimming away
2584
    PIs, and strong simplify. If n not 0, then do not do phase abs"""
.  
Baruch Sterin committed
2585
    global trim_allowed, temp_dec
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
    global smp_trace, aigs, last_simp
    chk_sat = 0
    smp_trace = []
    while True:
        if n_latches() == 0:
            print 'Circuit is combinational'
            chk_sat = 1
            break
        if test_no_simp():
            break
        ttime = time.time()
        set_globals()
        smp_trace = smp_trace + ['&scl']
        abc('&get; &scl; &put')
        if (n_ands() > 200000 or n_latches() > 50000 or n_pis() > 40000):
            smp_trace = smp_trace + ['scorr_T']
            scorr_T(50)
            ps()
.  
Baruch Sterin committed
2604
        if ((n_ands() > 0) or (n_latches()>0)):
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
            res =a_trim()
        if n_latches() == 0:
            break
        status = get_status()
        if (n == 0 and (not '_smp' in f_name) or '_cone' in f_name):
            best_fwrd_min([10,11])
            ps()
            status = try_scorr_constr()
        if ((n_ands() > 0) or (n_latches()>0)):
            res = a_trim()
        if n_latches() == 0:
            break
        status = process_status(status)
        if status <= Unsat:
            last_simp = [n_pis(),n_pos(),n_latches(),n_ands()]
            return [status,smp_trace,hist]
        print 'Starting simplify ',
        simplify(n,N)
        print 'Simplify: ',
        ps()
        if n_latches() == 0:
            break
        if trim_allowed and n == 0:
            t = min(15,.3*G_T)
            if (not '_smp' in f_name) or '_cone' in f_name: #try this only once on a design
                tt = 25
                if n_ands() > 500000:
                    tt = 30
                res,F = try_temps(tt) 
                if res:
                    aigs_pp('push','tempor')
                if n_latches() == 0:
                    break
                if n == 0: 
                    res,N = try_phases()
                    if res:
                        aigs_pp('push','phase')
                if n_latches() == 0:
                    break
            if ((n_ands() > 0) or (n_latches()>0)):
                res = a_trim()
        status = process_status(status)
        print 'Simplification time = %0.2f'%(time.time()-ttime)
        last_simp = [n_pis(),n_pos(),n_latches(),n_ands()]
        return [status, smp_trace,hist]
    last_simp = [n_pis(),n_pos(),n_latches(),n_ands()]
    return [check_sat(),smp_trace,hist]
.  
Baruch Sterin committed
2652

2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687

def try_scorr_constr():
    set_size()
    abc('w %s_savetemp.aig'%f_name)
    status = scorr_constr()
    if inferior_size():
        abc('r %s_savetemp.aig'%f_name)
    return status

def factors(n):
    l = [1,]
    nn = n
    while n > 1:
        for i in (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53):
            if not i <nn:
                break
            if n%i == 0:
                l = l + [i,]
                n = n/i
        if not n == 1:
            l = l + [n,]
        break
    return sorted(l)

def select(x,y):
    z = []
    for i in range(len(x)):
        if x[i]:
            z = z + [y[i],]
    return z
    
def ok_phases(n):
    """ only try those where the resulting n_ands does not exceed 60000"""
    f = factors(n)
    sp = subproducts(f)
2688
    s = map(lambda m:m*n_ands()< 120000,sp)
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
    z = select(s,sp)
    return z

def subproducts(ll):
    ss = (product(ll),)
    #print ll
    n = len(ll)
    if n == 1:
        return ss
    for i in range(n):
        kk = drop(i,ll)
        #print kk
        ss = ss+(product(kk),)
        #print ss
        ss = ss+subproducts(kk)
        #print ss
    result =tuple(set(ss))
    #result.sort()
    return tuple(sorted(result))

def product(ll):
    n = len(ll)
    p = 1
    if n == 1:
        return ll[0]
    for i in range(n):
        p = p*ll[i]
    return p

def drop(i,ll):
    return ll[:i]+ll[i+1:]

2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
def try_phases():
####    print 'entered try_phases ',
##    ps()
    no = n_pos()
    res = try_phase()
##    print 'after try_phase ',
##    ps()
    N = n_pos()/no
    if N > 1:
        res = True
    else:
        res = False
    return res,N

2735 2736 2737 2738 2739
def try_phase():
    """Tries phase abstraction. ABC returns the maximum clock phase it found using n_phases.
    Then unnrolling is tried up to that phase and the unrolled model is quickly
    simplified (with retiming to see if there is a significant reduction.
    If not, then revert back to original"""
2740
    global init_simp, smp_trace,aigs
2741 2742
    n = n_phases()
##    if ((n == 1) or (n_ands() > 45000) or init_simp == 0):
2743 2744
    if ((n == 1) or (n_ands() > 60000)):
        return False
2745
##    init_simp = 0
2746
    res = a_trim()
2747 2748 2749 2750 2751 2752 2753 2754 2755
    print 'Trying phase abstraction - Max phase = %d'%n,
    abc('w %s_phase_temp.aig'%f_name)
    na = n_ands()
    nl = n_latches()
    ni = n_pis()
    no = n_pos()
    z = ok_phases(n)
    print z,
    if len(z) == 1:
2756
        return False
2757 2758 2759 2760 2761 2762 2763
    #p = choose_phase()
    p = z[1]
    abc('phase -F %d'%p)
    if no == n_pos(): #nothing happened because p is not mod period
        print 'Phase %d is incompatible'%p
        abc('r %s_phase_temp.aig'%f_name)
        if len(z)< 3:
2764
            return False
2765 2766 2767 2768 2769 2770 2771
        else:
            p = z[2]
            #print 'Trying phase = %d:  '%p,
            abc('phase -F %d'%p)
            if no == n_pos(): #nothing happened because p is not mod period
                print 'Phase %d is incompatible'%p
                abc('r %s_phase_temp.aig'%f_name)
2772 2773 2774 2775 2776 2777 2778 2779 2780
                return False
            else:
                smp_trace = smp_trace + ['phase -F %d'%p]
                abc('r %s_phase_temp.aig'%f_name)
                abc('&get;&frames -o -F %d;&scl;&put'%p)
    else:
        abc('r %s_phase_temp.aig'%f_name)
        abc('&get;&frames -o -F %d;&scl;&put'%p)
        smp_trace = smp_trace + ['phase -F %d'%p]
2781
    print 'Simplifying with %d phases: => '%p,
2782 2783 2784
    smp_trace = smp_trace + ['simplify(1)']
    simplify(1)
##    res = a_trim() #maybe we don't need this because rel_cost uses n_real_inputs
2785 2786 2787 2788 2789 2790
    ps()
    cost = rel_cost([ni,nl,na])
    print 'New relative cost = %f'%(cost)
    if cost <  -.01:
        abc('w %s_phase_temp.aig'%f_name)
        if ((n_latches() == 0) or (n_ands() == 0)):
2791
            return True
2792
        if n_phases() == 1: #this bombs out if no latches
2793
            return False
2794
        else:
2795 2796
            result = try_phase()
            return result
2797 2798 2799
    elif len(z)>2: #Try the next eligible phase.
        abc('r %s_phase_temp.aig'%f_name)
        if p == z[2]: #already tried this
2800
            return False
2801 2802 2803 2804 2805
        p = z[2]
        print 'Trying phase = %d: => '%p,
        abc('phase -F %d'%p)
        if no == n_pos(): #nothing happened because p is not mod period
            print 'Phase = %d is not compatible'%p
2806 2807 2808 2809
            return False
        abc('r %s_phase_temp.aig'%f_name)
        abc('&get;&frames -o -F %d;&scl;&put'%p)
        smp_trace = smp_trace + ['phase -F %d'%p]
2810
        print 'Simplify with %d phases: '%p,
2811 2812
        simplify(1)
##        res =a_trim() #maybe we don't need this because rel_cost uses n_real_inputs
2813 2814 2815 2816 2817 2818 2819
        cost = rel_cost([ni,nl,na])
        print 'New relative cost = %f'%(cost)
        if cost < -.01:
            print 'Phase abstraction with %d phases obtained:'%p,
            print_circuit_stats()
            abc('w %s_phase_temp.aig'%f_name)
            if ((n_latches() == 0) or (n_ands() == 0)):
2820
                return True
2821
            if n_phases() == 1: # this bombs out if no latches
2822
                return True
2823
            else:
2824 2825 2826 2827
                result = try_phase()
                return result
        else:
            smp_trace = smp_trace + ['de_phase']
2828
    abc('r %s_phase_temp.aig'%f_name)
2829
    return False
2830 2831

def try_temp(t=15):
2832
    global smp_trace,aigs
2833
    btime = time.clock()
2834 2835 2836 2837 2838
##    res = a_trim() #maybe we don't want this here.
    print'Trying temporal decomposition - for max %0.2f sec. '%(t),
    abc('w %s_best_temp.aig'%f_name)
##    ni = n_pis()
    ni = n_real_inputs()
2839 2840 2841
    nl = n_latches()
    na = n_ands()
    best = [ni,nl,na]
2842 2843 2844 2845 2846
    cost_best = 0
    i_best = 0
    n_done = 0
    print 'best = ',
    print best
2847
    F = create_funcs([18],t) #create a timer function
2848 2849 2850
    F = F + [eval('(pyabc_split.defer(struc_temp)())')]
    F = F + [eval('(pyabc_split.defer(full_temp)())')]
    F = F + [eval('(pyabc_split.defer(two_temp)())')]
2851
    for i,res in pyabc_split.abc_split_all(F):
2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
##        print i,res
        if i == 0:
            break
        if n_latches() == 0:
            return True
        n_done = n_done+1
        cost = rel_cost(best)
        if cost<0:
            nri=n_real_inputs()
            best = (nri,n_latches(),n_ands())
            abc('w %s_best_temp.aig'%f_name)
            i_best = i
            cost_best = cost
            print 'cost = %.2f, best = '%cost,
            print best
##            if i == 1:
##                smp_trace = smp_trace + ['tempor -s']
##            if i == 2:
##                smp_trace = smp_trace + ['tempor']
##        if n_latches == 0:
##            break
        if n_done > 2:
            break
##    cost = rel_cost(best)
    cost = cost_best
.  
Baruch Sterin committed
2877
    print 'cost = %0.2f'%cost
2878 2879 2880
    abc('r %s_best_temp.aig'%f_name)
##    if cost < -.01:
    if cost<0:
2881
        ps()
2882
        return True
2883
    else:
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
##        smp_trace = smp_trace + ['de_tempor']
##        abc('r %s_best_temp.aig'%f_name)
        return False

def struc_temp():
    abc('tempor -s;scr')
    result = quick_simp()
    if result == 'UNSAT':
        return Unsat
    elif result == 'SAT':
        return Sat
    return Undecided

def full_temp():
    abc('tempor')
    return simplify()
2900

2901 2902 2903 2904
def try_temps(t=20):
    """ need to modify something to be able to update cex"""
    global smp_trace
    abc('w %s_try_temps.aig'%f_name)
2905
    best = (n_pis(),n_latches(),n_ands())
2906 2907
    npi = n_pis()
    F = 1
2908
    while True:
2909 2910 2911
        res = try_temp(t)
        ps()
        if n_latches() == 0:
2912
            break
2913 2914 2915
        if res == False:
            return False,F
        if ((best == (n_pis(),n_latches(),n_ands())) or n_ands() > .9 * best[2] ):
2916 2917
            break
        else:
2918
            smp_trace = smp_trace + ['tempor']
2919
            best = (n_pis(),n_latches(),n_ands())
2920
    return True,n_pis()/npi
2921 2922 2923
        
def rel_cost_t(J):
    """ weighted relative costs versus previous stats."""
.  
Baruch Sterin committed
2924
    if (n_latches() == 0 and J[1]>0):
2925 2926 2927 2928 2929 2930 2931 2932 2933
        return -10
    nli = J[0]+J[1]
    na = J[2]
    if ((nli == 0) or (na == 0)):
        return 100
    nri = n_real_inputs()
    #ri = (float(nri)-float(ni))/float(ni)
    rli = (float(n_latches()+nri)-float(nli))/float(nli)
    ra = (float(n_ands())-float(na))/float(na)
2934
    cost = 10*rli + 1*ra #changed from .5 to 1 on ra
2935 2936 2937 2938 2939
    return cost    

def rel_cost(J):
    """ weighted relative costs versus previous stats."""
    global f_name
.  
Baruch Sterin committed
2940
    if (n_latches() == 0 and J[1]>0):
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
        return -10
    nri = n_real_inputs()
    ni = J[0]
    nl = J[1]
    na = J[2]
    if (ni == 0 or na == 0 or nl == 0):
        return 100
    ri = (float(nri)-float(ni))/float(ni)
    rl = (float(n_latches())-float(nl))/float(nl)
    ra = (float(n_ands())-float(na))/float(na)
2951
    cost = 1*ri + 5*rl + .25*ra
2952 2953 2954
##    print 'Relative cost = %0.2f'%cost
    return cost

2955 2956 2957
def best_fwrd_min(J,t=30):
    global f_name, methods,smp_trace
    J=[18]+J
2958
    mtds = sublist(methods,J)
2959
    F = create_funcs(J,t)
2960 2961
    (m,result) = fork_best(F,mtds) #FORK here
    print '%s: '%mtds[m],
2962
    smp_trace = smp_trace + ['%s'%mtds[m]]
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
    
def try_forward():
    """Attempts most forward retiming, and latch correspondence there. If attempt fails to help simplify, then we revert back to the original design
    This can be effective for equivalence checking problems where synthesis used retiming"""
    abc('w %s_savetemp.aig'%f_name)
    if n_ands() < 30000:
        abc('dr')
        abc('lcorr')
        nl = n_latches()
        na = n_ands()
        abc('w %s_savetemp0.aig'%f_name)
        abc('r %s_savetemp.aig'%f_name) 
2975
        abc('dretime -m')
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
        abc('lcorr')
        abc('dr')
        if ((n_latches() <= nl) and (n_ands() < na)):
            print 'Forward retiming reduced size to: ',
            print_circuit_stats()
            return
        else:
            abc('r %s_savetemp0.aig'%f_name)
            return
    return

2987 2988 2989 2990 2991 2992 2993
def qqsimp():
    abc('&get;&scl;,reparam;&scorr -C 0;&scl;,reparam;&put')
    shrink()
    abc('w %ssimp.aig'%f_name)
    ps()


2994 2995 2996 2997
def quick_simp():
    """A few quick ways to simplify a problem before more expensive methods are applied.
    Uses & commands if problem is large. These commands use the new circuit based SAT solver"""
    na = n_ands()
.  
Baruch Sterin committed
2998
    if na < 60000:
2999 3000
        abc('scl -m;lcorr;drw')
    else:
3001 3002 3003
        abc('&get;&scl;&lcorr;&put')
    if n_ands() < 500000:
        abc('drw')
.  
Baruch Sterin committed
3004 3005 3006 3007 3008 3009 3010 3011
    print 'Using quick simplification',
    status = process_status(get_status())
    if status <= Unsat:
        result = RESULT[status]
    else:
        ps()
        result = 'UNDECIDED'
    return result
3012 3013 3014 3015

def scorr_constr():
    """Extracts implicit constraints and uses them in signal correspondence
    Constraints that are found are folded back when done"""
3016
    global aigs
3017 3018 3019 3020 3021
    na = max(1,n_ands())
    n_pos_before = n_pos()
    if ((na > 40000) or n_pos()>1):
        return Undecided_no_reduction
    abc('w %s_savetemp.aig'%f_name)
.  
Baruch Sterin committed
3022 3023
    na = max(1,n_ands())
##    f = 1
3024
    f = 18000/na  #**** THIS can create a bug 10/15/11. see below
.  
Baruch Sterin committed
3025 3026
    f = min(f,4)
    f = max(1,f)
3027
    print 'Looking for constraints - ',
.  
Baruch Sterin committed
3028 3029
    if n_ands() > 18000:
        cmd = 'unfold -s -F 2'
3030
    else:
.  
Baruch Sterin committed
3031
        cmd = 'unfold -F %d -C 5000'%f
3032 3033
    abc(cmd)
    if n_pos() == n_pos_before:
3034
        print 'none found'
3035 3036 3037 3038 3039
        return Undecided_no_reduction
    if (n_ands() > na): #no constraints found
        abc('r %s_savetemp.aig'%f_name)
        return Undecided_no_reduction
    na = max(1,n_ands())
3040 3041
    f = 1 #put here until bug is fixed.
    print 'found %d constraints'%((n_pos() - n_pos_before))
3042 3043
    abc('scorr -c -F %d'%f)
    abc('fold')
3044
    res = a_trim()
.  
Baruch Sterin committed
3045
    print 'Constrained simplification: ',
3046 3047 3048
    ps()
    return Undecided_no_reduction

3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
def a_trim():
    """ this is set up to put the aig on the aigs list if trim was successful"""
##    print 'trimming'
##    print 5.1
    pushed = check_push() #checking if a push is needed and if so do it.
                        #It is not needed if flops match previous aig
##    print 5.2
    res = trim()
##    print 5.3
    if res:
        aigs_pp()
##        aigs.push() #store the aig after rpm if it did something
    elif pushed: #since trim did not do anything, we don't need the last push done by check push
        dump() #dump the last aig on the list
##    print 5.4
    return res

3066 3067
def try_scorr_c(f):
    """ Trying multiple frames because current version has a bug."""
3068
    global aigs
3069 3070 3071 3072 3073 3074 3075 3076 3077
    set_globals()
    abc('unfold -F %d'%f)
    abc('scorr -c -F %d'%f)
    abc('fold')
    t = max(1,.1*G_T)
    abc('&get;,bmc3 -vt=%f'%t)
    if is_sat(): 
        return 0
    else:
3078
        res = a_trim()
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
        return 1
    

def input_x_factor():
    """Sets the global x_factor according to user input"""
    global x_factor, xfi
    print 'Type in x_factor:',
    xfi = x_factor = input()
    print 'x_factor set to %f'%x_factor


3090
def prove(a=0,abs_tried = False):
3091 3092 3093
    """Proves all the outputs together. If ever an abstraction
        was done then if SAT is returned,
        we make RESULT return "undecided".
.  
Baruch Sterin committed
3094 3095 3096 3097
        is a == 0 do smp and abs first
        If a == 1 do smp and spec first 
        if a == 2 do quick simplification instead of full simplification, then abs first, spec second"""
    global x_factor,xfi,f_name, last_verify_time,K_backup, t_init, sec_options, spec_found_cex
3098 3099
    spec_first = False
    max_bmc = -1
.  
Baruch Sterin committed
3100 3101 3102 3103 3104 3105 3106
    abs_found_cex_after_spec = spec_found_cex_after_abs = False
    if not '_smp' in f_name: #if already simplified, then don't do again
        if a == 2 : #do quick simplification
            result = quick_simp() #does not write 'smp' file
##            print result
        else :
            result = prove_part_1() #do full simplification here
3107
        if ((result == 'SAT') or (result == 'UNSAT')):
.  
Baruch Sterin committed
3108
            return result
3109 3110
    if n_latches() == 0:
        return 'UNDECIDED'
.  
Baruch Sterin committed
3111 3112
    if a == 1:
        spec_first = True
3113
    t_init = 2
.  
Baruch Sterin committed
3114 3115 3116 3117
    abs_found_cex_before_spec = spec_found_cex_before_abs = False
##    First phase
    if spec_first:
        result = prove_part_3() #speculation done here first
3118 3119 3120
        if result == 'UNDECIDED' and abs_tried and n_pos() <= 2:
            add_trace('de_speculate')
            return result
3121
    else:
3122
        abs_tried = True
.  
Baruch Sterin committed
3123
        result = prove_part_2() #abstraction done here first
3124
    if ((result == 'SAT') or (result == 'UNSAT')):
.  
Baruch Sterin committed
3125 3126 3127
        return result
##    Second phase
    if spec_first: #did spec already in first phase
3128
        t_init = 2
3129
        abs_tried = True
.  
Baruch Sterin committed
3130 3131 3132 3133 3134 3135
        result = prove_part_2() #abstraction done here second
        if result == 'SAT':
            abs_found_cex_after_spec = True
    else:
        result = prove_part_3()  #speculation done here second
        if result == 'SAT':
3136 3137 3138 3139
            if '_abs' in f_name:
                spec_found_cex_after_abs = True
            else:
                return result
.  
Baruch Sterin committed
3140 3141
    if result == 'UNSAT': 
        return result
3142
    status = get_status()
.  
Baruch Sterin committed
3143 3144 3145 3146
    if result == 'ERROR':
        status = Error
    if ('_abs' in f_name and spec_found_cex_after_abs): #spec file should not have been written in speculate
        f_name = revert(f_name,1) #it should be as if we never did abstraction.
3147
        add_trace('de_abstract')
.  
Baruch Sterin committed
3148 3149 3150 3151 3152 3153 3154
        print 'f_name = %s'%f_name
        abc('r %s.aig'%f_name) #restore previous
        t_init = 2
        if not '_rev' in f_name:
            print 'proving speculation first'
            write_file('rev') #maybe can get by with just changing f_name
            print 'f_name = %s'%f_name
3155
            result = prove(1,True) #1 here means do smp and then spec 
3156
            if ((result == 'SAT') or (result == 'UNSAT')):
.  
Baruch Sterin committed
3157 3158 3159
                return result
    elif ('_spec' in f_name and abs_found_cex_after_spec): #abs file should not have been written in abstract
        f_name = revert(f_name,1) #it should be as if we never did speculation.
3160
        add_trace('de_speculate')
.  
Baruch Sterin committed
3161 3162 3163 3164 3165 3166 3167 3168 3169
        abc('r %s.aig'%f_name) #restore previous 
        t_init = 2
        if not '_rev' in f_name:
            print 'proving abstraction first'
            write_file('rev') #maybe can get by with just changing f_name
            result = prove(0)
            if ((result == 'SAT') or (result == 'UNSAT')):
                return result
    else:
3170 3171
        return 'UNDECIDED'

.  
Baruch Sterin committed
3172
def prove_part_1():
3173
    global x_factor,xfi,f_name, last_verify_time,K_backup,aigs
3174
    print 'Initial: ',
.  
Baruch Sterin committed
3175
    ps()
3176 3177 3178
    x_factor = xfi
    set_globals()
    if n_latches() > 0:
3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
##        ps()
        res = try_frames_2()
        if res:
            print 'frames_2: ',
            ps()
            aigs_pp('push','phase')
        print '\n***Running pre_simp'
        add_trace('pre_simp')
        result = run_par_simplify()
        status = result[0]
        method = result[1]
        if 'scorr' in method:
            add_trace(method)
3192
    else:
3193 3194
        print '\n***Circuit is combinational, running check_sat'
        add_trace('comb_check')
3195 3196 3197
        status = check_sat()
    if ((status <= Unsat) or (n_latches() == 0)):
        return RESULT[status]
3198 3199 3200
    res =a_trim()
    if not '_smp' in f_name:
        write_file('smp') #need to check that this was not written in pre_simp
3201
    set_globals()
.  
Baruch Sterin committed
3202
    return RESULT[status]
3203

.  
Baruch Sterin committed
3204 3205 3206 3207
def run_par_simplify():
    set_globals()
    t = 1000
    funcs = [eval('(pyabc_split.defer(pre_simp)())')]
3208
    J = [35]+pdrs[:3]+bmcs[:3]+intrps[:1]+sims  # 35 is par_scorr
.  
Baruch Sterin committed
3209
    J = modify_methods(J,1)
3210
##    J = J + bestintrps
.  
Baruch Sterin committed
3211 3212
    funcs = create_funcs(J,t)+ funcs #important that pre_simp goes last
    mtds =sublist(methods,J) + ['pre_simp']
3213 3214
    print mtds
    result = fork_last(funcs,mtds)
.  
Baruch Sterin committed
3215
    status = get_status()
3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
    return [status] + [result]

def try_frames_2():
    abc('scl')
    nl = n_latches()
    if n_ands()> 35000:
        return
    abc('w %s_temp.aig'%f_name)
    abc('&get;&frames -o -F 2;&scl;&put')
    if n_latches() < .75*nl:
        print 'frames_2: Number of latches reduced to %d'%n_latches()
        add_trace('frames_2')
##        res = reparam()
##        xxxxx handle this
##        if res:
##            aigs.push()
        return True
    abc('r %s_temp.aig'%f_name)
    return False
.  
Baruch Sterin committed
3235 3236
    
def prove_part_2(ratio=.75):
3237
    """does the abstraction part of prove"""
.  
Baruch Sterin committed
3238
    global x_factor,xfi,f_name, last_verify_time,K_backup, trim_allowed,ifbip
3239
    print'\n***Running abstract'
.  
Baruch Sterin committed
3240 3241 3242
##    print 'ifbip = %d'%ifbip
    status = abstract(ifbip) #ABSTRACTION done here
    status = process_status(status)
3243
    print 'abstract done, status is %s'%str(status)
.  
Baruch Sterin committed
3244
    result = RESULT[status]
3245 3246
    if status < Unsat:
        print 'CEX in frame %d'%cex_frame()
.  
Baruch Sterin committed
3247 3248
        return result #if we found a cex we do not want to trim.
    return result
3249
    
.  
Baruch Sterin committed
3250
def prove_part_3():
3251
    """does the speculation part of prove"""
.  
Baruch Sterin committed
3252
    global x_factor,xfi,f_name, last_verify_time,init_initial_f_name
3253
    global max_bmc, sec_options
3254 3255 3256 3257 3258 3259
##    if ((n_ands() > 36000) and sec_options == ''):
##        sec_options = 'g'
##        print 'sec_options set to "g"'
    print '\n***Running speculate on %s: '%f_name,
    ps()
##    add_trace('speculate')
.  
Baruch Sterin committed
3260 3261
    status = speculate() #SPECULATION done here
    status = process_status(status)
3262
##    print 'speculate done, status is %d'%status
.  
Baruch Sterin committed
3263 3264 3265 3266 3267
    result = RESULT[status]
    if status < Unsat:
        print 'CEX in frame %d'%cex_frame()
        return result
    return result
3268 3269

def prove_all(dir,t):
.  
Baruch Sterin committed
3270 3271 3272
    """Prove all aig files in this directory using super_prove and record the results in results.txt
    Not called from any subroutine
    """
3273 3274 3275 3276 3277 3278
##    t = 1000 #This is the timeoout value
    xtime = time.time()
##    dir = main.list_aig('')
    results = []
    f =open('results_%d.txt'%len(dir), 'w')
    for name in dir:
3279
        read_file_quiet_i(name)
3280 3281 3282 3283 3284 3285 3286 3287
        print '\n         **** %s:'%name,
        ps()
        F = create_funcs([18,6],t) #create timer function as i = 0 Here is the timer
        for i,res in pyabc_split.abc_split_all(F):
            break
        tt = time.time()
        if i == 0:
            res = 'Timeout'
3288
        str = '%s: %s, time = %0.2f'%(name,res,(tt-xtime))
3289 3290 3291 3292 3293
        if res == 'SAT':
            str = str + ', cex_frame = %d'%cex_frame()
        str = str +'\n'
        f.write(str)
        f.flush()
3294
        results = results + ['%s: %s, time = %0.2f'%(name,res,(tt-xtime))]
3295 3296 3297
        xtime = tt
##    print results
    f.close()
.  
Baruch Sterin committed
3298
    return results  
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317

def remove_pos(lst):
    """Takes a list of pairs where the first part of a pair is the PO number and
    the second is the result 1 = disproved, 2 = proved, 3 = unresolved. Then removes
    the proved and disproved outputs and returns the aig with the unresolved
    outputs"""
    proved = disproved = unresolved = []
    for j in range(len(lst)):
        jj = lst[j]
        if jj[1] == 2:
            proved = proved + [jj[0]]
        if (jj[1] == 1 or (jj[1] == 0)):
            disproved = disproved +[jj[0]]
        if jj[1] > 2:
            unresolved = unresolved +[jj[0]]
    print '%d outputs proved'%len(proved)
    if not proved == []:
        if ((max(proved)>n_pos()-1) or min(proved)< 0):
            print proved
3318
        remove(proved,0)
3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
            

#functions for proving multiple outputs in parallel
#__________________________________________________

def prove_only(j):
    """ extract the jth output and try to prove it"""
    global max_bmc, init_initial_f_name, initial_f_name, f_name,x
    #abc('w %s__xsavetemp.aig'%f_name)
    extract(j,j)
    set_globals()
    ps()
    print '\nProving output %d'%(j)
    f_name = f_name + '_%d'%j
    result = prove_1()
    #abc('r %s__xsavetemp.aig'%f_name)
    if result == 'UNSAT':
        print '********  PROVED OUTPUT %d  ******** '%(j)
        return Unsat
    if result == 'SAT':
        print '********  DISPROVED OUTPUT %d  ******** '%(j)
        return Sat
    else:
        print '********  UNDECIDED on OUTPUT %d  ******** '%(j)
        return Undecided

def verify_only(j,t):
    """ extract the jth output and try to prove it"""
    global max_bmc, init_initial_f_name, initial_f_name, f_name,x, reachs, last_cex, last_winner, methods
##    ps()
##    print 'Output = %d'%j
    extract(j,j)
##    ps()
    set_globals()
    if n_latches() == 0:
        result = check_sat()
    else:
        f_name = f_name + '_%d'%j
        # make it so that jabc is not used here
        reachs_old = reachs
        reachs = reachs[1:] #just remove jabc from this.
        res = verify(slps+sims+pdrs+bmcs+intrps,t) #keep the number running at the same time as small as possible.
##        res = verify(sims+pdrs+bmcs+intrps,t) #keep the number running at the same time as small as possible.
        reachs = reachs_old
        result = get_status()
        assert res == result,'result: %d, status: %d'%(res,get_status())
    if result > Unsat:
##        print result
##        print '******* %d is undecided ***********'%j
        return result
    elif result == Unsat:
##        print '******** PROVED OUTPUT %d  ******** '%(j)
        return result
    elif ((result < Unsat) and (not result == None)):
        print '******** %s DISPROVED OUTPUT %d  ******** '%(last_cex,j)
##        print ('writing %d.status'%j), result, get_status()
        abc('write_status %d.status'%j)
        last_winner = last_cex
        return result
    else:
        print '****** %d result is %d'%(j,result) 
        return result

def verify_range(j,k,t):
    """ extract the jth thru kth output and try to prove their OR"""
    global max_bmc, init_initial_f_name, initial_f_name, f_name,x, reachs, last_cex, last_winner, methods
    extract(j,k)
    abc('orpos')
    set_globals()
    if n_latches() == 0:
        result = check_sat()
    else:
        f_name = f_name + '_%d'%j
        # make it so that jabc is not used here
        reachs_old = reachs
        reachs = reachs[1:] #just remove jabc from this.
        res = verify(sims+pdrs+bmcs+intrps,t) #keep the number running at the sme time as small as possible.
        reachs = reachs_old
        result = get_status()
        assert res == result,'result: %d, status: %d'%(res,get_status())
    if result > Unsat:
##        print result
##        print '******* %d is undecided ***********'%j
        return result
    elif result == Unsat:
##        print '******** PROVED OUTPUT %d  ******** '%(j)
        return result
    elif ((result < Unsat) and (not result == None)):
        print '******** %s DISPROVED OUTPUT %d  ******** '%(last_cex,j)
##        print ('writing %d.status'%j), result, get_status()
        abc('write_status %d.status'%j)
        last_winner = last_cex
        return result
    else:
        print '****** %d result is %d'%(j,result) 
        return result

def prove_n_par(n,j):
    """prove n outputs in parallel starting at j"""
    F = []
    for i in range(n):
        F = F + [eval('(pyabc_split.defer(prove_only)(%s))'%(j+i))]
    #print S
    #F = eval(S)
    result = []
    print 'Proving outputs %d thru %d in parallel'%(j,j+n-1)
    for i,res in pyabc_split.abc_split_all(F):
        result = result +[(j+i,res)]
    #print result
    return result

def prove_pos_par(t,BREAK):
    """Prove all outputs in parallel and break on BREAK"""
    return run_parallel([],t,BREAK)

def prove_pos_par0(n):
    """ Group n POs grouped and prove in parallel until all outputs have been proved"""
    f_name = initial_f_name
    abc('w %s__xsavetemp.aig'%f_name)
    result = []
    j = 0
    N = n_pos()
    while j < N-n:
        abc('r %s__xsavetemp.aig'%f_name)
        result = result + prove_n_par(n,j)
        j = j+n
    if N > j:
        result = result + prove_n_par(N-j,j)
    abc('r %s__xsavetemp.aig'%initial_f_name)
    ps()
.  
Baruch Sterin committed
3449
##    print result
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
    remove_pos(result)
    write_file('group')
    return

def prop_decomp():
    """decompose a single property into multiple ones (only for initial single output),
    by finding single and double literal primes of the outputs."""
    if n_pos()>1:
        return
    run_command('outdec -v -L 2')
    if n_pos()>1:
        ps()


def distribute(N,div):
    """
    we are going to verify outputs in groups
    """
    n = N/div
    rem = N - (div * (N/div))
    result = []
    for j in range(div):
        if rem >0:
            result = result +[n+1]
            rem = rem -1
        else:
            result = result + [n]
    return result    

        
def extract(n1,n2):
    """Extracts outputs n1 through n2"""
    no = n_pos()
    if n2 > no:
        return 'range exceeds number of POs'
    abc('cone -s -O %d -R %d'%(n1, 1+n2-n1))

def remove_intrps(J):
.  
Baruch Sterin committed
3488
    global n_proc,ifbip
3489
##    print J
.  
Baruch Sterin committed
3490 3491 3492 3493
    npr = n_proc
    if 18 in J:
        npr = npr+1
    if len(J) <= npr:
3494
##        print J
.  
Baruch Sterin committed
3495
        return J
3496
    JJ = []
.  
Baruch Sterin committed
3497 3498
    alli = [23,1,22] # if_no_bip, then this might need to be changed
    l = len(J)-npr
3499 3500
    alli = alli[l:]
##    J.reverse() #strip off in reverse order.
3501
    for i in J:
3502
        if i in alli: 
3503 3504
            continue
        else:
3505 3506
            JJ = JJ+[i]
##    print JJ
3507
    return JJ
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517

def restrict(lst,v=0):
    '''restricts the aig to the POs in the list'''
    #this assumes that there are no const-1 POs. Warning, this will not remove any const-0 POs
    N = n_pos()
    lst1 = lst + [N]
    r_lst = gaps(lst1) #finds POs not in lst
    if len(r_lst) == N:
        return
    remove(r_lst,v)
3518
        
3519 3520 3521 3522
def remove(lst,v=0):
    """Removes outputs in list
    WARNING will not remove all POs even if in lst
    """
3523 3524
    global po_map
    n_before = n_pos()
3525 3526 3527 3528
    zero(lst,v)
    l=remove_const_pos(v)
    assert len(lst) == (n_before - n_pos()),'Inadvertantly removed some const-0 POs.\nPO_before = %d, n_removed = %d, PO_after = %d'%(n_before, len(lst), n_pos())
    print 'PO_before = %d, n_removed = %d, PO_after = %d'%(n_before, len(lst), n_pos())
.  
Baruch Sterin committed
3529

3530

3531
def zero(list,v=0):
3532
    """Zeros out POs in list"""
3533 3534 3535 3536
    if v == 0:
        cmd = 'zeropo -s -N ' #puts const-0 in PO
    else:
        cmd = 'zeropo -so -N ' #puts const-1 in PO
3537
    for j in list:
3538 3539
        run_command('%s%d'%(cmd,j)) #-s prevents the strash after every zeropo
    abc('st')
3540

3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556
def listr_0_pos():
    """ returns a list of const-0 pos and removes them
    """  
    L = range(n_pos())
    L.reverse()
    ll = []
    for j in L:
        i = is_const_po(j)
        if i == 0:
            abc('removepo -N %d'%j) #removes const-0 output
##            print 'removed PO %d'%j
            ll = [j] + ll
    return ll

def list_0_pos():
    """ returns a list of const-0 pos and removes them
.  
Baruch Sterin committed
3557
    """
3558
    abc('w %s_savetemp.aig'%f_name)
.  
Baruch Sterin committed
3559 3560
    L = range(n_pos())
    L.reverse()
3561
    ll = []
.  
Baruch Sterin committed
3562
    for j in L:
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576
        i = is_const_po(j)
        if i == 0:
            abc('removepo -N %d'%j) #removes const-0 output
##            print 'removed PO %d'%j
            ll = [j] + ll
    abc('r %s_savetemp.aig'%f_name)
    return ll

def listr_1_pos():
    """ returns a list of const-1 pos and removes them
    """  
    L = range(n_pos())
    L.reverse()
    ll = []
.  
Baruch Sterin committed
3577
    for j in L:
3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
        i = is_const_po(j)
        if i == 1:
            abc('removepo -z -N %d'%j) #removes const-1 output
##            print n_pos()
            ll = [j] + ll
    return ll


def mark_const_pos(ll=[]):
    """ creates an indicator of which PO are const-0 and which are const-1
        does not change number of POs
    """
    n=n_pos()
    L = range(n)
    ll = [-1]*n
    for j in L:
        ll[j] = is_const_po(j)
.  
Baruch Sterin committed
3595 3596 3597
    print sumsize(ind)
    return ind

3598
def remove_const_pos(n=-1):
3599
    global po_map
3600 3601
    """removes the const 0  or 1 pos according to n, but no pis because we might
    get cexs and need the correct number of pis
3602
    """
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
    if n > -1:
        run_command('&get; &trim -i -V %d; &put'%n) #V is 0 or 1
    else:
        run_command('&get; &trim -i; &put') #removes both constants

def unmap_cex():
    """ aig before trim is put in reg-space and after trim in the &space
        Before and after need to have same number of flops in order o reconcile
        aigs list should be such that if before and after don't match in number of latches,
        then some operation changed the flops and we just update the aig with the new number
        reconcile leaves before aig in reg-space after cex has been updated so cex and aig
        always match
    """
    global aigs,hist
    print hist
##    while not aigs == []:
    while not len(hist) == 0:
        n = n_latches()
        abc('&get') #save the current aig in &-space
        print 'Number of PIs in cex = %d'%n_cex_pis()
        typ = aigs_pp('pop')
        print typ,
        ps()
        if typ == 'phase':
            typ2 =aigs_pp('pop') #gets the aig before phase
            abc('phase -c')
            print 'Number of PIs in cex = %d, Number of frames = %d'%(n_cex_pis(),cex_frame())
            run_command('testcex -a')
            hist = hist + [typ2]
            continue
        if typ == 'tempor':
            typ2 = aigs_pp('pop') #gets the aig before tempor
            abc('tempor -c')
            print 'Number of PIs in cex = %d, Number of frames = %d'%(n_cex_pis(),cex_frame())
            run_command('testcex -a')
            hist = hist + [typ2]
            continue
        if typ == 'reparam':
            nn = n_latches()
            abc('&get') #put 'after' in &space
            typ2 = aigs_pp('pop') #get previous to put before in reg-space
            run_command('reconcile')
            print 'Number of PIs in cex = %d'%n_cex_pis()
##                reconcile(True) #maps the cex from &-space aig to current aig
            run_command('testcex -a')
            if not typ2 == 'reparam0':
                hist = hist + [typ2] #put back (aig iss still there so just have to restore hist
            continue
            #else we just leave the aig updated
        else:
            assert typ == 'initial','type is not initial'
            size = str([n_pis(),n_pos(),n_latches(),n_ands()])
            print 'cex length = %d'%cex_frame()
            tr = ['cex length = %d'%cex_frame()] + ['cex matches original aig = %s'%size]
            print 'cex matches original aig'
            return tr
##    print 'cex matches original aig'
    
                                  
3662
def sp(n=0,t=900,check_trace=False):
3663
    """Alias for super_prove, but also resolves cex to initial aig"""
.  
Baruch Sterin committed
3664
    global initial_f_name
3665
    print 'Executing super_prove'
3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
    result = super_prove(n,t)
    print '%s is done and is %s'%(initial_f_name,result[0])
    print 'sp: ',
    print result
    if result[0] == 'SAT' and check_trace:
        res = unmap_cex()
        result1 = result[1]+ res
        result = ['SAT'] + result1
        report_cex()
    report_bmc_depth(max(max_bmc,n_bmc_frames()))
3676 3677
    return result

3678 3679 3680 3681 3682
def mp():
    multi_prove_iter()

def report_cex():
    abc('write_status %s_cex.status'%init_initial_f_name)
3683

.  
Baruch Sterin committed
3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694
def sumsize(L):
    d = count_less(L,0)
    u = count_less(L,1)-d
    s = count_less(L,2) - (d+u)
    return 'SAT = %d, UNSAT = %d, UNDECIDED = %d'%(s,u,d)

def unmap(L,L2,map):
    mx = max(list(map))
    assert mx <= len(L2),'max of map = %d, length of L2 = %d'%(mx,len(L))
    for j in range(len(map)):
        L[j] = L2[map[j]] #expand results of L2 into L
3695 3696 3697 3698 3699 3700 3701 3702
    return L

def unmap2(L2,map):
    mx = max(list(map))
    assert mx <= len(L2),'max of map = %d, length of L2 = %d'%(mx,len(L))
    L=[-1]*len(map)
    for j in range(len(map)):
        L[j] = L2[map[j]] #expand results of L2 into L
.  
Baruch Sterin committed
3703 3704 3705
    return L 

def create_map(L,N):
3706 3707 3708 3709 3710
    """ map equivalence classes into their representative."""
##    print L
    mapp = [-1]*N
    m = -1
    error = False
.  
Baruch Sterin committed
3711 3712 3713
    for j in range(len(L)):
        lj = L[j]
        for k in range(len(lj)):
3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754
            mapp[lj[k]] = j
##        print lj
        mm = min(lj)
##        print mm
        if not mm == lj[0]: #check if rep is not first on list
            print 'ERROR: rep is not first, mm = %d, lj[0] = %d'%(mm,lj[0])
            error = True
        if mm <= m: #check if iso_classes are in increasing order of representatives.
            print 'ERROR: in iso map mm < m, mm = %d, m = %d'%(mm,m)
            error = True
        m = mm
    assert not error,'ERROR'
    return mapp


def weave(L1,lst0,lst1):
    """ interleave values of L1 and with 1's in positions given in lst1,
        and 0's in lst0. It is assumed that these lists are in num order..
        Final list has len = len(L1)+len(lst0)+len(lst1)"""
    L = [-1]*(len(L1)+len(lst0)+len(lst1))
##    print len(L)
    if lst0 == []:
        if lst1 == []:
            return L1
        lst = lst1
        v = 1
    if lst1 == []:
        lst = lst0
        v = 0
    l = k = 0
    for j in range(len(L)):
##        print L
        if j == lst[l]:
            L[j] = v
            if l+1 < len(lst):
                l = l+1
        else: #put in value in L1
            L[j] = L1[k]
            if k+1 < len(L1):
                k = k+1
    return L
.  
Baruch Sterin committed
3755

3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799
def quick_mp(t):
    t1 =time.time()
    l1 = list_0_pos()
    S,l2,s = par_multi_sat(t)
    l2 = indices(s,1)
    remove(l2,1)
    abc('scl')
    simple()
    ps()
    print'time = %0.2f'%(time.time() - t1)

def indices(s,v):
    """return in indices of s that have value v"""
    L = []
    for i in range(len(s)):
        if s[i]== v:
            L = L+[i]
    return L

def expand(s,ind,v):
    """expand s by inserting value v at indices ind"""
    N = len(s)+len(ind)
    ind1 = ind+[N]
    g = gaps(ind1)
    ss = [-1]*N
    for i in ind:
        ss[i] = v
    j = 0
    for i in g: #put original values in ss
        ss[i] = s[j]
        j = j+1
    for j in ind:
        assert ss[j] == v, 'ss = %s, ind = %s'%(str(ss),str(ind))
    return ss

def remove_v(ss,v):
    s = []
    for i in range(len(ss)):
        if ss[i] == v:
            continue
        else:
            s = s + [ss[i]]
    return s
                 
3800
def multi_prove(op='simple',tt=900,n_fast=0, final_map=[]):
.  
Baruch Sterin committed
3801 3802
    """two phase prove process for multiple output functions"""
    global max_bmc, init_initial_f_name, initial_f_name,win_list, last_verify_time
3803
    global f_name_save,nam_save,_L_last,file_out
.  
Baruch Sterin committed
3804
    x_init = time.time()
3805 3806 3807 3808 3809 3810 3811
    abc('&get;&scl;,reparam -aig=%s_rpm.aig; r %s_rpm.aig')
    print 'Initial after &scl and reparam = ',
    ps()
    abc('w %s_initial_save.aig'%init_initial_f_name)
    #handle single output case differently
    _L_last = [-1]*n_pos()
    if n_pos() == 1:
3812
        result = sp(2000)
3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836
##        abc('w %s_unsolved.aig'%init_initial_f_name)
        rs=result[0]
        if rs == 'SAT':
            report_result(0,1)
            L = [1]
        elif rs == 'UNSAT':
            report_result(0,0)
            L = [0]
        elif rs == 'UNDECIDED':
            report_result(0,-1)
            L = [-1]
        else: #error
            L = [2]
        res = sumsize(L)
        rr = '\n@@@@ Time =  %d '%(time.time() - t_iter_start) + res
        print rr
        file_out.write(rr+ '\n')
        file_out.flush()
        return L
##    print 'Removing const-0 POs'
    NNN = n_pos()
    lst0 = listr_0_pos() #remove const-0 POs and record
##    print lst0
    lst0.sort()
.  
Baruch Sterin committed
3837 3838
    N = n_pos()
    L = [-1]*N
3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860
    print 'Removed %d const-0 POs'%len(lst0)
    res = 'SAT = 0, UNSAT = %d, UNDECIDED = %d'%(len(lst0),N)
    rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
    report_L(lst0,0) ##########
    rr = rr + res
    print rr
    file_out.write(rr + '\n')
    file_out.flush()
    ttt = n_ands()/1000
    if ttt < 10:
        ttt=10
    elif ttt<20:
        ttt = 20
    elif ttt< 30:
        ttt = 30
    else:
        ttt = 50
    S,lst1,s = par_multi_sat(ttt,1,1,1) #run engines in parallel looking for SAT outputs
    lst1 = indices(s,1)
##    print S,lst1
    #put 0 values into lst0
    lst10 = indices(s,0) #new unsat POs in local indices (with lst0 removed)
3861 3862
    if not lst10 == []:
        print 'lst10 = %s'%str(lst10)
3863 3864 3865 3866 3867
    lst11 = indices(s,1) #local variables
    ss = expand(s,lst0,0) #ss will reflect original indices
    report_s(ss)
    lst0_old = lst0
    lst0 = indices(ss,0) #additional unsat POs added to initial lst0 (in original indices)
3868
    print 'lst0 = %s'%str(lst0)
3869 3870 3871 3872 3873 3874 3875 3876
    assert len(lst0) == len(lst0_old)+len(lst10), 'lst0 = %s, lst0_old = %s, lst10 = %s'%(str(lst0),str(lst0_old),str(lst10))
    sss = remove_v(ss,0) #remove the 0's from ss
    assert len(sss) == len(ss)-len(lst0), 'len(sss) = %d, len(ss) = %d, len(lst0) = %d'%(len(sss),len(ss),len(lst0))
    lst1_1 = indices(sss,1) #The sats now reflect the new local indices.
                #It makes it as if the newly found unsat POs were removed initially
                #done with new code
    assert len(lst1_1) == len(lst1), 'mismatch, lst1 = %d, lst1_1 = %d'%(len(lst1),len(lst1_1))
    lst1 = lst1_1 #lst1 should be in original minus lst0
3877 3878
    print 'Found %d SAT POs'%len(lst1)
    print 'Found %d UNSAT POs'%len(lst10)
3879 3880 3881 3882 3883 3884 3885
    res = 'SAT = %d, UNSAT = %d, UNDECIDED = %d'%(len(lst1),len(lst0),NNN-(len(lst1)+len(lst0)))
    rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
    rr = rr + res
    print rr
    file_out.write(rr + '\n')
    file_out.flush()
    N = n_pos()
3886
    print len(lst10),n_pos()
3887 3888 3889 3890 3891 3892
    if not len(lst10) == n_pos() and len(lst10) > 0:
        remove(lst10,1) #remove 0 POs
        print 'Removed %d UNSAT POs'%len(lst10)
        N = n_pos()
    elif len(lst10) == n_pos():
        N = 0 #can't remove all POs. Must proceed as if there are no POs. But all POs are UNSAT.
3893 3894
    print len(lst1),N,S #note: have not removed the lst1 POs.
    if len(lst1) == N or S == 'UNSAT' or N == 0: #all POs are SAT
3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913
        L = [0]*N #could just put L as all 1's. If N = 0, all POs are UNSAT and lst1 = []
        for i in range(len(lst1)): #put 1's in SAT POs
            L[lst1[i]]=1
        L = weave(L,lst0,[]) #expand L, and put back 0 in L. 
        report_results(L)
        print sumsize(L)
        print 'Time = %.2f'%(time.time() - x_init) 
        return L
##    print 'removing them'
    if not len(lst1)== n_pos():
        remove(lst1,1) #here we removed all POs in lst1 (local indices)
        abc('&get;&scl;&put')
    ##    lst1 = bmcj_ss_r(2) #find easy cex outputs
    ##    write_file('bmc1')
        print 'Removed %d SAT POs'%len(lst1)
        N = n_pos()
    else:
        N = 0
    if N == 1: #this was wrong. Need to report in original indices???
3914
        result = sp(2000)
3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947
        rs=result[0]
        #need to find out original index of remaining PO
        if rs == 'SAT':
            v = 1
        elif rs == 'UNSAT':
            v = 0
        elif rs == 'UNDECIDED':
            v = -1
        else: #error should not happen, but be conservative
            v = -1
        L = [v]*N
        L = weave(list(L),[],lst1) #put back 1 in L
        L = weave(list(L),lst0,[]) #put back 0 in L
        report_results(L)
        res = sumsize(L)
        rr = '\n@@@@ Time =  %d '%(time.time() - t_iter_start) + res
        print rr
        file_out.write(rr+ '\n')
        file_out.flush()
        return L
    L1 =L = [-1]*N
    if N > 1 and N < 10000 and n_ands() < 500000: #keeps iso in
##    if N > 1 and N < 10000 and False: #temporarily disable iso
        print 'Mapping for first isomorphism: '
        res = iso() #reduces number of POs
        if res == True:
            abc('&get;&scl;&put')
            write_file('iso1')
            leq = eq_classes()
##        print leq
            map1 = create_map(leq,N) #creates map into original
##        print map1
            if not count_less(L,0) == N:
3948
                print L
3949 3950 3951 3952 3953 3954
            L1 = [-1]*n_pos()
##        L1 = pass_down(L,list(L1),map1) # no need to pass down because L will have no values at this point.
        else:
            map1 =range(N)
    else:
        map1 = range(N)
.  
Baruch Sterin committed
3955
    N = n_pos()
3956
##    print 4
.  
Baruch Sterin committed
3957
    r = pre_simp() #pre_simp
3958 3959 3960 3961
    write_file('smp1')
    NP = n_pos()/N #if NP > 1 then NP unrollings were done in pre_simp.
    if NP > 1:
        L1 = duplicate_values(L1,NP) # L1 has only -1s here. Put in same valuess for iso POs
.  
Baruch Sterin committed
3962 3963 3964 3965
    if n_pos() > N:
        assert NP>=2, 'NP not 2, n_pos = %d, N = %d, NP = %d'%(n_pos(),N,NP)
    print 'pre_simp done. NP = %d\n\n'%NP
    #WARNING: if phase abstraction done, then number of POs changed.
3966 3967 3968 3969 3970 3971 3972 3973 3974
    if r[0] == Unsat:
        print 'example is UNSAT'
        L1 = [0]*N #all outputs are UNSAT
        print sumsize(L1)
        print 'unmapping for iso'
        L = unmap(list(L),L1,map1)
        print "putting in easy cex's and easy unsat's"
        L = weave(list(L),[],lst1) #put back 1 in L
        L = weave(list(L),lst0,[]) #put back 0 in L
.  
Baruch Sterin committed
3975
        print sumsize(L)
3976 3977
        print 'Time = %.2f'%(time.time() - x_init)
        report_results(L)
.  
Baruch Sterin committed
3978 3979
        return L
    f_name_save = f_name
3980
    nam_save = '%s_initial_save.aig'%f_name
.  
Baruch Sterin committed
3981 3982
    #########do second iso here
    N = n_pos()
3983 3984 3985 3986 3987 3988
    if N == 1:
        map2 = [0]
        L2=[-1]
##        write_file('1')
##        L = output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP)
        L = output2(list(L2),map1,map2,lst0,lst1,NP)
3989
        result = simple(2000,1)
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032
        Ss = rs = result[0]
        if rs == 'SAT':
            L2 = [1]
        if rs == 'UNSAT':
            L2 = [0]
    else:
##        if False and N < 10000: #temp disable iso
        if N < 10000 and n_ands() < 500000: 
            print 'Mapping for second isomorphism: '
            res = iso() #second iso - changes number of POs
            if res == True:
                abc('&get;&scl;&put')
                map2 = create_map(eq_classes(),N) #creates map into previous
            else:
                map2 = range(n_pos())
        else:
            map2 = range(n_pos())
        write_file('iso2')
        print 'entering par_multi_sat'
        S,lbmc,s = par_multi_sat(2*ttt,1,1,1) #look for SAT POs
        lmbc = indices(s,1)
        print 'par_multi_sat ended'
        if len(lmbc)>0:
            print 'found %d SAT POs'%len(lmbc)
        L2 = s
##        #first mprove for 10-20 sec.
        ps()
        print 'Before first mprove2, L2 = %s'%sumsize(L2)
        DL = output2(list(L2),map1,map2,lst0,lst1,NP) #reporting intermediate results
##        DDL = output3(range(len(L2)),map1,map2,lst0,lst1,NP)
##        print 'DDL = %s'%str(DDL)
        if n_fast == 1:
            abc('w %s_unsolved.aig'%init_initial_f_name)
            return DL
        NN=n_ands()
        #create timeout time for first mprove2
        ttt = 10
        if NN >30000:
            ttt = 15
        if NN > 50000:
            ttt = 20
        abc('w %s_before_mprove2.aig'%f_name)
        print '%s_before_mprove2.aig written'%f_name
4033
        print 'L2 = %s'%str(L2)
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
        print 'Entering first mprove2 for %d sec.'%ttt,
        Ss,L2 = mprove2(list(L2),op,ttt,1) #populates L2 with results
##        print Ss,L2
        if Ss == 'SAT':
            print 'At least one PO is SAT'
        if Ss == 'ALL_SOLVED':
            if count_less(L2,0)>0:
                print 'ERROR'
##            L = output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP) # final report of results.
            L = output2(list(L2),map1,map2,lst0,lst1,NP)
            return L
        print 'After first mprove2: %s'%sumsize(L2)
.  
Baruch Sterin committed
4046 4047
    time_left = tt - (time.time()-x_init)
    N = count_less(L2,0)
4048 4049 4050
    if N > 0 and n_fast == 0:
##        output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP) #reporting new intermediate results
        L = output2(list(L2),map1,map2,lst0,lst1,NP)
.  
Baruch Sterin committed
4051
        t = max(100,time_left/N)
4052
        t_all = 100
.  
Baruch Sterin committed
4053 4054 4055
    S = sumsize(L2)
    T = '%.2f'%(time.time() - x_init)
    print '%s in time = %s'%(S,T)
4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106
    abc('w %s_unsolved.aig'%init_initial_f_name)
    N = n_pos()
    ttime = 100
    J = slps+intrps+pdrs+bmcs+sims
    #do each output for ttime sec.
    Nn = count_less(L2,0)
##    assert N == len(L2),'n_pos() = %d, len(L2) = %d'%(N,len(L2))
    if Nn > 0:
        found_sat = 0
        print 'final_all = %d, Ss = %s'%(final_all,str(Ss))
        if final_all and not Ss == 'SAT':
            print 'Trying to prove all %d remaining POs at once with super_prove'%Nn
            remove_proved_pos(L2)
            result = super_prove()
            if result[0] == 'UNSAT': #all remaining POs are UNSAT
                for i in range(len(L2)):
                    if L2[i] < 0:
                        L2[i] = 0
##                L = output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP) # final report of results. 
                L = output2(list(L2),map1,map2,lst0,lst1,NP)
                return L
            if result == 'SAT':
                found_sat = 1
        if found_sat or not final_all or Ss == 'SAT':
            print 'Trying each remaining PO for %d sec.'%ttime
            found_sat = 0
##            ttime = 10
            for i in range(N):
                if L2[i] > -1:
                    continue
                print '\n**** cone %d ****'%i
                abc('r %s_unsolved.aig'%init_initial_f_name)
                abc('cone -s -O %d'%i)
                abc('&get;&scl;&lcorr;&put')
                result = verify(J,ttime)
                r = result[0]
                if r > 2:
                    continue
                elif r == 2:
                    L2[i] = 0
                else:
                    L2[i] = 1
                    found_sat = 1
##                output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP)
                L = output2(list(L2),map1,map2,lst0,lst1,NP)
            if Ss == 'SAT' and found_sat: #previous solve_all was SAT and found at least 1 PO SAT
                abc('r %s_unsolved.aig'%init_initial_f_name)
                if not count_less(L2,0) == 0:
                    remove_proved_pos(L2)
                    simplify()
                    write_file('save')
4107
                    result = simple(2000,1)
4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341
                    if_found = False
                    if result[0] == 'UNSAT':
                        for i in range(N):
                            if L2[i] == -1:
                                L2[i] = 0
                    elif result[0] == 'SAT' and n_pos() == 1:
                        for i in range(N):
                            if L2[i] == -1:
                                if if_found == True:
                                    print 'Error: more that 1 UNDECIDED remained in L2'
                                    break
                                L2[i] = 1
                                if_found = True
                    else:
                        if result[0] == 'SAT':
                            print 'at least 1 unsolved PO is SAT'
##    L = output(list(L),list(L1),L2,map1,map2,lst0,lst1,NP) # final report of results.
    L = output2(list(L2),map1,map2,lst0,lst1,NP)
    return L

def create_unsolved(L):
    abc('r %s_initial_save.aig'%init_initial_f_name) 
    lst = []
    assert len(L) == n_pos(),'lengths of L and n_pos = %d,%d'%(len(L),n_pos())
    for i in range(len(L)):
        if L[i] > -1: #solved PO
            lst = lst + [i]
    assert max(lst) < n_pos(), 'error in lengths'
    assert count_less(L,0) == n_pos() - len(lst),'mismatch'
    remove(lst,-1) # remove solved
    
def multi_prove_iter():
    global t_iter_start,file_out,ff_name
    ff_name = init_initial_f_name
    file_out = open('%s_time_results.txt'%init_initial_f_name, 'w') #
    t_iter_start = time.time()
    L = multi_prove()
    d = count_less(L,0)
    u = count_less(L,1)-d
    s = count_less(L,2) - (d+u)
    rr =  '\n@@@@@ %s: Final time =  %.2f '%(init_initial_f_name,(time.time() - t_iter_start))
    rr = rr + 'SAT = %d, UNSAT = %d, UNDECIDED = %d '%(s,u,d)
    print rr
    file_out.write(rr)
    res = PO_results(L)
    file_out.write(res)
##    print res
    file_out.flush()
    file_out.close()
    #at this point could restrict to SAT(UNSAT) POs and invoke solver to verify all POs are SAT(UNSAT)
    return

def restrict_v(L,v):
    """ L is a list of 0,1,-1"""
    lst = []
    for j in range(len(L)):
        if L[j] == v:
            lst = lst + [j]
    restrict(lst)
    return lst

def PO_results(L):
    global ff_name
    S=U=UD=[]
    for j in range(len(L)):
        ll = L[j]
        if ll == -1:
            UD = UD + [j]
        elif ll == 0:
            U = U + [j]
        elif ll == 1:
            S = S + [j]
        else:
            print 'error, L contains a non -1,0,1'
    res = "[[SAT = %s], [UNSAT = %s], [UNDECIDED = %s]"%(str(S),str(U),str(UD))
    #restore initial unsolved POs
    abc('r %s.aig'%ff_name)
    if not UD == []:
        restrict(UD,0)
        abc('w %s_UNSOLVED.aig'%ff_name)
        print 'Unsolved POs restored as %s_UNSOLVED.aig'%ff_name
    else:
        print 'All POs were solved'
    abc('r %s.aig'%ff_name) #what if original had constraints.
    abc('fold')
    if not U == []: 
        restrict(U,1) #we use 1 here because do not want to remove const-0 POs which should be in U
        abc('w %s_UNSAT.aig'%ff_name)
        print 'Unsat POs restored as %s_UNSAT.aig'%ff_name
    abc('r %s.aig'%ff_name)
    abc('fold')
    if not S == []:
        restrict(S,0)
        abc('w %s_SAT.aig'%ff_name)
        print 'Sat POs restored as %s_SAT.aig'%ff_name
    return res

def syn3():
    t = time.clock()
    run_command('&get;&b; &jf -K 6; &b; &jf -K 4; &b;&put')
    ps()
    print 'time = %.2f'%(time.clock() - t)

def syn4():
    t = time.clock()
    abc('&get;&b; &jf -K 7; &fx; &b; &jf -K 5; &fx; &b;&put')
    ps()
    print 'time = %.2f'%(time.clock() - t)


def solve_parts(n):
    global t_iter_start,file_out
    r=range(n)
    r.reverse()
    name = init_initial_f_name
    results = []
    for i in r:
        file_out.write('\n@@@@ Starting part%d: \n'%i)
        file_out.flush()
        abc('r %s_part%d.aig'%(name,i))
        print '\nPart%d: '%i
        L = multi_prove()
        rr =  '\n@@@@ Time =  %.2f '%(time.time() - t_iter_start)
        rr = rr + 'Part%d: '%i
        ssl = sumsize(L)
        rr = rr + ssl
        results = results + [[ssl]]
        print rr
        file_out.write(rr + '\n')
        file_out.flush()
    return results

def cp(n=10):
    return chop_prove(n)

def chop_prove(n=10,t=100):
    global t_iter_start,file_out
    tm = time.time()
    abc('w %s_chop_temp.aig'%f_name)
    N = max(5,n_pos()/n)
    J = 0
    total = []
    np = n_pos()
    while J < np:
        abc('r %s_chop_temp.aig'%f_name)
        E = J+N-1
        R = N
        if E > np-1:
            R = N - (E - (np -1))
        abc('cone -s -O %d -R %d'%(J,R))
        npp = n_pos()
        print '\n\n*****     solving outputs %d to %d     *****'%(J,(J+R-1))
        f_map = str([J]*R + range(R))
        funcs = create_funcs(slps,t)
        funcs = funcs + [eval('(pyabc_split.defer(mp)(simple,%s,1,%s))'%(t,f_map))] #1 means do fast mp
##        funcs = funcs + [eval('(pyabc_split.defer(sp)())')]
        for i,res in pyabc_split.abc_split_all(funcs):
            print 'Method %d returned first with result = %s'%(i,res)
            if i == 0:
                res = 'SAT = 0, UNSAT = 0, UNDECIDED = %d'%npp
                rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
                rr = rr + 'chop%d: '%i
                rr = rr + res
                print rr
                file_out.write(rr + '\n')
                file_out.flush()
                break
            if i == 1:
                rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
                rr = rr + 'chop%d: '%i
                rr = rr + res
                file_out.write(rr + '\n')
                file_out.flush()
##                print res
                break
            else:
                if res == 'UNSAT':
                    res = 'SAT = 0, UNSAT = %d, UNDECIDED = 0'%npp
                    rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
                    rr = rr + 'chop%d: '%i
                    rr = rr + res
                    print rr
                    file_out.write(rr + '\n')
                    file_out.flush()
                    break
                else:
                    res = 'SAT = 0, UNSAT = 0, UNDECIDED = %d'%npp
                    rr = '\n@@@@ Time =  %.2f: '%(time.time() - t_iter_start)
                    rr = rr + 'chop%d: '%i
                    rr = rr + res
                    print rr
                    file_out.write(rr + '\n')
                    file_out.flush()
                    break
##        print res
        total = total + [[res]]
        print total
        J = J + R
    c = get_counts(total)
    tm = time.time() - tm
    rr = '\n@@@@ Total time for chop = %.2f, SAT = %d, UNSAT = %d, UNDECIDED = %d'%(tm,c[0],c[1],c[2])
    file_out.write(rr + '\n')
    file_out.flush()
    print rr
    return total

def get_counts(L):
    s=u=d=0
    for i in range(len(L)):
        li = L[i][0]
##        print li
        j1=li.find('=')
        j2 = li.find(',')
        num = int(li[j1+1:j2])
        s = s+num
        li = li[j2+1:]
        j1=li.find('=')
        j2 = li.find(',')
        num = int(li[j1+1:j2])
        u = u+num
        li = li[j2+1:]
        j1=li.find('=')
        j2 = li.find(',')
        num = int(li[j1+1:])
        d = d+num
    return [s,u,d]
        

def output(L,L1,L2,map1,map2,lst0,lst1,NP,final_map=[]):
    global t_iter_start
    print_all(L,L1,L2,map1,map2,lst0,lst1,NP,final_map=[])
    #print 'L = %s, L1 = %s, L2 = %s'%(sumsize(L),sumsize(L1),sumsize(L2))
    L1 = unmap(list(L1),L2,map2)
    print 'L1 after map2 = %s'%sumsize(L1)
.  
Baruch Sterin committed
4342
    if NP > 1: #an unrolling was done
4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610
        L1 = check_and_trim_L(NP,list(L1))#map into reduced size before unrolling was done by phase.
        print 'L1 = %s'%sumsize(L1)
    L = unmap(list(L),L1,map1)
    print 'L after map1 = %s'%sumsize(L)
    L = weave(list(L),[],lst1) #put back 1 in L
    print 'L after lst1 = %s'%sumsize(L)
    L = weave(list(L),lst0,[]) #put back 0 in L
    print 'L after lst0= %s'%sumsize(L) 
    report_results(list(L),final_map)
    return L

def output2(L2,map1,map2,lst0,lst1,NP,final_map=[]):
    global t_iter_start
##    print_all(L,L1,L2,map1,map2,lst0,lst1,NP,final_map=[])
    #print 'L = %s, L1 = %s, L2 = %s'%(sumsize(L),sumsize(L1),sumsize(L2))
    L1 = unmap2(L2,map2)
    print 'L1 after map2 = %s'%sumsize(L1)
##    if NP > 1: #an unrolling was done
##        L1 = check_and_trim_L(NP,list(L1))#map into reduced size before unrolling was done by phase.
##        print 'L1 = %s'%sumsize(L1)
    L = unmap2(L1,map1)
    print 'L after map1 = %s'%sumsize(L)
    L = weave(list(L),[],lst1) #put back 1 in L
    print 'L after lst1 = %s'%sumsize(L)
    L = weave(list(L),lst0,[]) #put back 0 in L
    print 'L after lst0= %s'%sumsize(L) 
    report_results(list(L),final_map)
    return L

def output3(L2,map1,map2,lst0,lst1,NP,final_map=[]):
    """ find out where results came from"""
    global t_iter_start
    L1 = unmap2(L2,map2)
    L = unmap2(L1,map1)
    L = weave(list(L),[],lst1) #put back 1 in L
    L = weave(list(L),lst0,[]) #put back 0 in L
    return L

def print_all(L,L1,L2,map1,map2,lst0,lst1,NP,final_map=[]):
##    return
    print 'L = ',
    print L
    print 'L1 = ',
    print L1
    print 'L2 = ',
    print L2
    print 'map1 = ',
    print map1
    print 'map2 = ',
    print map2
    print 'lst0 = ',
    print lst0
    print 'lst1 = ',
    print lst1


def rnge(n,m):
    """ return interval n+range(m)"""
    N = []
    for j in range(m):
        N = N + [n + j]
    return N

def create_cluster(n=0,p=1,L=100):
    """n is the start node and p is the multiplier on the # of POs to extract
        ll is the limit on the number of latches to include"""
    clstr=rem = [] #make a list of nodes to remove because not compatible
    N = 0 #number of end skips
    init = False
    skip=0 #number of initial skips
    abc('w temp.aig')
    np = n_pos()
    for i in range(np):
        if n + p*(i+1-skip) > np:
            if n_latches() > L:
                bp = n_pos()-p 
                remove(rnge(bp,p),1) #remove last p
                abc('scl')
            return clstr
        abc('r temp.aig')
        abc('cone -s -O %d -R %d;scl'%(n,p*(i+1-skip)))
        xx = n_pos()
        if n_latches() > L:
            if not init: #have not found start point yet
                n=n+p #increase start point
                print 'n,FF = %d,%d'%(n,n_latches())
                skip = skip + 1
                continue
        else:
            if not init:
##                nn=p*(i-skip)
##                clstr = clstr + rnge(nn,p*(i+1-skip))
##                print clstr #initial cluster
                init = True
##        abc('w old.aig')
        remove(rem,1)
        abc('scl')
        ps()
        if n_latches() > L:
            x = xx - p #remove last p POs
            rem = rem + rnge(x,p)
##            print len(rem)
            print 'x,len(rem) = %d,%d,%d'%(x,len(rem))
            N = N+1
        else:
            bn=p*(i-skip)
            nr=rnge(bn,p)
            clstr = clstr + nr
        if N > 100: #don't do more than 10 end-skips
            bp = n_pos()-p 
            remove(rnge(bp,p),1) #put last p on remove list
            abc('scl')
            return clstr

def generate_clusters(b=0,inc=10,end=100):
    abc('w temp_gen_clstr.aig')
    abc('w t_gen_cl.aig')
    clusters = []
    while True:
        abc('r t_gen_cl.aig')
        clstr = create_cluster(b,inc,end)
        clusters = clusters + [clstr]
        abc('r t_gen_cl.aig')
        if clstr == []:
            return clusters
        remove(clstr,1)
        abc('w t_gen_cl.aig')

def map_clusters_to_original(cl):
    L = range(n_pos())
    Clstrs = []
    k = 0
    for j in range(len(cl)):
        c = cl[j]
        cc = pick(L,c)
        Clstrs = Clstrs + [cc]
        L = pick_not(L,cc)
    return Clstrs

def pick(L,c):
    """ computes L(c) """
    x=[]
    for i in range(len(c)):
        x = x + [L[c[i]]]
    return x

def pick_not(L,c):
    """ computes L(~c)"""
    x = []
    for i in range(len(L)):
        if not i in c:
            x = x + [L[i]]
    return x

def report_L(lst=[],v=0):
    """lst must refer to original PO numbering"""
    global _L_last
    if lst == []:
        return
    for j in lst:
        if _L_last[j] == -1: #means not reported yet
            _L_last[j] = v
            report_result(j,v)

def report_s(s):
    """s must refer to original PO numbering
    Differs from above """
    global _L_last
    assert len(s) == len(_L_last), 'two lengths are not equal'
    if s == []:
        return
    for j in range(len(s)):
        if not _L_last[j] == s[j]: #means not reported yet
            assert _L_last[j] == -1, 'j = %d, _L_last[j] = %d, s[j] = %d'%(j,_L_last[j],s[j])
            if _L_last[j] == -1:
                _L_last[j] = s[j]
                report_result(j,s[j])
    

def report_results(L,final_map=[],if_final=False):
    global _L_last,t_iter_start,file_out
    out = '\n@@@@ Time = %.2f: results = %s'%((time.time()- t_iter_start),sumsize(L))
    print out
    file_out.write(out + '\n')
    file_out.flush()
    for j in range(len(L)):
        if not L[j] == _L_last [j]:
            assert _L_last[j] == -1, '_L_last[j] = %d, L[j] = %d'%(_L_last[j],L[j])
            report_result(j,L[j])
    _L_last = list(L) #update _L_last
##    print 'report: _L_last = %s'%sumsize(_L_last)
    print '\n'

def report_result(POn, REn, final_map=[]):
    if final_map == []:
        print 'PO = %d, Result = %d:  '%(POn, REn),
    else:
        print 'PO = %d, Result = %d:  '%(final_map[POn], REn),


def scorr_T(t=10000):
    global smp_trace, scorr_T_done
    if scorr_T_done:
        return
    scorr_T_done = 1
    print 'Trying scorr_T (scorr -C 2, &scorr, &scorr -C 0)'
    funcs = [eval('(pyabc_split.defer(abc)("scorr -C 2"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("&get;&scorr;&put"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("&get;&scorr -C 0;&put"))')]
    funcs = create_funcs(slps,t)+funcs
    mtds = sublist(methods,slps) + ['scorr2','&scorr','&scorr0']
    best = n_ands()
    abc('w %s_best_T.aig'%f_name)
    name1 = '%s_sc1.aig'%f_name
    if os.access(name1,os.R_OK):
        os.remove(name1)
    name2 = '%s_sc2.aig'%f_name
    if os.access(name2,os.R_OK):
        os.remove(name2)
    name3 = '%s_sc3.aig'%f_name
    if os.access(name3,os.R_OK):
        os.remove(name3)
    N=m_best = 0
    for i,res in pyabc_split.abc_split_all(funcs):
        if i == 0:
            break
        if i == 1:
            abc('w %s_sc1.aig'%f_name)
            print 'scorr: ',
            ps()
            N=N+1
        if N == 3 or n_latches() == 0:
                break
        if i == 2 or n_latches() == 0:
            abc('w %s_sc2.aig'%f_name)
            print '&scorr: ',
            ps()
            N=N+1
            if N == 3:
                break
        if i == 3 or n_latches() == 0:
            abc('w %s_sc3.aig'%f_name)
            print '&scorr0: ',
            ps()
            N=N+1
            if N == 3:
                break
    if os.access(name1,os.R_OK):
        abc('r %s'%name1)
        if n_ands() < best:
            best = n_ands()
            m_best = 1
            abc('w %s_best_T.aig'%f_name)
    if os.access(name2,os.R_OK):
        abc('r %s'%name2)
        if n_ands() < best:
            m_best = 2
            best = n_ands()
            abc('w %s_best_T.aig'%f_name)
    if os.access(name3,os.R_OK):
        abc('r %s'%name3)
        if n_ands() < best:
            m_best = 3
            best = n_ands()
            abc('w %s_best_T.aig'%f_name)
    smp_trace = smp_trace + ['%s'%mtds[m_best]]
    abc('r %s_best_T.aig'%f_name)

4611
def pscorr(t=900):
4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717
    result = par_scorr(t)
    if n_ands() == 0:
        return result
    else:
        return 'UNDECIDED'

def par_scorr(t=30,ratio = 1):
    t_init = time.time()
##    abc('dr -m;drw')
    abc('dretime;dc2')
    funcs = [eval('(pyabc_split.defer(abc)("scorr -vq -F 1"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("scorr -vq -F 2"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("scorr -vq -F 4"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("scorr -vq -F 8"))')]
    funcs = funcs + [eval('(pyabc_split.defer(abc)("scorr -vq -F 16"))')]
    funcs = create_funcs(slps,t)+funcs
    mtds = sublist(methods,slps) + ['scorr1','scorr2', 'scorr4', 'scorr8', 'scorr16']
    best = n_ands()
    print 'par_scorr: best = %d'%best
    abc('w %s_best.aig'%f_name)
    idone = []
    for i,res in pyabc_split.abc_split_all(funcs):
##        print i,res
        if i == 0: #timeout
            break
        else:
            idone = idone + [i]
            if n_ands() <= ratio * best:
                best = n_ands()
##                print 'par_scorr: best = %d, method = %s'%(best, mtds[i])
                abc('w %s_best.aig'%f_name)
                if best == 0 or len(idone) >= 5:
                    mtd = mtds[i]
                    break
            else:
                break
##    print 'Time: %.2f'%(time.time() - t_init)
    abc('r %s_best.aig'%f_name)
##    if best == 0:
##        print mtd
    return mtd

def par_scorr_q(t=10000,ratio = 1):
    abc('dretime;dc2')
    abc('bmc2 -T 5')
    depth = n_bmc_frames()
    mtds = funcs = []
    n=1
    while True:
        funcs = funcs + [eval('(pyabc_split.defer(abc)("scorr -vq -F %d"))'%n)]
        mtds = mtds + ['scorr%d'%n]
        n = 2* n
        if n > max(1,min(depth,16)):
            break
    funcs = create_funcs(slps,t)+funcs
    mtds = sublist(methods,slps) + mtds
    best = n_ands()
    print 'best = %d'%best
    abc('w %s_best.aig'%f_name)
    idone = []
    for i,res in pyabc_split.abc_split_all(funcs):
##        print i,res
        if i == 0:
            break
        else:
            idone = idone + [i]
            if n_ands() <= ratio * best:
                best = n_ands()
                print 'best = %d, method = %s'%(best, mtds[i])
                abc('w %s_best.aig'%f_name)
                if best == 0 or len(idone) >= len(mtds)-1:
                    break
            else:
                break
    abc('r %s_best.aig'%f_name)


def indicate_0_pos(L2):
    """
    puts 0's in L2 where the corresponding output is driven by a const-0
    """
##    assert n_pos() == len(L2), 'list L2=%d and n_pos=%d in current AIG dont match'%(len(L2),n_pos())
    for j in range(n_pos()):
        i=is_const_po(j)
        if i == 0:
            L2[j]=0
    return L2

def list_0_pos():
    """
    returns indices of outputs driven by  const-0
    """
    L = []
    for j in range(n_pos()):
        i=is_const_po(j) #returns const value of PO if const. Else -1
        if i == 0:
            L = L + [j]
    return L

def mprove2(L=0,op='simple',t=100,nn=0):
    global _L_last, f_name, skip_spec
    print 'mprove2 entered' ,
    if L == 0:
        L = [-1]*n_pos()
    ps()
    print 'mprove2 entered with L = ',
.  
Baruch Sterin committed
4718
    print sumsize(L)
4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740
    abc('w %s_mp2.aig'%f_name) #save aig before pos removed
    old_f_name = f_name #we may call sp() which can change f_name
    n = count_less(L,0)
    ind = []
    for j in range(len(L)):
        if L[j] > -1:
            ind = ind +[j]
    if len(ind) == n_pos(): #all POs already solved
        return 'ALL_SOLVED',L
    remove(ind,-1) #remove solved POs
    if len(ind)>0:
        print 'Removed %d proved POs'%len(ind)
    if n_pos() == 0:
        f_name = old_f_name
        abc('r %s_mp2.aig'%f_name)
        return 'ALL_SOLVED',L
    ps()
    N = n_pos()
    if N == 1: #only one PO left
        v = -1
        skip_spec_old = skip_spec
        skip_spec = True
4741
        result = simple(2000,1)
4742
        ff_name == f_name
4743
        result = sp(0,2000) #warning sp() can change f_name. 0 means simplify
4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
        f_name = ff_name
        skip_spec = skip_spec_old
        res = result[0]
        print 'result of sp = ',
        print res
####        print result
        if res == 'SAT':
            v = 1
        if res == 'UNSAT':
            v = 0
        i = L.index(-1)
##        print 'i=%d,v=%d,L=%s'%(i,v,str(L))
        L[i] = v
        f_name = old_f_name #if sp() changed f_name need to revert to old f_name
        abc('r %s_mp2.aig'%f_name)
        print 'reverting %s_mp2.aig'%f_name,
        ps()
        print sumsize(L)
        if v > -1:
            res = 'ALL_SOLVED'
        return res,L
    r = pre_simp()
    NP = n_pos()/N
    L1 = [-1]*n_pos()
    Llst0 = []
    if r[0] == Unsat:
        L1 = [0]*N
    else:
        Llst0 = list_0_pos()
        Llst0.sort()
        print 'Llst0 = %s'%str(Llst0)
        n_0 = len(Llst0)
        if n_0 > 0:
##            print 'Found %d const-0 POs'%n_0
            remove(Llst0,0)
            print 'Removed %d const-0 POs'%len(Llst0)
        if NP > 1: # we want to do iso here because more than one phase was found.
            iso()  # additional iso - changes number of POs
            map3 = create_map(eq_classes(),N) #creates map into previous
##        tb = min(n_pos(),20)
        N = n_pos()
        tb = min(N,50)
##        print 'Trying par_multi_sat for %d sec.'%tb
4787
        S,lst1,s = par_multi_sat(tb,1,1,0) #this gives a list of SAT POs
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807
        L2 = s10 = s
        n_solved = n_pos() - count_less(s10,0)
        if 1 in s10 or 0 in s10: #something solved 
            if n_solved < N: #not all solved
                rem = indices(s,0)+indices(s,1)
                rem.sort()
                remove(rem,1)
                """ if lst1 > 1 element, simplify and run par_multi_sat again to get lst2
                      then merge lst1 and lst2 appropriately to create new lst1 for below.
                """
                tb = tb+25
                gap = max(15,.2*tb)
                if len(rem) > 0:
                    s210 = s10
                    #iterate here as long as more than 1 PO is found SAT
                    n_solved = n_pos() - count_less(s210,0) 
                    while n_solved > 0:
                        gap = int(1+1.2*gap)
                        print 'gap = %.2f'%gap
                        pre_simp(1) #warning this may create const-0 pos
4808
                        S,lst2,s = par_multi_sat(tb,gap,1,0) #this can find UNSAT POs
4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
                        s210 = s
                        n_solved = n_pos() - count_less(s210,0)
                        s10 = put(s210,list(s10)) #put the new values found into s10
                        if count_less(s10,0) == 0 or n_solved == 0: #all solved or nothing solved
                            break #s10 has the results
                        else:
                            out = '\n@@@@ Time = %.2f: additional POs found SAT = %d'%((time.time()- t_iter_start),len(lst2))
                            print out
                            file_out.write(out + '\n')
                            file_out.flush()
                            rem = indices(s210,0)+indices(s210,1)
                            rem.sort()
                            remove(rem,1) #this zeros the l210 and then removes ALL const-1 POs.
                                            #If there are more than lst2 removed, it fires an assertion.
                            continue
4824
                    L2 = s10 #put results in s10
4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835
                else: #lst1 is empty or S == SAT'
                    print 'no cex found or S = UNSAT'
            else: #all solved
                print 'all POs solved'
            print 'Removed %d solved POs'%(len(s10) - count_less(s10,0))
        else:
            print 'nothing solved'
        write_file('bmc2')
        if -1 in s10:
            print 'Entering solve_all ',
            ps()
4836
            S,s210 = solve_all([-1]*n_pos(),900) #solve_all calls sp() or simple but preserves the aig and f_name
4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848
##            else: zzz
            if -1 in s210: #then no POs were solved by solve_all
##                abc('r %s_smp2_2.aig'%f_name)
                if n_pos() < 50:
                    print 'Entering mprove with %d sec. for each cone'%t,
                    ps()
                    print 'L2 before mprove: %s'%sumsize(L2)
                    s210 = mprove([-1]*n_pos(),op,t) #proving each output separately
                else:
                    s210 = [-1]*n_pos()
            print 's210 after mprove and before inject 1 %s:'%sumsize(s210)
            L2 = put(s210,s10) 
4849
            print 'L2 after inject 1 %s:'%sumsize(L2)
4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
        else: #all POs solved
            L2 = s10
        assert NP == 1, 'NP > 1: ERROR'
        if NP>1: 
            print 'NP = %d'%NP
            print 'L1 before unmap3: %s'%sumsize(L1)  #L1 should be all -1's of length before iso 
            L1 = unmap(list(L1),L2,map3)
            print 'L1 after unmap of map3: ',
            print sumsize(L1)
        else:
4860
            print 'L2 = %s'%str(L2)
4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971
            L1 = L2
        L1 = inject(list(L1),Llst0,0)
        print 'L1 after inject of Llst0 0s: %s:'%sumsize(L1)
    if NP >1:
        L1 = check_and_trim_L(NP,L1)
    assert len(L1)<=len(L),"L1 = %d larger than L = %d"%(len(L1),len(L))
##    print 'L = %s'%str(L)
    L = insert(L1,list(L)) # replace -1s in L with values in L1. Size of L1<=L L is really L2
    print sumsize(L)
    f_name = old_f_name
    abc('r %s_mp2.aig'%f_name) #restore aig
    if 1 in L:
        S = 'SAT'
    if not -1 in L:
        S = 'ALL_PROVED'
    return S,L

def merge(L1,L2,n=0):
    """L2 refers to POs that were solved after POs in L1 were removed
    modifies L2 to refer to the original POs.
    if n=0 adds in L1 and sorts
    """
    if L1 == []:
        return L2
    if L2 == []:
        if n == 0:
            return L1
        else:
            return [] 
    m = max(L1)
    LL1 = L1 + [3+m+max(L2)] #this makes sure that there are enough gaps
    g = gaps(LL1)
##    print g
    L = []
    for i in range(len(L2)):
        l2i=L2[i]
        assert  l2i < len(g),'ERROR, L2 = %s,g = %s'%(str(L2),str(g))
        L = L + [g[l2i]]
##        print L
    if n == 0:
        L = L + L1
        L.sort()        
    return L            #L is already sorted

def put(s2,s11):
    """ put in the values of s2 into where there are -1's in s1 into s1
    return s2 """
    s1 = list(s11)
    k = 0
    assert len(s2) == count_less(s1,0),'mismatch in put'
    for j in range(len(s1)):
        if s1[j] == -1:
            s1[j] = s2[k]
            k=k+1
    return s1

def gaps(L1):
    """L2 refers to POs that were solved after POs in L1 were removed
    modifies L2 to refer to the original POs.
    if n=0 adds in L1 and sorts
    """
    if L1 == [] or max(L1)+1 == len(L1):
        return []
    L1_gaps = []
    i=0
    for j in range(len(L1)):
        lj=L1[j]
##        print lj,i
        if lj == i:
            i = i+1
            continue
        assert lj > i,'Error'
        while  lj > i:
            L1_gaps = L1_gaps + [i]
            i = i+1
##            print L1_gaps
        i=i+1
    return L1_gaps

##    j=i=0
##    L = []
##    L1.sort()
##    L2.sort()
##    LL1 = L1 + [10000000] #make sure list L2 is processed to the end
####    print 'L1 and L2 is sorted %d, %d: '%(len(L1),len(L2))
##    if not L2 == []:
##        while True:
##    ##        print i,j
##            if LL1[i] <= L2[j]:
##                i = i+1
##            else:
##                L= L + [L2[j] + i]
##    ##            print L
##                j = j+1
##                if j == len(L2):
##                    break
##    if n == 0:
##        L = L + L1
##        L.sort()        
##    return L            #L is already sorted
    

def solve_all(L2,t):
    global f_name, skip_spec
    abc('w %s_solve_all.aig'%f_name)
    old_f_name = f_name
##    abc('orpos')
##    print 'solve_all for %.2f sec.: '%t,
##    ps()
    skip_spec_old = skip_spec
    skip_spec = True
4972
    tt = max(t,900)
4973
    print 'Entering simple for %d sec.'%tt
4974 4975
    result = simple(tt,1) #### temporary 1 means do not simplify
##    result = sp(0,t) #warning sp() may change f_name
4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028
    skip_spec = skip_spec_old
##    print 'solve_all: result = %s'%result
    if result[0] == 'UNSAT':
        L2 = [0]*len(L2)
    f_name = old_f_name
    abc('r %s_solve_all.aig'%f_name)
    return result[0],L2

def inject(L,lst,v):
    """
    expends the len(L) by len(lst). puts value v in expanded position
    Preserves values in L
    """
    k = i = j = 0 #i indexes L, j indexes lst, and k is total length of LL
    if lst == []:
        return L
    LL = []
    N = len(L) + len(lst)
    while True:
        if lst[j] == k:
            LL= LL + [v]
            if j < len(lst)-1:
                j = j+1
        else:
            LL = LL + [L[i]]
            if i < len(L)-1:
                i = i+1
        k = k+1
        if k >= N:
            break
    return LL

def insert(L1,L):
    """ insert L1 in L and return L"""
    k=0
    for j in range(len(L)):
        if L[j] > -1:
            continue
        else:
            L[j] = L1[k]
            k = k+1
            if k >= len(L1):
                break
    return L
                   
    

def duplicate_values(L1,NP):
    """ append values """
##    L=L1*NP
    L = L1
    for j in range(NP-1):
        L = L+[-1]*len(L1)
.  
Baruch Sterin committed
5029 5030
    return L

5031 5032 5033 5034 5035 5036 5037 5038 5039
##def duplicate_values2(L1,NP):
##    """ interleave values """
##    L = []
##    for j in range(len(L1)):
##        v = L1[j]
##        L = L + [v]*NP
##    return L

def check_and_trim_L(NP,L):
.  
Baruch Sterin committed
5040
    """This happens when an unrolling creates additional POs
5041 5042 5043 5044
    We want to check that L[j] = L[j+kN] etc to make sure the PO results agree
    in all phases, i.e. sat, unsat, or undecided. if one is sat then make all L[j+kN] sat,
    If one is unsat, then all L[j+kN] must be unsat. If not then make L[j]=-1.
    Return first N of L.
.  
Baruch Sterin committed
5045 5046 5047
    """
    N = len(L)/NP #original number of POs
    for j in range(N):
5048 5049
        if L[j] == 1:
            continue
.  
Baruch Sterin committed
5050
        for k in range(NP)[1:]: #k = 1,2,...,NP-1
5051
            if L[j+k*N] == 1:
.  
Baruch Sterin committed
5052 5053 5054 5055 5056 5057
                L[j] = 1
                break
            elif L[j] == -1:
                continue #we have to continue to look for a 1
            elif L[j] == 0:
                if L[j+k*N] == -1:
5058
                    print 'some copies of PO unsat and some undecided'
.  
Baruch Sterin committed
5059 5060 5061 5062 5063
                    L[j] = -1
                    break
            continue #have to make sure that all phases are 0
    return L[:N]

5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088
def pass_down(L,L1,map):
    """map maps L into L1.Populate L1 with values in L"""
##    print len(L),len(L1),len(map),max(map)
##    print sumsize(L)
##    print sumsize(L1)              
    for j in range(len(map)):
        if L[j] == -1:
            continue
        assert L1[map[j]] == -1 or L1[map[j]] == L[j], 'L1=%d, L = %d'%(L1[map[j]],L[j]) 
        L1[map[j]] = max(L[j],L1[map[j]])
    return L1

def mpr():
    tt = time.time()
    N=n_pos()
    r = pre_simp()
    if r == Unsat:
        L = [0]*N
    else:
        L = mprove([-1]*n_pos(),'simple',100)
    L = L[:N]
    print sumsize(L)
    print 'Time = %.2f'%(time.time() - tt)
    return L
                
.  
Baruch Sterin committed
5089

5090
def mprove(L,op='simple',tt=1000):
.  
Baruch Sterin committed
5091 5092
    """ 0 = unsat, 1 = sat, -1 = undecided"""
    global max_bmc, init_initial_f_name, initial_f_name,win_list, last_verify_time
5093 5094 5095 5096
    global f_name_save, nam_save, temp_dec, f_name
    f_name_save = f_name
    nam_save = '%s_mp_save.aig'%f_name
    abc('w %s'%nam_save)
.  
Baruch Sterin committed
5097
    N = len(L)
5098
    print 'Length L = %d, n_pos() = %d'%(len(L),n_pos())
.  
Baruch Sterin committed
5099 5100 5101
    t = tt #controls the amount of time spent on each cone
    funcs = [eval('(pyabc_split.defer(%s)())'%op)]
    funcs = create_funcs(slps,t)+funcs
5102
    mtds = sublist(methods,slps) + [op]
.  
Baruch Sterin committed
5103
    res = L
5104 5105 5106 5107 5108
    NN = count_less(L,0)
    rr = range(N)
    rr.reverse()
    init_name = init_initial_f_name
    for j in rr:
.  
Baruch Sterin committed
5109 5110
        if L[j] > -1:
            continue #already solved
5111 5112 5113
        print '\n************** No. Outputs = %d ******************************'%NN
        abc('r %s'%nam_save) #restore original function
##        ps()
.  
Baruch Sterin committed
5114
        x = time.time()
5115
        name = '%s_cone_%d.aig'%(f_name,j)
.  
Baruch Sterin committed
5116
        print '________%s(%s)__________'%(op,name)
5117 5118 5119 5120 5121
        abc('cone -s -O %d;scl'%j)
        abc('w %s_cone.aig'%f_name)
##        ps()
        read_file_quiet_i('%s_cone.aig'%f_name) #needed to reset initial settings
##        ps()
.  
Baruch Sterin committed
5122
        temp_dec = False
5123 5124 5125
        i,result = fork_last(funcs,mtds)
##        print '\ni = %d, result = %s'%(i,str(result))
        f_name = f_name_save #restore original f_name
.  
Baruch Sterin committed
5126 5127
        T = '%.2f'%(time.time() - x)
        out = get_status()
5128 5129 5130 5131 5132 5133
##        print '\nout= %d, result = %s'%(out,str(result))
        rslt = Undecided
        if not out == result:
            print 'out = %d, result = %d'%(out,result)
##        assert out == result,'out = %d, result = %d'%(out,result)
        if out == Unsat or result == 'UNSAT' or result == Unsat:
.  
Baruch Sterin committed
5134
            res[j] = 0
5135
            rslt = Unsat
.  
Baruch Sterin committed
5136 5137
        if out < Unsat:
            res[j] = 1
5138 5139 5140 5141 5142
            rslt = Sat
        print '\n%s: %s in time = %s'%(name,RESULT[rslt],T)
    abc('r %s'%nam_save) #final restore of original function for second mprove if necessary.
    init_initial_f_name = init_name
##    print res
.  
Baruch Sterin committed
5143 5144
    return res

5145 5146 5147 5148
##def sp1(options = ''):
##    global sec_options
##    sec_options = options
##    return super_prove(1)
5149

5150
def super_prove(n=0,t=900):
5151 5152 5153 5154
    """Main proof technique now. Does original prove and if after speculation there are multiple output left
    if will try to prove each output separately, in reverse order. It will quit at the first output that fails
    to be proved, or any output that is proved SAT
    n controls call to prove(n)
.  
Baruch Sterin committed
5155 5156 5157
    is n == 0 do smp and abs first, then spec
    if n == 1 do smp and spec first then abs
    if n == 2 just do quick simplification instead of full simplification, then abs first, spec second
5158
    """
.  
Baruch Sterin committed
5159
    global max_bmc, init_initial_f_name, initial_f_name,win_list, last_verify_time, f_name
5160 5161 5162 5163
##    print 'sec_options = %s'%sec_options
##    init_initial_f_name = initial_f_name
    size = str([n_pis(),n_pos(),n_latches(),n_ands()])
    add_trace('[%s: size = %s ]'%(f_name,size))
5164 5165 5166 5167 5168
    if x_factor > 1:
        print 'x_factor = %f'%x_factor
        input_x_factor()
    max_bmc = -1
    x = time.time()
5169
    add_trace('prove')
.  
Baruch Sterin committed
5170
    result = prove(n)
5171 5172 5173
    print 'prove result = ',
    print result
    tt = time.time() - x
5174
    if ((result == 'SAT') or (result == 'UNSAT')):
5175 5176 5177 5178 5179
        print '%s: total clock time taken by super_prove = %0.2f sec.'%(result,tt)
        add_trace('%s'%result)
        add_trace('Total time = %.2f'%tt)
        print m_trace
        return [result]+[m_trace]
.  
Baruch Sterin committed
5180
    elif ((result == 'UNDECIDED') and (n_latches() == 0)):
5181 5182 5183 5184 5185
        add_trace('%s'%result)
        add_trace('Total time = %.2f'%tt)
        print m_trace
        return [result]+[m_trace]
    print '%s: total clock time taken by super_prove so far = %0.2f sec.'%(result,(time.time() - x))
5186
    y = time.time()
5187 5188 5189 5190 5191 5192 5193 5194 5195 5196
    print 'Entering BMC_VER_result'
    add_trace('BMC_VER_result')
    result = BMC_VER_result() #this does backing up if cex is found
    print 'Total clock time taken by last gasp verification = %0.2f sec.'%(time.time() - y)
    tt = time.time() - x
    print 'Total clock time for %s = %0.2f sec.'%(init_initial_f_name,tt)
    add_trace('%s'%result)
    add_trace('Total time for %s = %.2f'%(init_initial_f_name,tt))
##    print m_trace
    return [result]+[m_trace]
5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209

def reachm(t):
    x = time.clock()
    abc('&get;&reachm -vcs -T %d'%t)
    print 'reachm done in time = %f'%(time.clock() - x)
    return get_status()

def reachp(t):
    x = time.clock()
    abc('&get;&reachp -rv -T %d'%t)
    print 'reachm2 done in time = %f'%(time.clock() - x)
    return get_status()

.  
Baruch Sterin committed
5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251
def scorr():
    run_command('scorr')
    ps()

def select_undecided(L):
    res = []
    for j in range(len(L)):
        l = L[j]
        if l[1] == 'UNDECIDED':
            res = res + [l[0]]
    return res
        
####def execute(L,t):
####    """
####    run the files in the list L using ss, sp, ssm each for max time = t
####    """
####    funcs1 = [eval('(pyabc_split.defer(ss)())')]
####    funcs1 = create_funcs(slps,t)+funcs1
####    mtds1 =sublist(methods,slps) + ['ss']
####    funcs2 = [eval('(pyabc_split.defer(sp)())')]
####    funcs2 = create_funcs(slps,t)+funcs2
####    mtds2 =sublist(methods,slps) + ['sp']
####    funcs3 = [eval('(pyabc_split.defer(ssm)())')]
####    funcs3 = create_funcs(slps,t)+funcs3
####    mtds3 =sublist(methods,slps) + ['ssm']
####    for j in range(len(L)):
####        name = L[j]
####        print '\n\n\n\n________ss__________'
####        read_file_quiet(name)
####        print '****ss****'
####        fork_last(funcs1,mtds1)
####        print '***Done with ss on %s\n'%name
####        print '\n\n******ssm************'
####        read_file_quiet(name)
####        print '****ssm****'
####        fork_last(funcs3,mtds3)
####        print '***Done with ssm on %s \n'%name

def execute_op(op,L,t):
    """
    run the files in the list L using operation "op", each for max time = t
    """
5252
    global res
.  
Baruch Sterin committed
5253 5254 5255 5256 5257 5258 5259 5260
    funcs = [eval('(pyabc_split.defer(%s)())'%op)]
    funcs = create_funcs(slps,t)+funcs
    mtds =sublist(methods,slps) + [op]
    res = []
    for j in range(len(L)):
        x = time.time()
        name = L[j]
        print '\n\n\n\n________%s__________'%op
5261
        read_file_quiet_i(name)
.  
Baruch Sterin committed
5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280
        m,result = fork_last(funcs,mtds)
        if result == Undecided:
            result = RESULT[result]
        T = '%.2f'%(time.time() - x)
        new_res = [name,result,T]
        res = res + [new_res]
        print '\n%s'%new_res
    return res

def x_ops(ops,L,t):
    """ execute each op in the set of ops on each file in the set of files of L, each for time t"""
    result = []
    for j in range(len(ops)):
        op = ops[j]
        result.append('Result of %s'%op)
        result.append(execute_op(op,L,t))
    return result

def iso(n=0):
5281 5282 5283 5284 5285 5286
    if n_ands() > 500000:
        return False
    if n_pos() < 2:
        print 'no more than 1 output'
        return False
    npos=n_pos()
.  
Baruch Sterin committed
5287
    if n == 0:
5288 5289 5290 5291
        abc('&get;&iso -q;&put')
        if n_pos() == npos:
            print 'no reduction'
            return False
.  
Baruch Sterin committed
5292 5293
    else:
        run_command('&get;&iso;iso;&put')
5294 5295 5296 5297 5298
        if n_pos() == npos:
            print 'no reduction'
            return False
    print 'Reduced n_pos from %d to %d'%(npos,n_pos())
    return True
.  
Baruch Sterin committed
5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343

def check_iso(N):
    ans = get_large_po()
    if ans == -1:
        return 'no output found'
    n_iso = count_iso(N)
    return n_iso

def count_iso(N):
    abc('&get;write_aiger -u file1.aig') #put this cone in & space and write file1
##    print 'PO %d is used'%i
    n_iso = 0 #start count
    for i in range(N):
        abc('permute;write_aiger -u file2.aig')
        n = filecmp.cmp('file1.aig','file2.aig')
        print n,
        n_iso = n_iso+n
    print 'the number of isomorphisms was %d out of %d'%(n_iso,N)
    return n_iso

def get_large_po():
##    remove_const_pos() #get rid of constant POs
    NL = n_latches()
    NO = n_pos()
    abc('&get') #put the in & space
    n_latches_max = 0
    nl = imax = -1
    for i in range(NO): #look for a big enough PO
        abc('&put;cone -s -O %d;scl'%i)
        nl = n_latches()
        if nl >.15*NL:
            imax = i
##            print 'cone %d has %d FF'%(i,nl)
            break
        if nl> n_latches_max:
            n_latches_max = nl
            imax = i
            print i,nl
        if i == NO-1:
            print 'no PO is big enough'
            return -1
    print 'PO_cone = %d, n_latches = %d'%(imax,nl)

def scorro():
    run_command('scorr -o')
5344
    l = remove_const_pos(0)
.  
Baruch Sterin committed
5345 5346 5347 5348 5349 5350 5351 5352 5353 5354
    ps()

def drw():
    run_command('drw')
    ps()

def dc2rs():
    abc('dc2rs')
    ps()

5355 5356 5357 5358 5359 5360
def reachn(t):
    x = time.clock()
    abc('&get;&reachn -rv -T %d'%t)
    print 'reachm3 done in time = %f'%(time.clock() - x)
    return get_status()
    
5361
def reachx(t=900):
5362 5363 5364 5365 5366
    x = time.time()
    abc('reachx -t %d'%t)
    print 'reachx  done in time = %f'%(time.time() - x)
    return get_status()

5367
def reachy(t=900):
5368 5369
    x = time.clock()
    abc('&get;&reachy -v -T %d'%t)
5370
##    print 'reachy done in time = %f'%(time.clock() - x)
5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395
    return get_status()
    
def create_funcs(J,t):
    """evaluates strings indexed by J in methods given by FUNCS
    Returns a list of lambda functions for the strings in FUNCs
    If J = [], then create provers for all POs"""
    funcs = []
    for j in range(len(J)):
        k=J[j]
        funcs = funcs + [eval(FUNCS[k])]
    return funcs

def check_abs():
    global init_initial_f_name
    abc('w %s_save.aig'%init_initial_f_name)
    ni = n_pis()
    nl = n_latches()
    na = n_ands()
    abc('r %s_smp_abs.aig'%init_initial_f_name)
    if ((ni == n_pis()) and (nl == n_latches()) and (na == n_ands())):
        return True
    else:
        abc('r %s_save.aig'%init_initial_f_name)
        return False

.  
Baruch Sterin committed
5396 5397
def modify_methods(J,dec=0):
    """ adjusts the engines to reflect number of processors"""
5398 5399 5400
    N = bmc_depth()
    L = n_latches()
    I = n_real_inputs()
5401 5402
    npr = n_proc - dec
    reachi = reachs
.  
Baruch Sterin committed
5403 5404
    if 18 in J: #if sleep in J add 1 more processor
        npr = npr+1
5405 5406 5407 5408 5409
    if ( ((I+L<550)&(N>100))  or  (I+L<400) or (L<80) ):
        if not 24 in J: #24 is reachy
            if L < 70 and not 4 in reachs:
                reachi = [4]+reachs #[4] = reachx
            J = reachi+J # add all reach methods
.  
Baruch Sterin committed
5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
            if len(J)>npr:
                J = remove_intrps(J) #removes only if len(J)<n_processes
    if len(J)< npr: #if not using all processors, add in pdrs
        for j in range(len(allpdrs)):
            if allpdrs[j] in J: #leave it in
                continue
            else: #add it in
                J = J + [allpdrs[j]]
                if len(J) == npr:
                    break            
5420 5421
    if len(J)>npr:
        J = remove_intrps(J)
.  
Baruch Sterin committed
5422 5423 5424 5425 5426 5427
    return J

def BMC_VER():
    """ a special version of BMC_VER_result that just works on the current network
    Just runs engines in parallel - no backing up
    """
5428
    global init_initial_f_name, methods, last_verify_time, n_proc,last_gasp_time
.  
Baruch Sterin committed
5429 5430
    xt = time.time()
    result = 5
5431
    t = max(2*last_verify_time,last_gasp_time)  ####
.  
Baruch Sterin committed
5432 5433 5434
    print 'Verify time set to %d'%t
    J = slps + pdrs + bmcs + intrps
    J = modify_methods(J)
5435 5436
    F = create_funcs(J,t)
    mtds = sublist(methods,J)
.  
Baruch Sterin committed
5437
    print mtds
5438 5439
    (m,result) = fork_break(F,mtds,'US')
    result = RESULT[result]
.  
Baruch Sterin committed
5440
    print 'BMC_VER result = %s'%result
5441 5442
    return result

5443 5444 5445
def BMC_VER_result(t=0):
##    return 'UNDECIDED'   #TEMP
    global init_initial_f_name, methods, last_verify_time,f_name,last_gasp_time
5446 5447
    xt = time.time()
    result = 5
.  
Baruch Sterin committed
5448
    abc('r %s.aig'%f_name)
5449 5450
    abc('scl')
    print '\n***Running proof on %s after scl:'%f_name,
.  
Baruch Sterin committed
5451
    ps()
5452 5453
    if t == 0:
        t = max(2*last_verify_time,last_gasp_time) #each time a new time-out is set t at least 1000 sec.
5454
    print 'Verify time set to %d'%t
5455
    J = slps + allpdrs2 + bmcs + intrps + sims
.  
Baruch Sterin committed
5456
    last_name = seq_name(f_name).pop()
5457 5458 5459
    if not last_name in ['abs','spec']:
        J = slps +allpdrs2 +bmcs + intrps + sims
##    if 'smp' == last_name or last_name == f_name: # then we try harder to prove it.
.  
Baruch Sterin committed
5460
    J = modify_methods(J) #if # processors is enough and problem is small enough then add in reachs
5461 5462 5463 5464 5465 5466 5467
    F = create_funcs(J,t)
    mtds = sublist(methods,J)
    print '%s'%mtds
    (m,result) = fork(F,mtds)
    result = get_status()
    if result == Unsat:
        return 'UNSAT'
5468 5469
##    if last_name == 'smp' or last_name == f_name:   # can't backup so just return result
    if not last_name in ['abs','spec']:
5470 5471
        if result < Unsat:
            return 'SAT'
5472
        if result > Unsat: #still undecided
5473
            return 'UNDECIDED'
.  
Baruch Sterin committed
5474
    else:    # (last_name == 'spec' or last_name == 'abs') - the last thing we did was an "abstraction"
5475
        if result < Unsat:
5476 5477 5478 5479
            if last_name == 'abs':
                add_trace('de_abstract')
            if last_name == 'spec':
                add_trace('de_speculate')
.  
Baruch Sterin committed
5480 5481
            f_name = revert(f_name,1) # revert the f_name back to previous
            abc('r %s.aig'%f_name)
5482
            abc('scl')
.  
Baruch Sterin committed
5483
            return BMC_VER_result() #recursion here.
5484 5485
        else:
            return 'UNDECIDED'
5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
            
def try_split():
    abc('w %s_savetemp.aig'%f_name)
    na = n_ands()
    split(3)
    if n_ands()> 2*na:
        abc('r %s_savetemp.aig'%f_name)
    
def time_diff():
    global last_time
    new_time = time.clock()
    diff = new_time - last_time
    last_time = new_time
    result = 'Lapsed time = %.2f sec.'%diff
    return result

def prove_all_ind():
    """Tries to prove output k by induction, using other outputs as constraints.
    If ever an output is proved
    it is set to 0 so it can't be used in proving another output to break circularity.
.  
Baruch Sterin committed
5506
    Finally all zero'ed outputs are removed.
5507 5508 5509
    Prints out unproved outputs Finally removes 0 outputs
    """
    global n_pos_proved, n_pos_before
.  
Baruch Sterin committed
5510
    print 'n_pos_proved = %d'%n_pos_proved
5511 5512
    n_proved = 0
    N = n_pos()
.  
Baruch Sterin committed
5513
##    l=remove_const_pos()
5514 5515 5516 5517 5518
##    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    if n_pos() == 1:
        return
    abc('w %s_osavetemp.aig'%f_name)
    lst = range(n_pos())
5519
##    lst.reverse()
5520 5521 5522 5523 5524
##    list.reverse()
##    for j in list[1:]:
    for j in lst:
##        abc('zeropo -N 0')
        abc('swappos -N %d'%j)
.  
Baruch Sterin committed
5525
##        l=remove_const_pos() #may not have to do this if constr works well with 0'ed outputs
5526 5527 5528 5529 5530
        abc('constr -N %d'%(n_pos()-1))
        abc('fold')
        n = max(1,n_ands())
        f = max(1,min(40000/n,16))
        f = int(f)
.  
Baruch Sterin committed
5531 5532
##        abc('ind -C 10000 -F %d'%f)
        abc('ind -C 1000 -F %d'%f)
5533 5534
##        run_command('print_status')
        status = get_status()
.  
Baruch Sterin committed
5535
        abc('r %s_osavetemp.aig'%f_name) #have to restore original here
5536 5537 5538 5539 5540 5541 5542 5543 5544 5545
        if status == Unsat:
##            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
            if j < n_pos_before - n_pos_proved:
                n_proved = n_proved + 1 # keeps track of real POs proved.
        elif status < Unsat:
            print '-%d'%j,
        else:
            print '*%d'%j,
5546
    l=remove_const_pos(0)
5547 5548
    n_pos_proved = n_pos_proved + n_proved 
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
.  
Baruch Sterin committed
5549
    print 'n_pos_proved = %d'%n_pos_proved
5550 5551
    #return status

.  
Baruch Sterin committed
5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563
def remove_iso(L):
    global n_pos_proved, n_pos_before
    lst = []
    for j in range(len(L)):
        ll = L[j][1:]
        if len(ll) == 0:
            continue
        else:
            lst = lst + ll
    zero(lst)
    n_pos_proved = n_pos_proved + count_less(lst,n_pos_before - n_pos_proved)
    print 'The number of POs removed by iso was %d'%len(lst)
5564
    l=remove_const_pos(0) #can an original PO be zero?
.  
Baruch Sterin committed
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577

def prove_all_iso():
    """Tries to prove output k by isomorphism. Gets number of iso-eq_classes as an array of lists.
    Updates n_pos_proved
    """
    global n_pos_proved, n_pos_before
    n_proved = 0
    N = n_pos()
    if n_pos() == 1:
        return
    print 'n_pos_proved = %d'%n_pos_proved
##    run_command('&get;&iso;&put')
    abc('&get;&iso')
5578
    L = eq_classes()
.  
Baruch Sterin committed
5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589
##    print L
    remove_iso(L)
    print '\nThe number of POs reduced by iso was from %d to %d'%(N,n_pos())

def count_less(L,n):
    count = 0
    for j in range(len(L)):
        if L[j] < n:
            count = count + 1
    return count

5590 5591 5592 5593 5594 5595 5596 5597
def prove_all_mtds(t):
    """
    Tries to prove output k  with multiple methods in parallel,
    using other outputs as constraints. If ever an output is proved
    it is set to 0 so it can't be used in proving another output to break circularity.
    Finally all zero'ed ooutputs are removed.
    """
    N = n_pos()
.  
Baruch Sterin committed
5598
##    l=remove_const_pos()
5599 5600 5601 5602 5603
##    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    abc('w %s_osavetemp.aig'%f_name)
    list = range(n_pos())
    for j in list:
        run_command('swappos -N %d'%j)
.  
Baruch Sterin committed
5604
##        l=remove_const_pos() #may not have to do this if constr works well with 0'ed outputs
5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617
        abc('constr -N %d'%(n_pos()-1))
        abc('fold')
##        cmd = '&get;,pdr -vt=%d'%t #put in parallel.
##        abc(cmd)
        verify(pdrs+bmcs+intrps+sims,t)
        status = get_status()
        abc('r %s_osavetemp.aig'%f_name)
        if status == Unsat:
            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
        print '%d'%j,
    assert not is_sat(), 'one of the POs is SAT' #we can do better than this
5618
    l=remove_const_pos(0)
5619 5620 5621 5622 5623 5624
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
    #return status

def prove_all_pdr(t):
    """Tries to prove output k by pdr, using other outputs as constraints. If ever an output is proved
    it is set to 0 so it can't be used in proving another output to break circularity.
5625
    Finally all zero'ed outputs are removed. """
5626
    N = n_pos()
.  
Baruch Sterin committed
5627
##    l=remove_const_pos()
5628 5629 5630 5631 5632
    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    abc('w %s_osavetemp.aig'%f_name)
    list = range(n_pos())
    for j in list:
        abc('swappos -N %d'%j)
.  
Baruch Sterin committed
5633
##        l=remove_const_pos() #may not have to do this if constr works well with 0'ed outputs
5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644
        abc('constr -N %d'%(n_pos()-1))
        abc('fold')
        cmd = '&get;,pdr -vt=%d'%t #put in parallel.
        abc(cmd)
        status = get_status()
        abc('r %s_osavetemp.aig'%f_name)
        if status == Unsat:
            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
        print '%d'%j,
5645
    l=remove_const_pos(0)
5646 5647 5648 5649 5650 5651
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
    #return status

def prove_each_ind():
    """Tries to prove output k by induction,  """
    N = n_pos()
5652
    l=remove_const_pos(0)
5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668
    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    abc('w %s_osavetemp.aig'%f_name)
    list = range(n_pos())
    for j in list:
        abc('cone -s -O %d'%j)
        n = max(1,n_ands())
        f = max(1,min(40000/n,16))
        f = int(f)
        abc('ind -u -C 10000 -F %d'%f)
        status = get_status()
        abc('r %s_osavetemp.aig'%f_name)
        if status == Unsat:
            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
        print '%d'%j,
5669
    l=remove_const_pos(0)
5670 5671 5672 5673 5674 5675 5676
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
    #return status

def prove_each_pdr(t):
    """Tries to prove output k by PDR. If ever an output is proved
    it is set to 0. Finally all zero'ed ooutputs are removed. """
    N = n_pos()
5677
    l=remove_const_pos(0)
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691
    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    abc('w %s_osavetemp.aig'%f_name)
    list = range(n_pos())
    for j in list:
        abc('cone -O %d -s'%j)
        abc('scl -m')
        abc('&get;,pdr -vt=%d'%t)
        status = get_status()
        abc('r %s_osavetemp.aig'%f_name)
        if status == Unsat:
            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
        print '%d'%j,
5692
    l=remove_const_pos(0)
5693 5694 5695 5696 5697 5698 5699
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
    #return status

def disprove_each_bmc(t):
    """Tries to prove output k by PDR. If ever an output is proved
    it is set to 0. Finally all zero'ed ooutputs are removed. """
    N = n_pos()
5700
    l=remove_const_pos(0)
5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714
    print '0 valued output removal changed POs from %d to %d'%(N,n_pos())
    abc('w %s_osavetemp.aig'%f_name)
    list = range(n_pos())
    for j in list:
        abc('cone -O %d -s'%j)
        abc('scl -m')
        abc('bmc3 -T %d'%t)
        status = get_status()
        abc('r %s_osavetemp.aig'%f_name)
        if status == Sat:
            print '+',
            abc('zeropo -N %d'%j)
            abc('w %s_osavetemp.aig'%f_name) #if changed, store it permanently
        print '%d'%j,
5715
    l=remove_const_pos(0)
5716 5717 5718
    print '\nThe number of POs reduced from %d to %d'%(N,n_pos())
    #return status

5719 5720 5721 5722
def add_pord(s):
    global pord_trace
    pord_trace = pord_trace + [s]

5723 5724
def pord_1_2(t):
    """ two phase pord. First one tries with 10% of the time. If not solved then try with full time"""
5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743
    global n_pos_proved, ifpord1, pord_on, pord_trace
    #first eliminate easy POs
    ttt = n_ands()/1000
    if ttt < 100:
        ttt=100
    elif ttt<200:
        ttt = 200
    elif ttt< 300:
        ttt = 300
    else:
        ttt = 500
    S,lst,L = par_multi_sat(ttt,1,1,1)
    lst = indices(L,1)
    if 1 in L:
        return [Sat]+[['par_multi_sat: SAT']]
    if -1 in L:
        restrict_v(L,-1)
    else: return [Unsat] + [['par_multi_sat: UNSAT']]
    pord_trace = []
5744 5745
    pord_on = True # make sure that we do not reparameterize after abstract in prove_2
    n_pos_proved = 0
.  
Baruch Sterin committed
5746
    if n_pos()<4:
5747
        return [Undecided] +[pord_trace]
5748
    if ifpord1:
5749 5750
        add_pord('pord1')
        t_time = .1*t
5751 5752 5753
        print 'Trying each output for %0.2f sec'%(.1*t)
        result = pord_all(.1*t) #we want to make sure that there is no easy cex.
        if (result <= Unsat):
5754 5755 5756 5757
            return [result] + [pord_trace]
    return [Undecided] + [pord_trace]
        
def pord_all(t,n=4):
5758
    """Tries to prove or disprove each output j by PDRM BMC3 or SIM. in time t"""
5759 5760
    global cex_list, n_pos_proved, last_cx, pord_on, ifpord1,pord_trace
    print 'last_cx = %d, time = %0.2f'%(last_cx,t)
5761 5762 5763 5764
    btime = time.time()
    N = n_pos()
    prove_all_ind() ############ change this to keep track of n_pos_proved
    nn = n_pos()
5765 5766
    abc('w %s_osavetemp.aig'%f_name)    
    if nn < n or nn*t > 300: #Just cut to the chase immediately.
5767 5768 5769
        return Undecided
    lst = range(n_pos())
    proved = disproved = []
.  
Baruch Sterin committed
5770 5771
    abc('&get') #using this space to save original file.
    ### Be careful that & space is not changed.
5772 5773 5774 5775 5776 5777 5778
    cx_list = []
    n_proved = 0
    lcx = last_cx + 1
    lst = lst[lcx:]+lst[:lcx]
    lst.reverse()
    n_und = 0
    for j in lst:
.  
Baruch Sterin committed
5779
        print '\ncone %s. '%j,
5780
        abc('&r -s %s_osavetemp.aig'%f_name) #for safety
.  
Baruch Sterin committed
5781 5782
        abc('&put; cone -s -O %d'%j) #puts the &space into reg-space and extracts cone j
        #requires that &space is not changed. &put resets status. Use &put -s to keep status
5783
        abc('scl -m')
.  
Baruch Sterin committed
5784 5785 5786
        ps()
##        print 'running sp2'
        ###
5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801
        result = run_sp2_par(t)
        if result == 'UNDECIDED':
            n_und = n_und + 1
            status = Undecided
            if ((n_und > 1) and not ifpord1):
                break
        elif result == 'SAT':
            status = Sat
            disproved = disproved + [j]
            last_cx = j
            cx = cex_get()
            cx_list = cx_list + [cx]
            assert len(cx_list) == len(disproved), cx_list
            if len(cx_list) > 0:
                break
5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035
        else: #is unsat here
            status = Unsat
            proved = proved + [j]
            if j < n_pos_before - n_pos_proved:
                n_proved = n_proved +1
##    n_pos_proved = n_pos_proved + n_proved. #this should not be here because we should start fresh
    print '\nProved %d outputs'%len(proved)
    print 'Disproved %d outputs'%len(disproved)
    print 'Time for pord_all was %0.2f'%(time.time() - btime)
    NN = len(proved+disproved)
    cex_list = cx_list
    if len(disproved)>0:
        assert status == Sat, 'status = %d'%status
        n_pos_proved = 0 #we want to reset this because of a bad speculation
        return Sat
    else:
        n_pos_proved = n_pos_proved + n_proved
        if nn == n_pos_proved:
            return Unsat
        abc('r %s_osavetemp.aig'%f_name)
##        abc('&put') # returning original to work spece
        remove(proved,0)
        print '\nThe number of unproved POs reduced from %d to %d'%(N,n_pos()),
        ps()
        if n_pos() > 0:
            return Undecided
        else:
            return Unsat

def bmc_ss(t):
    """
    finds a set cexs in t seconds starting at 2*N where N is depth of bmc -T 1
    The cexs are put in the global cex_list
    """
    global cex_list
    x = time.time()
    abc('bmc3 -a -C 1000000 -T %f'%(t))
    if is_sat():
        cex_list = cex_get_vector() #does this get returned from a concurrent process?
        n = count_non_None(cex_list)
        L = list_non_None(cex_list)
        print '%d cexs found in %0.2f sec'%(n,(time.time()-x))
##        remove_disproved_pos(cex_list)
    else:
        L = []
    return L

def iso_slp(t=30):
    F = [eval('pyabc_split.defer(sleep)(t))')]
    F = F = F+[eval('(pyabc_split.defer(iso)())')]
    for i,res in pyabc_split.abc_split_all(F):
        if i == 0:
            return 

##def iter_par_multi_sat(t=10,m=1):
##    while True:
##        abc('w %s_save.aig'%f_name)
##        S,lst1 = par_multi_sat(t,m) #run 3 engines in parallel looking for SAT outputs
##        lst1.sort()
##        print 'Found %d SAT POs'%len(lst1)
##        abc('r %s_save.aig'%f_name)
##        if len(lst1)==0:
##            break
##        remove(lst1,1)
##        pre_simp(1,1)
##        iso()

def show_partitions(L):
    for i in range(len(L)):
        abc('&r -s %s.aig'%L[i])
        print '\nSize = ',
        run_command('&ps')
        abc('&popart')
        eqs = eq_classes()
        N = len(eqs)
        print 'No. of partitions = %d'%N
        if N == 1:
            continue
        l = []
        for j in range(N):
            l=l + [len(eqs[j])]
        print l


def r_part(name):
    read_file_quiet_i(name)
    abc('&get;&scl;&scorr -C 2;&put')
    res1 = reparam()
    res2 = False
    npos = n_pos()
##    if n_pos() < 100:
##        res2 = iso()
##    ps()
    if n_pos() < 1000:
        iso()
    if n_pos() < 500:
        abc('r %s.aig'%name)
        abc('w %s_leaf.aig'%name)
        return 
##        abc('w %s_leaf.aig'%name)
##        return 
    res = two_eq_part()
    if res == False:
        abc('r %s.aig'%name)
        abc('w %s_leaf.aig'%name)
        return
    elif min(res) < .2*max(res) and min(res) < 500:
        abc('r %s.aig'%name)
        abc('w %s_leaf.aig'%name)
        return
    else:                           #recur
        r_part('%s_p0'%name)
        r_part('%s_p1'%name)
        return

def two_eq_part():
    abc('&get;&popart')
    part = eq_classes()
    if len(part) == 1:
        print 'Partition has only one part'
        return False
    abc('w %s_save.aig'%f_name)
    nn = n_pos()
    p1=p0 = []
    init = True
    for i in range(len(part)): #union first half together together
        if init == True:
            p0=p0 + part[i]
            if len(p0)>nn/2:
                init = False
        else:
            p1 = p1 + part[i]
    p0.sort()
    p1.sort()
    abc('&get')
    remove(p1,1)
    n0=n_pos()
##    print 'writing %s_p0.aig'%f_name
    abc('w %s_p0.aig'%f_name)
    abc('r %s_save.aig'%f_name)
    remove(p0,1)
##    print 'writing %s_p1.aig'%f_name
    n1=n_pos()
    abc('w %s_p1.aig'%f_name)
    return [n0,n1]

def merge_parts(p,n):
    parts = []
    end = []
    for i in range(len(p)):
        if len(p[i]) > n:
            parts = parts + [p[i]]
        else:
            end =end + p[i]
    parts = parts + [end]
    return parts
        

def extract_parts(S=11):
    abc('&get;&popart -S %d'%S)
    part = eq_classes()
    if len(part) == 1:
        print 'Partition has only one part'
        return 1
    parts = merge_parts(part,2)
    lp=len(parts)
    print 'Found %d parts'%lp
    abc('w %s_save.aig'%f_name)
    for i in range(lp):
        abc('r %s_save.aig'%f_name)
        p=[]
        for j in range(lp):
            if i == j:
                continue
            else:
                p = p + parts[j]
        remove(p,1)
        abc('&get;&scl;&lcorr;&put')
        abc('w %s_part%d.aig'%(f_name,i))
    return len(parts)

def two_part():
    abc('&get;&popart')
    part = eq_classes()
    if len(part) == 1:
        print 'Partition has only one part'
        return False
    part1 = part[1:] #all but the 0th
    p1=[]
    for i in range(len(part1)): #union together
        p1=p1 + part1[i]
    p1.sort()
    abc('w %s_p.aig'%f_name)
    remove(p1,1)
##    print 'writing %s_p0.aig'%f_name
    abc('w %s_p0.aig'%f_name)
    n0=n_pos()
    abc('r %s_p.aig'%f_name)
    p0 = part[0]
    p0.sort()
    remove(p0,1)
##    print 'writing %s_p1.aig'%f_name
    n1=n_pos()
    abc('w %s_p1.aig'%f_name)
    return [n0,n1]

def set_t_gap(t1,t2):
    nam = max(30000,n_ands())
    ratio = 1+float(nam-30000)/float(70000)
    gp = .5*ratio*t2
    gp = min(100,gp)
    t = min(100,ratio*t1)
    return (t,gp)
    
def par_multi_sat(t=10,gap=0,m=1,H=0):
    """ m = 1 means multiple of 1000 to increment offset"""
    global last_gap
    abc('w %s_save.aig'%f_name)
    if not t == 0:
        if gap == 0:
            gap = max(.2,.2*t)
            gap = max(15,gap)
        if gap > t:
            t=gap
        t,gt = set_t_gap(t,gap)
        gt = max(15,gt)
        if gt <= last_gap:
            gt = 1.2*last_gap
    else:
        t = gt = 5
    if gt > t:
        t = gt
    last_gap = gt
##    H = max(100, t/n_pos()+1)
6036
    if not H == 0:
6037 6038 6039 6040 6041 6042
        H = (gt*1000)/n_pos()
        H = max(min(H,1000*gt),100)
    tme = time.time()
    list0 = listr_0_pos() #reduces POs
    list0.sort()
##    print 'list0 = %s'%str(list0)
6043 6044 6045
    if len(list0)>0:
        print 'temporarily removed %d cost-0 POs'%len(list0)
    ps()
6046
    if len(list0)> 0:
6047
        print 'Found %d const-0 POs, but not removed'%len(list0)
6048 6049 6050 6051 6052 6053 6054 6055 6056
##        print ll
    print 'par_multi_sat entered for %.2f sec. and gap = %.2f  sec., H = %.2f'%(t,gt,H)
    base = m*1000
    if not m == 1:
        offset = (m-1)*32000
        abc('&get;&cycle -F %d;&put'%offset)
    mx = 1000000000/max(1,n_latches())
    N = n_pos()
    na = n_ands()
6057
    F = [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,H))'%(0*base))]
6058 6059
##    if na < 50000:
    F = F + [eval('(pyabc_split.defer(pdraz)(t,gt,H))')] #need pdr in??
6060
    F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,4,0))'%(0*base))]
6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089
    F = F + [eval('(pyabc_split.defer(sleep)(t))')]
    F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,4,0))'%(100))]
    F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(100))]
    if mx > 1*base:      
        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,1,97))'%(1*base))]
        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(1*base))]
##    if mx > 2*base:
##        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d))'%(2*base))]
##        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(2*base))]
    if mx > 4*base and na < 400000:
        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,4,23))'%(4*base))]
        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(4*base))]
##    if mx > 8*base and na < 300000:
##        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,3,53))'%(8*base))]
##        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(8*base))]
##    if mx > 16*base and na < 200000 :
##        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,2,79))'%(16*base))]
##        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(16*base))]
##    if mx > 32*base and na < 100000:      
##        F = F + [eval('(pyabc_split.defer(sim3az)(t,gt,%d,1,97))'%(32*base))]
##        F = F + [eval('(pyabc_split.defer(bmc3az)(t,gt,%d,0))'%(32*base))]
    ss=LL=L = [] 
    S = 'UNDECIDED'
    zero_done = two_done = False
    s=ss = [-1]*n_pos()
    ii = []
    nn = len(F)
    for i,res in pyabc_split.abc_split_all(F):
        ii = ii + [i]
6090 6091
        if len(ii) == len(F)-1: #all done but sleep
            break
6092 6093 6094 6095 6096 6097 6098 6099
        if i == 3: #sleep timeout
            print 'sleep timeout'
            break
##        if i == 1:
##            print 'PDR produced: %s'%str(res)
####        print i
        if i == 0:
            zero_done = True # bmc with start at 0 is done
6100
        if i == 2:
6101 6102 6103 6104 6105
            two_done = True
        if res == None: #this can happen if one of the methods bombs out
            print 'Method %d returned None'%i
            continue
##        print res
6106 6107 6108
        s1 = switch(res[1]) #res[1]= s
        s = merge_s(list(s),s1)
        print sumsize(s)
6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133
        ss = ss + [s1]
##        LL = LL + [res[0]]
##        L = L + res[0]
##        L = [x for x in set(L)] #uniquefy
        if count_less(s,0) == 0:
            S = 'UNSAT'
            break
 #        if i == 1 and is_unsat() and na < 50000: #pdr can return unsat.
##        if i == 1 and is_unsat(): #pdr can return unsat.
##            print 'Method pdr proved remaining POs UNSAT'
##            S = 'UNSAT'
##            L = res[0]
##            break
##        if not -1 in s:
##            S = 'UNSAT'
##            break
        if len(ss)>1 and zero_done and two_done:
            ss2 = ss[-2:] #checking if last 2 results agree
            r = ss2[0]
            if r == ss2[1] and count_less(r,1) < len(r): #at least 1 SAT PO found
                break
##        if len(LL) > 1 and zero_done and two_done:
##            ll2 = LL[-2:] #checking if last 2 results agree
##            if ll2[0] == ll2[1] and ll2[0] > 0:
##                break
6134
    print 'Found %d SAT POs in '%(len(L)),
6135
    print 'time = %.2f'%(time.time()-tme)
6136
    print sumsize(s)
6137 6138 6139 6140 6141 6142
##    L.sort()
##    print 'L_before = %s'%(str(L))
####    check_None_status(L,s,1) #now 1 in s means sat. s can have 0 in it, meaning it found some POs unsat.
##    L = merge(list(list0),list(L),1) #shift L according to list0 but do not include list0.
##    print 'L_shifted = %s'%(str(L))
##    # Need to return only SAT POs have to do the same for s
6143
    print 'len(s) = %d, len(list0) = %d'%(len(s),len(list0))
6144 6145
    ss = expand(s,list0,0)
##    assert list0 == indices(ss,0)
6146
    print sumsize(ss)
6147
##    assert check_consistancy(L,ss), 'inconsistant'
6148
    abc('r %s_save.aig'%f_name)
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226
    return S,[],ss


def check_consistancy(L,s):
    """ L is list of SAT's found. s is index of all"""
    consistant = True
    print 'checking s[L]'
    for j in L: #make sure that s[L] = 1
##        print j,
##        print s[j]
        if not s[j] == 1:
            print j,
            consistant = False
    print 'checking s=1 => L'
    for j in range(len(s)): #make sure that there are no other 1's
        if s[j] == 1:
            if not j in L:
                print j,
                consistant = False
    return consistant
        

def check_s(s1,s2):
    assert len(s1) == len(s2),'lengths do not match'
    miss = []
    for i in range(len(s1)):
        if (s1[i] == 0 and s2[i] == 1) or (s1[i] == 1 and s2[i] == 0):
            miss = miss + [i]
    print miss
        

def merge_s(s1,s2):
    assert len(s1) == len(s2), 'error in lengths, s1 = %s, s2 = %s'%(str(s1),str(s2))
    s = [-1]*len(s1)
    for i in range(len(s1)):
        if not s1[i] == s2[i]:
            if s1[i] == -1 or s2[i] == -1:
                s[i] = max(s1[i],s2[i])
            else:
                print 'error: conflict in values at i = %d'%i
                print 's1[i]=%d,s2[i]=%d'%(s1[i],s2[i])
        else: #put in common value
            s[i] = s1[i]
    return s

def switch(ss):
    """ This changes the convention of SAT and UNSAT to SAT = 1, UNSAT = 0"""
    s1 = ss
    for i in range(len(ss)):
        si = ss[i]
        if si == 0:
            s1[i] = 1
        elif si == 1:
            s1[i] = 0
    return s1
        

def pdr_ss_r(t):
    """
    assumes that 0 POs have been removed
    finds a set cexs in t seconds. Returns list of SAT POs found
    """
    global cex_list
    x = time.time()
    abc('pdr -az -T %f'%(t))
    if is_sat():
        print 'entering cex  get vector'
        cex_list = cex_get_vector() #does this get returned from a concurrent process?
##        n = count_non_None(cex_list)
        print len(cex_list)
        L = list_non_None(cex_list)
        n = len(L)
        print '%d cexs found in %0.2f sec.'%(n,(time.time()-x))
        if n == len(cex_list):
            print 'all remaining POs are SAT'
##            return L
        else:
            remove_disproved_pos(cex_list) #note that this will not remove all POs
6227
    else:
6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249
        L = []
    print 'T = %0.2f'%(time.time()-x)
    return L

def bmc_ss_r(t):
    """
    assumes that 0 POs have been removed
    finds a set cexs in t seconds. Returns list of SAT POs found
    """
    global cex_list
    x = time.time()
    abc('bmc3 -az -C 1000000 -T %f'%(t))
    if is_sat():
        print 'entering cex  get vector'
        cex_list = cex_get_vector() #does this get returned from a concurrent process?
##        n = count_non_None(cex_list)
        L = list_non_None(cex_list)
        n= len(L)
        print '%d cexs found in %0.2f sec.'%(n,(time.time()-x))
        if n == len(cex_list):
            print 'all remaining POs are SAT'
##            return L
6250
        else:
6251 6252 6253 6254 6255
            remove_disproved_pos(cex_list) #note that this will not remove all POs
    else:
        L = []
    print 'T = %0.2f'%(time.time()-x)
    return L
6256

6257
def sim_ss_r(t):
6258
    """
6259 6260
    assumes that 0 POs have been removed
    finds a set cexs in t seconds. Returns list of SAT POs found
6261 6262 6263
    """
    global cex_list
    x = time.time()
6264
    run_command('sim3 -az -T %f'%(t))
6265
    if is_sat():
6266
        print 'entering cex  get vector'
6267
        cex_list = cex_get_vector() #does this get returned from a concurrent process?
6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305
##        n = count_non_None(cex_list)
        L = list_non_None(cex_list)
        n = len(L)
        print '%d cexs found in %0.2f sec.'%(n,(time.time()-x))
        if n == len(cex_list):
            print 'all remaining POs are SAT'
##            return L
        else:
            remove_disproved_pos(cex_list) #note that this will not remove all POs
    else:
        L = []
    print 'T = %0.2f'%(time.time()-x)
    return L

def check_None_status(L,s=[],v=0):
    """ L is the PO numbers that had non_None in
    0 means sat and 1 means unsat is
    v tells which value means sat"""
    if s == []:
        s = status_get_vector()
    error = False
    for j in L:
        if s[j] == v:
            continue
        else:
            error = True
    for i in range(len(s)):
        if s[i] == v:
            if i in L:
                continue
        else:
            error = True
    if error:
        print 'status and non_None do not agree'
        print 'L = %d'%L
        print 'SAT and UNSAT counts switched'
        print sumsize(s)

6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326

def list_non_None(lst):
    """ return [i for i,s in enumerate(cex_list) if not s == None]"""
    L = []
    for i in range(len(lst)):
        if not lst[i] == None:
            L = L + [i]
    return L

def count_non_None(lst):
    #return len([i for i,s in enumerate(cex_list) if not s == None]
    count = 0
    for i in range(len(lst)):
        if not lst[i] == None:
            count = count + 1
    return count

def remove_disproved_pos(lst):
    for i in range(len(lst)):
        if not lst[i] == None:
            abc('zeropo -N %d'%i)
6327 6328 6329 6330 6331 6332 6333 6334 6335
    l=remove_const_pos(0)

def remove_proved_pos(lst):
    for i in range(len(lst)):
        if  lst[i] > -1:
            abc('zeropo -N %d'%i)
    remove_const_pos(0)


6336
        
6337
def bmc_j(t=900):
6338 6339 6340 6341 6342
    """ finds a cex in t seconds starting at 2*N where N is depth of bmc -T 1"""
    x = time.time()
    tt = min(5,max(1,.05*t))
    abc('bmc3 -T %0.2f'%tt)
    if is_sat():
6343
##        print 'cex found in %0.2f sec at frame %d'%((time.time()-x),cex_frame())
6344 6345 6346
        return get_status()
##    abc('bmc3 -T 1')
    N = n_bmc_frames()
.  
Baruch Sterin committed
6347
    N = max(1,N)
6348 6349 6350 6351 6352
##    print bmc_depth()
##    abc('bmc3 -C 1000000 -T %f -S %d'%(t,int(1.5*max(3,max_bmc))))
    cmd = 'bmc3 -J 2 -D 4000 -C 1000000 -T %f -S %d'%(t,2*N)
##    print cmd
    abc(cmd)
6353 6354 6355
##    if is_sat():
##        print 'cex found in %0.2f sec at frame %d'%((time.time()-x),cex_frame())
    return RESULT[get_status()]
6356

6357
def pdrseed(t=900):
.  
Baruch Sterin committed
6358 6359 6360
    """uses the abstracted version now"""
##    abc('&get;,treb -rlim=60 -coi=3 -te=1 -vt=%f -seed=521'%t)
    abc('&get;,treb -rlim=100 -vt=%f -seed=521'%t)
6361
    return RESULT[get_status()]
.  
Baruch Sterin committed
6362 6363

def pdrold(t):
6364
    abc('&get; ,pdr -vt=%f'%t)
6365
    return RESULT[get_status()]
.  
Baruch Sterin committed
6366

6367
def pdr(t=900):
.  
Baruch Sterin committed
6368 6369 6370
    abc('&get; ,treb -vt=%f'%t)
    return RESULT[get_status()]

6371
def pdr0(t=900):
6372 6373 6374
    abc('&get; ,pdr -rlim=100 -vt=%f'%t)
    return RESULT[get_status()]

6375
def pdra(t=900):
.  
Baruch Sterin committed
6376
##    abc('&get; ,treb -rlim=100 -ssize -pre-cubes=3 -vt=%f'%t)
6377
    abc('&get; ,treb -abs -rlim=100 -sat=abc -vt=%f'%t)
6378 6379
    return RESULT[get_status()]

6380
def pdrm(t=900):
.  
Baruch Sterin committed
6381
    abc('pdr -C 0 -T %f'%t)
6382 6383 6384
    return RESULT[get_status()]

def pdrmm(t):
.  
Baruch Sterin committed
6385
    abc('pdr -C 0 -M 298 -T %f'%t)
6386 6387
    return RESULT[get_status()]

6388 6389 6390 6391
def bmc2(t):
   abc('bmc2 -C 1000000 -T %f'%t)
   return RESULT[get_status()]

6392
def bmc(t=900):
6393 6394 6395
    abc('&get; ,bmc -vt=%d'%t)
    return RESULT[get_status()]

6396
def intrp(t=900):
6397 6398 6399
    abc('&get; ,imc -vt=%d'%t)
    return RESULT[get_status()]

6400
def bmc3az(t=900,gt=10,C=999,H=0):
6401
    t_init = time.time()
6402
    if  C > 999:
6403
        abc('&get; &cycle -F %d; &put'%C) 
6404
    abc('bmc3 -az -C 1000000 -T %d -G %d -H %.2f'%(t,gt,H))
6405 6406 6407
    cex_list = cex_get_vector()
    L = list_non_None(cex_list)
##    check_None_status(L)
6408 6409 6410 6411 6412
    print 'bmc3az(%.2f,%.2f,%d,%d): CEXs = %d, time = %.2f'%(t,gt,C,H,len(L),(time.time()-t_init))
##    s = status_get_vector()
    s = [-1]*n_pos()
    for j in L:
        s[j]=0 #0 here means SAT. It will be switched in par_multi_sat
6413 6414
    return L,s

6415
def pdraz(t=900,gt=10,H=0):
6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429
    print 'pdraz entered with t = %.2f, gt = %.2f, H = %.2f'%(t,gt,H)
    t_init = time.time()
    run_command('pdr -az -T %d -G %d -H %.2f'%(t,gt,H))
    cex_list = cex_get_vector()
    L = list_non_None(cex_list)
##    check_None_status(L)
    s = status_get_vector()
    if s == None:
        print "status_get_vector returned None"
    else:
        print 'Number of UNSAT POs = %d'%(len(s) - count_less(s,1))
    print 'pdraz(%.2f,%.2f,%d): CEXs = %d, time = %.2f'%(t,gt,H,len(L),(time.time()-t_init))
    return L,s

6430
def sim3az(t=900,gt=10,C=1000,W=5,N=0):
6431 6432
    """ N = random seed, gt is gap time, W = # words, F = #frames"""
    t_init = time.time()
6433
    if  C > 1000:
6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444
        abc('&get; &cycle -F %d; &put'%C) 
    abc('sim3 -az -T %.2f -G %.2f -F 40 -W %d -N %d'%(t,gt,W,N))
    cex_list = cex_get_vector()
    L = list_non_None(cex_list)
##    check_None_status(L)
    s = [-1]*n_pos()
    for i in L:
        s[i] = 0 #0 indicates SAT here
    print 'sim3az(%.2f,%.2f,%d,%d,%d): CEXs=%d, time = %.2f'%(t,gt,C,W,N,len(L),(time.time()-t_init))
    return L,s
    
6445
def bmc3(t=900):
6446 6447 6448
    abc('bmc3 -C 1000000 -T %d'%t)
    return RESULT[get_status()]

6449
def intrpm(t=900):
6450 6451 6452 6453
    abc('int -C 1000000 -F 10000 -K 1 -T %d'%t)
    print 'intrpm: status = %d'%get_status() 
    return RESULT[get_status()]

6454
def split(n):
6455
    global aigs
6456 6457
    abc('orpos;&get')
    abc('&posplit -v -N %d;&put;dc2'%n)
6458
    res =a_trim()
6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472

def keep_splitting():
    for j in range(5):
        split(5+j)
        no = n_pos()
        status = prove_g_pos_split()
        if status <= Unsat:
            return status
        if no == n_pos():
            return Undecided

def drill(n):
    run_command('&get; &reachm -vcs -H 5 -S %d -T 50 -C 40'%n)

6473

6474 6475 6476 6477
def pre_reduce():
    x = time.clock()
    pre_simp()
    write_file('smp')
.  
Baruch Sterin committed
6478 6479
    abstract(ifbip)
####    write_file('abs')
6480
    print 'Time = %0.2f'%(time.clock() - x)
6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513

def sublist(L,I):
    # return [s for i,s in enumerate(L) if i in I]
    z = []
    for i in range(len(I)):
        s = L[I[i]],
        s = list(s)
        z = z + s
    return z

#PARALLEL FUNCTIONS
"""  funcs should look like
funcs = [pyabc_split.defer(abc)('&get;,bmc -vt=50;&put'),pyabc_split.defer(super_prove)()]
After this is executed funcs becomes a special list of lambda functions
which are given to abc_split_all to be executed as in below.
It has been set up so that each of the functions works on the current aig and
possibly transforms it. The new aig and status is always read into the master when done
"""

def tf():
    result = top_fork()
    return result

def top_fork(J,t):
    global x_factor, final_verify_time, last_verify_time, methods
    set_globals()
    mtds = sublist(methods,J)
    F = create_funcs(J,t)
    print 'Running %s in parallel for max %d sec.'%(mtds,t)
    (m,result) = fork_last(F,mtds) #FORK here
    return get_status()

def run_sp2_par(t):
.  
Baruch Sterin committed
6514
    """ Runs the single method simple, timed for t seconds."""
6515 6516 6517 6518 6519
    global cex_list,methods, pord_trace
    J = slps+[6] #6 is the 'simple' method
##    mtds = sublist(methods,J)
##    print mtds,
    print 'time = %0.2f'%t
6520 6521 6522
    funcs = create_funcs(J,t) 
    y = time.time()
    for i,res in pyabc_split.abc_split_all(funcs):
.  
Baruch Sterin committed
6523
##        print 'i,res = %d,%s'%(i,res)
6524
        T = time.time()-y
6525
        if i == 0:
6526
            print 'Timer expired in %0.2f'%T
6527 6528
            return 'UNDECIDED'
        else:
.  
Baruch Sterin committed
6529
##            print i,res
6530 6531 6532 6533 6534 6535
            #note simple returns a vector
            mtd = res[1]
            ress = res[0]
            if ress == 'UNSAT':
                print 'simple proved UNSAT in %0.2f sec.'%T
                add_pord('UNSAT by %s'%mtd)
6536
                return 'UNSAT'
6537 6538
            elif ress == 'UNDECIDED':
                print 'simple returned UNDECIDED in %0.2f sec.'%T
6539
                return 'UNDECIDED'
6540 6541 6542
            if ress == 'SAT':
                print 'simple found cex in %0.2f sec.'%T
                add_pord('SAT by %s'%mtd)
6543
                return 'SAT'
6544 6545
            else:
                assert False, 'ress = %s'%ress
6546

6547
def run_parallel(J,t,BREAK='US'):
6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608
    """ Runs the listed methods J, each for time = t, in parallel and
    breaks according to BREAK = subset of '?USLB'"""
    global cex_list,  methods
    mtds = sublist(methods,J)
    F = create_funcs(J,t) #if J = [] we are going to create functions that process each output separately.
                            #if 18, then these are run in parallel with sleep
    if ((J == []) ):
        result = fork_break(F,mtds,BREAK)
##        #redirect here to suppress printouts.
##        with redirect.redirect( redirect.null_file, sys.stdout ):
##            with redirect.redirect( redirect.null_file, sys.stderr ):
##                result = fork_break(F,mtds,BREAK)
    elif 'L' in BREAK:
        result = fork_last(F,mtds)
    elif 'B' in BREAK:
        result = fork_best(F,mtds)
    else:
        result = fork_break(F,mtds,BREAK)
    return result

def fork_all(funcs,mtds):
    """Runs funcs in parallel and continue running until all are done"""
    global methods
    y = time.time()
    for i,res in pyabc_split.abc_split_all(funcs):
        status = prob_status()
        t = time.time()-y
        if not status == -1: #solved here
            if status == 1: #unsat
                print '%s proved UNSAT in %f sec.'%(mtds[i],t)
            else:
                print '%s found cex in %f sec. - '%(mtds[i],t),
                if not mtds[i] == 'REACHM':
                    print 'cex depth at %d'%cex_frame()
                else:
                    print ' '
            continue
        else:
            print '%s was undecided in %f sec. '%(mtds[i],t)
    return i,res

def fork_break(funcs,mtds,BREAK):
    """
    Runs funcs in parallel and breaks according to BREAK <= '?US'
    If mtds = 'sleep' or [], we are proving outputs in parallel
    Saves cex's found in cex_list in case we are proving POs.
    """
    global methods,last_verify_time,seed,cex_list,last_winner,last_cex
    seed = seed + 3 # since parallel processes do not chenge the seed in the prime process, we need to change it here
    cex_list = lst = []
    y = time.time() #use wall clock time because parent fork process does not use up compute time.
    for i,res in pyabc_split.abc_split_all(funcs):
        status = get_status()
        t = time.time()-y
        lm = len(mtds)
        if ((lm < 2) and not i == 0): # the only single mtds case is where it is 'sleep'
            M = 'Output %d'%(i-lm)
        else:
            M = mtds[i]
            last_winner = M
        if M == 'sleep':
6609 6610 6611
            print 'sleep: time expired in %0.2f sec.'%(t)
##            return 0,[Undecided]+[M]
##            assert status >= Unsat,'status = %d'%status
6612 6613
            break
        if ((status > Unsat) and '?' in BREAK): #undecided
6614 6615 6616 6617
            break
        elif status == Unsat or res == 'UNSAT': #unsat
            print '%s: UNSAT in %0.2f sec.'%(M,(t))
            status = Unsat
6618 6619
            if 'U' in BREAK:
                break
6620 6621
        elif status < Unsat or res == 'SAT': #status == 0 - cex found
            status = Sat
6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633
            if M in methods:                
                if methods.index(M) in exbmcs+allreachs+allpdrs+[1]: #set the known best depth so far. [1] is interp
                    set_max_bmc(n_bmc_frames())
            last_cex = M
            print '%s: -- cex in %0.2f sec. at depth %d => '%(M,t,cex_frame()),
            cex_list = cex_list+[cex_get()] #accumulates multiple cex's and puts them on list.
            if len(cex_list)>1:
                print 'len(cex_list): %d'%len(cex_list)
            if 'S' in BREAK:
                break
        else:
            continue
6634 6635
    add_trace('%s by %s'%(RESULT[status],M))
    return i,[status]+[M]
6636 6637 6638 6639 6640

def fork_best(funcs,mts):
    """ fork the functions, If not solved, take the best result in terms of AIG size"""
    global f_name
    n = len(mts)-1
6641
    r = range(len(mts))
6642 6643 6644 6645 6646 6647
    y = time.time()
    m_best = -1
    best_size = [n_pis(),n_latches(),n_ands()]
##    print best_size
    abc('w %s_best_aig.aig'%f_name)
    for i,res in pyabc_split.abc_split_all(funcs):
6648 6649 6650 6651 6652 6653 6654
        if mts[i] == 'sleep':
            m_best = i
            break
        r = delete(r,i)
        if len(r) == 1:
            if mts[r[0]] == 'sleep':
                break
6655 6656 6657 6658 6659
        status = prob_status()
        if not status == -1: #solved here
            m = i
            t = time.time()-y
            if status == 1: #unsat
6660
                print '%s proved UNSAT in %f sec.'%(mts[i],t)
6661
            else:
6662
                print '%s found cex in %f sec. - '%(mts[i],t),
6663 6664 6665 6666 6667 6668 6669 6670
            break
        else:
            cost = rel_cost(best_size)
            if cost < 0:
                best_size = [n_pis(),n_latches(),n_ands()]
                m_best = i
                abc('w %s_best_aig.aig'%f_name)
    abc('r %s_best_aig.aig'%f_name)
6671
    add_trace('%s'%mts[m_best])
6672 6673
    return m_best,res

6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685
def delete(r,i):
    """ remove element in the list r that corresponds to i """
    ii = r.index(i)
    z = []
    for i in range(len(r)):
        if i == ii:
            continue
        else:
            z = z + [r[i]]
    return z
    

.  
Baruch Sterin committed
6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701
def take_best(funcs,mts):
    """ fork the functions, If not solved, take the best result in terms of AIG size"""
    global f_name
    n = len(mts)-1
    y = time.time()
    m_best = -1
    best_size = 1000000
    abc('w %s_best_aig.aig'%f_name)
    for i,res in pyabc_split.abc_split_all(funcs):
        if n_ands() < best_size:
            best_size = n_ands()
            m_best = i
            abc('w %s_best_aig.aig'%f_name)
    abc('r %s_best_aig.aig'%f_name)
    return m_best,res

6702 6703 6704
def fork_last(funcs,mtds):
    """ fork the functions, and take first definitive answer, but
    if last method ends first, then kill others"""
6705
    global m_trace,hist,sec_options
6706 6707 6708
    n = len(mtds)-1
    m = -1
    y = time.time()
6709 6710
    sres =lst = ''
##    print mtds
.  
Baruch Sterin committed
6711
    #print 'starting fork_last'
6712
    for i,res in pyabc_split.abc_split_all(funcs):
6713
##        print i,res
6714
        status = prob_status()
6715 6716 6717 6718
        if mtds[i] == 'par_scorr' and n_ands() == 0:
            add_trace('UNSAT by %s'%res)
            return i,Unsat
        if not status == -1 or res in ['SAT','UNSAT']: #solved here
6719 6720
            m = i
            t = int(time.time()-y)
6721 6722 6723
            if status == 1 or res == 'UNSAT': #unsat
                sres = str(res)
                res = Unsat
6724 6725
                print '%s proved UNSAT in %d sec.'%(mtds[i],t)
            else:
6726 6727
                res = Sat
                print '%s found cex in %0.2f sec. - '%(mtds[i],(t)),
6728 6729
            break
        elif i == n:
6730 6731 6732 6733
##            print res
            if mtds[i] == 'pre_simp':
                m_trace = m_trace + [res[1]]
                hist = res[2]
6734 6735
            t = int(time.time()-y)
            m = i
6736 6737 6738 6739 6740 6741 6742
            if mtds[i] == 'initial_speculate':
                return m,res
            else:
                print '%s: UNDECIDED in %d sec.'%(mtds[i],t)
                res = Undecided
                ps()                
                break
6743
        elif mtds[i] == 'sleep':
.  
Baruch Sterin committed
6744
            res = Undecided
6745
            t = time.time()-y
6746
            print 'Timer expired in %0.2f'%t
6747 6748
            break
        lst = lst + ', '+mtds[i]
6749 6750 6751 6752 6753
##    sres = str(res)
    if sres[:5] == 'scorr':
        add_trace('UNSAT by %s'%sres)
        return m,Unsat
    add_trace('%s by %s'%(RESULT[res],mtds[i]))
6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767
    return m,res

def fork(funcs,mtds):
    """ runs funcs in parallel This keeps track of the verify time
    when a cex was found, and if the time to find
    the cex was > 1/2 allowed time, then last_verify_time is increased by 2"""
    global win_list, methods, last_verify_time,seed
    beg_time = time.time()
    i,res = fork_break(funcs,mtds,'US') #break on Unsat of Sat.
    t = time.time()-beg_time        #wall clock time because fork does not take any compute time.
    if t > .4*last_verify_time:
##    if t > .15*last_verify_time: ##### temp
        t = last_verify_time = last_verify_time + .1*t
        #print 'verify time increased to %s'%convert(t)
6768 6769
    assert res[0] == get_status(),'res: %d, status: %d'%(res,get_status())
##    add_trace('%s by %s'%(RESULT[res[0]],mtds[i]))
6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805
    return i,res

def save_time(M,t):
    global win_list,methods
    j = methods.index(M)
    win_list = win_list + [(j,t)]
    #print win_list

def summarize(lst):
    result = [0]*10
    for j in range(len(lst)):
        k = lst[j]
        result[k[0]]=result[k[0]]+k[1]
    return result

def top_n(lst,n):
    result = []
    ll = list(lst) #makes a copy
    m = min(n,len(ll))
    for i in range(m):
        mx_index = ll.index(max(ll))
        result = result + [mx_index]
        ll[mx_index] = -1
    return result

def super_pre_simp():
    while True:
        nff = n_latches()
        print 'Calling pre_simp'
        pre_simp()
        if n_latches() == nff:
            break

#______________________________
#new synthesis command

.  
Baruch Sterin committed
6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025
####def synculate(t):
####    """
####    Finds candidate sequential equivalences and refines them by simulation, BMC, or reachability
####    using any cex found. If any are proved, then they are used to reduce the circuit. The final aig
####    is a new synthesized circuit where all the proved equivalences are merged.
####    If we put this in a loop with increasing verify times, then each time we work with a simpler model
####    and new equivalences. Should approach srm. If in a loop, we can remember the cex_list so that we don't
####    have to deal with disproved equivalences. Then use refine_with_cexs to trim the initial equivalences.
####    If used in synthesis, need to distinguish between
####    original outputs and new ones. Things to take care of: 1. a PO should not go away until it has been processes by merged_proved_equivalences
####    2. Note that &resim does not use the -m option where as in speculation - m is used. It means that if
####    an original PO isfound to be SAT, the computation quits becasue one of the output
####    miters has been disproved.
####    """    
####    global G_C,G_T,n_pos_before, x_factor, n_latches_before, last_verify_time, f_name,cex_list, max_verify_time
####    
####    
####    def refine_with_cexs():
####        """Refines the gores file to reflect equivalences that go away because of cexs in cex_list"""
####        global f_name
####        abc('&r %s_gores.aig'%f_name)
####        for j in range(len(cex_list)):
####            cex_put(cex_list[j])
####            run_command('&resim') #put the jth cex into the cex space and use it to refine the equivs
####        abc('&w %s_gores.aig'%f_name)
####        return
####    
####    def generate_srms():
####        """generates a synthesized reduced model (srms) from the gores file"""
####        global f_name, po_map
####        abc('&r %s_gores.aig; &srm -sf; r gsrms.aig; w %s_gsrms.aig'%(f_name,f_name))
####        print 'New srms = ',ps()
####        po_map = range(n_pos())
####        return 'OK'
####
####    def merge_proved_equivalences():
####        #this only changes the gores file.
####        run_command('&r %s_gores.aig; &equiv_mark -vf %s_gsrms.aig; &reduce -v; &w %s_gores.aig'%(f_name,f_name,f_name))
####        return
####
####    def generate_equivalences():
####        set_globals()
####        t = max(1,.5*G_T)
####        r = max(1,int(t))
####        cmd = "&get; &equiv2 -C %d -F 200 -T %f -S 1 -R %d"%((G_C),t,r)
####        abc(cmd)
####        #run_command('&ps')
####        eq_simulate(.5*t)
####        #run_command('&ps')
####        cmd = '&semi -W 63 -S 5 -C 500 -F 20 -T %d'%(.5*t)
####        abc(cmd)
####        #run_command('&ps')
####        run_command('&w %s_gores.aig'%f_name)
####
####    l=remove_const_pos() #makes sure no 0 pos to start
####    cex_list = []
####    n_pos_before = n_pos()
####    n_latches_before = n_latches()
######    print 'Generating equivalences'
####    generate_equivalences()
######    print 'Generating srms file'
####    generate_srms() #this should not create new 0 pos
######    if n_pos()>100:
######        removed
####    l=remove_const_pos()
####    n_pos_last = n_pos()
####    if n_pos_before == n_pos():
####        print 'No equivalences found. Quitting synculate'
####        return Undecided_no_reduction
####    print 'Initial synculation: ',ps()
######    ps()
####    set_globals()
####    simp_sw = init = True
####    simp_sw = False #temporary
####    print '\nIterating synculation refinement'
####    abc('w initial_sync.aig')
####    max_verify_time = t
####    print 'max_verify_time = %d'%max_verify_time
####    """
####        in the following loop we increase max_verify_time by twice time spent to find last cexs or Unsat's
####        We iterate only when we have proved cex + unsat > 1/2 n_pos. Then we update srms and repeat.        
####    """
####    while True:                 # refinement loop
####        t = max_verify_time     #this may have been increased since the last loop
######        print 'max_verify_time = %d'%max_verify_time
####        set_globals()
####        if not init:
####            generate_srms()     #generates a new gsrms file and leaves it in workspace
######            print 'generate_srms done'
####            if n_pos() == n_pos_before:
####                break
####            if n_pos() == n_pos_last:   #if nothing new, then quit if max_verification time is reached.
####                if t > max_verify_time:
####                    break
####            if simp_sw:                     #Warning: If this holds then simplify could create some 0 pos
####                na = n_ands()
####                simplify()
####                while True:
####                    npo = n_pos()
######                    print 'npos = %d'%npo
####                    merge_proved_equivalences() #So we need to merge them here. Can merging create more???
####                    generate_srms()
####                    if npo == n_pos():
####                        break
####                if n_ands() > .7*na:            #if not significant reduction, stop simplification
####                    simp_sw = False             #simplify only once.
####            if n_latches() == 0:
####                return check_sat()
####        n_pos_last = n_pos()
####        init = False                        # make it so that next time it is not the first time through
####        syn_par(t)
####        if (len(cex_list)+len(result)) == 0: #nothing happened aand ran out of time.
####            break
####        abc('w %s_gsrms.aig'%f_name)
####        #print 'No. of cexs after syn_parallel = %d'%len(cex_list)
####        merge_proved_equivalences()         #changes the underlying gores file by merging fanouts of proved eqs
####        #print 'merge done'
####        refine_with_cexs()                  #changes the gores file by refining the equivalences in it using cex_list.
####        #print 'refine_with_cexs done'
####        continue
####    extract(0,n_pos_before) #get rid of unproved outputs
####    return
####
####def syn_par(t):
####    """prove n outputs at once and quit at first cex. Otherwise if no cex found return aig
####    with the unproved outputs"""
####    global trim_allowed,max_verify_time, n_pos_before
####    global cex_list, result
####    b_time = time.time()
####    n = n_pos()
####    if n == n_pos_before:
####        return
####    mx = n_pos()
####    if n_pos() - n_pos_before > 50:
####        mx = n_pos_before + 50
####    r = range(n_pos_before, mx)     
####    N = max(1,(mx-n_pos_before)/2)
####    abc('w %s__ysavetemp.aig'%f_name) 
####    F = [eval(FUNCS[18])] #create a timer function
####    #print r
####    for i in r:
####        F = F + [eval('(pyabc_split.defer(verify_only)(%d,%d))'%(i,t))]
####    cex_list = result = []
####    outcome = ''
####    #redirect printout here
######    with redirect.redirect( redirect.null_file, sys.stdout ):
######        with redirect.redirect( redirect.null_file, sys.stderr ):
####    for i,res in pyabc_split.abc_split_all(F):
####        status = get_status()
######        print i
####        if i == 0:          #timed out
####            outcome = 'time expired after = %d'%(time.time() - b_time)
####            break
####        if status < Unsat:
####            cex_list = cex_list + [cex_get()]                    
####        if status == Unsat:
####            result = result + [r[i-1]]
####        if (len(result)+len(cex_list))>= N:
####            T = time.time() - b_time
####            if T > max_verify_time/2:
####                max_verify_time = 2*T
####            break
####        continue
####    if not outcome == '':
####        print outcome
######    print 'cex_list,prove_list = ',cex_list,result
####    abc('r %s__ysavetemp.aig'%f_name) #restore initial aig so that pos can be 0'ed out
####    if not result == []: # found some unsat's
######        min_r = min(result)
######        max_r = max(result)
######        no = n_pos()
######        assert (0 <= min_r and max_r < no), 'min_r, max_r, length = %d, %d, %d'%(min_r,max_r,len(result))
####        zero(result)
####    return
####    #print "Number PO's proved = %d"%len(result)
####
####def absec(n):
####    #abc('w t.aig')
####    for j in range(n):
####        print '\nFrame %d'%(j+1)
####        run_command('absec -F %d'%(j+1))
####        if is_unsat():
####            print 'UNSAT'
####            break
####    
####
####"""
####    we might be proving some original pos as we go, and on the other hand we might have some equivalences that we
####    can't prove. There are two uses, in verification
####    verification - we want to remove the proved pos whether they are original or not. But if a cex for an original, then need to
####                    remember this.
####    synthesis - the original outputs need to be kept and ignored in terms of cex's - supposedly they can't be proved.
####"""
####
####""" Experimental"""
####
####def csec():
####    global f_name
####    if os.path.exists('%s_part0.aig'%f_name):
####        os.remove('%s_part0.aig'%f_name)
####    run_command('demiter')
####    if not os.path.exists('%s_part0.aig'%f_name):
####        return
####    run_command('r %s_part0.aig'%f_name)
####    ps()
####    run_command('comb')
####    ps()
####    abc('w %s_part0comb.aig'%f_name)
####    run_command('r %s_part1.aig'%f_name)
####    ps()
####    run_command('comb')
####    ps()
####    abc('w %s_part1comb.aig'%f_name)
####    run_command('&get; &cec %s_part0comb.aig'%(f_name))
####    if is_sat():
####        return 'SAT'
####    if is_unsat():
####        return 'UNSAT'
####    else:
####        return 'UNDECIDED'
7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046

    ###########################
####        we will verify outputs ORed in groups of g[i]
####        here we take div = N so no ORing
##        div = max(1,N/1)
##        g = distribute(N,div)
##        if len(g) <= 1:
##            t = tt
##        g.reverse()
####        print g
##        x = 0
##        G = []
##        for i in range(div):
##            y = x+g[i]
##            F = F + [eval('(pyabc_split.defer(verify_range)(%d,%d,%s))'%(x,y,convert(t)))]
##            G = G + [range(x,y)]
##            x = y
####        print G
###########################


.  
Baruch Sterin committed
7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088
""" These commands map into luts and leave the result in mapped format. To return to aig format, you
have to do 'st'
"""
def sop_balance(k=4):
    '''minimizes LUT logic levels '''
##    kmax = k
    kmax=min(k+2,15)
    abc('st; if -K %d;ps'%kmax)
    print nl(),
##    for i in range(1):
##        abc('st; if -K %d;ps'%kmax)
##        run_command('ps')
    kmax=min(k+2,15)
    abc('st; if  -g -C %d -K %d -F 2;ps'%(10,kmax)) #balance
    print nl(),
    for i in range(1):
        abc('st;dch; if -C %d -K %d;ps'%(10,kmax))
        print nl(),

def speedup(k=4):
    run_command('speedup;if -K %d'%k)
    print nl()

def speed_tradeoff(k=4):
    print nl(),
    best = n_nodes()
    abc('write_blif %s_bestsp.blif'%f_name)
    L_init = n_levels()
    while True:
        L_old = n_levels()
        L = L_old -1
        abc('speedup;if -D %d -F 2 -K %d -C 11'%(L,k))
        if n_nodes() < best:
            best = n_nodes()
            abc('write_blif %s_bestsp.blif'%f_name)
        if n_levels() == L_old:
            break
        print nl(),
        continue
    abc('r %s_bestsp.blif'%f_name)
    return

7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110
def area_tradeoff(k=4):
    print nl(),
    best = n_nodes()
    abc('write_blif %s_bestsp.blif'%f_name)
    L_init = n_levels()
    while True:
        L_old = n_levels()
        L = L_old +1
        n_nodes_old = n_nodes()
        abc('speedup;if -a -D %d -F 2 -K %d -C 11'%(L,k))
        if n_nodes() < best:
            best = n_nodes()
            abc('write_blif %s_bestsp.blif'%f_name)
##        if n_levels() == L_old:
        if n_nodes == n_nodes_old:
            break
        print nl(),
        continue
    abc('r %s_bestar.blif'%f_name)
    return


.  
Baruch Sterin committed
7111 7112
def map_lut_dch(k=4):
    '''minimizes area '''
7113
    abc('st; dch; if -a  -F 2 -K %d -C 11; mfs2 -a -L 50 ; lutpack -L 50'%k)
7114
    
.  
Baruch Sterin committed
7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163
def map_lut_dch_iter(k=8):
##    print 'entering map_lut_dch_iter with k = %d'%k
    best = n_nodes()
    abc('write_blif %s_best.blif'%f_name)
##    abc('st;dch;if -a -K %d -F 2 -C 11; mfs -a -L 1000; lutpack -L 1000'%k)
##    if n_nodes() < best:
##        abc('write_blif %s_best.blif'%f_name)
##        best = n_nodes()
##        print nl(),
##    else:
##        abc('r %s_best.blif'%f_name)
##    best = n_nodes()
##    abc('write_blif %s_best.blif'%f_name)
##    print 'best = %d'%best
    n=0
    while True:
        map_lut_dch(k)
        if n_nodes()< best:
            best = n_nodes()
##            print 'best=%d'%best
            n = 0
            abc('write_blif %s_best.blif'%f_name)
            print nl(),
            continue
        else:
            n = n+1
            if n>2:
                break    
    abc('r %s_best.blif'%f_name)

def dmitri_iter(k=8):
    best = 100000
    n=0
    while True:
        dmitri(k)
        if n_nodes()< best:
            best = n_nodes()
##            print '\nbest=%d'%best
            n = 0
            abc('write_blif %s_best.blif'%f_name)
            continue
        else:
            n = n+1
        if n>2:
            break
    abc('r %s_best.blif'%f_name)
##    run_command('cec -n %s.aig'%f_name)
    print nl()

7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194
def shrink():
    tm = time.time()
    best = n_ands()
    while True:
        abc('&get;&if -K 4 -F 1 -A 0 -a;&shrink;&put')
        print n_ands(),
        if n_ands()< .99*best:
            best = n_ands()
            continue
        break
    print 't = %.2f, '%(time.time()-tm),
    ps()

def shrink_lut():
    tm = time.time()
    abc('&get;&if -K 4 -F 1 -A 0 -a;&put')
    best = n_nodes()
    print best,
    abc('&shrink')
    while True:
        abc('&if -K 4 -F 1 -A 0 -a;&put')
        print n_nodes(),
        if n_nodes() < .99*best:
            best = n_nodes()
            abc('&shrink')
            continue
        break
    abc('&put')
    print 'time = %.2f, '%(time.time()-tm),
    ps()

.  
Baruch Sterin committed
7195 7196 7197

def map_lut(k=4):
    '''minimizes edge count'''
7198
    for i in range(5):
.  
Baruch Sterin committed
7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209
        abc('st; if -e -K %d; ps;  mfs ;ps; lutpack -L 50; ps'%(k))
        print nl(),

def extractax(o=''):
    abc('extract -%s'%o)

def nl():
    return [n_nodes(),n_levels()]

def dc2_iter(th=.999):
    abc('st')
7210
    tm = time.time()
.  
Baruch Sterin committed
7211 7212 7213 7214 7215 7216 7217
    while True:
        na=n_ands()
        abc('dc2')
        print n_ands(),
##        print nl(),
        if n_ands() > th*na:
            break
7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233
    print 't = %.2f, '%(time.time()-tm),
    ps()
##    print n_ands()

def drw_iter(th=.999):
    abc('st')
    tm = time.time()
    while True:
        na=n_ands()
        abc('drw')
        print n_ands(),
##        print nl(),
        if n_ands() > th*na:
            break
    print 't = %.2f, '%(time.time()-tm),
    ps()
.  
Baruch Sterin committed
7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304
##    print n_ands()

def adc2_iter(th=.999):
    abc('st;&get')
    while True:  
        na=n_ands()
        abc('&dc2;&put')
##        print n_ands(),
        if n_ands() > th*na:
            break
    print n_ands()
        
def try_extract():
##    abc('dc2;dc2')
    print 'Initial: ',
    ps()
    na = n_ands()
##    abc('w %s_savetemp.aig'%f_name)
    #no need to save initial aig since fork_best will return initial if best.
    J = [32,33]
    mtds = sublist(methods,J)
    F = create_funcs(J,0)
    (m,result) = take_best(F,mtds) #FORK here
    if not m == -1:
        print 'Best extract is %s: '%mtds[m],
        ps()
##    if (n_ands() < na):
##        return
##    else:
##        abc('r %s_savetemp.aig'%f_name)

def speedup_iter(k=8):
    abc('st;if -K %d'%k)
    run_command('ps')
    abc('write_blif %s_bests.blif'%f_name)
    run_command('ps')
    best = n_levels()
    print 'n_levels before speedup = %d'%n_levels()
    n=0
    while True:
        nl()
        abc('speedup;if -K %d'%k)
        if n_levels() < best:
            best = n_levels()
            abc('write_blif %s_bests.blif'%f_name)
            n=0
        else:
            n = n+1
        if n>2:
            break
    abc('r %s_bests.blif'%f_name)
    print 'n_levels = %d'%n_levels()

def jog(n=16):
    """ applies to a mapped blif file"""
    run_command('eliminate -N %d;fx'%n)
    run_command('if -K %d'%(n/2))
    run_command('fx')

def perturb_f(k=4):
    abc('st;dch;if -g -K %d'%(k))
##    snap()
    abc('speedup;if -K %d -C 10'%(k))
    jog(5*k)
##    snap()
##    abc('if -a -K %d -C 11 -F 2;mfs -a -L 50;lutpack -L 50'%k

def perturb(k=4):
    abc('st;dch;if -g -K %d'%k)
##    snap()
    abc('speedup;if -K %d -C 10'%(k))
7305
    
.  
Baruch Sterin committed
7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360
def preprocess(k=4):
    n_initial = n_nodes()
    abc('write_blif %s_temp_initial.blif'%f_name)
##    abc('st;dc2')
    abc('w %s_temp_initial.aig'%f_name)
    ni = n_pis() + n_latches()
    res = 1
    if ni >= 101:
        abc('st;if -a -F 2 -K %d'%k)
        return res
##    dc2_iter()
    abc('st;if -a -K %d'%k) # to get plain direct map
    if n_nodes() > n_initial:
        abc('r %s_temp_initial.blif'%f_name)
        res = 1
    #plain
    n_plain = n_nodes()
##    print nl()
    abc('write_blif %s_temp_plain.blif'%f_name)
    #clp
    abc('st;clp; if -a -K %d'%k)
##    print nl()
    abc('write_blif %s_temp_clp.blif'%f_name)
    n_clp = n_nodes()
    #clp_lutmin
    abc('r %s_temp_initial.blif'%f_name)
    abc('st;clp;lutmin -K %d;'%k)
    abc('write_blif %s_temp_clp_lut.blif'%f_name)
    n_clp_lut = n_nodes()
##    print nl()
    if n_plain <= min(n_clp,n_clp_lut):
        abc('r %s_temp_plain.blif'%f_name)
        res = 1
    elif n_clp < n_clp_lut:
        abc('r %s_temp_clp.blif'%f_name)
        res = 1
    else:
        abc('r %s_temp_clp_lut.blif'%f_name)
        res = 1
##    print nl()
    return res

def snap():
##    abc('fraig;fraig_store')
    abc('fraig_store')

def snap_bestk(k):
    abc('write_blif %s_temp.blif'%f_name)
    unsave_bestk(k)
    snap()
    abc('r %s_temp.blif'%f_name)

def cec_it():
    """ done because &r changes the names. Can't use -n because rfraig_store reorders pis and pos."""
    abc('write_blif %s_temp.blif'%f_name)
7361
    abc('&r -s %s.aig;&put'%f_name)
.  
Baruch Sterin committed
7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601
    run_command('cec %s_temp.blif'%f_name)
    abc('r %s_temp.blif'%f_name)

def save_bestk(b,k):
##    if os.access('%s_best%d.blif'%(f_name,k),os.R_OK):
##        res = get_bestk(k)
##    else:
    """ saves the best, returns bestk and if not best, leaves blif unchanged""" 
    res = b
    if n_nodes() < res:
        res = n_nodes()
        abc('write_blif %s_best%d.blif'%(f_name,k))
        print 'best%d = %d'%(k,res)
    return res
##    unsave_bestk(k)

def unsave_bestk(k):
    abc('r %s_best%d.blif'%(f_name,k))
        
def unsnap(k=4):
##    snap()
    abc('fraig_restore')
    map_lut_dch(k)
##    abc('fraig_restore;if -a -F 2 -C 11 -K %d'%k)

def map_until_conv(k=4):
    kk = 2*k
    # make sure that no residual results are left over.
    if os.access('%s_best%d.blif'%(f_name,k),os.R_OK):
        os.remove('%s_best%d.blif'%(f_name,k))
    if os.access('%s_best%d.blif'%(f_name,kk),os.R_OK):
        os.remove('%s_best%d.blif'%(f_name,kk))
    tt = time.time()
    #get initial map and save
    map_lut_dch(k)
    bestk = save_bestk(100000,k)
    print nl()
##    snap()
    res = preprocess() #get best of initial, clp, and lutmin versions
    print nl()
##    map_lut_dch(k)
##    ###
##    bestk = save_bestk(bestk,k)
##    map_iter(k)
##    bestk = save_bestk(bestk,k)
##    ###
    map_lut_dch_iter(kk) #initialize with mapping with 2k input LUTs
    bestkk = save_bestk(100000,kk)
    snap()
    unsnap(k) #have to do snap first if want current result snapped in.
        # unsnap fraigs snapshots and does map_lut_dch at end
    print nl()
    bestk = save_bestk(bestk,k)
    abc('r %s_bestk%d.blif'%(f_name,k))
    map_iter(k) #1
    bestk = save_bestk(bestk,k)
    while True:
        print 'Perturbing with %d-Lut'%kk
##        snap()
        map_lut_dch_iter(kk)
##        snap()
        bestkk_old = bestkk
        bestkk = save_bestk(bestkk,kk)
        if bestkk >= bestkk_old:
            break
##        snap()
##        jog(kk)
##        perturb_f(k)
##        snap()
##        perturb_f(k)
##        snap()
##        unsave_bestk(k)
##        map_lut_dch(k+1)
##        snap()
##        snap_bestk(k)
        snap()
        unsnap(k) #fraig restore and map
##        bestk = save_bestk(bestk,k)
##        snap()
        bestk_old = bestk
        map_iter(k)
        bestk = save_bestk(bestk,k)
        if bestk >= bestk_old:
            break
        continue
    abc('fraig_restore') #dump what is left in fraig_store
    unsave_bestk(k)
    print '\nFinal size = ',
    print nl()
    print 'time for %s = %.02f'%(f_name,(time.time()-tt))
##    cec_it()

def get_bestk(k=4):
    abc('write_blif %s_temp.blif'%f_name)
    unsave_bestk(k)
    res = n_nodes()
    abc('r %s_temp.blif'%f_name)
    return res

def map_iter(k=4):
    tt = time.time()
    bestk = get_bestk(k)
##    bestk = n_nodes()
##    bestk = save_bestk(bestk,k)
##    abc('st;dch;if -a -F 2 -K %d -C 11; mfs -a -L 1000; lutpack -L 1000'%k)#should be same as Initial
##    map_lut_dch_iter(k) ####
    map_lut_dch(k)
    bestk = save_bestk(bestk,k)
    n=0
    unsave_bestk(k)
    while True:
##        snap()
        perturb(k) #
##        snap()
        perturb(k)
##        snap_bestk(k)
##        unsnap(k)
##        bestk = save_bestk(bestk,k)
##        snap()
##        map_lut_dch(k+1)
##        abc('if -K %d'%(k+1))
##        snap()
##        unsnap(k)
        old_bestk = bestk
##        print old_bestk
        map_lut_dch_iter(k)
        bestk = save_bestk(bestk,k)
        print bestk
        if bestk < old_bestk:
            n=0 # keep it up
            continue
        elif n == 2: #perturb 
            break
        else:
            n = n+1
            print '%d-perturb'%n
##            snap()
##            unsave_bestk(k)
    unsave_bestk(k)

def map_star(k=4):
    tt = time.time()
    map_until_conv(k)
    abc('write_blif %s_best_star.blif'%f_name)
    best = n_nodes()
    while True:
        jog(2*k)
        map_until_conv(k)
        if n_nodes() >= best:
            break
        else:
            best = n_nodes()
            abc('write_blif %s_best_star.blif'%f_name)
    abc('r %s_best_star.blif'%f_name)
    print 'SIZE = %d, TIME = %.2f for %s'%(n_nodes(),(time.time() - tt),f_name)

def decomp_444():
    abc('st; dch; if -K 10 -S 444')
    abc('write_blif -S 444 %s_temp.blif; r %s_temp.blif'%(f_name,f_name)) 

def dmitri(k=8):
##    abc('w t.aig')
##    dc2_iter()
##    print 'first iter done:  %d'%n_ands()
##    abc('dc2rs')
####    dc2_iter()
##    print 'second iter done:  %d'%n_ands()
##    sop_balance(k)
##    abc('w t_before.aig')
##    run_command('cec -n t.aig')
##    speedup_iter(k)
##    print 'n_levels after speedup = %d'%n_levels()
##    abc('write_blif %s_save.blif'%f_name)
##    nn=n_levels()
    abc('st;dch; if -g -K %d'%(k))
##    print 'n_levels after sop balance = %d'%n_levels()
##    if n_levels() > nn:
##        run_command('r %s_save.blif'%f_name)
##        print 'n_levels = %d'%n_levels()
##    print 'final n_levels = %d'%n_levels()
##    print 'sop_balance done:  ',
##    print nl()
##    run_command('st;w t_after.aig')
##    run_command('cec -n t.aig')
    abc('if -G %d '%k)
##    print 'after if -G %d:  '%k,
##    print nl()
##    run_command('cec -n t.aig')
    abc('cubes')
##    print 'after cubes:  ',
##    print nl()
##    run_command('cec -n t.aig')
    abc('addbuffs -v')
##    print 'after addbuffs:  ',
    print nl(),
##    run_command('cec -n t.aig')

def lut():
    dc2_iter()
    abc('extract -a')
    print nl()
    dc2_iter()
##    ps()
    sop_balance(6)
    map_lut_dch()
    map_lut()
    print nl()
##    run_command('ps')

################################## gate level abstraction
    """
    Code for using
    for abstraction
    """

def bip_abs(t=100):
    """ t is ignored here"""
    set_globals()
    time = max(1,.1*G_T)
    abc('&get;,bmc -vt=%f'%time)
    set_max_bmc(bmc_depth())
    c = 2*G_C
    f = max(2*max_bmc,20)
    b = min(max(10,max_bmc),200)
    t1 = x_factor*max(1,2*G_T)
    t = max(t1,t)
    s = min(max(3,c/30000),10) # stability between 3 and 10 
##    cmd = '&get;,abs -bob=%d -stable=%d -timeout=%d -vt=%d -depth=%d -dwr=vabs'%(b,s,t,t,f)
    cmd = '&get;,abs -timeout=%d -vt=%d -dwr=%s_vabs'%(t,t,f_name)
    print 'Running %s'%cmd
##    abc(cmd)
    run_command(cmd)
    bmc_depth()
    abc('&w %s_greg.aig'%f_name)
    return max_bmc

def check_frames():
    abc('read_status vta.status')
    return n_bmc_frames()

7602
def vta_abs(t):
.  
Baruch Sterin committed
7603 7604
    """ Do gate-level abstraction for F frames """
    r = 100 *(1 - abs_ratio)
7605 7606 7607
##    abc('orpos; &get;&vta -dv -A %s_vabs.aig -P 2 -T %d -R %d; &vta_gla;&w %s_gla.aig;&gla_derive; &put; w %s_gabs.aig'%(f_name,t,r,f_name,f_name))
    abc('orpos; &get;&vta -dv -A %s_vabs.aig -P 2 -T %d -R %d; &vta_gla;&w %s_gla.aig'%(f_name,t,r,f_name))
    
.  
Baruch Sterin committed
7608
##    write_file('abs')
7609
    
.  
Baruch Sterin committed
7610 7611 7612 7613 7614 7615 7616

def sizeof():
    return [n_pis(),n_pos(),n_latches(),n_ands()]

def abstract(ifb=2):
    global abs_ratio
##    print 'ifb = %d'%ifb
7617 7618
    if ifb == 0: #new way using vta_abs and no bip
        add_trace('abstracta')
.  
Baruch Sterin committed
7619 7620 7621
        return abstracta(False)
    elif ifb == 1: #old way using ,abs
        assert ifb == ifbip, 'call to abstract has ifb not = global ifbip'
7622
        add_trace('abstractb')
.  
Baruch Sterin committed
7623 7624 7625
        return abstractb()
    else:
        #new way using ,abs -dwr -- (bip_abs)
7626
        add_trace('abstracta')
.  
Baruch Sterin committed
7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664
        return abstracta(True)

def abstracta(if_bip=True):
    """
    if_bip = 0 it uses a new abstraction based on &vta (gate level abstraction) and no bip operations
    Right now, if we do not prove it with abstraction in the time allowed,
    we abandon abstraction and go on with speculation
    if_bip = 1, we use ,abs -dwr
    """
    global G_C, G_T, latches_before_abs, x_factor, last_verify_time, x, win_list, j_last, sims
    global latches_before_abs, ands_before_abs, pis_before_abs, abs_ratio
##    n_vabs = 0
    latches_before_abs = n_latches()
    ands_before_abs = n_ands()
    pis_before_abs = n_real_inputs()
    tt = time.time()
    print 'using abstracta, ',
##    print 'if_bip = %d'%if_bip
##    latch_ratio = abs_ratio
##    t = 100
    t = 1000 #temporary
    t = abs_time
    if if_bip == 0:
        t = 1000 #timeout on vta
        t = abs_time
    tt = time.time()
    if n_pos() > 1 and if_bip == 0:
        abc('orpos')
        print 'POs ORed together, ',
    initial_size = sizeof()
    abc('w %s_before_abs.aig'%f_name)
    # 25 below means that it will quit if #FF+#ANDS > 75% of original
##    funcs = [eval("(pyabc_split.defer(abc)('orpos;&get;&vta -d -R 25'))")] #right now we need orpos
    if if_bip:
        print 'using bip_abs'
        mtds = ['bip_abs']
        funcs = [eval('(pyabc_split.defer(bip_abs)(t))')]
    else:
7665 7666 7667 7668 7669 7670 7671 7672 7673
        if gla:
            print 'using gla_abs_iter for %0.2f sec.'%(t-2)
            mtds = ['gla_abs_iter']
            add_trace('gla_abs')
            funcs = [eval('(pyabc_split.defer(gla_abs_iter)(t-2))')]
        else:
            print 'using vta_abs for %0.2f sec.'%(t-2)
            mtds = ['vta_abs']
            funcs = [eval('(pyabc_split.defer(vta_abs)(t-2))')]
.  
Baruch Sterin committed
7674
    funcs = funcs + [eval('(pyabc_split.defer(monitor_and_prove)())')]
7675 7676 7677 7678
##    J = [34,30]
    J = pdrs[:1]+bmcs[:1] #just use one pdr and one bmc here.
##    J = pdrs+bmcs
##    J = modify_methods(J,2)
.  
Baruch Sterin committed
7679 7680 7681 7682 7683 7684 7685
    funcs = funcs + create_funcs(J,1000)
    mtds = mtds + ['monitor_and_prove'] + sublist(methods,J)
    print 'methods = ',
    print mtds
    vta_term_by_time=0
    for i,res in pyabc_split.abc_split_all(funcs):
##        print i,res
7686
        if i == 0: #vta or gla ended first
.  
Baruch Sterin committed
7687 7688
            print 'time taken = %0.2f'%(time.time() - tt)
            if is_sat():
7689 7690
                print 'vta/gla abstraction found cex in frame %d'%cex_frame()
                add_trace('SAT by gla')
.  
Baruch Sterin committed
7691 7692
                return Sat
            if is_unsat():
7693 7694
                print 'vta/gla abstraction proved UNSAT'
                add_trace('UNSAT by gla')
.  
Baruch Sterin committed
7695 7696
                return Unsat
            else: #undecided
7697 7698 7699 7700
                if if_bip:
                    abc('&r -s %s_greg.aig; &abs_derive; &put; w %s_gabs.aig'%(f_name,f_name))
                else:
                    abc('&r -s %s_gla.aig;&gla_derive; &put; w %s_gabs.aig'%(f_name,f_name))   
.  
Baruch Sterin committed
7701 7702 7703 7704 7705
                if time.time() - tt < .95*t:
                    print 'abstraction terminated but not by timeout'
                    vta_term_by_time = 0
                    break
                else:
7706
                    print 'abstraction terminated by a timeout of %0.2f'%t
.  
Baruch Sterin committed
7707 7708 7709 7710 7711
##                    print 'final abstraction: ',
##                    ps()
                    vta_term_by_time=1
                    break
        if i == 1: #monitor and prove ended first (sleep timed out)
7712
            print 'monitor_and_prove: '
.  
Baruch Sterin committed
7713
##            print i,res
7714 7715 7716 7717 7718 7719 7720
            if res == None:
                print 'monitor and prove had an error'
                continue
            result = res[0]
            if res[0] > Undecided: #we abandon abstraction
                add_trace('de_abstract')
                print 'monitor and prove timed out or too little reduction'
.  
Baruch Sterin committed
7721 7722
                abc('r %s_before_abs.aig'%f_name)
                return Undecided_no_reduction
7723 7724
            if res[0] == Undecided:
                break
.  
Baruch Sterin committed
7725 7726 7727 7728
            else: 
                if not initial_size == sizeof(): #monitor and prove should not return SAT in this case'
                    assert not is_sat(), 'monitor_and_prove returned SAT on abstraction!' 
                print 'time taken = %0.2f'%(time.time() - tt)
7729 7730
                if is_unsat() or res[0] == 'UNSAT' or res[0] == Unsat:
                    add_trace('UNSAT by %s'%res[1])
.  
Baruch Sterin committed
7731
                    return Unsat
7732 7733
                elif is_sat() or res[0] < Unsat:
                    add_trace('SAT by %s'%res[1])
.  
Baruch Sterin committed
7734 7735 7736 7737 7738 7739
                    return Sat
                else:
                    abc('r %s_before_abs.aig'%f_name)
                    return Undecided_no_reduction
        else: #one of the engines got an answer
            print 'time taken = %0.2f'%(time.time() - tt)
7740
##            add_trace('initial %s'%mtds[i])
.  
Baruch Sterin committed
7741 7742
            if is_unsat():
                print 'Initial %s proved UNSAT'%mtds[i]
7743
                add_trace('UNSAT by initial %s'%mtds[i])
.  
Baruch Sterin committed
7744 7745 7746
                return Unsat
            if is_sat():
                print 'Initial %s proved SAT'%mtds[i]
7747
                add_trace('SAT by initial %s'%mtds[i])
.  
Baruch Sterin committed
7748 7749 7750
                return Sat
            else: # an engine failed here
                print 'Initial %s terminated without result'%mtds[i]
7751
                add_trace('method %s failed'%mtds[i])
.  
Baruch Sterin committed
7752 7753
##                return Undecided
                continue
7754
    if  vta_term_by_time == 0 and if_bip == 0 and gabs: #vta timed out itself
.  
Baruch Sterin committed
7755 7756 7757
        print 'Trying to verify final abstraction',
        ps()
        result = verify([7,9,19,23,24,30],100)
7758 7759
        if result[0] == Unsat:
            add_trace('UNSAT by %s'%result[1])
.  
Baruch Sterin committed
7760
            print 'Abstraction proved valid'
7761
            return result[0]
.  
Baruch Sterin committed
7762
    # should do abstraction refinement here if if_bip==1
7763 7764
    if if_bip == 0 and gabs: # thus using vta or gla abstraction and no refinement
        print 'abstraction no good - restoring initial simplified AIG',
.  
Baruch Sterin committed
7765
        abc('r %s_before_abs.aig'%f_name)
7766 7767
        add_trace('de_abstract')
        ps()
.  
Baruch Sterin committed
7768
        return Undecided_no_reduction
7769
    else: # thus using bip_abs (ifbip=1) or gate abstraction (ifbip=0&gabs=False) and refinement
.  
Baruch Sterin committed
7770 7771
        if is_sat():
            print 'Found true counterexample in frame %d'%cex_frame()
7772
            add_trace('SAT')
.  
Baruch Sterin committed
7773 7774
            return Sat_true
        if is_unsat():
7775
            add_trace('UNSAT')
.  
Baruch Sterin committed
7776 7777 7778 7779 7780
            return Unsat
    ##    set_max_bmc(NBF)
        NBF = bmc_depth()
        print 'Abstraction good to %d frames'%max_bmc
        #note when things are done in parallel, the &aig is not restored!!!
7781 7782 7783 7784
        if if_bip:
            abc('&r -s %s_greg.aig; &w initial_greg.aig; &abs_derive; &put; w initial_gabs.aig; w %s_gabs.aig'%(f_name,f_name))
        else:
            run_command('&r -s %s_gla.aig; &w initial_gla.aig; &gla_derive; &put; w initial_gabs.aig; w %s_gabs.aig'%(f_name,f_name))
.  
Baruch Sterin committed
7785 7786 7787 7788 7789 7790 7791 7792 7793 7794
        set_max_bmc(NBF)
        print 'Initial abstraction: ',
        ps()
        abc('w %s_init_abs.aig'%f_name)
        latches_after = n_latches()
    ##    if latches_after >= .90*latches_before_abs: #the following should match similar statement
    ##    if ((rel_cost_t([pis_before_abs, latches_before_abs, ands_before_abs])> -.1) or
    ##        (latches_after >= .75*latches_before_abs)):
        if small_abs(abs_ratio):
            abc('r %s_before_abs.aig'%f_name)
7795
            print "Too little reduction!"
.  
Baruch Sterin committed
7796
            print 'Abstract time wasted = %0.2f'%(time.time()-tt)
7797
            add_trace('de_abstract')
.  
Baruch Sterin committed
7798 7799 7800
            return Undecided_no_reduction
        sims_old = sims
        sims=sims[:1] #make it so that rarity sim is not used since it can't find a cex
7801 7802 7803 7804 7805 7806 7807 7808
##        result = Undecided_no_reduction
        print 'small_abs = %.2f, vta_term_by_time = %d'%(small_abs(abs_ratio),vta_term_by_time)
        if not vta_term_by_time:
            print 'Entering abstraction_refinement'
            result = abstraction_refinement(latches_before_abs, NBF,abs_ratio)
            sims = sims_old
            if result <= Unsat:
                return result
.  
Baruch Sterin committed
7809
        if small_abs(abs_ratio): #r is ratio of final to initial latches in absstraction. If greater then True
7810
##        if small_abs(abs_ratio) or result == Undecided_no_reduction or vta_term_by_time: #r is ratio of final to initial latches in absstraction. If greater then True
.  
Baruch Sterin committed
7811
            abc('r %s_before_abs.aig'%f_name) #restore original file before abstract.
7812
            print "Too little reduction!  ",
.  
Baruch Sterin committed
7813
            print 'Abstract time wasted = %0.2f'%(time.time()-tt)
7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840
            add_trace('de_abstract')
            return Undecided_no_reduction
        elif vta_term_by_time:
            abc('r %s_gabs.aig'%f_name)
            print 'Simplifying and testing abstraction'
            reparam()
            result = simplify()
            assert result >= Unsat, 'simplify returned SAT'
            if result > Unsat: #test if abstraction is unsat
                result = simple()
                res = result[0]
                if res == 'UNSAT':
                    return Unsat
                else:
                    abc('r %s_before_abs.aig'%f_name) #restore original file before abstract.
                    print "Timed out with bad abstraction",
                    print 'Abstract time wasted = %0.2f'%(time.time()-tt)
                    add_trace('de_abstract')
                    return Undecided_no_reduction
##                if res == 'SAT':
####                    result = Sat #this was an error
##                    result = Undecided_no_reduction
##                elif res == 'UNSAT':
##                    result = Unsat
##                else:
##                    result = Undecided_no_reduction
##                return result
.  
Baruch Sterin committed
7841 7842 7843 7844 7845
        else:
            write_file('abs') #this is only written if it was not solved and some change happened.
        print 'Abstract time = %0.2f'%(time.time()-tt)
    return result

7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970
def gla_abs_iter(t):
    """ this iterates &gla every x sec and checks if it should be stopped or continued.
        Uses the fact that when &gla ends
        it leaves the result in the &-space showing which elements are abstracted.
        cex_abs_depth, time_abs_prev and time_abs come from monitor_and_prove
        gla_abs_iter and monitor_and_prove are run in parallel
        """
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
    it_interval = 10000 
    total = t
    tt = time.time()
    run_command('orpos;&get')
##    run_command('&w %s_gla.aig'%f_name)
    abs_depth = abs_depth_prev = 0
##    while True:
    r = 100 *(1 - abs_ratio)
    q = 99 #############TEMP
##    run_command('&r %s_gla.aig'%f_name)
    time_remain = total - (time.time() - tt) #time remaining
    it = min(it_interval,time_remain)
##    if it < 2:
##        break
    #gla and vabs are the file with the abstraction info and gabs is the derived file.
    cmd = '&gla -mvs -B 1 -A %s_vabs.aig -T %d -R %d -Q %d -S %d'%(f_name,it,r,q,abs_depth)
    print 'Executing %s'%cmd
    name = '%s_vabs.aig'%f_name
    run_command(cmd)
    if os.access(name,os.R_OK):
        run_command('&r -s %s_vabs.aig'%f_name) #get the last abstraction result
        run_command('&w %s_gla.aig'%f_name) #saves the result of abstraction.
    else:
        run_command('&r -s %s_abs_old.aig'%f_name) #get the last abstraction result
        run_command('&w %s_gla.aig'%f_name) #saves the result of abstraction.
    print 'wrote %s_gla file'%f_name
    run_command('&gla_derive;&put')
    run_command('w %s_gabs.aig'%f_name)
##        break
##        abs_depth_prev = abs_depth
##        abs_depth = n_bmc_frames()
##        print 'abs_depth = %d'%abs_depth
##        #test here if done
##        if (time.time()-tt) > total:
##            break
##        print 'reading abs_values'
##        read_abs_values()
##        print 'values read'
##        if abs_done(time_remain):
##            print 'abs_done'
##            break
##        else:
##            continue
    
def read_abs_values():
    """here we read in the abs values written by monitor and prove"""
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
    if not os.access('%s_ab.txt'%f_name,os.R_OK):
        print '%s_ab.txt does not exist'%f_name
        return #file does not exist so do nochange values
##    print '%s_ab.txt file exists and is readable'%f_name
    ab = open('%s_ab.txt'%f_name,'r')
    print '%s_ab.txt is opened'%f_name
    s = ab.readline()
##    print s
    cex_abs_depth = int(s)
    s = ab.readline()
##    print s
    time_abs_prev = float(s)
    s = ab.readline()
##    print s
    time_abs = float(s)
    s = ab.readline()
##    print s
    abs_depth_prev = float(s)
    s = ab.readline()
##    print s
    abs_depth = float(s)
    ab.close()
##    print 'read: ',
##    print cex_abs_depth,time_abs_prev,time_abs,abs_depth_prev,abs_depth
##    print 'it is closed'

def write_abs_values():
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
    """here we write in the abs values written by monitor and prove"""
##    print 'write: ',
##    print cex_abs_depth,time_abs_prev,time_abs,abs_depth_prev,abs_depth
    ab = open('%s_ab.txt'%f_name,'w')
    ab.write(str(cex_abs_depth)+'\n')
    ab.write(str(time_abs_prev)+'\n')
    ab.write(str(time_abs)+'\n')
    ab.write(str(abs_depth_prev)+'\n')
    ab.write(str(abs_depth))
    ab.close()

def abs_done(time_remain):
    """ heuristic to see if we are not making any progress and should quit
        look at frame of last cex found (cex_abs_depth)  for current abstraction using a parallel engine
        look at depth of current abstraction (abs_depth) and last abstraction (abs_deptth_prev)
        look at time between new abstractions time_abs - time_abs_prev.
        compute approximate frames_per_sec
        if  frames_to_next_cex > frames_per_sec * time_remain
        then won't get there is the time allowed.
        We have to pass all the information along when we are doing things in parallel by writing a file
        with this info in it and reading it in later. This is because monitor_and prove
        runs in parallel and global variables are not passed around.
    """
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
##    print 'checking if abs has enough time to next cex'
    frames_to_next_cex = cex_abs_depth - abs_depth
    div = time_abs - time_abs_prev
    div = max(.1,div)
    frames_per_sec = (abs_depth - abs_depth_prev)/div
    if frames_per_sec <= 0:
        return False #something wrong 
    print 'frames_per_sec = %0.2f, frames_to_next_cex = %d, time remaining = %0.2f'%(frames_per_sec, frames_to_next_cex, time_remain)
    if frames_to_next_cex > 0.2*(frames_per_sec * time_remain): #later frames will take longer so factor of 5 here
        print 'not enough abs time to next cex'
        return True
    return False

##def gla_abs(t): 
##    """ Do gate-level abstraction for F frames """
##    r = 100 *(1 - abs_ratio)
##    run_command('orpos; &get;&gla -dv -A %s_vabs.aig -T %d -R %d; &w %s_gla.aig'%(f_name,t,r,f_name))
    
7971
        
.  
Baruch Sterin committed
7972 7973
def monitor_and_prove():
    """
7974 7975 7976 7977 7978
    monitor and prove. Runs in parallel with abstraction method.
    It looks for a new vabs and if found, will try to verify it in parallel
    We want to write a file that has variables
    cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
    which will be used by abs_done called by gla_abs_iter which is to replace gla_abs
.  
Baruch Sterin committed
7979 7980
    """
    global ifbip
7981
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
.  
Baruch Sterin committed
7982
    #write the current aig as vabs.aig so it will be regularly verified at the beginning.
7983 7984 7985
    name = '%s_vabs.aig'%f_name
    if os.access('%s'%name,os.R_OK): #make it so that there is no initial abstraction
        os.remove('%s'%name)
.  
Baruch Sterin committed
7986 7987 7988
    initial_size = sizeof()
    print 'initial size = ',
    print initial_size
7989 7990 7991 7992 7993 7994 7995 7996 7997 7998
    time_abs = time_abs_prev = time.time()
    cex_abs_depth = 0
    abs_depth = abs_depth_prev = 0
    write_abs_values()
##    if read_and_sleep(5): # wait until first abstraction when res is False
##        #time has run out as controlled by abs_time
##        return [Undecided_no_reduction] + ['read_and_sleep']
    t = abs_time +10
    tt = time.time()
##    print 'first read and sleep done'
.  
Baruch Sterin committed
7999
    #a return of Undecided means that abstraction might be good and calling routine will check this
8000
    while True: #here we iterate looking for a new abstraction and trying to prove it
.  
Baruch Sterin committed
8001
        time_done = abs_bad = 0
8002 8003 8004 8005 8006 8007
        funcs = [eval('(pyabc_split.defer(read_and_sleep)())')]
        J = sims+intrps+pdrs+bmcs
        J = modify_methods(J,1)
        funcs = funcs + create_funcs(J,t) 
        mtds = ['read_and_sleep'] + sublist(methods,J)
        print 'methods = %s'%mtds
.  
Baruch Sterin committed
8008
        for i,res in pyabc_split.abc_split_all(funcs):
8009
##            print 'Mon. & Pr.: ,
.  
Baruch Sterin committed
8010
##            print i,res
8011
            if i == 0: # read_and_sleep terminated
.  
Baruch Sterin committed
8012
                if res == False: #found new abstraction
8013 8014 8015 8016 8017
                    read_abs_values()
                    time_abs_prev = time_abs
                    time_abs = time.time()
                    print 'time between new abstractions = %0.2f'%(time_abs - time_abs_prev)
                    write_abs_values()
.  
Baruch Sterin committed
8018 8019
                    abs_bad = 0 #new abs starts out good.
                    if not initial_size == sizeof() and n_latches() > abs_ratio * initial_size[2]:
8020
                        return [Undecided_no_reduction]+['read_and_sleep']
.  
Baruch Sterin committed
8021 8022 8023 8024
                    else:
                        break
                elif res == True: # read and sleep timed out
                    time_done = 1
8025
                    print 'read_and_sleep timed out'
.  
Baruch Sterin committed
8026
                    if abs_bad:
8027
                        return [Undecided_no_reduction]+['read_and_sleep']
.  
Baruch Sterin committed
8028
                    else: #abs is still good. Let other engines continue
8029
                        return [Undecided]+['read_and_sleep'] #calling routine handles >Unsat all the same right now.
.  
Baruch Sterin committed
8030 8031 8032
                else:
                    assert False, 'something wrong. read and sleep did not return right thing'
            if i > 0: #got result from one of the verify engines
8033 8034 8035 8036 8037
                print 'monitor_and_prove: Method %s terminated'%mtds[i],
##                print i,res
                if res == None:
                    print 'Method %s failed'%mtds[i]
                    continue
.  
Baruch Sterin committed
8038
##                print 'method %s found SAT in frame %d'%(mtds[i],cex_frame())
8039 8040 8041 8042 8043 8044 8045 8046 8047 8048
                if is_unsat() or res == Unsat or res == 'UNSAT':
                    print '\nParallel %s proved UNSAT on current abstr\n'%mtds[i]
                    return [Unsat] + [mtds[i]]
                elif is_sat() or res < Unsat or res == 'SAT': #abstraction is not good yet.
                    print 'method = %s'%mtds[i]
                    if not mtds[i] == 'RareSim': #the other engines give a better estimate of true cex depth
                        read_abs_values()
                        cex_abs_depth = cex_frame()
                        write_abs_values()
                    print '\nParallel %s found SAT on current abstr in frame %d\n'%(mtds[i],cex_frame())
.  
Baruch Sterin committed
8049 8050
##                    print 'n_vabs = %d'%n_vabs
                    if initial_size == sizeof():# the first time we were working on an aig before abstraction
8051 8052
                        print initial_size == abstraction_size
                        return [Sat]+[mtds[i]]
.  
Baruch Sterin committed
8053 8054 8055 8056
##                    print 'current abstraction invalid'
                    abs_bad = 1 
                    break #this kills off other verification engines working on bad abstraction
                else: #one of the engines undecided for some reason - failed?
8057
                    print '\nParallel %s terminated without result on current abstr\n'%mtds[i]
.  
Baruch Sterin committed
8058 8059
                    continue
        if abs_bad and not time_done: #here we wait until have a new vabs.
8060 8061 8062 8063 8064
            time_remain = t -(time.time() - tt)
            abc('r %s_abs.aig'%f_name) #read in the abstraction to destroy is_sat().
            if abs_done(time_remain):
                return [Undecided]+['timeout']
            res = read_and_sleep(5) #this will check every 5 sec, until abs_time sec has passed without new abs            
.  
Baruch Sterin committed
8065 8066 8067
            if res == False: #found new vabs. Now continue if vabs small enough
##                print 'n_vabs = %d'%n_vabs
                if (not initial_size == sizeof()) and n_latches() > abs_ratio * initial_size[2]:
8068
                    return [Undecided_no_reduction]+['no reduction']
.  
Baruch Sterin committed
8069 8070 8071 8072
                else:
                    continue
            elif res ==True: #read_and_sleep timed out
##                print 'read_and_sleep timed out'
8073
                return [Undecided_no_reduction]+['no reduction']
.  
Baruch Sterin committed
8074 8075 8076
            else:
                break #this should not happen
        elif abs_bad and time_done:
8077 8078
##            print 'current abstraction bad, time has run out'
            return [Undecided_no_reduction]+['no reduction']
.  
Baruch Sterin committed
8079
        elif time_done: #abs is good here
8080 8081
##            print 'current abstraction still good, time has run out'
            return [Undecided]+['reduction'] #this will cause calling routine to try to verify the final abstraction
.  
Baruch Sterin committed
8082 8083
                            #right now handles the same as Undecided_no_reduction-if time runs out we quit abstraction
        else: #abs good and time not done
8084 8085 8086
            continue
##            print 'current abstraction still good, time has not run out'
    return [len(funcs)]+['error']
.  
Baruch Sterin committed
8087 8088 8089 8090

def read_and_sleep(t=5):
    """
    keep looking for a new vabs every 5 seconds. This is usually run in parallel with
8091 8092
    &vta -d or &gla
    Returns False when new abstraction is found, and True when time runs out.
.  
Baruch Sterin committed
8093
    """
8094
    global cex_abs_depth, abs_depth, abs_depth_prev, time_abs_prev, time_abs
.  
Baruch Sterin committed
8095 8096 8097
    #t is not used at present
    tt = time.time()
    T = 1000 #if after the last abstraction, no answer, then terminate
8098
    T = abs_time + 10
.  
Baruch Sterin committed
8099 8100 8101 8102
    set_size()
    name = '%s_vabs.aig'%f_name
##    if ifbip > 0:
##        name = '%s_vabs.aig'%f_name
8103 8104
##    print 'name = %s'%name
    sleep(5)
.  
Baruch Sterin committed
8105 8106
    while True:
        if time.time() - tt > T: #too much time between abstractions
8107
##            print 'read_and_sleep timed out in %d sec.'%T
.  
Baruch Sterin committed
8108
            return True
8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141
        if os.access(name,os.R_OK):
            #possible race condition
            run_command('&r -s %s; &w %s_vabs_old.aig'%(name,f_name))
##            print '%s exists'%name
            if not os.access(name,os.R_OK): #if not readable now then what was read in might not be OK.
                print '%s does not exist'%name
                continue
##            print '%s is read'%name
##            run_command('&r %s;read_status %s_vabs.status'%(name,f_name)) #need to use & space to keep the abstraction information
            os.remove(name)
            run_command('read_status %s_vabs.status'%f_name)
##            print '%s and %s_vabs.status have been read'%(name,f_name)
##            print 'reading %s_vabs.status'%f_name
            #name is the derived model (not the model with abstraction info
            run_command('&r -s %s_vabs_old.aig'%f_name)
            run_command('&w %s_gla.aig'%f_name)
            run_command('&gla_derive;&put')
            run_command('w %s_gabs.aig'%f_name)
##            print '%s is removed'%name
            read_abs_values()
            time_abs_prev = time_abs
            time_abs = time.time()
##            print 'abs values has been read'
            run_command('read_status %s_vabs.status'%f_name) 
            abs_depth_prev = abs_depth
            abs_depth = n_bmc_frames()
            write_abs_values()
##            print 'abs values has been written'
            time_remain = T - (time.time() - tt)
            if abs_done(time_remain):
            	return True
##            if not check_size():
            if True:
.  
Baruch Sterin committed
8142 8143
                print '\nNew abstraction: ',
                ps()
8144
##                print 'Time = %0.2f'%(time.time() - tt)
.  
Baruch Sterin committed
8145 8146 8147 8148 8149 8150 8151
                set_size()
                abc('w %s_abs.aig'%f_name)
                return False
            #if same size, keep going.
        print '.',
        sleep(5)
####################################################