xref: /petsc/src/sys/tests/ex12.c (revision 8fb5bd83c3955fefcf33a54e3bb66920a9fa884b)
1 
2 static char help[] = "Tests timing PetscSortInt().\n\n";
3 
4 #include <petscsys.h>
5 
6 int main(int argc,char **argv)
7 {
8   PetscInt       i,n = 1000,*values;
9 #if defined(PETSC_USE_LOG)
10   PetscLogEvent  event;
11 #endif
12   PetscRandom    rand;
13   PetscReal      value;
14   PetscBool      values_view=PETSC_FALSE;
15   PetscMPIInt    rank;
16 
17   PetscCall(PetscInitialize(&argc,&argv,(char*)0,help));
18   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
19   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
20   PetscCall(PetscOptionsGetBool(NULL,0,"-values_view",&values_view,NULL));
21 
22   PetscCall(PetscRandomCreate(PETSC_COMM_SELF,&rand));
23   PetscCall(PetscRandomSetFromOptions(rand));
24 
25   PetscCall(PetscMalloc1(n,&values));
26   for (i=0; i<n; i++) {
27     PetscCall(PetscRandomGetValueReal(rand,&value));
28     values[i] = (PetscInt)(n*value + 2.0);
29   }
30   PetscCall(PetscSortInt(n,values));
31 
32   PetscCall(PetscLogEventRegister("Sort",0,&event));
33   PetscCall(PetscLogEventBegin(event,0,0,0,0));
34 
35   for (i=0; i<n; i++) {
36     PetscCall(PetscRandomGetValueReal(rand,&value));
37     values[i] = (PetscInt)(n*value + 2.0);
38   }
39   PetscCall(PetscSortInt(n,values));
40   PetscCall(PetscLogEventEnd(event,0,0,0,0));
41 
42   for (i=1; i<n; i++) {
43     PetscCheck(values[i] >= values[i-1],PETSC_COMM_SELF,PETSC_ERR_PLIB,"Values not sorted");
44     if (values_view && rank == 0) PetscCall(PetscPrintf(PETSC_COMM_SELF,"%" PetscInt_FMT " %" PetscInt_FMT "\n",i,values[i]));
45   }
46   PetscCall(PetscFree(values));
47   PetscCall(PetscRandomDestroy(&rand));
48 
49   PetscCall(PetscFinalize());
50   return 0;
51 }
52 
53 /*TEST
54 
55    test:
56       args: -values_view
57 
58 TEST*/
59