Actual source code: randomc.c

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

  6:     Numbers can be shuffled for use as random tuples

  8:     Multiple random number generators may be used

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

 14: #include <petsc/private/randomimpl.h>
 15: #include <petscviewer.h>

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

 20: /*@
 21:   PetscRandomDestroy - Destroys a `PetscRandom` object that was created by `PetscRandomCreate()`.

 23:   Collective

 25:   Input Parameter:
 26: . r - the random number generator object

 28:   Level: intermediate

 30: .seealso: `PetscRandom`, `PetscRandomGetValue()`, `PetscRandomCreate()`, `VecSetRandom()`
 31: @*/
 32: PetscErrorCode PetscRandomDestroy(PetscRandom *r)
 33: {
 34:   PetscFunctionBegin;
 35:   if (!*r) PetscFunctionReturn(PETSC_SUCCESS);
 37:   if (--((PetscObject)*r)->refct > 0) {
 38:     *r = NULL;
 39:     PetscFunctionReturn(PETSC_SUCCESS);
 40:   }
 41:   PetscTryTypeMethod(*r, destroy);
 42:   PetscCall(PetscHeaderDestroy(r));
 43:   PetscFunctionReturn(PETSC_SUCCESS);
 44: }

 46: /*@C
 47:   PetscRandomGetSeed - Gets the random seed.

 49:   Not collective

 51:   Input Parameter:
 52: . r - The random number generator context

 54:   Output Parameter:
 55: . seed - The random seed

 57:   Level: intermediate

 59: .seealso: `PetscRandom`, `PetscRandomCreate()`, `PetscRandomSetSeed()`, `PetscRandomSeed()`
 60: @*/
 61: PetscErrorCode PetscRandomGetSeed(PetscRandom r, PetscInt64 *seed)
 62: {
 63:   PetscFunctionBegin;
 65:   if (seed) {
 66:     PetscAssertPointer(seed, 2);
 67:     *seed = (PetscInt64)r->seed;
 68:   }
 69:   PetscFunctionReturn(PETSC_SUCCESS);
 70: }

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

 75:   Not collective

 77:   Input Parameters:
 78: + r    - The random number generator context
 79: - seed - The random seed

 81:   Level: intermediate

 83:   Example Usage:
 84: .vb
 85:       PetscRandomSetSeed(r,a positive integer);
 86:       PetscRandomSeed(r);
 87:       PetscRandomGetValue() will now start with the new seed.

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

 93: .seealso: `PetscRandom`, `PetscRandomCreate()`, `PetscRandomGetSeed()`, `PetscRandomSeed()`
 94: @*/
 95: PetscErrorCode PetscRandomSetSeed(PetscRandom r, PetscInt64 seed)
 96: {
 97:   PetscFunctionBegin;
 99:   r->seed = (unsigned long)seed;
100:   PetscCall(PetscInfo(NULL, "Setting seed to %d\n", (int)seed));
101:   PetscFunctionReturn(PETSC_SUCCESS);
102: }

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

107:   Collective

109:   Input Parameter:
110: . rnd - The random number generator context

112:   Level: intermediate

114: .seealso: `PetscRandomSetFromOptions()`, `PetscRandomSetType()`
115: */
116: static PetscErrorCode PetscRandomSetTypeFromOptions_Private(PetscRandom rnd, PetscOptionItems PetscOptionsObject)
117: {
118:   PetscBool   opt;
119:   const char *defaultType;
120:   char        typeName[256];

122:   PetscFunctionBegin;
123:   if (((PetscObject)rnd)->type_name) {
124:     defaultType = ((PetscObject)rnd)->type_name;
125:   } else {
126:     defaultType = PETSCRANDER48;
127:   }

129:   PetscCall(PetscRandomRegisterAll());
130:   PetscCall(PetscOptionsFList("-random_type", "PetscRandom type", "PetscRandomSetType", PetscRandomList, defaultType, typeName, 256, &opt));
131:   if (opt) {
132:     PetscCall(PetscRandomSetType(rnd, typeName));
133:   } else {
134:     PetscCall(PetscRandomSetType(rnd, defaultType));
135:   }
136:   PetscFunctionReturn(PETSC_SUCCESS);
137: }

