Actual source code: ex1.c

      2: static char help[] = "Creating a general 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: */
      8: #include <petscis.h>
      9: #include <petscviewer.h>

     11: int main(int argc,char **argv)
     12: {
     13:   PetscInt       *indices,n;
     14:   const PetscInt *nindices;
     15:   PetscMPIInt    rank;
     16:   IS             is;

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

     21:   /*
     22:      Create an index set with 5 entries. Each processor creates
     23:    its own index set with its own list of integers.
     24:   */
     25:   PetscMalloc1(5,&indices);
     26:   indices[0] = rank + 1;
     27:   indices[1] = rank + 2;
     28:   indices[2] = rank + 3;
     29:   indices[3] = rank + 4;
     30:   indices[4] = rank + 5;
     31:   ISCreateGeneral(PETSC_COMM_SELF,5,indices,PETSC_COPY_VALUES,&is);
     32:   /*
     33:      Note that ISCreateGeneral() has made a copy of the indices
     34:      so we may (and generally should) free indices[]
     35:   */
     36:   PetscFree(indices);

     38:   /*
     39:      Print the index set to stdout
     40:   */
     41:   ISView(is,PETSC_VIEWER_STDOUT_SELF);

     43:   /*
     44:      Get the number of indices in the set
     45:   */
     46:   ISGetLocalSize(is,&n);

     48:   /*
     49:      Get the indices in the index set
     50:   */
     51:   ISGetIndices(is,&nindices);
     52:   /*
     53:      Now any code that needs access to the list of integers
     54:    has access to it here through indices[].
     55:    */
     56:   PetscPrintf(PETSC_COMM_SELF,"[%d] First index %" PetscInt_FMT "\n",rank,nindices[0]);

     58:   /*
     59:      Once we no longer need access to the indices they should
     60:      returned to the system
     61:   */
     62:   ISRestoreIndices(is,&nindices);

     64:   /*
     65:      One should destroy any PETSc object once one is completely
     66:     done with it.
     67:   */
     68:   ISDestroy(&is);
     69:   PetscFinalize();
     70:   return 0;
     71: }

     73: /*TEST

     75:    test:

     77: TEST*/