Actual source code: randreg.c
petsc-3.10.5 2019-03-28
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:   PetscRandomInitializePackage();
    123:   PetscFunctionListAdd(&PetscRandomList,sname,function);
    124:   return(0);
    125: }

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

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

    144:   Not Collective

    146:   Level: advanced

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

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