139: /*@
140:   PetscRandomSetFromOptions - Configures the random number generator from the options database.

142:   Collective

144:   Input Parameter:
145: . rnd - The random number generator context

147:   Options Database Keys:
148: + -random_seed seed         - provide a seed to the random number generator
149: - -random_no_imaginary_part - makes the imaginary part of the random number zero, this is useful when you want the
150:                               same code to produce the same result when run with real numbers or complex numbers for regression testing purposes

152:   Level: beginner

154:   Note:
155:   Must be called after `PetscRandomCreate()` but before the rnd is used.

157: .seealso: `PetscRandom`, `PetscRandomCreate()`, `PetscRandomSetType()`
158: @*/
159: PetscErrorCode PetscRandomSetFromOptions(PetscRandom rnd)
160: {
161:   PetscBool set, noimaginary = PETSC_FALSE;
162:   PetscInt  seed;

164:   PetscFunctionBegin;

167:   PetscObjectOptionsBegin((PetscObject)rnd);

169:   /* Handle PetscRandom type options */
170:   PetscCall(PetscRandomSetTypeFromOptions_Private(rnd, PetscOptionsObject));

172:   /* Handle specific random generator's options */
173:   PetscTryTypeMethod(rnd, setfromoptions, PetscOptionsObject);
174:   PetscCall(PetscOptionsInt("-random_seed", "Seed to use to generate random numbers", "PetscRandomSetSeed", 0, &seed, &set));
175:   if (set) {
176:     PetscCall(PetscRandomSetSeed(rnd, (unsigned long int)seed));
177:     PetscCall(PetscRandomSeed(rnd));
178:   }
179:   PetscCall(PetscOptionsBool("-random_no_imaginary_part", "The imaginary part of the random number will be zero", "PetscRandomSetInterval", noimaginary, &noimaginary, &set));
180: #if defined(PETSC_HAVE_COMPLEX)
181:   if (set) {
182:     if (noimaginary) {
183:       PetscScalar low, high;
184:       PetscCall(PetscRandomGetInterval(rnd, &low, &high));
185:       low  = low - PetscImaginaryPart(low);
186:       high = high - PetscImaginaryPart(high);
187:       PetscCall(PetscRandomSetInterval(rnd, low, high));
188:     }
189:   }
190: #endif
191:   PetscOptionsEnd();
192:   PetscCall(PetscRandomViewFromOptions(rnd, NULL, "-random_view"));
193:   PetscFunctionReturn(PETSC_SUCCESS);
194: }

196: /*@
197:   PetscRandomSetOptionsPrefix - Sets the prefix used for searching for all
198:   `PetscRandom` options in the database.

200:   Logically Collective

202:   Input Parameters:
203: + r      - the random number generator context
204: - prefix - the prefix to prepend to all option names

206:   Level: advanced

208:   Note:
209:   A hyphen (-) must NOT be given at the beginning of the prefix name.
210:   The first character of all runtime options is AUTOMATICALLY the hyphen.

212: .seealso: `PetscRandom`, `PetscRandomSetFromOptions()`
213: @*/
214: PetscErrorCode PetscRandomSetOptionsPrefix(PetscRandom r, const char prefix[])
215: {
216:   PetscFunctionBegin;
218:   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)r, prefix));
219:   PetscFunctionReturn(PETSC_SUCCESS);
220: }

222: #if defined(PETSC_HAVE_SAWS)
223: #include <petscviewersaws.h>
224: #endif

226: /*@
227:   PetscRandomViewFromOptions - View a `PetscRandom` object based on the options database

229:   Collective

231:   Input Parameters:
232: + A    - the random number generator context
233: . obj  - Optional object
234: - name - command line option

236:   Options Database Key:
237: . -name [viewertype][:...] - option name and values. See `PetscObjectViewFromOptions()` for the possible arguments

239:   Level: intermediate

241: .seealso: `PetscRandom`, `PetscRandomView`, `PetscObjectViewFromOptions()`, `PetscRandomCreate()`
242: @*/
243: PetscErrorCode PetscRandomViewFromOptions(PetscRandom A, PetscObject obj, const char name[])
244: {
245:   PetscFunctionBegin;
247:   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
248:   PetscFunctionReturn(PETSC_SUCCESS);
249: }

