NEURON
nrnran123.h
Go to the documentation of this file.
1 #pragma once
2 
3 /* interface to Random123 */
4 /* http://www.thesalmons.org/john/random123/papers/random123sc11.pdf */
5 
6 /*
7 The 4x32 generators utilize a uint32x4 counter and uint32x4 key to transform
8 into an almost cryptographic quality uint32x4 random result.
9 There are many possibilites for balancing the sharing of the internal
10 state instances while reserving a uint32 counter for the stream sequence
11 and reserving other portions of the counter vector for stream identifiers
12 and global index used by all streams.
13 
14 We currently provide a single instance by default in which the policy is
15 to use the 0th counter uint32 as the stream sequence, words 2, 3 and 4 as the
16 stream identifier, and word 0 of the key as the global index. Unused words
17 are constant uint32 0.
18 
19 It is also possible to use Random123 directly without reference to this
20 interface. See Random123-1.02/docs/html/index.html
21 of the full distribution available from
22 http://www.deshawresearch.com/resources_random123.html
23 */
24 
25 #include <cstdint>
26 
27 
28 struct nrnran123_State;
29 
31  std::uint32_t v[4];
32 };
33 
34 /* global index. eg. run number */
35 /* all generator instances share this global index */
36 extern void nrnran123_set_globalindex(std::uint32_t gix);
37 extern std::uint32_t nrnran123_get_globalindex();
38 
39 /** Construct a new Random123 stream based on the philox4x32 generator.
40  *
41  * @param id1 stream ID
42  * @param id2 optional defaults to 0
43  * @param id3 optional defaults to 0
44  * @return an nrnran123_State object representing this stream
45  */
46 extern nrnran123_State* nrnran123_newstream(std::uint32_t id1,
47  std::uint32_t id2 = 0,
48  std::uint32_t id3 = 0);
49 
50 /** @deprecated use nrnran123_newstream instead **/
51 extern nrnran123_State* nrnran123_newstream3(std::uint32_t id1,
52  std::uint32_t id2,
53  std::uint32_t id3);
54 
55 /** Construct a new Random123 stream based on the philox4x32 generator.
56  *
57  * @note This overload constructs each stream instance as an independent stream.
58  * Independence is derived by using id1=1, id2=nrnmpi_myid,
59  * id3 = ++internal_static_uint32_initialized_to_0
60  */
62 
63 /** Destroys the given Random123 stream. */
65 
66 /** Get sequence number and selector from an nrnran123_State object */
67 extern void nrnran123_getseq(nrnran123_State* s, std::uint32_t* seq, char* which);
68 
69 /** Set a Random123 sequence for a sequnece ID and which selector.
70  *
71  * @param s an Random123 state object
72  * @param seq the sequence ID for which to initialize the random number sequence
73  * @param which the selector (0 <= which < 4) of the sequence
74  */
75 extern void nrnran123_setseq(nrnran123_State* s, std::uint32_t seq, char which);
76 
77 /** Set a Random123 sequence for a sequnece ID and which selector.
78  *
79  * This overload encodes the sequence ID and which in one double. This is done specifically to be
80  * able to expose the Random123 API in HOC, which only supports real numbers.
81  *
82  * @param s an Random123 state object
83  * @param seq4which encodes both seq and which as seq*4+which
84  */
85 extern void nrnran123_setseq(nrnran123_State* s, double seq4which); // seq*4+which);
86 
87 /** Get stream IDs from Random123 State object */
88 extern void nrnran123_getids(nrnran123_State* s, std::uint32_t* id1, std::uint32_t* id2);
89 
90 
91 /** Get stream IDs from Random123 State object */
93  std::uint32_t* id1,
94  std::uint32_t* id2,
95  std::uint32_t* id3);
96 
97 /** @brief. Deprecated, use nrnran123_getids **/
99  std::uint32_t* id1,
100  std::uint32_t* id2,
101  std::uint32_t* id3);
102 
103 extern void nrnran123_setids(nrnran123_State*, std::uint32_t id1, std::uint32_t id2, std::uint32_t id3);
104 
105 // Get a random uint32_t in [0, 2^32-1]
106 extern std::uint32_t nrnran123_ipick(nrnran123_State*);
107 // Get a random double on [0, 1]
108 // nrnran123_dblpick minimum value is 2.3283064e-10 and max value is 1-min
109 extern double nrnran123_dblpick(nrnran123_State*);
110 
111 /* nrnran123_negexp min value is 2.3283064e-10, max is 22.18071 if mean is 1.0 */
112 extern double nrnran123_negexp(nrnran123_State*); // mean = 1.0
113 extern double nrnran123_negexp(nrnran123_State*, double mean);
114 extern double nrnran123_normal(nrnran123_State*); // mean = 0.0, std = 1.0
115 extern double nrnran123_normal(nrnran123_State*, double mean, double std);
116 
117 extern double nrnran123_uniform(nrnran123_State*); // same as dblpick
118 extern double nrnran123_uniform(nrnran123_State*, double min, double max);
119 
120 /* more fundamental (stateless) (though the global index is still used) */
121 extern nrnran123_array4x32 nrnran123_iran(std::uint32_t seq, std::uint32_t id1, std::uint32_t id2 = 0, std::uint32_t id3 = 0);
122 
123 /** @brief. Deprecated, use nrnran123_iran **/
124 extern nrnran123_array4x32 nrnran123_iran3(std::uint32_t seq,
125  std::uint32_t id1,
126  std::uint32_t id2,
127  std::uint32_t id3);
nrnran123_array4x32 nrnran123_iran(std::uint32_t seq, std::uint32_t id1, std::uint32_t id2=0, std::uint32_t id3=0)
void nrnran123_getids3(nrnran123_State *, std::uint32_t *id1, std::uint32_t *id2, std::uint32_t *id3)
Definition: nrnran123.cpp:97
void nrnran123_getids(nrnran123_State *s, std::uint32_t *id1, std::uint32_t *id2)
Get stream IDs from Random123 State object.
Definition: nrnran123.cpp:82
void nrnran123_set_globalindex(std::uint32_t gix)
Definition: nrnran123.cpp:17
std::uint32_t nrnran123_get_globalindex()
Definition: nrnran123.cpp:22
double nrnran123_uniform(nrnran123_State *)
Definition: nrnran123.cpp:128
nrnran123_array4x32 nrnran123_iran3(std::uint32_t seq, std::uint32_t id1, std::uint32_t id2, std::uint32_t id3)
Definition: nrnran123.cpp:171
double nrnran123_normal(nrnran123_State *)
Definition: nrnran123.cpp:148
double nrnran123_dblpick(nrnran123_State *)
Definition: nrnran123.cpp:122
void nrnran123_deletestream(nrnran123_State *s)
Destroys the given Random123 stream.
Definition: nrnran123.cpp:46
nrnran123_State * nrnran123_newstream(std::uint32_t id1, std::uint32_t id2=0, std::uint32_t id3=0)
Construct a new Random123 stream based on the philox4x32 generator.
Definition: nrnran123.cpp:37
void nrnran123_setseq(nrnran123_State *s, std::uint32_t seq, char which)
Set a Random123 sequence for a sequnece ID and which selector.
Definition: nrnran123.cpp:55
std::uint32_t nrnran123_ipick(nrnran123_State *)
Definition: nrnran123.cpp:110
nrnran123_State * nrnran123_newstream3(std::uint32_t id1, std::uint32_t id2, std::uint32_t id3)
Definition: nrnran123.cpp:27
void nrnran123_setids(nrnran123_State *, std::uint32_t id1, std::uint32_t id2, std::uint32_t id3)
Definition: nrnran123.cpp:104
double nrnran123_negexp(nrnran123_State *)
Definition: nrnran123.cpp:141
void nrnran123_getseq(nrnran123_State *s, std::uint32_t *seq, char *which)
Get sequence number and selector from an nrnran123_State object.
Definition: nrnran123.cpp:50
s
Definition: multisend.cpp:521
std::uint32_t v[4]
Definition: nrnran123.h:31