Date: 2016-07-24T16:47:05+00:00

Actual source code: randreg.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

      2: #include <../src/sys/classes/random/randomimpl.h>         /*I "petscsys.h" I*/

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

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

     12:   Collective on PetscRandom

     14:   Input Parameters:
     15: + rnd   - The random number generator context
     16: - type - The name of the random type

     18:   Options Database Key:
     19: . -random_type <type> - Sets the random type; use -help for a list
     20:                      of available types

     22:   Notes:
     23:   See "petsc/include/petscsys.h" for available random types (for instance, PETSCRAND48, PETSCRAND).

     25:   Level: intermediate

     27: .keywords: random, set, type
     28: .seealso: PetscRandomGetType(), PetscRandomCreate()
     29: @*/

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

     39:   PetscObjectTypeCompare((PetscObject)rnd, type, &match);
     40:   if (match) return(0);

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

     45:   if (rnd->ops->destroy) {
     46:     (*rnd->ops->destroy)(rnd);

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

     53:   PetscObjectChangeTypeName((PetscObject)rnd, type);
     54:   return(0);
     55: }

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

     62:   Not Collective

     64:   Input Parameter:
     65: . rnd  - The random number generator context

     67:   Output Parameter:
     68: . type - The type name

     70:   Level: intermediate

     72: .keywords: random, get, type, name
     73: .seealso: PetscRandomSetType(), PetscRandomCreate()
     74: @*/
     75: PetscErrorCode  PetscRandomGetType(PetscRandom rnd, PetscRandomType *type)
     76: {
     80:   *type = ((PetscObject)rnd)->type_name;
     81:   return(0);
     82: }

     86: /*@C
     87:   PetscRandomRegister -  Adds a new PetscRandom component implementation

     89:   Not Collective

     91:   Input Parameters:
     92: + name        - The name of a new user-defined creation routine
     93: - create_func - The creation routine itself

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

     98:   Sample usage:
     99: .vb
    100:     PetscRandomRegister("my_rand",  MyPetscRandomtorCreate);
    101: .ve

    103:   Then, your random type can be chosen with the procedural interface via
    104: .vb
    105:     PetscRandomCreate(MPI_Comm, PetscRandom *);
    106:     PetscRandomSetType(PetscRandom,"my_random_name");
    107: .ve
    108:    or at runtime via the option
    109: .vb
    110:     -random_type my_random_name
    111: .ve

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

    116:   Level: advanced

    118: .keywords: PetscRandom, register

    120: .seealso: PetscRandomRegisterAll(), PetscRandomRegisterDestroy(), PetscRandomRegister()
    121: @*/
    122: PetscErrorCode  PetscRandomRegister(const char sname[], PetscErrorCode (*function)(PetscRandom))
    123: {

    127:   PetscFunctionListAdd(&PetscRandomList,sname,function);
    128:   return(0);
    129: }

    131: #if defined(PETSC_HAVE_RAND)
    132: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom);
    133: #endif
    134: #if defined(PETSC_HAVE_DRAND48)
    135: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom);
    136: #endif
    137: #if defined(PETSC_HAVE_SPRNG)
    138: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Sprng(PetscRandom);
    139: #endif
    140: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rander48(PetscRandom);

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

    147:   Not Collective

    149:   Level: advanced

    151: .keywords: PetscRandom, register, all
    152: .seealso:  PetscRandomRegister(), PetscRandomRegisterDestroy()
    153: @*/
    154: PetscErrorCode  PetscRandomRegisterAll(void)
    155: {

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