Actual source code: random.c
1: #define PETSC_DLL
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/utils/random/randomimpl.h
16: #if defined (PETSC_HAVE_STDLIB_H)
17: #include <stdlib.h>
18: #endif
20: /* Logging support */
21: PetscCookie PETSC_RANDOM_COOKIE = 0;
25: /*@
26: PetscRandomDestroy - Destroys a context that has been formed by
27: PetscRandomCreate().
29: Collective on PetscRandom
31: Intput Parameter:
32: . r - the random number generator context
34: Level: intermediate
36: .seealso: PetscRandomGetValue(), PetscRandomCreate(), VecSetRandom()
37: @*/
38: PetscErrorCode PetscRandomDestroy(PetscRandom r)
39: {
43: if (--r->refct > 0) return(0);
44: PetscHeaderDestroy(r);
45: return(0);
46: }
50: /*@C
51: PetscRandomGetInterval - Gets the interval over which the random numbers
52: will be randomly distributed. By default, this interval is [0,1).
54: Not collective
56: Input Parameters:
57: . r - the random number generator context
59: Output Parameters:
60: + low - The lower bound of the interval
61: - high - The upper bound of the interval
63: Level: intermediate
65: Concepts: random numbers^range
67: .seealso: PetscRandomCreate(), PetscRandomSetInterval()
68: @*/
69: PetscErrorCode PetscRandomGetInterval(PetscRandom r,PetscScalar *low,PetscScalar *high)
70: {
73: if (low) {
75: *low = r->low;
76: }
77: if (high) {
79: *high = r->low+r->width;
80: }
81: return(0);
82: }
86: /*@C
87: PetscRandomSetInterval - Sets the interval over which the random numbers
88: will be randomly distributed. By default, this interval is [0,1).
90: Not collective
92: Input Parameters:
93: + r - the random number generator context
94: . low - The lower bound of the interval
95: - high - The upper bound of the interval
97: Level: intermediate
99: Concepts: random numbers^range
101: .seealso: PetscRandomCreate(), PetscRandomGetInterval()
102: @*/
103: PetscErrorCode PetscRandomSetInterval(PetscRandom r,PetscScalar low,PetscScalar high)
104: {
107: #if defined(PETSC_USE_COMPLEX)
108: if (PetscRealPart(low) >= PetscRealPart(high)) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"only low < high");
109: if (PetscImaginaryPart(low) >= PetscImaginaryPart(high)) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"only low < high");
110: #else
111: if (low >= high) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"only low < high: Instead %G %G",low,high);
112: #endif
113: r->low = low;
114: r->width = high-low;
115: r->iset = PETSC_TRUE;
116: return(0);
117: }
121: /*@C
122: PetscRandomGetSeed - Gets the random seed.
124: Not collective
126: Input Parameters:
127: . r - The random number generator context
129: Output Parameter:
130: . seed - The random seed
132: Level: intermediate
134: Concepts: random numbers^seed
136: .seealso: PetscRandomCreate(), PetscRandomSetSeed(), PetscRandomSeed()
137: @*/
138: PetscErrorCode PetscRandomGetSeed(PetscRandom r,unsigned long *seed)
139: {
142: if (seed) {
144: *seed = r->seed;
145: }
146: return(0);
147: }
151: /*@C
152: PetscRandomSetSeed - Sets the random seed.
154: Not collective
156: Input Parameters:
157: + r - The random number generator context
158: - seed - The random seed
160: Level: intermediate
162: Usage:
163: PetscRandomSetSeed(r,a positive integer);
164: PetscRandomSeed(r); PetscRandomGetValue() will now start with the new seed.
166: PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes
167: the seed. The random numbers generated will be the same as before.
169: Concepts: random numbers^seed
171: .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSeed()
172: @*/
173: PetscErrorCode PetscRandomSetSeed(PetscRandom r,unsigned long seed)
174: {
177: r->seed = seed;
178: return(0);
179: }
181: /* ------------------------------------------------------------------- */
184: /*
185: PetscRandomSetTypeFromOptions_Private - Sets the type of random generator from user options. Defaults to type PETSCRAND48 or PETSCRAND.
187: Collective on PetscRandom
189: Input Parameter:
190: . rnd - The random number generator context
192: Level: intermediate
194: .keywords: PetscRandom, set, options, database, type
195: .seealso: PetscRandomSetFromOptions(), PetscRandomSetType()
196: */
197: static PetscErrorCode PetscRandomSetTypeFromOptions_Private(PetscRandom rnd)
198: {
199: PetscTruth opt;
200: const char *defaultType;
201: char typeName[256];
205: if (rnd->type_name) {
206: defaultType = rnd->type_name;
207: } else {
208: #if defined(PETSC_HAVE_DRAND48)
209: defaultType = PETSCRAND48;
210: #elif defined(PETSC_HAVE_RAND)
211: defaultType = PETSCRAND;
212: #endif
213: }
215: if (!PetscRandomRegisterAllCalled) {
216: PetscRandomRegisterAll(PETSC_NULL);
217: }
218: PetscOptionsList("-random_type","PetscRandom type","PetscRandomSetType",PetscRandomList,defaultType,typeName,256,&opt);
219: if (opt) {
220: PetscRandomSetType(rnd, typeName);
221: } else {
222: PetscRandomSetType(rnd, defaultType);
223: }
224: return(0);
225: }
229: /*@
230: PetscRandomSetFromOptions - Configures the random number generator from the options database.
232: Collective on PetscRandom
234: Input Parameter:
235: . rnd - The random number generator context
237: Notes: To see all options, run your program with the -help option, or consult the users manual.
238: Must be called after PetscRandomCreate() but before the rnd is used.
240: Level: beginner
242: .keywords: PetscRandom, set, options, database
243: .seealso: PetscRandomCreate(), PetscRandomSetType()
244: @*/
245: PetscErrorCode PetscRandomSetFromOptions(PetscRandom rnd)
246: {
252: PetscOptionsBegin(rnd->comm, rnd->prefix, "PetscRandom options", "PetscRandom");
254: /* Handle PetscRandom type options */
255: PetscRandomSetTypeFromOptions_Private(rnd);
257: /* Handle specific random generator's options */
258: if (rnd->ops->setfromoptions) {
259: (*rnd->ops->setfromoptions)(rnd);
260: }
261: PetscOptionsEnd();
262: PetscRandomViewFromOptions(rnd, rnd->name);
263: return(0);
264: }
268: /*@C
269: PetscRandomView - Views a random number generator object.
271: Collective on PetscRandom
273: Input Parameters:
274: + rnd - The random number generator context
275: - viewer - an optional visualization context
277: Notes:
278: The available visualization contexts include
279: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
280: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
281: output where only the first processor opens
282: the file. All other processors send their
283: data to the first processor to print.
285: You can change the format the vector is printed using the
286: option PetscViewerSetFormat().
288: Level: beginner
290: .seealso: PetscRealView(), PetscScalarView(), PetscIntView()
291: @*/
292: PetscErrorCode PetscRandomView(PetscRandom rnd,PetscViewer viewer)
293: {
294: PetscErrorCode ierr;
295: PetscTruth iascii;
300: if (!viewer) viewer = PETSC_VIEWER_STDOUT_(rnd->comm);
303: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
304: if (iascii) {
305: PetscMPIInt rank;
306: MPI_Comm_rank(rnd->comm,&rank);
307: PetscViewerASCIISynchronizedPrintf(viewer,"[%D] Random type %s, seed %D\n",rank,rnd->type_name,rnd->seed);
308: PetscViewerFlush(viewer);
309: } else {
310: const char *tname;
311: PetscObjectGetName((PetscObject)viewer,&tname);
312: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for this object",tname);
313: }
314: return(0);
315: }
317: #undef __FUNCT__
319: /*@
320: PetscRandomViewFromOptions - This function visualizes the type and the seed of the generated random numbers based upon user options.
322: Collective on PetscRandom
324: Input Parameters:
325: . rnd - The random number generator context
326: . title - The title
328: Level: intermediate
330: .keywords: PetscRandom, view, options, database
331: .seealso: PetscRandomSetFromOptions()
332: @*/
333: PetscErrorCode PetscRandomViewFromOptions(PetscRandom rnd, char *title)
334: {
335: PetscTruth opt;
336: PetscViewer viewer;
337: char typeName[1024];
338: char fileName[PETSC_MAX_PATH_LEN];
339: size_t len;
340:
344: PetscOptionsHasName(rnd->prefix, "-random_view", &opt);
345: if (opt) {
346: PetscOptionsGetString(rnd->prefix, "-random_view", typeName, 1024, &opt);
347: PetscStrlen(typeName, &len);
348: if (len > 0) {
349: PetscViewerCreate(rnd->comm, &viewer);
350: PetscViewerSetType(viewer, typeName);
351: PetscOptionsGetString(rnd->prefix, "-random_view_file", fileName, 1024, &opt);
352: if (opt) {
353: PetscViewerFileSetName(viewer, fileName);
354: } else {
355: PetscViewerFileSetName(viewer, rnd->name);
356: }
357: PetscRandomView(rnd, viewer);
358: PetscViewerFlush(viewer);
359: PetscViewerDestroy(viewer);
360: } else {
361: PetscRandomView(rnd, PETSC_VIEWER_STDOUT_(rnd->comm));
362: }
363: }
364: return(0);
365: }
370: /*@
371: PetscRandomCreate - Creates a context for generating random numbers,
372: and initializes the random-number generator.
374: Collective on MPI_Comm
376: Input Parameters:
377: + comm - MPI communicator
379: Output Parameter:
380: . r - the random number generator context
382: Level: intermediate
384: Notes:
385: The random type has to be set by PetscRandomSetType().
387: This is only a primative "parallel" random number generator, it should NOT
388: be used for sophisticated parallel Monte Carlo methods since it will very likely
389: not have the correct statistics across processors. You can provide your own
390: parallel generator using PetscRandomRegister();
392: If you create a PetscRandom() using PETSC_COMM_SELF on several processors then
393: the SAME random numbers will be generated on all those processors. Use PETSC_COMM_WORLD
394: or the appropriate parallel communicator to eliminate this issue.
396: Use VecSetRandom() to set the elements of a vector to random numbers.
398: Example of Usage:
399: .vb
400: PetscRandomCreate(PETSC_COMM_SELF,&r);
401: PetscRandomSetType(r,PETSCRAND48);
402: PetscRandomGetValue(r,&value1);
403: PetscRandomGetValueReal(r,&value2);
404: PetscRandomGetValueImaginary(r,&value3);
405: PetscRandomDestroy(r);
406: .ve
408: Concepts: random numbers^creating
410: .seealso: PetscRandomSetType(), PetscRandomGetValue(), PetscRandomGetValueReal(), PetscRandomGetValueImaginary(), PetscRandomSetInterval(), PetscRandomDestroy(), VecSetRandom()
411: @*/
413: PetscErrorCode PetscRandomCreate(MPI_Comm comm,PetscRandom *r)
414: {
415: PetscRandom rr;
417: PetscMPIInt rank;
421: *r = PETSC_NULL;
422: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
423: PetscRandomInitializePackage(PETSC_NULL);
424: #endif
426: PetscHeaderCreate(rr,_p_PetscRandom,struct _PetscRandomOps,PETSC_RANDOM_COOKIE,-1,"PetscRandom",comm,PetscRandomDestroy,0);
427: PetscLogObjectMemory(rr, sizeof(struct _p_PetscRandom));
428: PetscMemzero(rr->ops, sizeof(struct _PetscRandomOps));
429: rr->bops->publish = PETSC_NULL;
430: rr->type_name = PETSC_NULL;
432: MPI_Comm_rank(comm,&rank);
433: rr->data = PETSC_NULL;
434: rr->low = 0.0;
435: rr->width = 1.0;
436: rr->iset = PETSC_FALSE;
437: rr->seed = 0x12345678 + 76543*rank;
438: *r = rr;
439: return(0);
440: }
444: /*@C
445: PetscRandomSeed - Seed the generator.
447: Not collective
449: Input Parameters:
450: . r - The random number generator context
452: Level: intermediate
454: Usage:
455: PetscRandomSetSeed(r,a positive integer);
456: PetscRandomSeed(r); PetscRandomGetValue() will now start with the new seed.
458: PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes
459: the seed. The random numbers generated will be the same as before.
461: Concepts: random numbers^seed
463: .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSetSeed()
464: @*/
465: PetscErrorCode PetscRandomSeed(PetscRandom r)
466: {
473: (*r->ops->seed)(r);
474: PetscObjectStateIncrease((PetscObject)r);
475: return(0);
476: }
480: /*@
481: PetscRandomGetValue - Generates a random number. Call this after first calling
482: PetscRandomCreate().
484: Not Collective
486: Intput Parameter:
487: . r - the random number generator context
489: Output Parameter:
490: . val - the value
492: Level: intermediate
494: Notes:
495: Use VecSetRandom() to set the elements of a vector to random numbers.
497: Example of Usage:
498: .vb
499: PetscRandomCreate(PETSC_COMM_WORLD,&r);
500: PetscRandomGetValue(r,&value1);
501: PetscRandomGetValue(r,&value2);
502: PetscRandomGetValue(r,&value3);
503: PetscRandomDestroy(r);
504: .ve
506: Concepts: random numbers^getting
508: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
509: @*/
510: PetscErrorCode PetscRandomGetValue(PetscRandom r,PetscScalar *val)
511: {
519: (*r->ops->getvalue)(r,val);
520: PetscObjectStateIncrease((PetscObject)r);
521: return(0);
522: }
526: /*@
527: PetscRandomGetValueReal - Generates a random number. Call this after first calling
528: PetscRandomCreate().
530: Not Collective
532: Intput Parameter:
533: . r - the random number generator context
535: Output Parameter:
536: . val - the value
538: Level: intermediate
540: Notes:
541: Use VecSetRandom() to set the elements of a vector to random numbers.
543: Example of Usage:
544: .vb
545: PetscRandomCreate(PETSC_COMM_WORLD,&r);
546: PetscRandomGetValueReal(r,&value1);
547: PetscRandomGetValueReal(r,&value2);
548: PetscRandomGetValueReal(r,&value3);
549: PetscRandomDestroy(r);
550: .ve
552: Concepts: random numbers^getting
554: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
555: @*/
556: PetscErrorCode PetscRandomGetValueReal(PetscRandom r,PetscReal *val)
557: {
565: (*r->ops->getvaluereal)(r,val);
566: PetscObjectStateIncrease((PetscObject)r);
567: return(0);
568: }
572: /*@
573: PetscRandomGetValueImaginary - Generates a random number. Call this after first calling
574: PetscRandomCreate().
576: Not Collective
578: Intput Parameter:
579: . r - the random number generator context
581: Output Parameter:
582: . val - the value
584: Options Database Keys:
585: + -random_type petscrand48
586: . -random_type petscrand
587: - -random_type sprng, uses SPRNG package
589: Level: intermediate
591: Notes:
592: Use VecSetRandom() to set the elements of a vector to random numbers.
594: Example of Usage:
595: .vb
596: PetscRandomCreate(PETSC_COMM_WORLD,&r);
597: PetscRandomGetValueImaginary(r,&value1);
598: PetscRandomGetValueImaginary(r,&value2);
599: PetscRandomGetValueImaginary(r,&value3);
600: PetscRandomDestroy(r);
601: .ve
603: Concepts: random numbers^getting
605: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
606: @*/
607: PetscErrorCode PetscRandomGetValueImaginary(PetscRandom r,PetscScalar *val)
608: {
616: (*r->ops->getvalueimaginary)(r,val);
617: PetscObjectStateIncrease((PetscObject)r);
618: return(0);
619: }