Actual source code: randomc.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2: /*
  3:     This file contains routines for interfacing to random number generators.
  4:     This provides more than just an interface to some system random number
  5:     generator:

  7:     Numbers can be shuffled for use as random tuples

  9:     Multiple random number generators may be used

 11:     We are still not sure what interface we want here.  There should be
 12:     one to reinitialize and set the seed.
 13:  */

 15:  #include <../src/sys/classes/random/randomimpl.h>
 16:  #include <petscviewer.h>

 18: /* Logging support */
 19: PetscClassId PETSC_RANDOM_CLASSID;

 21: /*@
 22:    PetscRandomDestroy - Destroys a context that has been formed by
 23:    PetscRandomCreate().

 25:    Collective on PetscRandom

 27:    Intput Parameter:
 28: .  r  - the random number generator context

 30:    Level: intermediate

 32: .seealso: PetscRandomGetValue(), PetscRandomCreate(), VecSetRandom()
 33: @*/
 34: PetscErrorCode  PetscRandomDestroy(PetscRandom *r)
 35: {

 39:   if (!*r) return(0);
 41:   if (--((PetscObject)(*r))->refct > 0) {*r = 0; return(0);}
 42:   if ((*r)->ops->destroy) {
 43:     (*(*r)->ops->destroy)(*r);
 44:   }
 45:   PetscHeaderDestroy(r);
 46:   return(0);
 47: }


 50: /*@C
 51:    PetscRandomGetSeed - Gets the random seed.

 53:    Not collective

 55:    Input Parameters:
 56: .  r - The random number generator context

 58:    Output Parameter:
 59: .  seed - The random seed

 61:    Level: intermediate

 63: .seealso: PetscRandomCreate(), PetscRandomSetSeed(), PetscRandomSeed()
 64: @*/
 65: PetscErrorCode  PetscRandomGetSeed(PetscRandom r,unsigned long *seed)
 66: {
 69:   if (seed) {
 71:     *seed = r->seed;
 72:   }
 73:   return(0);
 74: }

 76: /*@C
 77:    PetscRandomSetSeed - Sets the random seed. You MUST call PetscRandomSeed() after this call to have the new seed used.

 79:    Not collective

 81:    Input Parameters:
 82: +  r  - The random number generator context
 83: -  seed - The random seed

 85:    Level: intermediate

 87:    Usage:
 88:       PetscRandomSetSeed(r,a positive integer);
 89:       PetscRandomSeed(r);  PetscRandomGetValue() will now start with the new seed.

 91:       PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes
 92:         the seed. The random numbers generated will be the same as before.

 94: .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSeed()
 95: @*/
 96: PetscErrorCode  PetscRandomSetSeed(PetscRandom r,unsigned long seed)
 97: {

102:   r->seed = seed;
103:   PetscInfo1(NULL,"Setting seed to %d\n",(int)seed);
104:   return(0);
105: }

107: /* ------------------------------------------------------------------- */
108: /*
109:   PetscRandomSetTypeFromOptions_Private - Sets the type of random generator from user options. Defaults to type PETSCRAND48 or PETSCRAND.

111:   Collective on PetscRandom

113:   Input Parameter:
114: . rnd - The random number generator context

116:   Level: intermediate

118: .seealso: PetscRandomSetFromOptions(), PetscRandomSetType()
119: */
120: static PetscErrorCode PetscRandomSetTypeFromOptions_Private(PetscOptionItems *PetscOptionsObject,PetscRandom rnd)
121: {
122:   PetscBool      opt;
123:   const char     *defaultType;
124:   char           typeName[256];

128:   if (((PetscObject)rnd)->type_name) {
129:     defaultType = ((PetscObject)rnd)->type_name;
130:   } else {
131:     defaultType = PETSCRANDER48;
132:   }

134:   PetscRandomRegisterAll();
135:   PetscOptionsFList("-random_type","PetscRandom type","PetscRandomSetType",PetscRandomList,defaultType,typeName,256,&opt);
136:   if (opt) {
137:     PetscRandomSetType(rnd, typeName);
138:   } else {
139:     PetscRandomSetType(rnd, defaultType);
140:   }
141:   return(0);
142: }

