Actual source code: ex2.c

petsc-master 2017-05-28
Report Typos and Errors

  2: static char help[] = "Demonstrates creating a stride index set.\n\n";

  4: /*T
  5:     Concepts: index sets^creating a stride index set;
  6:     Concepts: stride^creating a stride index set;
  7:     Concepts: IS^creating a stride index set;

  9:     Description: Creates an index set based on a stride. Views that index set
 10:     and then destroys it.
 11: T*/

 13: /*
 14:   Include petscis.h so we can use PETSc IS objects. Note that this automatically
 15:   includes petscsys.h.
 16: */

 18:  #include <petscis.h>
 19:  #include <petscviewer.h>

 21: int main(int argc,char **argv)
 22: {
 24:   PetscInt       i,n,first,step;
 25:   IS             set;
 26:   const PetscInt *indices;

 28:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 30:   n     = 10;
 31:   first = 3;
 32:   step  = 2;

 34:   /*
 35:     Create stride index set, starting at 3 with a stride of 2
 36:     Note each processor is generating its own index set
 37:     (in this case they are all identical)
 38:   */
 39:   ISCreateStride(PETSC_COMM_SELF,n,first,step,&set);
 40:   ISView(set,PETSC_VIEWER_STDOUT_SELF);

 42:   /*
 43:     Extract indices from set.
 44:   */
 45:   ISGetIndices(set,&indices);
 46:   PetscPrintf(PETSC_COMM_WORLD,"Printing indices directly\n");
 47:   for (i=0; i<n; i++) {
 48:     PetscPrintf(PETSC_COMM_WORLD,"%D\n",indices[i]);
 49:   }

 51:   ISRestoreIndices(set,&indices);

 53:   /*
 54:       Determine information on stride
 55:   */
 56:   ISStrideGetInfo(set,&first,&step);
 57:   if (first != 3 || step != 2) SETERRQ(PETSC_COMM_SELF,1,"Stride info not correct!\n");
 58:   ISDestroy(&set);
 59:   PetscFinalize();
 60:   return ierr;
 61: }