Actual source code: ex1.c

petsc-3.3-p7 2013-05-11
  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.
 11:     
 12: T*/
 13: 
 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>

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

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

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

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

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

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

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

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