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, PetscRandomType type)
 33: {
 34:   PetscErrorCode (*r)(PetscRandom);
 35:   PetscTruth     match;

 40:   PetscTypeCompare((PetscObject)rnd, type, &match);
 41:   if (match) return(0);

 43:   PetscFListFind(PetscRandomList,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, PetscRandomType *type)
 75: {

 81:   if (!PetscRandomRegisterAllCalled) {
 82:     PetscRandomRegisterAll(PETSC_NULL);
 83:   }
 84:   *type = rnd->type_name;
 85:   return(0);
 86: }

 90: /*@C
 91:   PetscRandomRegister - See PetscRandomRegisterDynamic()

 93:   Level: advanced
 94: @*/
 95: PetscErrorCode  PetscRandomRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(PetscRandom))
 96: {
 97:   char           fullname[PETSC_MAX_PATH_LEN];

101:   PetscFListConcat(path,name,fullname);
102:   PetscFListAdd(&PetscRandomList,sname,fullname,(void (*)(void))function);
103:   return(0);
104: }


107: /*--------------------------------------------------------------------------------------------------------------------*/
110: /*@C
111:    PetscRandomRegisterDestroy - Frees the list of Random types that were registered by PetscRandomRegister()/PetscRandomRegisterDynamic().

113:    Not Collective

115:    Level: advanced

117: .keywords: PetscRandom, register, destroy
118: .seealso: PetscRandomRegister(), PetscRandomRegisterAll(), PetscRandomRegisterDynamic()
119: @*/
120: PetscErrorCode  PetscRandomRegisterDestroy(void)
121: {

125:   PetscFListDestroy(&PetscRandomList);
126:   PetscRandomRegisterAllCalled = PETSC_FALSE;
127:   return(0);
128: }

131: #if defined(PETSC_HAVE_RAND)
132: EXTERN PetscErrorCode  PetscRandomCreate_Rand(PetscRandom);
133: #endif
134: #if defined(PETSC_HAVE_DRAND48)
135: EXTERN PetscErrorCode  PetscRandomCreate_Rand48(PetscRandom);
136: #endif
137: #if defined(PETSC_HAVE_SPRNG)
138: EXTERN PetscErrorCode  PetscRandomCreate_Sprng(PetscRandom);
139: #endif

144: /*@C
145:   PetscRandomRegisterAll - Registers all of the components in the PetscRandom package.

147:   Not Collective

149:   Input parameter:
150: . path - The dynamic library path

152:   Level: advanced

154: .keywords: PetscRandom, register, all
155: .seealso:  PetscRandomRegister(), PetscRandomRegisterDestroy(), PetscRandomRegisterDynamic()
156: @*/
157: PetscErrorCode  PetscRandomRegisterAll(const char path[])
158: {

162:   PetscRandomRegisterAllCalled = PETSC_TRUE;
163: #if defined(PETSC_HAVE_RAND)
164:   PetscRandomRegisterDynamic(PETSCRAND,  path,"PetscRandomCreate_Rand",  PetscRandomCreate_Rand);
165: #endif
166: #if defined(PETSC_HAVE_DRAND48)
167:   PetscRandomRegisterDynamic(PETSCRAND48,path,"PetscRandomCreate_Rand48",PetscRandomCreate_Rand48);
168: #endif
169: #if defined(PETSC_HAVE_SPRNG)
170:   PetscRandomRegisterDynamic(SPRNG,path,"PetscRandomCreate_Sprng",PetscRandomCreate_Sprng);
171: #endif
172:   return(0);
173: }