Actual source code: ex12.c
petsc-3.12.4 2020-02-04
Report Typos and Errors

      2: static char help[] = "Tests timing PetscSortInt().\n\n";

      4:  #include <petscsys.h>

      6: int main(int argc,char **argv)
      7: {
      8:   PetscInt       i,n = 1000,*values;
      9:   int            event;
     10:   PetscRandom    rand;
     11:   PetscReal      value;
     13:   PetscBool      values_view=PETSC_FALSE;
     14:   PetscMPIInt    rank;

     16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     17:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     18:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     19:   PetscOptionsGetBool(NULL,0,"-values_view",&values_view,NULL);

     21:   PetscRandomCreate(PETSC_COMM_SELF,&rand);
     22:   PetscRandomSetFromOptions(rand);

     24:   PetscMalloc1(n,&values);
     25:   for (i=0; i<n; i++) {
     26:     PetscRandomGetValueReal(rand,&value);
     27:     values[i] = (PetscInt)(n*value + 2.0);
     28:   }
     29:   PetscSortInt(n,values);

     31:   PetscLogEventRegister("Sort",0,&event);
     32:   PetscLogEventBegin(event,0,0,0,0);

     34:   for (i=0; i<n; i++) {
     35:     PetscRandomGetValueReal(rand,&value);
     36:     values[i] = (PetscInt)(n*value + 2.0);
     37:   }
     38:   PetscSortInt(n,values);
     39:   PetscLogEventEnd(event,0,0,0,0);

     41:   for (i=1; i<n; i++) {
     42:     if (values[i] < values[i-1]) SETERRQ(PETSC_COMM_SELF,1,"Values not sorted");
     43:     if (values_view && !rank) {PetscPrintf(PETSC_COMM_SELF,"%D %D\n",i,values[i]);}
     44:   }
     45:   PetscFree(values);
     46:   PetscRandomDestroy(&rand);

     48:   PetscFinalize();
     49:   return ierr;
     50: }

     54: /*TEST

     56:    test:
     57:       args: -values_view

     59: TEST*/