251: /*@
252:   PetscRandomView - Views a random number generator object.

254:   Collective

256:   Input Parameters:
257: + rnd    - The random number generator context
258: - viewer - an optional visualization context

260:   Level: beginner

262:   Note:
263:   The available visualization contexts include
264: +     `PETSC_VIEWER_STDOUT_SELF` - standard output (default)
265: -     `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard
266:   output where only the first processor opens
267:   the file.  All other processors send their
268:   data to the first processor to print.

270: .seealso: `PetscRandom`, `PetscRealView()`, `PetscScalarView()`, `PetscIntView()`
271: @*/
272: PetscErrorCode PetscRandomView(PetscRandom rnd, PetscViewer viewer)
273: {
274:   PetscBool isascii;
275: #if defined(PETSC_HAVE_SAWS)
276:   PetscBool issaws;
277: #endif

279:   PetscFunctionBegin;
282:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)rnd), &viewer));
284:   PetscCheckSameComm(rnd, 1, viewer, 2);
285:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
286: #if defined(PETSC_HAVE_SAWS)
287:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws));
288: #endif
289:   if (isascii) {
290:     PetscMPIInt rank;
291:     PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)rnd, viewer));
292:     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)rnd), &rank));
293:     PetscCall(PetscViewerASCIIPushSynchronized(viewer));
294:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Random type %s, seed %lu\n", rank, ((PetscObject)rnd)->type_name, rnd->seed));
295:     PetscCall(PetscViewerFlush(viewer));
296:     PetscCall(PetscViewerASCIIPopSynchronized(viewer));
297: #if defined(PETSC_HAVE_SAWS)
298:   } else if (issaws) {
299:     PetscMPIInt rank;
300:     const char *name;

302:     PetscCall(PetscObjectGetName((PetscObject)rnd, &name));
303:     PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
304:     if (!((PetscObject)rnd)->amsmem && rank == 0) {
305:       char dir[1024];

307:       PetscCall(PetscObjectViewSAWs((PetscObject)rnd, viewer));
308:       PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/Low", name));
309:       PetscCallSAWs(SAWs_Register, (dir, &rnd->low, 1, SAWs_READ, SAWs_DOUBLE));
310:     }
311: #endif
312:   }
313:   PetscFunctionReturn(PETSC_SUCCESS);
314: }

316: /*@
317:   PetscRandomCreate - Creates an object for generating random numbers,
318:   and initializes the random-number generator.

320:   Collective

322:   Input Parameter:
323: . comm - MPI communicator

325:   Output Parameter:
326: . r - the random number generator object

328:   Level: intermediate

330:   Notes:
331:   The random type has to be set by `PetscRandomSetType()`.

333:   This is only a primitive "parallel" random number generator, it should NOT
334:   be used for sophisticated parallel Monte Carlo methods since it will very likely
335:   not have the correct statistics across processors. You can provide your own
336:   parallel generator using `PetscRandomRegister()`;

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

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

344:   Example of Usage:
345: .vb
346:       PetscRandomCreate(PETSC_COMM_SELF,&r);
347:       PetscRandomSetType(r,PETSCRAND48);
348:       PetscRandomGetValue(r,&value1);
349:       PetscRandomGetValueReal(r,&value2);
350:       PetscRandomDestroy(&r);
351: .ve

353: .seealso: `PetscRandomSetType()`, `PetscRandomGetValue()`, `PetscRandomGetValueReal()`, `PetscRandomSetInterval()`,
354:           `PetscRandomDestroy()`, `VecSetRandom()`, `PetscRandomType`, `PetscRandom`
355: @*/
356: PetscErrorCode PetscRandomCreate(MPI_Comm comm, PetscRandom *r)
357: {
358:   PetscRandom rr;
359:   PetscMPIInt rank;

361:   PetscFunctionBegin;
362:   PetscAssertPointer(r, 2);
363:   PetscCall(PetscRandomInitializePackage());

365:   PetscCall(PetscHeaderCreate(rr, PETSC_RANDOM_CLASSID, "PetscRandom", "Random number generator", "Sys", comm, PetscRandomDestroy, PetscRandomView));
366:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
367:   rr->data  = NULL;
368:   rr->low   = 0.0;
369:   rr->width = 1.0;
370:   rr->iset  = PETSC_FALSE;
371:   rr->seed  = 0x12345678 + 76543 * rank;
372:   PetscCall(PetscRandomSetType(rr, PETSCRANDER48));
373:   *r = rr;
374:   PetscFunctionReturn(PETSC_SUCCESS);
375: }

377: /*@
378:   PetscRandomSeed - Seed the random number generator.

380:   Not collective

382:   Input Parameter:
383: . r - The random number generator context

385:   Level: intermediate

387:   Example Usage:
388: .vb
389:       PetscRandomSetSeed(r,a positive integer);
390:       PetscRandomSeed(r);
391:       PetscRandomGetValue() will now start with the new seed.

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

397: .seealso: `PetscRandomCreate()`, `PetscRandomGetSeed()`, `PetscRandomSetSeed()`
398: @*/
399: PetscErrorCode PetscRandomSeed(PetscRandom r)
400: {
401:   PetscFunctionBegin;

405:   PetscUseTypeMethod(r, seed);
406:   PetscCall(PetscObjectStateIncrease((PetscObject)r));
407:   PetscFunctionReturn(PETSC_SUCCESS);
408: }