Actual source code: sprng.c
1: #define PETSC_DLL
3: #include ../src/sys/random/randomimpl.h
4: #if defined (PETSC_HAVE_STDLIB_H)
5: #include <stdlib.h>
6: #endif
8: #define USE_MPI
9: #define SIMPLE_SPRNG
11: #include "sprng.h"
16: PetscErrorCode PetscRandomSeed_Sprng(PetscRandom r)
17: {
19: init_sprng(r->seed,SPRNG_DEFAULT);
20: return(0);
21: }
25: PetscErrorCode PetscRandomGetValue_Sprng(PetscRandom r,PetscScalar *val)
26: {
28: #if defined(PETSC_USE_COMPLEX)
29: if (r->iset) {
30: *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low) +
31: (PetscImaginaryPart(r->width)*sprng() + PetscImaginaryPart(r->low)) * PETSC_i;
32: } else {
33: *val = sprng() + sprng()*PETSC_i;
34: }
35: #else
36: if (r->iset) *val = r->width * sprng() + r->low;
37: else *val = sprng();
38: #endif
39: return(0);
40: }
44: PetscErrorCode PetscRandomGetValueReal_Sprng(PetscRandom r,PetscScalar *val)
45: {
47: #if defined(PETSC_USE_COMPLEX)
48: if (r->iset) *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low);
49: else *val = sprng();
50: #else
51: if (r->iset) *val = r->width * sprng() + r->low;
52: else *val = sprng();
53: #endif
54: return(0);
55: }
57: static struct _PetscRandomOps PetscRandomOps_Values = {
58: /* 0 */
59: PetscRandomSeed_Sprng,
60: PetscRandomGetValue_Sprng,
61: PetscRandomGetValueReal_Sprng,
62: 0,
63: /* 5 */
64: 0
65: };
67: /*MC
68: PETSCSPRNG- access to the publically available random number generator sprng
70: Options Database Keys:
71: . -random_type <rand,rand48,sprng>
73: Level: beginner
75: PETSc must have been config/configure.py with the option --download-sprng to use
76: this random number generator.
78: This is NOT currently using a parallel random number generator. Sprng does have
79: an MPI version we should investigate.
81: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCRAND48
82: M*/
87: PetscErrorCode PetscRandomCreate_Sprng(PetscRandom r)
88: {
92: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
93: PetscObjectChangeTypeName((PetscObject)r,PETSCSPRNG);
94: PetscPublishAll(r);
95: return(0);
96: }