Note that we have included the SPRNG header file We have also defined the macroSIMPLE_SPRNG before including the SPRNG header file in order to invoke the simple interface.
When sprng is called the first time, it initializes a default stream using default parameters, since no explicit initialization has been performed. It then returns double precision random numbers in [0,1).
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | Not Available |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o simple-simple simple-simple.c -L../lib -lsprng -lgmp
Note: -64 indicates the 64 bit compilation available on the Power Challenge.
Output:
sprng/EXAMPLES:sif% simple-simple Printing 3 random numbers in [0,1): 0.580114 0.740295 0.892874
Note that we have included the SPRNG header file.
First we initialize a random number stream. The value of the seed is defined to be 985456376. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return double precision random numbers in [0,1) and then isprng is called to return random integers in [0,231).
Code:
Simple Interface | Default/Pointer
Checking Interface |
|||
---|---|---|---|---|
C | FORTRAN | C | C++ | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
f77 -DPOINTER_SIZE=8 -64 -I../include -o sprngf sprngf.F -L../lib -lsprng -lgmp
Note: -DPOINTER_SIZE=8 indicates that the pointer size is 8 bytes, rather than the default 4 bytes assumed by SPRNG.
Output:
sprng/EXAMPLES:sif% sprngf Print information about new stream: Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Printing 3 random numbers in [0,1): 0.707488 0.664048 0.005616 Printing 3 random integers in [0,2^31): 1873949618 1484742006 602016304
Note that we have included the SPRNG and MPI header files at the beginning.
First we initialize MPI.
We then initialize a random number stream. The value of the seed is defined to be 985456376. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return double precision random numbers in [0,1).
Code:
Simple Interface | Default/Pointer
Checking Interface |
|||
---|---|---|---|---|
C | C++ | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
CC -64 -I../include -o sprng-simple_mpi sprng-simple_mpi.C -L../lib -lsprng -lgmp -L/usr/lib64 -lmpi
Note: CC is the C++ compiler.
Output:
sprng/EXAMPLES:sif% mpirun -np 2 sprng-simple_mpi Process 0, print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Process 0, random number 1: 0.70748766363408 Process 0, random number 2: 0.66404841879710 Process 0, random number 3: 0.00561565119362 Process 1, print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863259 multiplier = 44485709377909 Process 1, random number 1: 0.99144237784703 Process 1, random number 2: 0.15098918187292 Process 1, random number 3: 0.09757740755008Note that the order in which the output is printed may differ.
Note that we have included the SPRNG and MPI header files sprng.h and mpi.h respectively at the beginning. Before including the SPRNG header file, we have defined the macroFLOAT_GEN, which results in single precision random numbers being generated by calls to sprng.
First we initialize MPI.
We then initialize a random number stream. The value of the seed is defined to be 985456376. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return single precision random numbers in [0,1).
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
f90 -DPOINTER_SIZE=8 -64 -I../include -o fsprngf_mpi fsprngf_mpi.F -L../lib -lsprng -lgmp -L/usr/lib64 -lmpi
Note: f90 is the FORTRAN 90 compiler.
Output:
sprng/EXAMPLES:sif% mpirun -np 2 fsprngf_mpi Process 0: Print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Process 0, random number 1: 0.707488 Process 0, random number 2: 0.664048 Process 0, random number 3: 0.005616 Process 1: Print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863259 multiplier = 44485709377909 Process 1, random number 1: 0.991442 Process 1, random number 2: 0.150989 Process 1, random number 3: 0.097577Note that the order in which the output is printed may differ.
Note that we have included the SPRNG header file at the beginning.
First we initialize a random number stream. The value of a seed is obtained from a call to make_sprng_seed and is based on system date and time information. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return double precision random numbers in [0,1).
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o seed-simple seed-simple.c -L../lib -lsprng -lgmp
Output:
sprng/EXAMPLES:sif% seed-simple Printing information about new stream Linear Congruential Generator seed = 178783069, prime = 11863279 multiplier = 44485709377909 Printing 3 random numbers in [0,1): 0.856970 0.110647 0.795213
Note that we have included the SPRNG and MPI header files at the beginning. Before including the SPRNG header file, we have defined the macroUSE_MPI, which enables SPRNG to make MPI calls to ensure that the same seed is obtained on each process.
First we initialize MPI.
We then initialize a random number stream. The value of the seed is obtained from a call to make_sprng_seed. This function makes MPI calls to ensure that the same seed is returned in each process, assuming that the MPI version of SPRNG has been installed. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return double precision random numbers in [0,1).
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o seed-simple_mpi seed-simple_mpi.c -L../lib -lsprng -lgmp -L/usr/lib64 -lmpi
Output:
sprng/EXAMPLES:sif% mpirun -np 2 seed-simple_mpi Process 0: seed = 837321565 Process 1: seed = 837321565 Process 0: Print information about stream: Linear Congruential Generator seed = 837321565, prime = 11863279 multiplier = 44485709377909 process 0, random number 1: 0.732603 process 0, random number 2: 0.753095 process 0, random number 3: 0.084726 Process 1: Print information about stream: Linear Congruential Generator seed = 837321565, prime = 11863259 multiplier = 44485709377909 process 1, random number 1: 0.200456 process 1, random number 2: 0.445652 process 1, random number 3: 0.576297Note that the order in which the output is printed may differ.
Note that we have included the SPRNG header file sprng.h at the beginning.
First we initialize a random number stream, if this is a new run. The value of the seed is defined to be 985456376. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
If this is the continuation of a previous run, then we read in the state of a stream from the input file, and unpack it to obtain a random number stream.
sprng is next called to return double precision random numbers in [0,1).
Finally, we pack the state of the stream into an array and store the contents of this array into the output file.
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o checkpoint-simple checkpoint-simple.c -L../lib -lsprng -lgmp
Output:
sprng/EXAMPLES:sif% checkpoint Enter name of file to store final state of the stream: temp Enter name of file to read from: (enter 9 for a new run) 9 Printing 5 random numbers in [0,1): 1 0.707488 2 0.664048 3 0.005616 4 0.872626 5 0.691387 sprng/EXAMPLES:sif% checkpoint Enter name of file to store final state of the stream: temp Enter name of file to read from: (enter 9 for a new run) temp Printing 5 random numbers in [0,1): 1 0.280336 2 0.005228 3 0.533545 4 0.710482 5 0.891603
Note that we have included the SPRNG and MPI header files at the beginning.
First we initialize MPI.
We then initialize a random number stream on process 0. The value of the seed is defined to be 985456376. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called by process 0 to return double precision random numbers in [0,1).
Process 0 then packs the state of the stream into an array and sends it to process 1, which unpacks it and prints a few more random numbers from that stream. In this process, the original default stream on process 1 is over-written by the unpacked stream.
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
C | FORTRAN | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o message-simple_mpi message-simple_mpi.c -L../lib -lsprng -lgmp -L/usr/lib64 -lmpi
Output:
sprng/EXAMPLES:sif% mpirun -np 2 message-simple_mpi Process 0: Print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Process 0: Print 2 random numbers in [0,1): Process 0: 0.707488 Process 0: 0.664048 Process 0 sends stream to process 1 Process 1 has received the packed stream Process 1: Print information about stream: Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Process 1 prints 2 numbers from received stream: Process 1: 0.005616 Process 1: 0.872626Note that the order in which the output is printed may differ.
Note that we have included the SPRNG and MPI header files at the beginning.
First we initialize MPI and determine the number of processes and the rank of the local process.
We then initialize a random number stream. The value of the seed is defined to be 985456376. We shall have two streams per process, but is only one of these streams will be distinct. The other stream will be identical on each process. This is useful in situations in which we would like all the processes to take the same decision based on a random number. Thus the number of distinct streams is one more than the total number of processor, resulting in nstreams = nprocs + 1. For the distinct stream on each process, we initialize it with stream number as the rank of the local process, myid. The common stream is initialized with the stream number nprocs.
sprng is next called for each stream. It returns a double precision random numbers in [0,1) with the stream ID as its argument.
Finally, we free the memory used to store the states of the two streams by calls to free_sprng.
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
Not Available | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o 2streams_mpi 2streams_mpi.c -L../lib -lsprng -lgmp -L/usr/lib64 -lmpi
Output:
sprng/EXAMPLES:sif% mpirun -np 2 2streams_mpi Process 0: Print information about new stream Linear Congruential Generator seed = 985456376, prime = 11863279 multiplier = 44485709377909 Process 0: This stream is identical on all processes Linear Congruential Generator seed = 985456376, prime = 11863253 multiplier = 44485709377909 Process 0, random number (distinct stream) 1: 0.707488 Process 0, random number (distinct stream) 2: 0.664048 Process 0, random number (shared stream) 1: 0.700906 Process 0, random number (shared stream) 2: 0.602204 Process 1: Print information about new stream Linear Congruential Generator seed = 985456376, prime = 11863259 multiplier = 44485709377909 Process 1: This stream is identical on all processes Linear Congruential Generator seed = 985456376, prime = 11863253 multiplier = 44485709377909 Process 1, random number (distinct stream) 1: 0.991442 Process 1, random number (distinct stream) 2: 0.150989 Process 1, random number (shared stream) 1: 0.700906 Process 1, random number (shared stream) 2: 0.602204Note that the order in which the output is printed may differ.
Note that we have included the SPRNG header file at the beginning.
First we initialize a random number stream. The value of the seed is defined to be 985456376. Since there is only one stream, the number of streams nstreams = 1 and the stream number streamnum = 0. stream stores the returned ID of the initialized stream. We call print_sprng immediately after initialization so that we can record the particular stream we obtained, for later reference.
sprng is next called to return double precision random numbers in [0,1), with the stream ID as its argument.
We next spawn two streams from stream. Their ID's are stored in an array allocated by sprng. The variable new points to this array. We print information about the newly spawned streams, and then print a few random numbers from the second stream spawned.
Finally, we free the memory used to store the states of the streams by calls to free_sprng.
Code:
Simple Interface | Default/Pointer
Checking Interface |
||
---|---|---|---|
Not Available | C | FORTRAN |
Compilation: Example compilation on the SGI Power Challenge
Output:
Note that we have included
the SPRNG header file sprng.h
at the beginning. We have also defined the
macroCHECK_POINTERS
before including the SPRNG header file in order to invoke the interface
with pointer checking.
First we initialize a random number stream. The value of the seed
is defined to be
985456376. Since there is only one stream, the
number of streams nstreams = 1 and the stream number streamnum
= 0. stream stores the returned ID
of the initialized stream. We call
print_sprng immediately after
initialization so that we can record the particular stream we obtained,
for later reference.
sprng is next called to return double precision random numbers
in [0,1), with the stream ID as its argument.
Finally, we free the memory used to store the state of the stream by
a call to free_sprng.
we then try to use the freed stream again, which is an incorrect usage.
sprng returns -1.0 due to the invalid stream being passed
as an argument.
Code:
Compilation: Example compilation on the SGI Power Challenge
Output:
Note that we have included
the SPRNG header file at
the beginning. We have also defined the macroSIMPLE_SPRNG
before including the SPRNG header file in order to invoke the simple interface.
We define the macro
myrandom to sprng after including
the SPRNG header. This replaces the former function call by calls to SPRNG
instead.
We then add statements to initialize SPRNG.
Code:
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include convert.c -L../lib -lsprng -lgmp
Note: In the code provided with SPRNG, users need to define the macro
CONVERT
in order to get this converted code.
Output:
Note that we have included
the SPRNG header file sprng_f.h
at the beginning. We have also included this header file in the subroutine
sub1.
If we wished to define a macro for use by SPRNG, we need only do it once
in any file, before the first time a SPRNG header is included.
Subroutine sub1 is then called, which makes calls to sprng.
Code:
Compilation: Example compilation on the SGI Power Challenge
Output:
The user specifies the number of samples to be generated on the square.
The final state of the computations is stored in a file specified by the
user. The user can restart the computations by reading from this file in
subsequent runs. The user should also specify whether the current run is
a new one (in which case a new random number stream is initialized) or
an old one (in which case the state of the random number stream is read
from a file).
Note that we have included
the SPRNG header file at
the beginning. We have also defined the macroSIMPLE_SPRNG
before including the SPRNG header file in order to invoke the simple interface.
The function initialize is then called to take suitable actions
based on the information input by the user. A new run involves initializing
a random number stream. In a continuation of a previous run, the state
of the random number stream is read from a file whose name is input by
the user. The final state of the computations is also written into this
file. The user should also input the number of samples to be generated
in this run. In a new run, make_sprng_seed is called to produce
a seed based on system date and time information.
Next the function count_in_circle is called, which generates
points on a square of side 2. The number of points that fall on a circle
of unit radius inscribed within this square is returned by this function.
We then estimate PI as four times the proportion of points that
fall within the circle inscribed in the square.
Finally we call the function save_state to save the state of
the random number stream, the cumulative number of sample points generated
in all the runs, and the cumulative number of sample points that lie within
the inscribed circle in all the runs.
Code:
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o pi-simple pi-simple.c -L../lib -lsprng -lgmp
Output:
First we initialize MPI and call the function initialize to
initialize a random number stream. If this is the continuation of a previously
checkpointed run, then process 0 reads the previous state and passes to
to each process.
Next we divide the number of samples to be generated among all the processes
and then call the function count_in_circle as in the sequential
case. We then sum the number of points that fall within the circle across
all the processes by a call to MPI_REDUCE and estimate PI
as four times the proportion of points that fall within this circle inscribed
in the square.
Finally we call the function save_state to save the state of
the random number stream, the cumulative number of sample points generated
in all the runs, and the cumulative number of sample points that lie within
the inscribed circle in all the runs. Each process passes the state of
its stream to process 0, which then saves it to a file.
Code:
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o pi-simple pi-simple.c -L../lib -lsprng -lgmp
Compilation: Example compilation on the SGI Power Challenge
cc -64 -I../include -o pi-simple_mpi pi-simple_mpi.c -L../lib -lsprng
-lgmp -L/usr/lib64 -lmpi
Output:
sprng/EXAMPLES:sif% spawn
Print information about stream:
Linear Congruential Generator
seed = 985456376, prime = 11863279
multiplier = 44485709377909
Printing 2 random numbers in [0,1):
0.707488
0.664048
Spawned two streams
Information on first spawned stream:
Linear Congruential Generator
seed = 985456376, prime = 11863259
multiplier = 44485709377909
Information on second spawned stream:
Linear Congruential Generator
seed = 985456376, prime = 11863253
multiplier = 44485709377909
Printing 2 random numbers from second spawned stream:
0.700906
0.602204
11. invalid_ID
This program demonstrates the handling of invalid pointers in the interface
with pointer checking.
Simple Interface
Pointer
Checking Interface
Not Available
C
FORTRAN
sprng/EXAMPLES:sif% invalid_ID
Print information about random number stream:
Linear Congruential Generator
seed = 985456376, prime = 11863279
multiplier = 44485709377909
Printing 3 random numbers in [0,1):
0.707488
0.664048
0.005616
Expect a SPRNG error message on the use of an invalid stream ID
ERROR: Invalid generator ID 10014ed8
sprng returns -1.000000 on being given an invalid stream ID
Note that the incorrect ID, 10017258, may vary.
12. convert
This program demonstrates converting user code to call SPRNG instead of
the original random number generator myrandom.
Simple Interface
Default/Pointer
Checking Interface
C
FORTRAN
Not Available
sprng/EXAMPLES:sif% a.out
Print information about random number stream:
Linear Congruential Generator
seed = 985456376, prime = 11863279
multiplier = 44485709377909
Printing 3 random numbers in [0,1):
0.707488
0.664048
0.005616
13. subroutine
This program demonstrates the use of SPRNG in subroutines in FORTRAN programs.
Simple Interface
Default/Pointer
Checking Interface
Not Available
FORTRAN
sprng/EXAMPLES:sif% a.out
Printing information about new stream
Linear Congruential Generator
seed = 985456376, prime = 11863279
multiplier = 44485709377909
Printing 3 double precision numbers in [0,1):
1 0.7074876636340832
2 0.6640484187971047
3 0.0056156511936152
14. pi-simple
This is a serial program that demonstrates the use of several SPRNG features
in a simple example application involving the computation of PI
through a Monte Carlo method. We generate random points on a square of
side 2 which circumscribes a circle of unit radius. We determine the proportion
of points that fall within this circle. Since the ratio of the area of
the circle to the area of the square is PI/4, the estimated value
of PI is four times the proportion of points that lie within the
circle.
C
FORTRAN
sprng/EXAMPLES:sif% pi-simple
Enter 9 for a new run, or 2 for the continuation of an old run:
9
Enter name of file to store final state of the stream:
temp
Enter number of new samples:
10000
Linear Congruential Generator
seed = 437891907, prime = 11863279
multiplier = 44485709377909
pi is estimated as 3.1227999999999998 from 10000 samples.
Error = 0.0187927, standard error = 0.0164218
sprng/EXAMPLES:sif% pi-simple
Enter 9 for a new run, or 2 for the continuation of an old run:
2
Enter name of file to store final state of the stream:
temp
Enter number of new samples:
5000
pi is estimated as 3.1165333333333334 from 15000 samples.
Error = 0.0250593, standard error = 0.0134084
Note: Results may vary due to the use of a random seed.
15. pi-simple_mpi
This is a parallel version of is a parallel version of pi-simple
in which we divide the work of generating the samples among the available
processes.
C
sprng/EXAMPLES:sif% mpirun -np 2 pi-simple_mpi
Enter 9 for a new run, or 2 for the continuation of an old run:
9
Enter name of file to store final state of the stream:
temp
Enter number of new samples:
5000
Linear Congruential Generator
Linear Congruential Generator
seed = 42578755, prime = 11863279
multiplier = 44485709377909
seed = 42578755, prime = 11863259
multiplier = 44485709377909
pi is estimated as 3.1568000000000001 from 5000 samples.
Error = 0.0152073, standard error = 0.023224
Note: Results may vary due to the use of a random seed.
Ashok Srinivasan
ashoks@ncsa.uiuc.edu
Last modified: 1 Apr, 1997