144: /*@
145:   PetscRandomSetFromOptions - Configures the random number generator from the options database.

147:   Collective on PetscRandom

149:   Input Parameter:
150: . rnd - The random number generator context

152:   Options Database:
153: + -random_seed <integer> - provide a seed to the random number generater
154: - -random_no_imaginary_part - makes the imaginary part of the random number zero, this is useful when you want the
155:                               same code to produce the same result when run with real numbers or complex numbers for regression testing purposes

157:   Notes:
158:     To see all options, run your program with the -help option.
159:           Must be called after PetscRandomCreate() but before the rnd is used.

161:   Level: beginner

163: .seealso: PetscRandomCreate(), PetscRandomSetType()
164: @*/
165: PetscErrorCode  PetscRandomSetFromOptions(PetscRandom rnd)
166: {
168:   PetscBool      set,noimaginary = PETSC_FALSE;
169:   PetscInt       seed;


174:   PetscObjectOptionsBegin((PetscObject)rnd);

176:   /* Handle PetscRandom type options */
177:   PetscRandomSetTypeFromOptions_Private(PetscOptionsObject,rnd);

179:   /* Handle specific random generator's options */
180:   if (rnd->ops->setfromoptions) {
181:     (*rnd->ops->setfromoptions)(PetscOptionsObject,rnd);
182:   }
183:   PetscOptionsInt("-random_seed","Seed to use to generate random numbers","PetscRandomSetSeed",0,&seed,&set);
184:   if (set) {
185:     PetscRandomSetSeed(rnd,(unsigned long int)seed);
186:     PetscRandomSeed(rnd);
187:   }
188:   PetscOptionsBool("-random_no_imaginary_part","The imaginary part of the random number will be zero","PetscRandomSetInterval",noimaginary,&noimaginary,&set);
189: #if defined(PETSC_HAVE_COMPLEX)
190:   if (set) {
191:     if (noimaginary) {
192:       PetscScalar low,high;
193:       PetscRandomGetInterval(rnd,&low,&high);
194:       low  = low - PetscImaginaryPart(low);
195:       high = high - PetscImaginaryPart(high);
196:       PetscRandomSetInterval(rnd,low,high);
197:     }
198:   }
199: #endif
200:   PetscOptionsEnd();
201:   PetscRandomViewFromOptions(rnd,NULL, "-random_view");
202:   return(0);
203: }

205: #if defined(PETSC_HAVE_SAWS)
206:  #include <petscviewersaws.h>
207: #endif
208: /*@C
209:    PetscRandomView - Views a random number generator object.

211:    Collective on PetscRandom

213:    Input Parameters:
214: +  rnd - The random number generator context
215: -  viewer - an optional visualization context

217:    Notes:
218:    The available visualization contexts include
219: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
220: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
221:          output where only the first processor opens
222:          the file.  All other processors send their
223:          data to the first processor to print.

225:    You can change the format the vector is printed using the
226:    option PetscViewerPushFormat().

228:    Level: beginner

230: .seealso:  PetscRealView(), PetscScalarView(), PetscIntView()
231: @*/
232: PetscErrorCode  PetscRandomView(PetscRandom rnd,PetscViewer viewer)
233: {
235:   PetscBool      iascii;
236: #if defined(PETSC_HAVE_SAWS)
237:   PetscBool      issaws;
238: #endif

243:   if (!viewer) {
244:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)rnd),&viewer);
245:   }
248:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
249: #if defined(PETSC_HAVE_SAWS)
250:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
251: #endif
252:   if (iascii) {
253:     PetscMPIInt rank;
254:     PetscObjectPrintClassNamePrefixType((PetscObject)rnd,viewer);
255:     MPI_Comm_rank(PetscObjectComm((PetscObject)rnd),&rank);
256:     PetscViewerASCIIPushSynchronized(viewer);
257:     PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Random type %s, seed %lu\n",rank,((PetscObject)rnd)->type_name,rnd->seed);
258:     PetscViewerFlush(viewer);
259:     PetscViewerASCIIPopSynchronized(viewer);
260: #if defined(PETSC_HAVE_SAWS)
261:   } else if (issaws) {
262:     PetscMPIInt rank;
263:     const char  *name;

265:     PetscObjectGetName((PetscObject)rnd,&name);
266:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
267:     if (!((PetscObject)rnd)->amsmem && !rank) {
268:       char       dir[1024];

270:       PetscObjectViewSAWs((PetscObject)rnd,viewer);
271:       PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/Low",name);
272:       PetscStackCallSAWs(SAWs_Register,(dir,&rnd->low,1,SAWs_READ,SAWs_DOUBLE));
273:     }
274: #endif
275:   }
276:   return(0);
277: }

