Actual source code: randreg.c
1: #define PETSC_DLL
3: #include ../src/sys/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, const PetscRandomType type)
33: {
34: PetscErrorCode (*r)(PetscRandom);
35: PetscTruth match;
40: PetscTypeCompare((PetscObject)rnd, type, &match);
41: if (match) return(0);
43: PetscFListFind(PetscRandomList,((PetscObject)rnd)->comm, type,(void (**)(void)) &r);
44: if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type);
46: if (rnd->ops->destroy) {
47: (*rnd->ops->destroy)(rnd);
48: }
49: (*r)(rnd);
50: PetscRandomSeed(rnd);
52: PetscObjectChangeTypeName((PetscObject)rnd, type);
53: return(0);
54: }
58: /*@C
59: PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.
61: Not Collective
63: Input Parameter:
64: . rnd - The random number generator context
66: Output Parameter:
67: . type - The type name
69: Level: intermediate
71: .keywords: random, get, type, name
72: .seealso: PetscRandomSetType(), PetscRandomCreate()
73: @*/
74: PetscErrorCode PetscRandomGetType(PetscRandom rnd, const PetscRandomType *type)
75: {
79: *type = ((PetscObject)rnd)->type_name;
80: return(0);
81: }
85: /*@C
86: PetscRandomRegister - See PetscRandomRegisterDynamic()
88: Level: advanced
89: @*/
90: PetscErrorCode PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom))
91: {
92: char fullname[PETSC_MAX_PATH_LEN];
96: PetscFListConcat(path,name,fullname);
97: PetscFListAdd(&PetscRandomList,sname,fullname,(void (*)(void))function);
98: return(0);
99: }
102: /*--------------------------------------------------------------------------------------------------------------------*/
105: /*@C
106: PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic().
108: Not Collective
110: Level: advanced
112: .keywords: PetscRandom, register, destroy
113: .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic()
114: @*/
115: PetscErrorCode PetscRandomRegisterDestroy(void)
116: {
120: PetscFListDestroy(&PetscRandomList);
121: PetscRandomRegisterAllCalled = PETSC_FALSE;
122: return(0);
123: }
126: #if defined(PETSC_HAVE_RAND)
127: EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
128: #endif
129: #if defined(PETSC_HAVE_DRAND48)
130: EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
131: #endif
132: #if defined(PETSC_HAVE_SPRNG)
133: EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
134: #endif
139: /*@C
140: PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.
142: Not Collective
144: Input parameter:
145: . path - The dynamic library path
147: Level: advanced
149: .keywords: PetscRandom, register, all
150: .seealso: PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic()
151: @*/
152: PetscErrorCode PetscRandomRegisterAll(const char path[])
153: {
157: PetscRandomRegisterAllCalled = PETSC_TRUE;
158: #if defined(PETSC_HAVE_RAND)
159: PetscRandomRegisterDynamic(PETSCRAND, path,"PetscRandomCreate_Rand", PetscRandomCreate_Rand);
160: #endif
161: #if defined(PETSC_HAVE_DRAND48)
162: PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);
163: #endif
164: #if defined(PETSC_HAVE_SPRNG)
165: PetscRandomRegisterDynamic(PETSCSPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);
166: #endif
167: return(0);
168: }