Actual source code: ex12.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests timing PetscSortInt().\n\n";

  4: #include <petscsys.h>

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

 18:   PetscInitialize(&argc,&argv,(char*)0,help);
 19:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 20:   PetscOptionsGetInt(NULL,"-n",&n,NULL);
 21:   PetscOptionsGetBool(0,"-values_view",&values_view,NULL);

 23:   PetscRandomCreate(PETSC_COMM_SELF,&rand);
 24:   PetscRandomSetFromOptions(rand);

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

 33:   PetscLogEventRegister("Sort",0,&event);
 34:   PetscLogEventBegin(event,0,0,0,0);

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

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

 50:   PetscFinalize();
 51:   return 0;
 52: }