Actual source code: randreg.c
2: #include <petsc/private/randomimpl.h>
4: PetscFunctionList PetscRandomList = NULL;
5: PetscBool PetscRandomRegisterAllCalled = PETSC_FALSE;
7: /*@C
8: PetscRandomSetType - Builds a context for generating particular type of random numbers.
10: Collective on PetscRandom
12: Input Parameters:
13: + rnd - The random number generator context
14: - type - The name of the random type
16: Options Database Key:
17: . -random_type <type> - Sets the random type; use -help for a list
18: of available types
20: Notes:
21: See "petsc/include/petscsys.h" for available random types (for instance, PETSCRAND48, PETSCRAND).
23: Level: intermediate
25: .seealso: PetscRandomGetType(), PetscRandomCreate()
26: @*/
28: PetscErrorCode PetscRandomSetType(PetscRandom rnd, PetscRandomType type)
29: {
30: PetscErrorCode (*r)(PetscRandom);
31: PetscBool match;
36: PetscObjectTypeCompare((PetscObject)rnd, type, &match);
37: if (match) return(0);
39: PetscFunctionListFind(PetscRandomList,type,&r);
40: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type);
42: if (rnd->ops->destroy) {
43: (*rnd->ops->destroy)(rnd);
45: rnd->ops->destroy = NULL;
46: }
47: (*r)(rnd);
48: PetscRandomSeed(rnd);
50: PetscObjectChangeTypeName((PetscObject)rnd, type);
51: return(0);
52: }
54: /*@C
55: PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.
57: Not Collective
59: Input Parameter:
60: . rnd - The random number generator context
62: Output Parameter:
63: . type - The type name
65: Level: intermediate
67: .seealso: PetscRandomSetType(), PetscRandomCreate()
68: @*/
69: PetscErrorCode PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
70: {
74: *type = ((PetscObject)rnd)->type_name;
75: return(0);
76: }
78: /*@C
79: PetscRandomRegister - Adds a new PetscRandom component implementation
81: Not Collective
83: Input Parameters:
84: + name - The name of a new user-defined creation routine
85: - create_func - The creation routine itself
87: Notes:
88: PetscRandomRegister() may be called multiple times to add several user-defined randome number generators
90: Sample usage:
91: .vb
92: PetscRandomRegister("my_rand", MyPetscRandomtorCreate);
93: .ve
95: Then, your random type can be chosen with the procedural interface via
96: .vb
97: PetscRandomCreate(MPI_Comm, PetscRandom *);
98: PetscRandomSetType(PetscRandom,"my_random_name");
99: .ve
100: or at runtime via the option
101: .vb
102: -random_type my_random_name
103: .ve
105: Notes:
106: For an example of the code needed to interface your own random number generator see
107: src/sys/random/impls/rand/rand.c
109: Level: advanced
111: .seealso: PetscRandomRegisterAll(), PetscRandomRegisterDestroy(), PetscRandomRegister()
112: @*/
113: PetscErrorCode PetscRandomRegister(const char sname[], PetscErrorCode (*function)(PetscRandom))
114: {
118: PetscRandomInitializePackage();
119: PetscFunctionListAdd(&PetscRandomList,sname,function);
120: return(0);
121: }
123: #if defined(PETSC_HAVE_RAND)
124: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
125: #endif
126: #if defined(PETSC_HAVE_DRAND48)
127: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
128: #endif
129: #if defined(PETSC_HAVE_SPRNG)
130: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
131: #endif
132: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rander48(PetscRandom);
133: #if defined(PETSC_HAVE_RANDOM123)
134: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Random123(PetscRandom);
135: #endif
136: #if defined(PETSC_HAVE_CUDA)
137: PETSC_EXTERN PetscErrorCode PetscRandomCreate_CURAND(PetscRandom);
138: #endif
140: /*@C
141: PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.
143: Not Collective
145: Level: advanced
147: .seealso: PetscRandomRegister(), PetscRandomRegisterDestroy()
148: @*/
149: PetscErrorCode PetscRandomRegisterAll(void)
150: {
154: if (PetscRandomRegisterAllCalled) return(0);
155: PetscRandomRegisterAllCalled = PETSC_TRUE;
156: #if defined(PETSC_HAVE_RAND)
157: PetscRandomRegister(PETSCRAND,PetscRandomCreate_Rand);
158: #endif
159: #if defined(PETSC_HAVE_DRAND48)
160: PetscRandomRegister(PETSCRAND48,PetscRandomCreate_Rand48);
161: #endif
162: #if defined(PETSC_HAVE_SPRNG)
163: PetscRandomRegister(PETSCSPRNG,PetscRandomCreate_Sprng);
164: #endif
165: PetscRandomRegister(PETSCRANDER48,PetscRandomCreate_Rander48);
166: #if defined(PETSC_HAVE_RANDOM123)
167: PetscRandomRegister(PETSCRANDOM123,PetscRandomCreate_Random123);
168: #endif
169: #if defined(PETSC_HAVE_CUDA)
170: PetscRandomRegister(PETSCCURAND,PetscRandomCreate_CURAND);
171: #endif
172: return(0);
173: }