Date: 2022-03-04T17:37:33+00:00

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: {
     24:   PetscInt       *indices,n;
     25:   const PetscInt *nindices;
     26:   PetscMPIInt    rank;
     27:   IS             is;

     29:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     30:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

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

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

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

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

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

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

     84: /*TEST

     86:    test:

     88: TEST*/