cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
arquer
Explorer
Explorer
4,020 Views
Registered: ‎03-23-2015

strange randomization behavior

Hello,

 

Since XSIm does no support setting a global random seed through command line/tcl for the time being I am passing a paramenter to my simulation (SEED) which I use to randomize the TB.

 

inside the testbench I have someting like this:

 

(...)
initial begin

(...)
$display("Seeting sim seed to : %d", SEED); $srandom(SEED); fork forever begin $display("urandom_range generated number: %d", $urandom_range(1,256)); $display("urandom generated number: %d", $urandom()); (...) random_access_read_data = new[$urandom_range(1, 256)]; $display("Random size: %03d", random_access_read_data.size()); startaddr = $urandom_range(32'h800, 32'hFFF - random_access_write_data.size()*4); $display("Random start address: 0x%04x", startaddr); (...) end join_none (..) end

I have two simulation to which I pass different seeds. I would expect alll the $display() calls to show different values, however I get:

 

Sim1:

Seeting sim seed to :              1652197961
urandom_range generated number:    208
urandom generated number:          -1192071567
Ransom size:                       147
Ransom start address:              0x0d4c

Sim2:

Seeting sim seed to :             844802871
urandom_range generated number:   158
urandom generated number:         -1444523949
Ransom size:                      147
Ransom start address:             0x0d4c

As you can see the SEED passed are indeed different, and so are the two first generated random numbers (both with urandom_range and urandom). However the values that I actually care about, the size of the dynamic array and the start address are the same!

How can it be if those are generated using the same calls as the first three?!

 

I really don't know what to make of this :(

0 Kudos
10 Replies
sunilku
Xilinx Employee
Xilinx Employee
3,979 Views
Registered: ‎08-10-2015

Hi @arquer,

 

I am getting different results for different seed values.

Test case:

===================

module test;
parameter p1 = 12;
parameter p2 = 14;
int random_access_read_data[];
int startaddr;

initial begin 

  $srandom(p1);

  $display("urandom_range number: %d", $urandom_range(1,256));
  $display("urandom number: %d", $urandom());
  random_access_read_data = new[$urandom_range(1, 256)];
  $display("Random size: %03d", random_access_read_data.size());

  $srandom(p2);

  $display("urandom_range number: %d", $urandom_range(1,256));
  $display("urandom number: %d", $urandom());
  random_access_read_data = new[$urandom_range(1, 256)];
  $display("Random size: %03d", random_access_read_data.size());
end
endmodule

==================

Result:

urandom_range number:        241
urandom number: 1141840264
Random size: 219
urandom_range number:         63
urandom number:  427998515
Random size: 077

 

Please share your design file so that I can check and update you.

 

 

Thanks,

Sunilkumar.

0 Kudos
arquer
Explorer
Explorer
3,950 Views
Registered: ‎03-23-2015

Hi @sunilku

 

the issue seems to appear when I have both a fork/join (as in my example) and also a @(posedge Clk) in between the random calls, please try the following:

 

(...)

logic Clk = 0;
always #5 Clk = !Clk;
initial begin (...) $display("Seeting sim seed to : %d", SEED); $srandom(SEED); fork forever begin $display("urandom_range generated number: %d", $urandom_range(1,256)); $display("urandom generated number: %d", $urandom()); (...)
@posege(Clk);
(...) random_access_read_data = new[$urandom_range(1, 256)]; $display("Random size: %03d", random_access_read_data.size()); startaddr = $urandom_range(32'h800, 32'hFFF - random_access_write_data.size()*4); $display("Random start address: 0x%04x", startaddr); (...) end join_none (..) end

With this you should see how the generated values before the posedge change with the provided SEED but the ones after don't.. :(

0 Kudos
sunilku
Xilinx Employee
Xilinx Employee
3,941 Views
Registered: ‎08-10-2015

Hi @arquer,

 

Can you please share the complete test case.

 

 

Thanks,

Sunilkumar

0 Kudos
arquer
Explorer
Explorer
3,934 Views
Registered: ‎03-23-2015

I can not share the whole design... it is huge. That is why I took the time to write a minimum code which reproduces the issue...

 

 

 

