RepastHPC  2.3.1
initialize_random.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * DistributionFactory.h
36  *
37  * Created on: Oct 27, 2009
38  * Author: nick
39  */
40 
41 #ifndef INITRANDOM_H_
42 #define INITRANDOM_H_
43 
44 #include <boost/mpi/communicator.hpp>
45 
46 #include "Properties.h"
47 
48 #define GLOBAL_RANDOM_SEED_PROPERTY "global.random.seed"
49 #define RANDOM_SEED_PROPERTY "random.seed"
50 
51 namespace repast {
52 
57 void initializeRandom(Properties& props, boost::mpi::communicator* comm = 0);
58 
59 /*
60  * Generates a random number seed to use to initialize the random singleton
61  *
62  * Usage:
63  *
64  * Two properties can be specified in the properties object: "global.random.seed"
65  * and "random.seed"
66  *
67  * If global.random.seed is used, it must include either a numeric value usable as a seed
68  * OR the string 'AUTO'. If the numeric value is specified, this value is used as the
69  * random number seed on all processes. If the 'AUTO' value is specified, a seed is generated
70  * from system time on process 0 and communicated to all processes, so that all processes
71  * use the same seed. Note that if 'AUTO is used, a communicator must be provided to this
72  * function or an error will be thrown.
73  *
74  * If no global.random.seed is present in the properties collection, each processor will have
75  * its own random number seed. The seed is determined by the value for random.seed
76  * in the properties object and the presence or absence of a communicator object passed to
77  * this function. The value of random.seed must be either a numeric value usable as a seed
78  * or the string 'AUTO'; if no value for random.seed is specified, behavior is identical
79  * to random.number=AUTO. If the 'AUTO' value is specified or the property is omitted, a seed is
80  * generated from the system time; note that each process may have a different value for the
81  * seed because the system time will not necessarily be synchronized (though it cannot also
82  * be guaranteed that each process will have a unique value, and the values will likely be close
83  * together and thus not be completely independent). If a communicator object is passed to
84  * the function, the seeds used on each process will be different, but will be derived from
85  * the seed on process 0.
86  *
87  * If either 'global.random.seed' or 'random.seed' is "AUTO" in the properties collection,
88  * it is re-set with the value used for the seed; if both are absent, a random.seed value is added.
89  * If the seeds are different on each process, the property value in each process's instance
90  * will reflect the seed that it used; for seeds created using a communicator, the seed on
91  * process '0' should be preferentially recorded because it can be used to recreate the
92  * whole simulation run.
93  *
94  * In summary:
95  *
96  * If 'global.random.seed' IS in the properties collection:
97  *
98  * global.random.seed=<numeric>:
99  * All processes will use this value as the random number seed.
100  *
101  * global.random.seed=AUTO:
102  * A seed based on system time will be generated on proc 0 and sent to all other processes;
103  * a communicator must be passed to this function.
104  *
105  *
106  * If 'global.random.seed' is NOT in the properties collection:
107  *
108  * random.seed=AUTO (or omitted) & No communicator passed:
109  * All processes use local system time to create their random number seeds
110  *
111  * random.seed=<numeric> & No communicator passed:
112  * All processes use the value of random.seed (if all properties collections are the
113  * same on all processes, the seed is effectively global).
114  *
115  * random.seed=AUTO (or omitted) & Communicator passed:
116  * System time on process 0 is used to create a seed that is broadcast to all other processes;
117  * this value is then used by each process to derive different random seeds according to
118  * rank, and these are then used to initialize the random number generation system.
119  *
120  * random.seed=<numeric> & Communicator passed:
121  * The random number seed in the properties collection will be used by each process to create
122  * different random seeds according to rank; these are then used to initialize the random
123  * number generation system.
124  */
125 void initializeSeed(Properties& props, boost::mpi::communicator* comm);
126 
127 }
128 
129 #endif