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/random/randomimpl.h
16: #if defined (PETSC_HAVE_STDLIB_H)
17: #include <stdlib.h>
18: #endif
20: /* Logging support */
21: PetscCookie PETSC_RANDOM_COOKIE;
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 (--((PetscObject)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 (((PetscObject)rnd)->type_name) {
206: defaultType = ((PetscObject)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) {PetscRandomRegisterAll(PETSC_NULL);}
216: PetscOptionsList("-random_type","PetscRandom type","PetscRandomSetType",PetscRandomList,defaultType,typeName,256,&opt);
217: if (opt) {
218: PetscRandomSetType(rnd, typeName);
219: } else {
220: PetscRandomSetType(rnd, defaultType);
221: }
222: return(0);
223: }
227: /*@
228: PetscRandomSetFromOptions - Configures the random number generator from the options database.
230: Collective on PetscRandom
232: Input Parameter:
233: . rnd - The random number generator context
235: Notes: To see all options, run your program with the -help option, or consult the users manual.
236: Must be called after PetscRandomCreate() but before the rnd is used.
238: Level: beginner
240: .keywords: PetscRandom, set, options, database
241: .seealso: PetscRandomCreate(), PetscRandomSetType()
242: @*/
243: PetscErrorCode PetscRandomSetFromOptions(PetscRandom rnd)
244: {
250: PetscOptionsBegin(((PetscObject)rnd)->comm, ((PetscObject)rnd)->prefix, "PetscRandom options", "PetscRandom");
252: /* Handle PetscRandom type options */
253: PetscRandomSetTypeFromOptions_Private(rnd);
255: /* Handle specific random generator's options */
256: if (rnd->ops->setfromoptions) {
257: (*rnd->ops->setfromoptions)(rnd);
258: }
259: PetscOptionsEnd();
260: PetscRandomViewFromOptions(rnd, ((PetscObject)rnd)->name);
261: return(0);
262: }
266: /*@C
267: PetscRandomView - Views a random number generator object.
269: Collective on PetscRandom
271: Input Parameters:
272: + rnd - The random number generator context
273: - viewer - an optional visualization context
275: Notes:
276: The available visualization contexts include
277: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
278: - PETSC_VIEWER_STDOUT_WORLD - synchronized standard
279: output where only the first processor opens
280: the file. All other processors send their
281: data to the first processor to print.
283: You can change the format the vector is printed using the
284: option PetscViewerSetFormat().
286: Level: beginner
288: .seealso: PetscRealView(), PetscScalarView(), PetscIntView()
289: @*/
290: PetscErrorCode PetscRandomView(PetscRandom rnd,PetscViewer viewer)
291: {
292: PetscErrorCode ierr;
293: PetscTruth iascii;
298: if (!viewer) {
299: PetscViewerASCIIGetStdout(((PetscObject)rnd)->comm,&viewer);
300: }
303: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
304: if (iascii) {
305: PetscMPIInt rank;
306: MPI_Comm_rank(((PetscObject)rnd)->comm,&rank);
307: PetscViewerASCIISynchronizedPrintf(viewer,"[%D] Random type %s, seed %D\n",rank,((PetscObject)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(((PetscObject)rnd)->prefix, "-random_view", &opt);
345: if (opt) {
346: PetscOptionsGetString(((PetscObject)rnd)->prefix, "-random_view", typeName, 1024, &opt);
347: PetscStrlen(typeName, &len);
348: if (len > 0) {
349: PetscViewerCreate(((PetscObject)rnd)->comm, &viewer);
350: PetscViewerSetType(viewer, typeName);
351: PetscOptionsGetString(((PetscObject)rnd)->prefix, "-random_view_file", fileName, 1024, &opt);
352: if (opt) {
353: PetscViewerFileSetName(viewer, fileName);
354: } else {
355: PetscViewerFileSetName(viewer, ((PetscObject)rnd)->name);
356: }
357: PetscRandomView(rnd, viewer);
358: PetscViewerFlush(viewer);
359: PetscViewerDestroy(viewer);
360: } else {
361: PetscViewer viewer;
362: PetscViewerASCIIGetStdout(((PetscObject)rnd)->comm,&viewer);
363: PetscRandomView(rnd, viewer);
364: }
365: }
366: return(0);
367: }
372: /*@
373: PetscRandomCreate - Creates a context for generating random numbers,
374: and initializes the random-number generator.
376: Collective on MPI_Comm
378: Input Parameters:
379: + comm - MPI communicator
381: Output Parameter:
382: . r - the random number generator context
384: Level: intermediate
386: Notes:
387: The random type has to be set by PetscRandomSetType().
389: This is only a primative "parallel" random number generator, it should NOT
390: be used for sophisticated parallel Monte Carlo methods since it will very likely
391: not have the correct statistics across processors. You can provide your own
392: parallel generator using PetscRandomRegister();
394: If you create a PetscRandom() using PETSC_COMM_SELF on several processors then
395: the SAME random numbers will be generated on all those processors. Use PETSC_COMM_WORLD
396: or the appropriate parallel communicator to eliminate this issue.
398: Use VecSetRandom() to set the elements of a vector to random numbers.
400: Example of Usage:
401: .vb
402: PetscRandomCreate(PETSC_COMM_SELF,&r);
403: PetscRandomSetType(r,PETSCRAND48);
404: PetscRandomGetValue(r,&value1);
405: PetscRandomGetValueReal(r,&value2);
406: PetscRandomGetValueImaginary(r,&value3);
407: PetscRandomDestroy(r);
408: .ve
410: Concepts: random numbers^creating
412: .seealso: PetscRandomSetType(), PetscRandomGetValue(), PetscRandomGetValueReal(), PetscRandomGetValueImaginary(), PetscRandomSetInterval(),
413: PetscRandomDestroy(), VecSetRandom(), PetscRandomType
414: @*/
416: PetscErrorCode PetscRandomCreate(MPI_Comm comm,PetscRandom *r)
417: {
418: PetscRandom rr;
420: PetscMPIInt rank;
424: *r = PETSC_NULL;
425: #ifndef PETSC_USE_DYNAMIC_LIBRARIES
426: PetscRandomInitializePackage(PETSC_NULL);
427: #endif
429: PetscHeaderCreate(rr,_p_PetscRandom,struct _PetscRandomOps,PETSC_RANDOM_COOKIE,-1,"PetscRandom",comm,PetscRandomDestroy,0);
431: MPI_Comm_rank(comm,&rank);
432: rr->data = PETSC_NULL;
433: rr->low = 0.0;
434: rr->width = 1.0;
435: rr->iset = PETSC_FALSE;
436: rr->seed = 0x12345678 + 76543*rank;
437: *r = rr;
438: return(0);
439: }
443: /*@C
444: PetscRandomSeed - Seed the generator.
446: Not collective
448: Input Parameters:
449: . r - The random number generator context
451: Level: intermediate
453: Usage:
454: PetscRandomSetSeed(r,a positive integer);
455: PetscRandomSeed(r); PetscRandomGetValue() will now start with the new seed.
457: PetscRandomSeed(r) without a call to PetscRandomSetSeed() re-initializes
458: the seed. The random numbers generated will be the same as before.
460: Concepts: random numbers^seed
462: .seealso: PetscRandomCreate(), PetscRandomGetSeed(), PetscRandomSetSeed()
463: @*/
464: PetscErrorCode PetscRandomSeed(PetscRandom r)
465: {
472: (*r->ops->seed)(r);
473: PetscObjectStateIncrease((PetscObject)r);
474: return(0);
475: }
479: /*@
480: PetscRandomGetValue - Generates a random number. Call this after first calling
481: PetscRandomCreate().
483: Not Collective
485: Intput Parameter:
486: . r - the random number generator context
488: Output Parameter:
489: . val - the value
491: Level: intermediate
493: Notes:
494: Use VecSetRandom() to set the elements of a vector to random numbers.
496: Example of Usage:
497: .vb
498: PetscRandomCreate(PETSC_COMM_WORLD,&r);
499: PetscRandomGetValue(r,&value1);
500: PetscRandomGetValue(r,&value2);
501: PetscRandomGetValue(r,&value3);
502: PetscRandomDestroy(r);
503: .ve
505: Concepts: random numbers^getting
507: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
508: @*/
509: PetscErrorCode PetscRandomGetValue(PetscRandom r,PetscScalar *val)
510: {
518: (*r->ops->getvalue)(r,val);
519: PetscObjectStateIncrease((PetscObject)r);
520: return(0);
521: }
525: /*@
526: PetscRandomGetValueReal - Generates a random number. Call this after first calling
527: PetscRandomCreate().
529: Not Collective
531: Intput Parameter:
532: . r - the random number generator context
534: Output Parameter:
535: . val - the value
537: Level: intermediate
539: Notes:
540: Use VecSetRandom() to set the elements of a vector to random numbers.
542: Example of Usage:
543: .vb
544: PetscRandomCreate(PETSC_COMM_WORLD,&r);
545: PetscRandomGetValueReal(r,&value1);
546: PetscRandomGetValueReal(r,&value2);
547: PetscRandomGetValueReal(r,&value3);
548: PetscRandomDestroy(r);
549: .ve
551: Concepts: random numbers^getting
553: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
554: @*/
555: PetscErrorCode PetscRandomGetValueReal(PetscRandom r,PetscReal *val)
556: {
564: (*r->ops->getvaluereal)(r,val);
565: PetscObjectStateIncrease((PetscObject)r);
566: return(0);
567: }
571: /*@
572: PetscRandomGetValueImaginary - Generates a random number. Call this after first calling
573: PetscRandomCreate().
575: Not Collective
577: Intput Parameter:
578: . r - the random number generator context
580: Output Parameter:
581: . val - the value
583: Options Database Keys:
584: + -random_type rand48
585: . -random_type rand
586: - -random_type sprng, uses SPRNG package
588: Level: intermediate
590: Notes:
591: Use VecSetRandom() to set the elements of a vector to random numbers.
593: Example of Usage:
594: .vb
595: PetscRandomCreate(PETSC_COMM_WORLD,&r);
596: PetscRandomGetValueImaginary(r,&value1);
597: PetscRandomGetValueImaginary(r,&value2);
598: PetscRandomGetValueImaginary(r,&value3);
599: PetscRandomDestroy(r);
600: .ve
602: Concepts: random numbers^getting
604: .seealso: PetscRandomCreate(), PetscRandomDestroy(), VecSetRandom()
605: @*/
606: PetscErrorCode PetscRandomGetValueImaginary(PetscRandom r,PetscScalar *val)
607: {
615: (*r->ops->getvalueimaginary)(r,val);
616: PetscObjectStateIncrease((PetscObject)r);
617: return(0);
618: }