Actual source code: ex1.c
petsc-3.5.4 2015-05-23
2: static char help[] = "Tests PetscRandom functions.\n\n";
4: #include <petscsys.h>
6: /* Usage:
7: mpiexec -n <np> ./ex1 -n <num_of_random_numbers> -random_type <type> -log_summary
8: -view_randomvalues <view_rank>
9: -random_view ascii -random_view :filename
10: */
14: int main(int argc,char **argv)
15: {
16: PetscInt i,n = 1000,*values;
17: PetscRandom rnd;
18: PetscScalar value;
20: PetscMPIInt rank;
21: PetscInt view_rank=-1;
22: #if defined(PETSC_USE_LOG)
23: PetscLogEvent event;
24: #endif
26: PetscInitialize(&argc,&argv,(char*)0,help);
27: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
28: PetscOptionsGetInt(NULL,"-n",&n,NULL);
29: PetscOptionsGetInt(NULL,"-view_randomvalues",&view_rank,NULL);
31: PetscRandomCreate(PETSC_COMM_WORLD,&rnd);
32: #if defined(PETSC_HAVE_DRAND48)
33: PetscRandomSetType(rnd,PETSCRAND48);
34: #elif defined(PETSC_HAVE_RAND)
35: PetscRandomSetType(rnd,PETSCRAND);
36: #endif
37: PetscRandomSetFromOptions(rnd);
39: PetscMalloc1(n,&values);
40: for (i=0; i<n; i++) {
41: PetscRandomGetValue(rnd,&value);
42: if (view_rank == (PetscInt)rank) {
43: PetscPrintf(PETSC_COMM_SELF,"[%D] value[%d] = %g\n",rank,i,PetscRealPart(value));
44: }
45: values[i] = (PetscInt)(n*PetscRealPart(value) + 2.0);
46: }
47: PetscSortInt(n,values);
49: PetscLogEventRegister("Sort",0,&event);
50: PetscLogEventBegin(event,0,0,0,0);
52: PetscRandomSeed(rnd);
53: for (i=0; i<n; i++) {
54: PetscRandomGetValue(rnd,&value);
55: values[i] = (PetscInt)(n*PetscRealPart(value) + 2.0);
56: /* printf("value[%d] = %g\n",i,value); */
57: }
58: PetscSortInt(n,values);
59: PetscLogEventEnd(event,0,0,0,0);
61: for (i=1; i<n; i++) {
62: if (values[i] < values[i-1]) SETERRQ(PETSC_COMM_SELF,1,"Values not sorted");
63: }
64: PetscFree(values);
65: PetscRandomDestroy(&rnd);
67: PetscFinalize();
68: return 0;
69: }