Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verilator random number generated seeded with lrand48(), which isn't deterministic across platforms #1396

Closed
veripoolbot opened this issue Feb 4, 2019 · 3 comments

Comments

@veripoolbot
Copy link

@veripoolbot veripoolbot commented Feb 4, 2019


Author Name: Stan Sokorac
Original Redmine Issue: 1396 from https://www.veripool.org

Original Assignee: Wilson Snyder (@wsnyder)


Verilator uses a deterministic random number generator, but does the initial seeding using lrand48(), which will produce a different random seed on different platforms, even when seeded the same. This means that simulations that originally ran on, say, Linux, can't be reproduced accurately on OSX, or even different versions of Linux.

C++ offers mt19937_64 random number generator which is guaranteed to produce the same sequence on numbers on every system, given the same seed. For example, running this code below on different OSes will give you different numbers for lrand48, but always the same for mt19937.

#include <cstdlib>
#include <random>

int main(int argc, char *argv[])
{
  srand(1);
  for (int i=0; i < 3; i++)
     printf("lrand48[%d]=%ld\n", i, lrand48());

  std::mt19937_64 mt(1);
  for (int i=0; i < 3; i++)
     printf("mt19937[%d]=%llu\n", i, mt());
}

</code>

It would be helpful if Verilator used it to provide better reproducibility across systems. We've worked around this by using our own DPI random() function instead of $random, however there's no way around this for other randomness, like -x-assign/-x-initial.

Alternatively, some way to provide our own random seed/function to override the built-in ones would also do the trick.

@veripoolbot

This comment has been minimized.

Copy link
Author

@veripoolbot veripoolbot commented Feb 5, 2019


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2019-02-05T01:31:23Z


mt19937 is a good idea, but Verilator does not yet require C++11.

I added the +verilator+seed runtime option, pass the same number to every run and you should be all set, or have your main() call Verilator::randSeed(#). The default is old behavior, perhaps it should instead be a constant.

Fixed in git towards 4.012.

@veripoolbot

This comment has been minimized.

Copy link
Author

@veripoolbot veripoolbot commented Feb 8, 2019


Original Redmine Comment
Author Name: Armond Paiva
Original Date: 2019-02-08T17:52:48Z


Minor correction on setting the seed. It can be set programmatically with 'Verilated::randSeed(#)', not 'Verilator::randSeed(#)'

@veripoolbot

This comment has been minimized.

Copy link
Author

@veripoolbot veripoolbot commented Mar 24, 2019


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2019-03-24T01:15:50Z


In 4.012.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.