Actual source code: randreg.c

petsc-3.10.0 2018-09-12
Report Typos and Errors

  2:  #include <../src/sys/classes/random/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: .keywords: random, set, type
 26: .seealso: PetscRandomGetType(), PetscRandomCreate()
 27: @*/

 29: PetscErrorCode  PetscRandomSetType(PetscRandom rnd, PetscRandomType type)
 30: {
 31:   PetscErrorCode (*r)(PetscRandom);
 32:   PetscBool      match;

 37:   PetscObjectTypeCompare((PetscObject)rnd, type, &match);
 38:   if (match) return(0);

 40:   PetscFunctionListFind(PetscRandomList,type,&r);
 41:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown random type: %s", type);

 43:   if (rnd->ops->destroy) {
 44:     (*rnd->ops->destroy)(rnd);

 46:     rnd->ops->destroy = NULL;
 47:   }
 48:   (*r)(rnd);
 49:   PetscRandomSeed(rnd);

 51:   PetscObjectChangeTypeName((PetscObject)rnd, type);
 52:   return(0);
 53: }

 55: /*@C
 56:   PetscRandomGetType - Gets the type name (as a string) from the PetscRandom.

 58:   Not Collective

 60:   Input Parameter:
 61: . rnd  - The random number generator context

 63:   Output Parameter:
 64: . type - The type name

 66:   Level: intermediate

 68: .keywords: random, get, type, name
 69: .seealso: PetscRandomSetType(), PetscRandomCreate()
 70: @*/
 71: PetscErrorCode  PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
 72: {
 76:   *type = ((PetscObject)rnd)->type_name;
 77:   return(0);
 78: }

 80: /*@C
 81:   PetscRandomRegister -  Adds a new PetscRandom component implementation

 83:   Not Collective

 85:   Input Parameters:
 86: + name        - The name of a new user-defined creation routine
 87: - create_func - The creation routine itself

 89:   Notes:
 90:   PetscRandomRegister() may be called multiple times to add several user-defined randome number generators

 92:   Sample usage:
 93: .vb
 94:     PetscRandomRegister("my_rand",  MyPetscRandomtorCreate);
 95: .ve

 97:   Then, your random type can be chosen with the procedural interface via
 98: .vb
 99:     PetscRandomCreate(MPI_Comm, PetscRandom *);
100:     PetscRandomSetType(PetscRandom,"my_random_name");
101: .ve
102:    or at runtime via the option
103: .vb
104:     -random_type my_random_name
105: .ve

107:   Notes:
108:     For an example of the code needed to interface your own random number generator see
109:          src/sys/random/impls/rand/rand.c

111:   Level: advanced

113: .keywords: PetscRandom, register

115: .seealso: PetscRandomRegisterAll(), PetscRandomRegisterDestroy(), PetscRandomRegister()
116: @*/
117: PetscErrorCode  PetscRandomRegister(const char sname[], PetscErrorCode (*function)(PetscRandom))
118: {

122:   PetscFunctionListAdd(&PetscRandomList,sname,function);
123:   return(0);
124: }

126: #if defined(PETSC_HAVE_RAND)
127: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
128: #endif
129: #if defined(PETSC_HAVE_DRAND48)
130: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
131: #endif
132: #if defined(PETSC_HAVE_SPRNG)
133: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
134: #endif
135: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rander48(PetscRandom);
136: #if defined(PETSC_HAVE_RANDOM123)
137: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Random123(PetscRandom);
138: #endif

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

143:   Not Collective

145:   Level: advanced

147: .keywords: PetscRandom, register, all
148: .seealso:  PetscRandomRegister(), PetscRandomRegisterDestroy()
149: @*/
150: PetscErrorCode  PetscRandomRegisterAll(void)
151: {

155:   if (PetscRandomRegisterAllCalled) return(0);
156:   PetscRandomRegisterAllCalled = PETSC_TRUE;
157: #if defined(PETSC_HAVE_RAND)
158:   PetscRandomRegister(PETSCRAND,  PetscRandomCreate_Rand);
159: #endif
160: #if defined(PETSC_HAVE_DRAND48)
161:   PetscRandomRegister(PETSCRAND48,PetscRandomCreate_Rand48);
162: #endif
163: #if defined(PETSC_HAVE_SPRNG)
164:   PetscRandomRegister(PETSCSPRNG, PetscRandomCreate_Sprng);
165: #endif
166:   PetscRandomRegister(PETSCRANDER48,PetscRandomCreate_Rander48);
167: #if defined(PETSC_HAVE_RANDOM123)
168:   PetscRandomRegister(PETSCRANDOM123, PetscRandomCreate_Random123);
169: #endif
170:   return(0);
171: }