Thursday, March 17, 2016

constraints -- blessing or cursing ?


Sometimes, we might hit the constraint solver timeout error in our simulations. I was curious in knowing the reason. It has to do more with how we are coding the constraints.  I found the below link very interesting.


One classic example is below and I think we can relate that to our complex benches  :-
When iterative constraints are used on arrays, each element has a constraint on it. If the constraints are simple enough to implement with out using constraint block, simulation time may be saved. In the following example, there are two constraints blocks, one for size other for each element. In reality there may be more constraint blocks. 



EXAMPLE:
class Eth_pkt;
rand byte Payload[] ;
constraint size_c { Payload.size() inside {[46:1500]}; }
constraint element_c { foreach ( Payload[ i ] ) Payload[ i ] inside {[50:100]}; }
endclass

program iterative_68;
Eth_pkt obj
;
initial
begin
obj 
= new();
for(int i=0;i< 10000;i++)
begin
if(obj.randomize())
$display(" RANDOMIZATION DONE ");
end
$finish(2);
end
endprogram
RESULT:

# ** Note: Data structure takes 3407960 bytes of memory
# Process time 705.51 seconds


The above logic can implemented using post_randomize. Check how these two example with ur vendor tool and look at the simulation speed. You may find difference.
EXAMPLE:
class Eth_pkt;
rand integer length;
byte Payload[] ;
constraint size_c { length inside {[46:1500]}; }
function void post_randomize;
Payload 
= new[length];
for(int i=0;i< length;i++)
Payload
[ i ] = 50 + $urandom % 51 ;
endfunction
endclass

program iterative_69;
Eth_pkt obj
;
initial
begin
obj 
= new();
for(int i=0;i< 10000;i++)
begin
if(obj.randomize())
$display(" RANDOMIZATION DONE ");
end
$finish(2);
end
endprogram

# ** Note: Data structure takes 3539032 bytes of memory
# Process time 3.92 seconds 

I copy-pasted this code and ran in VCS simulator.
Look at the time difference J. I really got amazed.

CPU Time:    101.660 seconds;       Data structure size:   0.0Mb

CPU Time:      1.190 seconds;       Data structure size:   0.0Mb



No comments:

Post a Comment