swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Saving and reading rng state from (ascii) file?


From: Jan Kreft
Subject: Re: Saving and reading rng state from (ascii) file?
Date: Tue, 13 Jun 2000 17:13:01 +0100 (BST)

Thanks Marcus,

I thought you were on vacation?

Anyway, I went ahead and wrote a procedure, first for testing only, that
gets the state, saves it to file, reads the file in, and sets the state to
what was read in from file. It works in the sense that I get the same
results from my simulation whether I skip this step or not.

It is ugly in the sense that the state structure is discarded, and more
than necessary saved, so the idea of using ascii to have it human readable
didn't work out.

Here it is:

-saveRngState {
  
  unsigned i;
  unsigned rngStateSize;
  unsigned *rngState;

  char filename[256];
  FILE *file;
  timeval_t now = [geckoControl getCurrentTime];
  int thisRun = [geckoControl getNumRun];

  int num = 0;
  int numread;
  char *line;
  const char delimiters[] = " \t\n\r";
  char *token;

  rngStateSize = [randSource getStateSize];
  rngState = (unsigned *) [globalZone alloc: [randSource getStateSize]];  
  [randSource putStateInto: rngState];

  sprintf(filename, "%s_%03ld_%06ld.rng", [geckoControl getFileStem], thisRun, 
now);
  file = fopen(filename, "w"); // overwrite, don't append
  for (i=0; i < (unsigned) rngStateSize/sizeof(unsigned); i++)
    fprintf(file, "%u\n", rngState[i]);
  fclose(file);

  file = fopen(filename, "r");
  if (file != NULL) {
    i = 0;
    numread = getline(&line, &num, file);
    while (numread > 1) {
      token = strtok (line, delimiters);
      while (token != NULL) {
        // fill token string into unsigned integer number
        rngState[i] =  (unsigned) strtoul(token, NULL, 10); // 
string-to-unsigned-long
        i++;
        token = strtok (NULL, delimiters);
      }    
      numread = getline(&line, &num, file);
    }
    fclose(file);
  }

  [randSource setStateFrom: rngState];
  [globalZone free: rngState];

  return self;
}

On 13 Jun 2000, Marcus G. Daniels wrote:

> >>>>> "JK" == Jan Kreft <address@hidden> writes:
> 
> JK> I'm trying to regularly save the state of the default MT19937 rng
> JK> into a file, to be able to restart a run where it broke off due to
> JK> power cut etc.
> 
> The generators should be extended with
> -{lisp,hdf5}{In,Out{Deep,Shallow}} methods so that loading and saving
> can be done in the same way as other objects.  One problem with the
> way it is now is that the state structure is private and specific to
> the generator.  However, you can load and save the date in untyped
> binary format using C functions like fread or fwrite.  The size can be
> acquired from getStateSize.
> 
> 
>                   ==================================
>    Swarm-Support is for discussion of the technical details of the day
>    to day usage of Swarm.  For list administration needs (esp.
>    [un]subscribing), please send a message to <address@hidden>
>    with "help" in the body of the message.
> 
> 


                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]