xref: /petsc/src/sys/classes/random/impls/random123/random123.c (revision 0619917b5a674bb687c64e7daba2ab22be99af31)
1 #include <petsc/private/randomimpl.h>
2 #include <Random123/threefry.h>
3 
4 /* The structure of the Random123 methods are similar enough that templates could be used to make the other CBRNGs in
5  * the package (aes, ars, philox) available, as well as different block sizes.  But threefry4x64 is a good default,
6  * and I'd rather get a simple implementation up and working and come back if there's interest. */
7 typedef struct _n_PetscRandom123 {
8   threefry4x64_ctr_t counter;
9   threefry4x64_key_t key;
10   threefry4x64_ctr_t result;
11   PetscInt           count;
12 } PetscRandom123;
13 
14 R123_ULONG_LONG PETSCR123_SEED_0 = R123_64BIT(0x615D333D2655FE14);
15 R123_ULONG_LONG PETSCR123_SEED_1 = R123_64BIT(0xAFF6369B3EE9FE96);
16 R123_ULONG_LONG PETSCR123_SEED_2 = R123_64BIT(0x5956EBC717B60E07);
17 R123_ULONG_LONG PETSCR123_SEED_3 = R123_64BIT(0xEE8612A0CBEABFF1);
18 
19 PetscErrorCode PetscRandomSeed_Random123(PetscRandom r)
20 {
21   threefry4x64_ukey_t ukey;
22   PetscRandom123     *r123 = (PetscRandom123 *)r->data;
23 
24   PetscFunctionBegin;
25   ukey.v[0] = (R123_ULONG_LONG)r->seed;
26   ukey.v[1] = PETSCR123_SEED_1;
27   ukey.v[2] = PETSCR123_SEED_2;
28   ukey.v[3] = PETSCR123_SEED_3;
29   /* The point of seeding should be that every time the sequence is seeded you get the same output.  In this CBRNG,
30    * that means we have to initialize the key and reset the counts */
31   r123->key          = threefry4x64keyinit(ukey);
32   r123->counter.v[0] = 0;
33   r123->counter.v[1] = 1;
34   r123->counter.v[2] = 2;
35   r123->counter.v[3] = 3;
36   r123->result       = threefry4x64(r123->counter, r123->key);
37   r123->count        = 0;
38   PetscFunctionReturn(PETSC_SUCCESS);
39 }
40 
41 static PetscReal PetscRandom123Step(PetscRandom123 *r123)
42 {
43   PetscReal scale = ((PetscReal)1.) / (UINT64_MAX + ((PetscReal)1.));
44   PetscReal shift = .5 * scale;
45   PetscInt  mod   = (r123->count++) % 4;
46   PetscReal ret;
47 
48   ret = r123->result.v[mod] * scale + shift;
49 
50   if (mod == 3) {
51     r123->counter.v[0] += 4;
52     r123->counter.v[1] += 4;
53     r123->counter.v[2] += 4;
54     r123->counter.v[3] += 4;
55     r123->result = threefry4x64(r123->counter, r123->key);
56   }
57 
58   return ret;
59 }
60 
61 PetscErrorCode PetscRandomGetValue_Random123(PetscRandom r, PetscScalar *val)
62 {
63   PetscRandom123 *r123 = (PetscRandom123 *)r->data;
64   PetscScalar     rscal;
65 
66   PetscFunctionBegin;
67 #if defined(PETSC_USE_COMPLEX)
68   {
69     PetscReal re = PetscRandom123Step(r123);
70     PetscReal im = PetscRandom123Step(r123);
71 
72     if (r->iset) {
73       re = re * PetscRealPart(r->width) + PetscRealPart(r->low);
74       im = im * PetscImaginaryPart(r->width) + PetscImaginaryPart(r->low);
75     }
76 
77     rscal = PetscCMPLX(re, im);
78   }
79 #else
80   rscal = PetscRandom123Step(r123);
81   if (r->iset) rscal = rscal * r->width + r->low;
82 #endif
83   *val = rscal;
84   PetscFunctionReturn(PETSC_SUCCESS);
85 }
86 
87 PetscErrorCode PetscRandomGetValueReal_Random123(PetscRandom r, PetscReal *val)
88 {
89   PetscRandom123 *r123 = (PetscRandom123 *)r->data;
90   PetscReal       rreal;
91 
92   PetscFunctionBegin;
93   rreal = PetscRandom123Step(r123);
94   if (r->iset) rreal = rreal * PetscRealPart(r->width) + PetscRealPart(r->low);
95   *val = rreal;
96   PetscFunctionReturn(PETSC_SUCCESS);
97 }
98 
99 PetscErrorCode PetscRandomDestroy_Random123(PetscRandom r)
100 {
101   PetscFunctionBegin;
102   PetscCall(PetscFree(r->data));
103   PetscFunctionReturn(PETSC_SUCCESS);
104 }
105 
106 static struct _PetscRandomOps PetscRandomOps_Values = {
107   PetscDesignatedInitializer(seed, PetscRandomSeed_Random123),
108   PetscDesignatedInitializer(getvalue, PetscRandomGetValue_Random123),
109   PetscDesignatedInitializer(getvaluereal, PetscRandomGetValueReal_Random123),
110   PetscDesignatedInitializer(getvalues, NULL),
111   PetscDesignatedInitializer(getvaluesreal, NULL),
112   PetscDesignatedInitializer(destroy, PetscRandomDestroy_Random123),
113 };
114 
115 /*MC
116    PETSCRANDOM123 - access to Random123 counter based pseudorandom number generators (currently threefry4x64)
117 
118    Options Database Keys:
119 . -random_type <rand,rand48,sprng,random123> - select the random number generator at runtim
120 
121    Note:
122    PETSc must be ./configure with the option --download-random123 to use this random number generator.
123 
124   Level: beginner
125 
126 .seealso: `RandomCreate()`, `RandomSetType()`, `PETSCRAND`, `PETSCRAND48`, `PETSCSPRNG`, `PetscRandomSetFromOptions()`
127 M*/
128 
129 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Random123(PetscRandom r)
130 {
131   PetscRandom123 *r123;
132 
133   PetscFunctionBegin;
134   PetscCall(PetscNew(&r123));
135   r->data   = r123;
136   r->ops[0] = PetscRandomOps_Values;
137   PetscCall(PetscObjectChangeTypeName((PetscObject)r, PETSCRANDOM123));
138   PetscCall(PetscRandomSeed(r));
139   PetscFunctionReturn(PETSC_SUCCESS);
140 }
141