module moduleTb#(
    parameter longint SEED       = 123456789
) ();

    timeunit 1ns;
    timeprecision 1ps;
    logic       Clk = 1'b1;

    always #5 Clk = !Clk;

    initial begin
        logic [31:0] random_access_read_data [];
        logic [31:0] random_access_write_data [];
        int startaddr;

        $display("Setting sim seed to : %d", SEED);
        $srandom(SEED);

        fork
            forever begin
                $display("urandom_range generated number: %d", $urandom_range(1,256));
                $display("urandom generated number: %d", $urandom());
                //Comment the following line and the issue seems to go away...
                //@(posedge Clk);
                random_access_read_data = new[$urandom_range(1, 256)];
                $display("Random size: %03d", random_access_read_data.size());
                random_access_write_data = new[random_access_read_data.size()];
                startaddr = $urandom_range(32'h800, 32'hFFF - random_access_write_data.size()*4);
                $display("Random start address: 0x%04x", startaddr);
                $finish();
            end
        join_none

    end

endmodule

 

 If you run this code as with vivado 2017.2 you should see:

 

Sim1:

Setting sim seed to :             844802871
urandom_range generated number:        158
urandom generated number: 2850443347
Random size: 144
Random start address: 0x0d5a

Sim2:

Setting sim seed to :            1652197961
urandom_range generated number:        208
urandom generated number: 3102895729
Random size: 087
Random start address: 0x0ad9

So different numbers, all good.

However if you uncomment that @(posedge Clk) line, you will see:

 

Sim1:

Setting sim seed to :             844802871
urandom_range generated number:        158
urandom generated number: 2850443347
Random size: 147
Random start address: 0x0970

Sim2:

Setting sim seed to :            1652197961
urandom_range generated number:        208
urandom generated number: 3102895729
Random size: 147
Random start address: 0x0970

Seeds are different and generated values before the posedge are indeed different, however after that line the seed seems to be restored to some other value....

0 Kudos
arquer
Explorer
Explorer
3,910 Views
Registered: ‎03-23-2015

Anyone?

 

If someone could at least verify the issue I am seeing I will go ahead and open a support ticket. I just wanna make sure I am not doing something wrong.

 

Thanks.

0 Kudos
sunilku
Xilinx Employee
Xilinx Employee
3,889 Views
Registered: ‎08-10-2015

Hi @arquer,

 

I am not seeing any difference in the result. In both scenarios I am seeing the same results if I run simulation multiple times.

Please find the results below.

sim1:
Setting sim seed to :             123456789
urandom_range generated number:         83
urandom generated number: 2730531909
Random size: 055
Random start address: 0x0e46

sim2:
Setting sim seed to :             123456789
urandom_range generated number:         83
urandom generated number: 2730531909
Random size: 055
Random start address: 0x0e46


If i uncomment the the line  @(posedge Clk)

sim1:
Setting sim seed to :             123456789
urandom_range generated number:         83
urandom generated number: 2730531909
Random size: 147
Random start address: 0x0970

sim2:
Setting sim seed to :             123456789
urandom_range generated number:         83
urandom generated number: 2730531909
Random size: 147
Random start address: 0x0970.

 

 

Which OS and Vivado version are you using?

 

Thanks,

Sunilkumar

0 Kudos
arquer
Explorer
Explorer
3,880 Views
Registered: ‎03-23-2015

Hi @sunilku

 

the reason you are not seeing any variation in the random numbers generated is because both of your simulations have te same SEED parameter (the default one).

 

You can see this at the beggining of the simulation when the SEED paramenter is printed out.

 

You can modify the seed either by directly modifying the code or by adding an elaboration parameter to elaboration such as:

 

-generic_top "SEED=XXXXXXX"

 

You can try to assign the same seeds I assigned and hopefully you should see the exact same numbers as me.

0 Kudos
dsheils
Moderator
Moderator
3,793 Views
Registered: ‎01-05-2017

Hi All,

I looked into the support case for the issue in this thread. As @arquer mentioned above, you'll get the same sim results if the SEED parameter is the same. You'll need to change it for each simulation run.

 

Best Regards,

D

 

Can you please mark the appropriate post as an answer "Accept as solution" in case it helped resolve your query?

This will help other users in future looking for the same query with an accepted answer.

 

It will also close this thread.

-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
0 Kudos
arquer
Explorer
Explorer
3,786 Views
Registered: ‎03-23-2015

Hi @dsheils,

 

Have you tested the provided examples with the same seeds I posted the results for?

 

Thanks.

0 Kudos
dsheils
Moderator
Moderator
2,059 Views
Registered: ‎01-05-2017

Just to wrap this one up, the issue above was identified as being an issue with the tools. A CR was filed to get it fixed in a future version of Vivado.

-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------