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 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: .seealso: 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:   if (rnd->ops->destroy) {
     41:     (*rnd->ops->destroy)(rnd);

     43:     rnd->ops->destroy = NULL;
     44:   }
     45:   (*r)(rnd);
     46:   PetscRandomSeed(rnd);

     48:   PetscObjectChangeTypeName((PetscObject)rnd, type);
     49:   return 0;
     50: }

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

     55:   Not Collective

     57:   Input Parameter:
     58: . rnd  - The random number generator context

     60:   Output Parameter:
     61: . type - The type name

     63:   Level: intermediate

     65: .seealso: PetscRandomSetType(), PetscRandomCreate()
     66: @*/
     67: PetscErrorCode  PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
     68: {
     71:   *type = ((PetscObject)rnd)->type_name;
     72:   return 0;
     73: }

     75: /*@C
     76:   PetscRandomRegister -  Adds a new PetscRandom component implementation

     78:   Not Collective

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

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

     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:   Notes:
    103:     For an example of the code needed to interface your own random number generator see
    104:          src/sys/random/impls/rand/rand.c

    106:   Level: advanced

    108: .seealso: PetscRandomRegisterAll(), PetscRandomRegisterDestroy(), PetscRandomRegister()
    109: @*/
    110: PetscErrorCode  PetscRandomRegister(const char sname[], PetscErrorCode (*function)(PetscRandom))
    111: {
    112:   PetscRandomInitializePackage();
    113:   PetscFunctionListAdd(&PetscRandomList,sname,function);
    114:   return 0;
    115: }

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

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

    137:   Not Collective

    139:   Level: advanced

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