File: prand.c

package info (click to toggle)
suitesparse 1%3A5.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 152,716 kB
  • sloc: ansic: 774,385; cpp: 24,213; makefile: 6,310; fortran: 1,927; java: 1,826; csh: 1,686; ruby: 725; sh: 535; perl: 225; python: 209; sed: 164; awk: 60
file content (326 lines) | stat: -rw-r--r-- 10,569 bytes parent folder | download
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//------------------------------------------------------------------------------
// GraphBLAS/Demo/Source/prand: parallel random number generator
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights Reserved.
// http://suitesparse.com   See GraphBLAS/Doc/License.txt for license.

//------------------------------------------------------------------------------

// A simple thread-safe parallel pseudo-random nuumber generator.

#include "GraphBLAS.h"
#undef GB_PUBLIC
#define GB_LIBRARY
#include "graphblas_demos.h"

//------------------------------------------------------------------------------
// prand macros
//------------------------------------------------------------------------------

// Generate the next seed, and extract a random 15-bit value from a seed.

#define PRAND_RECURENCE(seed) ((seed) * 1103515245 + 12345)

#define PRAND_15_MAX 32767 
#define PRAND_15(seed) (((seed)/65536) % (PRAND_15_MAX + 1))

//------------------------------------------------------------------------------
// global types and operators
//------------------------------------------------------------------------------

// These can be shared by all threads in a user application, and thus are
// safely declared as global objects.

GrB_Type prand_type = NULL ;
GrB_UnaryOp prand_next_op = NULL ;
GrB_UnaryOp prand_iget_op = NULL ;
GrB_UnaryOp prand_xget_op = NULL ;
GrB_BinaryOp prand_dup_op = NULL ;

//------------------------------------------------------------------------------
// prand_next_op:  unary operator to construct the next seed
//------------------------------------------------------------------------------

// z = f(x), where x is the old seed and z is the new seed.

GB_PUBLIC
void prand_next_f (prand_t *z, const prand_t *x)
{
    for (int k = 0 ; k < 5 ; k++)
    {
        z->seed [k] = PRAND_RECURENCE (x->seed [k]) ;
    }
}

//------------------------------------------------------------------------------
// prand_iget:  unary operator to construct get a random integer from the seed
//------------------------------------------------------------------------------

// z = f(x), where x is a random seed, and z is an unsigned 64-bit
// pseudo-random number constructed from the seed.

GB_PUBLIC
void prand_iget_f (uint64_t *z, const prand_t *x)
{
    uint64_t i = 0 ;
    for (int k = 0 ; k < 5 ; k++)
    {
        i = PRAND_15_MAX * i + PRAND_15 (x->seed [k]) ;
    }
    (*z) = i ;
}

//------------------------------------------------------------------------------
// prand_xget:  unary operator to construct get a random double from the seed
//------------------------------------------------------------------------------

// z = f(x), where x is a random seed, and z is a double precision
// pseudo-random number constructed from the seed, in the range 0 to 1.

GB_PUBLIC
void prand_xget_f (double *z, prand_t *x)
{
    uint64_t i ;
    prand_iget_f (&i, x) ;
    (*z) = ((double) i) / ((double) UINT64_MAX) ;
}

//------------------------------------------------------------------------------
// prand_dup:  binary operator to build a vector
//------------------------------------------------------------------------------

// This is required by GrB_Vector_build, but is never called since no
// duplicates are created.  This is the SECOND operator for the prand_type.

#if defined ( __INTEL_COMPILER )
// disable icc warnings
//  869:  unused parameters
#pragma warning (disable: 869 )
#elif defined (  __GNUC__ )
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

GB_PUBLIC
void prand_dup_f (prand_t *z, /* unused: */ const prand_t *x, const prand_t *y)
{
    (*z) = (*y) ;
}

//------------------------------------------------------------------------------
// prand_init:  create the random seed type and its operators
//------------------------------------------------------------------------------

#define PRAND_FREE_ALL                                      \
{                                                           \
    GrB_Type_free (&prand_type) ;                                \
    GrB_UnaryOp_free (&prand_next_op) ;                             \
    GrB_UnaryOp_free (&prand_iget_op) ;                             \
    GrB_UnaryOp_free (&prand_xget_op) ;                             \
    GrB_BinaryOp_free (&prand_dup_op) ;                              \
}

#undef  OK
#define OK(method)                                          \
{                                                           \
    GrB_Info info = method ;                                \
    if (info != GrB_SUCCESS)                                \
    {                                                       \
        PRAND_FREE_ALL ;                                    \
        printf ("GraphBLAS error:\n%s\n", GrB_error ( )) ;  \
        return (info) ;                                     \
    }                                                       \
}

