1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/****************************************************************************/
/* ____Demonstrates checkpointing____ */
/* In a new run, this program initializes a random number stream and prints */
/* a few random numbers. Finally it packs the state of the stream into a */
/* file. In a continuation of an old run this program reads in the state of */
/* a stream from a file, prints a few random numbers, and stores the final */
/* state of the stream into a file. */
/****************************************************************************/
#include <stdio.h>
#define SIMPLE_SPRNG /* simple interface */
#include "sprng.h" /* SPRNG header file */
#define SEED 985456376
main(int argc, char *argv[])
{
int i, size;
double rn;
FILE *fp;
char buffer[MAX_PACKED_LENGTH], outfile[80], infile[80], *bytes;
int gtype; /*--- */
/*--- reading in a generator type */
#include "gen_types_menu.h"
printf("Type in a generator type (integers: 0,1,2,3,4,5): ");
scanf("%d", >ype);
/*********************** Initialize streams *******************************/
printf("Enter name of file to store final state of the stream:\n");
scanf("%s", outfile);
printf("Enter name of file to read from:\n\t(enter 9 for a new run)\n");
scanf("%s", infile);
if(infile[0] == '9') /* initialize stream the first time */
init_sprng(gtype,SEED,SPRNG_DEFAULT);
else /* read stream state from file afterwards */
{
fp = fopen(infile,"r");
if(fp == NULL)
{
fprintf(stderr,"Could not open file %s for reading\nCheck path or permissions\n", infile);
exit(1);
}
fread(&size,1,sizeof(int),fp);
fread(buffer,1,size,fp);
unpack_sprng(buffer);
fclose(fp);
}
/*********************** print random numbers *****************************/
printf(" Printing 5 random numbers in [0,1): \n");
for(i=0; i<5; i++)
{
rn = sprng(); /* generate a double precision random number */
printf("%d %f\n", i+1, rn);
}
/************************* store stream state *****************************/
size = pack_sprng(&bytes); /* pack stream state into an array */
fp = fopen(outfile,"w"); /* open file to store stream state */
if(fp == NULL)
{
fprintf(stderr,"Could not open file %s for writing\nCheck path or permissions\n", outfile);
exit(1);
}
fwrite(&size,1,sizeof(int),fp); /* store # of bytes required for storage */
fwrite(bytes,1,size,fp); /* store stream state */
fclose(fp);
free(bytes); /* free memory needed to store stream state */
}
|