279: /*@
280:    PetscRandomCreate - Creates a context for generating random numbers,
281:    and initializes the random-number generator.

283:    Collective

285:    Input Parameters:
286: .  comm - MPI communicator

288:    Output Parameter:
289: .  r  - the random number generator context

291:    Level: intermediate

293:    Notes:
294:    The random type has to be set by PetscRandomSetType().

296:    This is only a primative "parallel" random number generator, it should NOT
297:    be used for sophisticated parallel Monte Carlo methods since it will very likely
298:    not have the correct statistics across processors. You can provide your own
299:    parallel generator using PetscRandomRegister();

301:    If you create a PetscRandom() using PETSC_COMM_SELF on several processors then
302:    the SAME random numbers will be generated on all those processors. Use PETSC_COMM_WORLD
303:    or the appropriate parallel communicator to eliminate this issue.

305:    Use VecSetRandom() to set the elements of a vector to random numbers.

307:    Example of Usage:
308: .vb
309:       PetscRandomCreate(PETSC_COMM_SELF,&r);
310:       PetscRandomSetType(r,PETSCRAND48);
311:       PetscRandomGetValue(r,&value1);
312:       PetscRandomGetValueReal(r,&value2);
313:       PetscRandomDestroy(&r);
314: .ve

316: .seealso: PetscRandomSetType(), PetscRandomGetValue(), PetscRandomGetValueReal(), PetscRandomSetInterval(),
317:           PetscRandomDestroy(), VecSetRandom(), PetscRandomType
318: @*/

320: PetscErrorCode  PetscRandomCreate(MPI_Comm comm,PetscRandom *r)
321: {
322:   PetscRandom    rr;
324:   PetscMPIInt    rank;

328:   *r = NULL;
329:   PetscRandomInitializePackage();

331:   PetscHeaderCreate(rr,PETSC_RANDOM_CLASSID,"PetscRandom","Random number generator","Sys",comm,PetscRandomDestroy,PetscRandomView);

333:   MPI_Comm_rank(comm,&rank);

335:   rr->data  = NULL;
336:   rr->low   = 0.0;
337:   rr->width = 1.0;
338:   rr->iset  = PETSC_FALSE;
339:   rr->seed  = 0x12345678 + 76543*rank;
340:   PetscRandomSetType(rr,PETSCRANDER48);
341:   *r = rr;
342:   return(0);
343: }

345: /*@
346:    PetscRandomSeed - Seed the generator.

348:    Not collective

350:    Input Parameters:
351: .  r - The random number generator context

353:    Level: intermediate

355:    Usage:
356:       PetscRandomSetSeed(r,a positive integer);
357:       PetscRandomSeed(r);  PetscRandomGetValue() will now start with the new seed.

359:       PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes
360:         the seed. The random numbers generated will be the same as before.

362: .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSetSeed()
363: @*/
364: PetscErrorCode  PetscRandomSeed(PetscRandom r)
365: {


372:   (*r->ops->seed)(r);
373:   PetscObjectStateIncrease((PetscObject)r);
374:   return(0);
375: }