GB_PUBLIC
GrB_Info prand_init ( )
{
    prand_type = NULL ;
    prand_next_op = NULL ;
    prand_iget_op = NULL ;
    prand_xget_op = NULL ;
    prand_dup_op = NULL ;
    OK (GrB_Type_new (&prand_type, sizeof (prand_t))) ;
    OK (GrB_UnaryOp_new (&prand_next_op, (GxB_unary_function) prand_next_f,
        prand_type, prand_type)) ;
    OK (GrB_UnaryOp_new (&prand_iget_op, (GxB_unary_function) prand_iget_f,
        GrB_UINT64, prand_type)) ;
    OK (GrB_UnaryOp_new (&prand_xget_op, (GxB_unary_function) prand_xget_f,
        GrB_FP64, prand_type)) ;
    OK (GrB_BinaryOp_new (&prand_dup_op, (GxB_binary_function) prand_dup_f,
        prand_type, prand_type, prand_type)) ;
    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------
// prand_finalize:  free the random seed type and its operators
//------------------------------------------------------------------------------

GB_PUBLIC
GrB_Info prand_finalize ( )
{
    PRAND_FREE_ALL ;
    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------
// prand_next: get the next random numbers
//------------------------------------------------------------------------------

GB_PUBLIC
GrB_Info prand_next
(
    GrB_Vector Seed
)
{
    return (GrB_Vector_apply (Seed, NULL, NULL, prand_next_op, Seed, NULL)) ;
}

//------------------------------------------------------------------------------
// prand_seed:  create a vector of random seeds
//------------------------------------------------------------------------------

// Returns a vector of random seed values.

#define PRAND_FREE_WORK                                     \
{                                                           \
    free (I) ;                                              \
    free (X) ;                                              \
}

#undef  PRAND_FREE_ALL
#define PRAND_FREE_ALL                                      \
{                                                           \
    PRAND_FREE_WORK ;                                       \
    GrB_Vector_free (Seed) ;                                \
}

GB_PUBLIC
GrB_Info prand_seed
(
    GrB_Vector *Seed,   // vector of random number seeds
    int64_t seed,       // scalar input seed
    GrB_Index n,        // size of Seed to create
    int nthreads        // # of threads to use (OpenMP default if <= 0)
)
{

    GrB_Index *I = NULL ;
    prand_t   *X = NULL ;

    // allocate the Seed vector
    OK (GrB_Vector_new (Seed, prand_type, n)) ;

    // allocate the I and X arrays
    I = (GrB_Index *) malloc ((n+1) * sizeof (GrB_Index)) ;
    X = (prand_t *) malloc ((n+1) * sizeof (prand_t)) ;
    if (I == NULL || X == NULL)
    {
        PRAND_FREE_ALL ;
        return (GrB_OUT_OF_MEMORY) ;
    }

    // determine # of threads to use
    int nthreads_max = 1 ;
    #ifdef _OPENMP
    nthreads_max = omp_get_max_threads ( ) ;
    #endif
    if (nthreads <= 0 || nthreads > nthreads_max)
    {
        nthreads = nthreads_max ;
    }

    // construct the tuples for the initial seeds
    int64_t i, len = (int64_t) n  ;
    #pragma omp parallel for num_threads(nthreads) schedule(static)
    for (i = 0 ; i < len ; i++)
    {
        I [i] = i ;
        for (int k = 0 ; k < 5 ; k++)
        {
            X [i].seed [k] = (100000000*(seed) + 10*i + k + 1) ;
        }
    }

    // build the Seed vector
    OK (GrB_Vector_build_UDT (*Seed, I, X, n, prand_dup_op)) ;

    // free workspace
    PRAND_FREE_WORK ;

    // advance to the first set of random numbers
    OK (prand_next (*Seed)) ;

    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------
// prand_print:  print the Seed vector
//------------------------------------------------------------------------------

// This is meant for testing, not production use.

#undef  PRAND_FREE_ALL
#define PRAND_FREE_ALL ;

GB_PUBLIC
GrB_Info prand_print
(
    GrB_Vector Seed,
    int pr              // 0: print nothing, 1: print some, 2: print all
)
{
    if (pr > 0)
    {
        GrB_Index n ;
        OK (GrB_Vector_nvals (&n, Seed)) ;
        printf ("\nSeed: length %g\n", (double) n) ;
        prand_t x ;
        for (int k = 0 ; k < 5 ; k++) x.seed [k] = -1 ;
        for (int64_t i = 0 ; i < (int64_t) n ; i++)
        {
            if (GrB_Vector_extractElement_UDT (&x, Seed, i) == GrB_SUCCESS)
            {
                printf ("%g: ", (double) i) ;
                for (int k = 0 ; k < 5 ; k++)
                {
                    printf (" %.18g", (double) (x.seed [k])) ;
                }
                printf ("\n") ;
            }
            if (pr == 1 && i > 10) break ;
        }
    }
    return (GrB_SUCCESS) ;
}

//------------------------------------------------------------------------------
// prand_iget: return a vector of random uint64 integers
//------------------------------------------------------------------------------

GB_PUBLIC
GrB_Info prand_iget
(
    GrB_Vector X,
    GrB_Vector Seed
)
{
    OK (GrB_Vector_apply (X, NULL, NULL, prand_iget_op, Seed, NULL)) ;
    return (prand_next (Seed)) ;
}

//------------------------------------------------------------------------------
// prand_xget: return a vector of random doubles, in range 0 to 1 inclusive
//------------------------------------------------------------------------------

GB_PUBLIC
GrB_Info prand_xget
(
    GrB_Vector X,
    GrB_Vector Seed
)
{
    OK (GrB_Vector_apply (X, NULL, NULL, prand_xget_op, Seed, NULL)) ;
    return (prand_next (Seed)) ;
}