Date: 2025-03-28T21:11:20+00:00

Actual source code: ex1.c

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

      3: /*
      4:     Include petscis.h so we can use PETSc IS objects. Note that this automatically
      5:   includes petscsys.h.
      6: */
      7: #include <petscis.h>
      8: #include <petscviewer.h>

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

     17:   PetscFunctionBeginUser;
     18:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
     19:   PetscCallMPI(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:   PetscCall(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:   PetscCall(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:   PetscCall(PetscFree(indices));

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

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

     48:   /*
     49:      Get the indices in the index set
     50:   */
     51:   PetscCall(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:   PetscCall(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:   PetscCall(ISRestoreIndices(is, &nindices));

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

     73: /*TEST

     75:    test:

     77: TEST*/