17
17
18
18
// A very simple thread-safe parallel pseudo-random nuumber generator.
19
19
20
- // FIXME: ready for src?
21
-
22
- // FIXME: add LAGraph_Random_Init to LAGraph_Init,
23
- // and added LAGraph_Random_Finalize to LAGraph_Finalize.
24
-
25
- // FIXME: is the new init function more complicated than it needs to be?
20
+ // FIXME: ready for src
26
21
27
22
#include "LG_internal.h"
28
23
#include "LAGraphX.h"
@@ -91,9 +86,8 @@ void LG_rand_next_f2 (uint64_t *z, const uint64_t *x)
91
86
92
87
// From these references, the recommendation is to create the initial state of
93
88
// a random number generator with an entirely different random number
94
- // generator. splitmix64 is recommended, but here we initialize the State(i)
95
- // with xorshift (i+1) to get a good start, add the scalar seed, and then
96
- // randomize the State with splitmix64.
89
+ // generator. splitmix64 is recommended, so we initialize the State(i) with
90
+ // (i+seed) then randomize the State with splitmix64.
97
91
98
92
// References:
99
93
//
@@ -121,17 +115,12 @@ void LG_rand_next_f2 (uint64_t *z, const uint64_t *x)
121
115
122
116
#endif
123
117
124
- // The init function computes z = splitmix64 (xorshift (i+1) + seed)
118
+ // The init function computes z = splitmix64 (i + seed)
125
119
void LG_rand_init_func (uint64_t * z , const void * x ,
126
120
GrB_Index i , GrB_Index j , const uint64_t * seed )
127
121
{
128
- // state = xorshift64 (i+1) + seed
129
- uint64_t state = i + 1 ;
130
- state ^= state << 13 ;
131
- state ^= state >> 7 ;
132
- state ^= state << 17 ;
133
- state += (* seed ) ;
134
- // result = shiftmix64 (state)
122
+ uint64_t state = i + (* seed ) ;
123
+ // result = splitmix64 (state)
135
124
uint64_t result = (state += 0x9E3779B97f4A7C15 ) ;
136
125
result = (result ^ (result >> 30 )) * 0xBF58476D1CE4E5B9 ;
137
126
result = (result ^ (result >> 27 )) * 0x94D049BB133111EB ;
@@ -146,11 +135,7 @@ void LG_rand_init_func (uint64_t *z, const void *x,
146
135
"void LG_rand_init_func (uint64_t *z, const void *x, \n" \
147
136
" GrB_Index i, GrB_Index j, const uint64_t *seed) \n" \
148
137
"{ \n" \
149
- " uint64_t state = i + 1 ; \n" \
150
- " state ^= state << 13 ; \n" \
151
- " state ^= state >> 7 ; \n" \
152
- " state ^= state << 17 ; \n" \
153
- " state += (*seed) ; \n" \
138
+ " uint64_t state = i + (*seed) ; \n" \
154
139
" uint64_t result = (state += 0x9E3779B97f4A7C15) ; \n" \
155
140
" result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9 ; \n" \
156
141
" result = (result ^ (result >> 27)) * 0x94D049BB133111EB ; \n" \
@@ -170,16 +155,21 @@ void LG_rand_init_func (uint64_t *z, const void *x,
170
155
GrB_IndexUnaryOp_free (&LG_rand_init_op) ; \
171
156
}
172
157
173
- int LAGraph_Random_Init (char * msg )
158
+ int LAGraph_Random_Init (char * msg ) // FIXME: remove this method
159
+ {
160
+ return (LG_Random_Init (msg )) ;
161
+ }
162
+
163
+ int LG_Random_Init (char * msg )
174
164
{
175
165
LG_CLEAR_MSG ;
166
+ LG_FREE_WORK ; // free the two ops in case LG_Random_Init is called twice
176
167
LG_rand_next_op = NULL ;
177
168
LG_rand_init_op = NULL ;
178
169
179
170
#if LAGRAPH_SUITESPARSE
180
171
{
181
172
// give SuiteSparse:GraphBLAS the strings that define the functions
182
- // using the xorshift generator from LAGraph v1.2
183
173
GRB_TRY (GxB_UnaryOp_new (& LG_rand_next_op ,
184
174
(GxB_unary_function ) LG_rand_next_f2 ,
185
175
GrB_UINT64 , GrB_UINT64 ,
@@ -192,7 +182,6 @@ int LAGraph_Random_Init (char *msg)
192
182
#else
193
183
{
194
184
// vanilla GraphBLAS, no strings to define the new operators
195
- // using the xorshift generator from LAGraph v1.2
196
185
GRB_TRY (GrB_UnaryOp_new (& LG_rand_next_op ,
197
186
(GxB_unary_function ) LG_rand_next_f2 ,
198
187
GrB_UINT64 , GrB_UINT64 )) ;
@@ -209,13 +198,18 @@ int LAGraph_Random_Init (char *msg)
209
198
// LAGraph_Random_Finalize: free the random state operator
210
199
//------------------------------------------------------------------------------
211
200
212
- int LAGraph_Random_Finalize (char * msg )
201
+ int LG_Random_Finalize (char * msg )
213
202
{
214
203
LG_CLEAR_MSG ;
215
204
LG_FREE_WORK ;
216
205
return (GrB_SUCCESS ) ;
217
206
}
218
207
208
+ int LAGraph_Random_Finalize (char * msg ) // FIXME: remove this method
209
+ {
210
+ return (LG_Random_Finalize (msg )) ;
211
+ }
212
+
219
213
//------------------------------------------------------------------------------
220
214
// LAGraph_Random_Seed: create a vector of random states
221
215
//------------------------------------------------------------------------------
@@ -249,8 +243,7 @@ int LAGraph_Random_Seed // construct a random state vector
249
243
LG_CLEAR_MSG ;
250
244
LG_ASSERT (State != NULL , GrB_NULL_POINTER ) ;
251
245
252
- // LAGraph v1.2:
253
- // State = splitmix64 (xorshift64 (i+1) + seed)
246
+ // State = splitmix64 (i + seed)
254
247
GRB_TRY (GrB_apply (State , NULL , NULL , LG_rand_init_op , State , seed ,
255
248
NULL )) ;
256
249
0 commit comments