Actual source code: randreg.c

petsc-3.8.2 2017-11-09
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: For an example of the code needed to interface your own random number generator see
108:          src/sys/random/impls/rand/rand.c

110:   Level: advanced

112: .keywords: PetscRandom, register

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

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

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

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

142:   Not Collective

144:   Level: advanced

146: .keywords: PetscRandom, register, all
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:   return(0);
170: }