Actual source code: ex1.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  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>

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

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

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

 51:   /*
 52:      Print the index set to stdout
 53:   */
 54:   ISView(is,PETSC_VIEWER_STDOUT_SELF);

 56:   /*
 57:      Get the number of indices in the set
 58:   */
 59:   ISGetLocalSize(is,&n);

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

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

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