Actual source code: ex1.c


  2: static char help[] = "Creating a general index set.\n\n";

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

  9:     Description: Creates an index set based on a set of integers. Views that index set
 10:     and then destroys it.

 12: T*/

 14: /*
 15:     Include petscis.h so we can use PETSc IS objects. Note that this automatically
 16:   includes petscsys.h.
 17: */
 18: #include <petscis.h>
 19: #include <petscviewer.h>

 21: int main(int argc,char **argv)
 22: {
 23:   PetscInt       *indices,n;
 24:   const PetscInt *nindices;
 25:   PetscMPIInt    rank;
 26:   IS             is;

 28:   PetscInitialize(&argc,&argv,(char*)0,help);
 29:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 31:   /*
 32:      Create an index set with 5 entries. Each processor creates
 33:    its own index set with its own list of integers.
 34:   */
 35:   PetscMalloc1(5,&indices);
 36:   indices[0] = rank + 1;
 37:   indices[1] = rank + 2;
 38:   indices[2] = rank + 3;
 39:   indices[3] = rank + 4;
 40:   indices[4] = rank + 5;
 41:   ISCreateGeneral(PETSC_COMM_SELF,5,indices,PETSC_COPY_VALUES,&is);
 42:   /*
 43:      Note that ISCreateGeneral() has made a copy of the indices
 44:      so we may (and generally should) free indices[]
 45:   */
 46:   PetscFree(indices);

 48:   /*
 49:      Print the index set to stdout
 50:   */
 51:   ISView(is,PETSC_VIEWER_STDOUT_SELF);

 53:   /*
 54:      Get the number of indices in the set
 55:   */
 56:   ISGetLocalSize(is,&n);

 58:   /*
 59:      Get the indices in the index set
 60:   */
 61:   ISGetIndices(is,&nindices);
 62:   /*
 63:      Now any code that needs access to the list of integers
 64:    has access to it here through indices[].
 65:    */
 66:   PetscPrintf(PETSC_COMM_SELF,"[%d] First index %" PetscInt_FMT "\n",rank,nindices[0]);

 68:   /*
 69:      Once we no longer need access to the indices they should
 70:      returned to the system
 71:   */
 72:   ISRestoreIndices(is,&nindices);

 74:   /*
 75:      One should destroy any PETSc object once one is completely
 76:     done with it.
 77:   */
 78:   ISDestroy(&is);
 79:   PetscFinalize();
 80:   return 0;
 81: }

 83: /*TEST

 85:    test:

 87: TEST*/