Actual source code: randreg.c


  2: #include <petsc/private/randomimpl.h>

  4: PetscFunctionList PetscRandomList              = NULL;
  5: PetscBool         PetscRandomRegisterAllCalled = PETSC_FALSE;

  7: /*@C
  8:   PetscRandomSetType - Builds a context for generating a particular type of random numbers.

 10:   Collective

 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:   Note:
 21:   See "petsc/include/petscsys.h" for available random types (for instance, `PETSCRAND48`, `PETSCRAND`).

 23:   Level: intermediate

 25: .seealso: `PetscRandom`, `PetscRandomGetType()`, `PetscRandomCreate()`
 26: @*/

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

 34:   PetscObjectTypeCompare((PetscObject)rnd, type, &match);
 35:   if (match) return 0;

 37:   PetscFunctionListFind(PetscRandomList, type, &r);

 40:   PetscTryTypeMethod(rnd, destroy);
 41:   rnd->ops->destroy = NULL;

 43:   (*r)(rnd);
 44:   PetscRandomSeed(rnd);

 46:   PetscObjectChangeTypeName((PetscObject)rnd, type);
 47:   return 0;
 48: }

 50: /*@C
 51:   PetscRandomGetType - Gets the type name (as a string) from the `PetscRandom`.

 53:   Not Collective

 55:   Input Parameter:
 56: . rnd  - The random number generator context

 58:   Output Parameter:
 59: . type - The type name

 61:   Level: intermediate

 63: .seealso: `PetscRandom`, `PetscRandomSetType()`, `PetscRandomCreate()`
 64: @*/
 65: PetscErrorCode PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
 66: {
 69:   *type = ((PetscObject)rnd)->type_name;
 70:   return 0;
 71: }

 73: /*@C
 74:   PetscRandomRegister -  Adds a new `PetscRandom` implementation

 76:   Not Collective

 78:   Input Parameters:
 79: + name        - The name of a new user-defined creation routine
 80: - create_func - The creation routine itself

 82:   Notes:
 83:   `PetscRandomRegister()` may be called multiple times to add several user-defined randome number generators

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

 87:   Sample usage:
 88: .vb
 89:     PetscRandomRegister("my_rand",  MyPetscRandomtorCreate);
 90: .ve

 92:   Then, your random type can be chosen with the procedural interface via
 93: .vb
 94:     PetscRandomCreate(MPI_Comm, PetscRandom *);
 95:     PetscRandomSetType(PetscRandom,"my_random_name");
 96: .ve
 97:    or at runtime via the option
 98: .vb
 99:     -random_type my_random_name
100: .ve

102:   Level: advanced

104: .seealso: `PetscRandom`, `PetscRandomRegisterAll()`, `PetscRandomRegisterDestroy()`, `PetscRandomRegister()`
105: @*/
106: PetscErrorCode PetscRandomRegister(const char sname[], PetscErrorCode (*function)(PetscRandom))
107: {
108:   PetscRandomInitializePackage();
109:   PetscFunctionListAdd(&PetscRandomList, sname, function);
110:   return 0;
111: }

113: #if defined(PETSC_HAVE_RAND)
114: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
115: #endif
116: #if defined(PETSC_HAVE_DRAND48)
117: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
118: #endif
119: #if defined(PETSC_HAVE_SPRNG)
120: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
121: #endif
122: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rander48(PetscRandom);
123: #if defined(PETSC_HAVE_RANDOM123)
124: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Random123(PetscRandom);
125: #endif
126: #if defined(PETSC_HAVE_CUDA)
127: PETSC_EXTERN PetscErrorCode PetscRandomCreate_CURAND(PetscRandom);
128: #endif

130: /*@C
131:   PetscRandomRegisterAll - Registers all of the components in the `PetscRandom` package.

133:   Not Collective

135:   Level: advanced

137: .seealso: `PetscRandom`, `PetscRandomRegister()`, `PetscRandomRegisterDestroy()`
138: @*/
139: PetscErrorCode PetscRandomRegisterAll(void)
140: {
141:   if (PetscRandomRegisterAllCalled) return 0;
142:   PetscRandomRegisterAllCalled = PETSC_TRUE;
143: #if defined(PETSC_HAVE_RAND)
144:   PetscRandomRegister(PETSCRAND, PetscRandomCreate_Rand);
145: #endif
146: #if defined(PETSC_HAVE_DRAND48)
147:   PetscRandomRegister(PETSCRAND48, PetscRandomCreate_Rand48);
148: #endif
149: #if defined(PETSC_HAVE_SPRNG)
150:   PetscRandomRegister(PETSCSPRNG, PetscRandomCreate_Sprng);
151: #endif
152:   PetscRandomRegister(PETSCRANDER48, PetscRandomCreate_Rander48);
153: #if defined(PETSC_HAVE_RANDOM123)
154:   PetscRandomRegister(PETSCRANDOM123, PetscRandomCreate_Random123);
155: #endif
156: #if defined(PETSC_HAVE_CUDA)
157:   PetscRandomRegister(PETSCCURAND, PetscRandomCreate_CURAND);
158: #endif
159:   return 0;
160: }