Actual source code: ex2.c


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

  4: /*
  5:   Include petscis.h so we can use PETSc IS objects. Note that this automatically
  6:   includes petscsys.h.
  7: */

  9: #include <petscis.h>
 10: #include <petscviewer.h>

 12: int main(int argc, char **argv)
 13: {
 14:   PetscInt        i, n, first, step;
 15:   IS              set;
 16:   const PetscInt *indices;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));

 21:   n     = 10;
 22:   first = 3;
 23:   step  = 2;

 25:   /*
 26:     Create stride index set, starting at 3 with a stride of 2
 27:     Note each processor is generating its own index set
 28:     (in this case they are all identical)
 29:   */
 30:   PetscCall(ISCreateStride(PETSC_COMM_SELF, n, first, step, &set));
 31:   PetscCall(ISView(set, PETSC_VIEWER_STDOUT_SELF));

 33:   /*
 34:     Extract indices from set.
 35:   */
 36:   PetscCall(ISGetIndices(set, &indices));
 37:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Printing indices directly\n"));
 38:   for (i = 0; i < n; i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT "\n", indices[i]));

 40:   PetscCall(ISRestoreIndices(set, &indices));

 42:   /*
 43:       Determine information on stride
 44:   */
 45:   PetscCall(ISStrideGetInfo(set, &first, &step));
 46:   PetscCheck(first == 3 && step == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Stride info not correct!");
 47:   PetscCall(ISDestroy(&set));
 48:   PetscCall(PetscFinalize());
 49:   return 0;
 50: }

 52: /*TEST

 54:    test:

 56: TEST*/