Actual source code: ex3.c

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

  3: #include <petscis.h>
  4: #include <petscviewer.h>

  6: int main(int argc, char **argv)
  7: {
  8:   PetscInt        i, n = 4, inputindices[] = {0, 1, 3, 4}, bs = 3, issize;
  9:   const PetscInt *indices;
 10:   IS              set;
 11:   PetscBool       isblock;

 13:   PetscFunctionBeginUser;
 14:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));

 16:   /*
 17:     Create a block index set. The index set has 4 blocks each of size 3.
 18:     The indices are {0,1,2,3,4,5,9,10,11,12,13,14}
 19:     Note each processor is generating its own index set
 20:     (in this case they are all identical)
 21:   */
 22:   PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, n, inputindices, PETSC_COPY_VALUES, &set));
 23:   PetscCall(ISView(set, PETSC_VIEWER_STDOUT_SELF));

 25:   /*
 26:     Extract indices from set.
 27:   */
 28:   PetscCall(ISGetLocalSize(set, &issize));
 29:   PetscCall(ISGetIndices(set, &indices));
 30:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Printing indices directly\n"));
 31:   for (i = 0; i < issize; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT "\n", indices[i]));
 32:   PetscCall(ISRestoreIndices(set, &indices));

 34:   /*
 35:     Extract the block indices. This returns one index per block.
 36:   */
 37:   PetscCall(ISBlockGetIndices(set, &indices));
 38:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Printing block indices directly\n"));
 39:   for (i = 0; i < n; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT "\n", indices[i]));
 40:   PetscCall(ISBlockRestoreIndices(set, &indices));

 42:   /*
 43:     Check if this is really a block index set
 44:   */
 45:   PetscCall(PetscObjectTypeCompare((PetscObject)set, ISBLOCK, &isblock));
 46:   PetscCheck(isblock, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Index set is not blocked!");

 48:   /*
 49:     Determine the block size of the index set
 50:   */
 51:   PetscCall(ISGetBlockSize(set, &bs));
 52:   PetscCheck(bs == 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Block size is not 3!");

 54:   /*
 55:     Get the number of blocks
 56:   */
 57:   PetscCall(ISBlockGetLocalSize(set, &n));
 58:   PetscCheck(n == 4, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of blocks not 4!");

 60:   PetscCall(ISDestroy(&set));
 61:   PetscCall(PetscFinalize());
 62:   return 0;
 63: }

 65: /*TEST

 67:    test:

 69: TEST*/