Actual source code: randreg.c
1: #define PETSC_DLL
3: #include src/sys/utils/random/randomimpl.h
5: PetscFList PetscRandomList = PETSC_NULL;
6: PetscTruth PetscRandomRegisterAllCalled = PETSC_FALSE;
10: /*@C
11: PetscRandomSetType - Builds a context for generating particular type of random numbers.
13: Collective on PetscRandom
15: Input Parameters:
16: + rnd - The random number generator context
17: - type - The name of the random type
19: Options Database Key:
20: . -random_type <type> - Sets the random type; use -help for a list
21: of available types
23: Notes:
24: See "petsc/include/petscsys.h" for available random types (for instance, PETSCRAND48, PETSCRAND).
26: Level: intermediate
28: .keywords: random, set, type
29: .seealso: PetscRandomGetType(), PetscRandomCreate()
30: @*/
32: PetscErrorCode PetscRandomSetType(PetscRandom rnd, PetscRandomType type)
33: {
34: PetscErrorCode (*r)(PetscRandom);
35: PetscTruth match;
40: PetscTypeCompare((PetscObject)rnd, type, &match);
41: if (match) return(0);
43: /* Get the function pointers for the random requested */
44: if (!PetscRandomRegisterAllCalled) {
45: PetscRandomRegisterAll(PETSC_NULL);
46: }
47: PetscFListFind(rnd->comm, PetscRandomList, type,(void (**)(void)) &r);
48: if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type);
50: if (rnd->ops->destroy) {
51: (*rnd->ops->destroy)(rnd);
52: }
53: (*r)(rnd);
54: PetscRandomSeed(rnd);
56: PetscObjectChangeTypeName((PetscObject)rnd, type);
57: return(0);
58: }
62: /*@C
63: PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.
65: Not Collective
67: Input Parameter:
68: . rnd - The random number generator context
70: Output Parameter:
71: . type - The type name
73: Level: intermediate
75: .keywords: random, get, type, name
76: .seealso: PetscRandomSetType(), PetscRandomCreate()
77: @*/
78: PetscErrorCode PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
79: {
85: if (!PetscRandomRegisterAllCalled) {
86: PetscRandomRegisterAll(PETSC_NULL);
87: }
88: *type = rnd->type_name;
89: return(0);
90: }
94: /*@C
95: PetscRandomRegister - See PetscRandomRegisterDynamic()
97: Level: advanced
98: @*/
99: PetscErrorCode PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom))
100: {
101: char fullname[PETSC_MAX_PATH_LEN];
105: PetscFListConcat(path,name,fullname);
106: PetscFListAdd(&PetscRandomList,sname,fullname,(void (*)(void))function);
107: return(0);
108: }
111: /*--------------------------------------------------------------------------------------------------------------------*/
114: /*@C
115: PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic().
117: Not Collective
119: Level: advanced
121: .keywords: PetscRandom, register, destroy
122: .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic()
123: @*/
124: PetscErrorCode PetscRandomRegisterDestroy(void)
125: {
129: if (PetscRandomList) {
130: PetscFListDestroy(&PetscRandomList);
131: PetscRandomList = PETSC_NULL;
132: }
133: PetscRandomRegisterAllCalled = PETSC_FALSE;
134: return(0);
135: }
138: #if defined(PETSC_HAVE_RAND)
139: EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
140: #endif
141: #if defined(PETSC_HAVE_DRAND48)
142: EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
143: #endif
144: #if defined(PETSC_HAVE_SPRNG)
145: EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
146: #endif
151: /*@C
152: PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.
154: Not Collective
156: Input parameter:
157: . path - The dynamic library path
159: Level: advanced
161: .keywords: PetscRandom, register, all
162: .seealso: PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic()
163: @*/
164: PetscErrorCode PetscRandomRegisterAll(const char path[])
165: {
169: PetscRandomRegisterAllCalled = PETSC_TRUE;
170: #if defined(PETSC_HAVE_RAND)
171: PetscRandomRegisterDynamic(PETSCRAND, path,"PetscRandomCreate_Rand", PetscRandomCreate_Rand);
172: #endif
173: #if defined(PETSC_HAVE_DRAND48)
174: PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);
175: #endif
176: #if defined(PETSC_HAVE_SPRNG)
177: PetscRandomRegisterDynamic(SPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);
178: #endif
179: return(0);
180: }