void random_set_state_wrapper(int stateid) { random_state state; /* State to restore. */ THREAD_LOCK(state_table_lock); state = table_get_data(state_table, stateid); THREAD_UNLOCK(state_table_lock); if (!state) die("Unmatched state handle."); random_set_state(state); return; }
void read_checkpoint ( char *filename, int *gen, multipop **mpop ) { FILE *f; char *buffer; ephem_const **eind; int random_state_bytes; int i, j; char *rand_state; /* miscellaneous buffer for reading. */ buffer = (char *)MALLOC ( MAXCHECKLINELENGTH ); /* open the file. */ f = fopen ( filename, "rb" ); if ( f == NULL ) { error ( E_FATAL_ERROR, "couldn't read checkpoint \"%s\".", filename ); } oprintf ( OUT_SYS, 30, "reading from checkpoint \"%s\".\n", filename ); /** confirm the magic word that starts every checkpoint file. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); if ( strcmp ( buffer, CK_MAGIC ) ) error ( E_FATAL_ERROR, "\"%s\" is not a lil-gp v1.0 checkpoint file.", filename ); /* skip the human-readable id line. */ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "id line: %s", buffer ); #endif /** read and print the timestamp. **/ fscanf ( f, "%*s " ); fgets ( buffer, MAXCHECKLINELENGTH, f ); /* chop the newline. */ buffer[strlen(buffer)-1] = 0; oprintf ( OUT_SYS, 30, " checkpoint timestamp: [%s].\n", buffer ); /* skip the "section: global" line. */ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be global section: %s", buffer ); #endif /* read the generation number. */ fscanf ( f, "%*s %d\n", gen ); /** read the random number state encoded as a string of hex chars. **/ /* first read the length. */ fscanf ( f, "%*s %d ", &random_state_bytes ); #ifdef DEBUG fprintf ( stderr, "%d random state bytes.\n", random_state_bytes ); #endif /* allocate the buffer. */ rand_state = (char *)MALLOC ( random_state_bytes+1 ); /* read the hex data into the buffer. */ read_hex_block ( rand_state, random_state_bytes, f ); /* set the state. */ random_set_state ( rand_state ); /* free the buffer. */ FREE ( rand_state ); /* slurp the newline character following the hex data. */ fgetc ( f ); /** skip the "section: parameter" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be parameter section: %s", buffer ); #endif /* read the parameter database. */ read_parameter_database ( f ); /* make internal copies of function set(s). */ if ( app_build_function_sets() ) error ( E_FATAL_ERROR, "app_build_function_sets() failure." ); /** skip the "section: erc" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be erc section: %s", buffer ); #endif /* read the list of ephemeral constants, and index them */ eind = read_ephem_list ( f ); /** skip the "section: erc" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be population section: %s", buffer ); #endif /** read the population **/ /* allocate memory. */ *mpop = (multipop *)MALLOC ( sizeof ( multipop ) ); /* read number of subpops. */ fscanf ( f, "%*s %d\n", &((**mpop).size) ); /* allocate subpop list. */ (**mpop).pop = (population **)MALLOC ( (**mpop).size * sizeof ( population * ) ); for ( i = 0; i < (**mpop).size; ++i ) { /** skip each "subpop: #" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be subpop %d: %s", i, buffer ); #endif /* read the population. */ (**mpop).pop[i] = read_population ( eind, f ); } /** skip the "section: application" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be application section: %s", buffer ); #endif /* read application-specific stuff. */ app_read_checkpoint ( f ); /** skip the "section: statistics" line. **/ fgets ( buffer, MAXCHECKLINELENGTH, f ); #ifdef DEBUG printf ( "should be statistics section: %s", buffer ); #endif /* read the statistics. */ read_stats_checkpoint ( *mpop, eind, f ); /* close'n'free. */ FREE ( eind ); FREE ( buffer ); fclose ( f ); oprintf ( OUT_SYS, 30, "population read from checkpoint \"%s\".\n", filename ); }