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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//------------------------------------------------------------------------------
// LAGraph_Random: generate a random vector (of any sparsity structure)
//------------------------------------------------------------------------------
// LAGraph, (c) 2019-2025 by The LAGraph Contributors, All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
//
// For additional details (including references to third party source code and
// other files) see the LICENSE file or contact permission@sei.cmu.edu. See
// Contributors.txt for a full list of contributors. Created, in part, with
// funding and support from the U.S. Government (see Acknowledgments.txt file).
// DM22-0790
// Contributed by Timothy A. Davis, Texas A&M University
//------------------------------------------------------------------------------
// A very simple thread-safe parallel pseudo-random number generator. These
// have a period of 2^(64)-1. For longer periods, a better method is needed.
// In the future, the State vector could be based on a 256-bit user-defined
// type, created by LG_Random_Init below. The LAGraph_Random_Seed and
// LAGraph_Random_Next API would not change. Instead, they would select the
// operators based on the type of the State vector (GrB_UINT64 in the current
// method, or the 256-bit type in a future method). Then we would need a
// single new method, say LAGraph_Random_Value, which extracts the random
// number from the State. It would compute R = f(State), where R is GrB_UINT64
// and the State vector (with its 256-bit data type) is unchanged. With the
// current method, R=State is implicit.
//------------------------------------------------------------------------------
// global operator
//------------------------------------------------------------------------------
// These operators can be shared by all threads in a user application, and thus
// are safely declared as global objects.
GrB_UnaryOp LG_rand_next_op = NULL ;
GrB_IndexUnaryOp LG_rand_init_op = NULL ;
//------------------------------------------------------------------------------
// unary and index-unary ops to construct the first and next states
//------------------------------------------------------------------------------
// z = f(x), where x is the old state and z is the new state.
// using xorshift64, from https://en.wikipedia.org/wiki/Xorshift
// with a state of uint64_t.
// Reference: Marsaglia, George (July 2003). "Xorshift RNGs". Journal of
// Statistical Software. 8 (14). https://doi.org/10.18637/jss.v008.i14 .
// For this random number generator, the output random number is the same
// as the state.
The default initial state is given below, but is unused here:
#define LG_RAND_MARSAGLIA_SEED 88172645463325252LL
struct xorshift64_state {
uint64_t a;
};
// the state must be initialized to nonzero
uint64_t xorshift64(struct xorshift64_state *state)
{
uint64_t x = state->a;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return state->a = x;
}
// return a random uint64_t; for internal use in LAGraph
uint64_t
// return a random uint64_t; as a unary operator
void
// From these references, the recommendation is to create the initial state of
// a random number generator with an entirely different random number
// generator. splitmix64 is recommended, so we initialize the State(i) with
// splitmix64 (i+seed). The method cannot return a value of zero, so it is
// suitable as a seed for the xorshift64 generator, above.
// References:
//
// David Blackman and Sebastiano Vigna. Scrambled linear pseudorandom number
// generators. ACM Trans. Math. Softw., 47:1−32, 2021.
//
// Steele GL, Vigna S. Computationally easy, spectrally good multipliers for
// congruential pseudorandom number generators. Software: Practice and
// Experience 2022; 52(2): 443–458. https://doi.org/10.1002/spe.3030
//
// Guy L. Steele, Doug Lea, and Christine H. Flood. 2014. Fast splittable
// pseudorandom number generators. SIGPLAN Not. 49, 10 (October 2014), 453–472.
// https://doi.org/10.1145/2714064.2660195
//
// The splitmix64 below method is the mix64variant13 in the above paper.
struct splitmix64_state {
uint64_t s;
};
uint64_t splitmix64(struct splitmix64_state *state)
{
uint64_t result = (state->s += 0x9E3779B97F4A7C15LL) ;
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9LL ;
result = (result ^ (result >> 27)) * 0x94D049BB133111EBLL ;
return result ^ (result >> 31);
}
// The init function computes z = splitmix64 (i + seed), but it does not
// advance the seed value on return.
void
//------------------------------------------------------------------------------
// LG_Random_Init: create the random state operator
//------------------------------------------------------------------------------
typedef void ;
typedef void
;
int
//------------------------------------------------------------------------------
// LG_Random_Finalize: free the random state operator
//------------------------------------------------------------------------------
int
//------------------------------------------------------------------------------
// LAGraph_Random_Seed: create a vector of random states
//------------------------------------------------------------------------------
// Initializes a vector with random state values. The State vector must be
// allocated on input, and should be of type GrB_UINT64. Its sparsity
// structure is unchanged.
// for testing only
bool random_hack = false ;
int LAGraph_Random_Seed // construct a random State vector
//------------------------------------------------------------------------------
// LAGraph_Random_Next: return next vector of random seeds
//------------------------------------------------------------------------------
int LAGraph_Random_Next // advance to next random vector