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: PetscFunctionBeginUser;
19: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
20: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
22: /*
23: Create an index set with 5 entries. Each processor creates
24: its own index set with its own list of integers.
25: */
26: PetscCall(PetscMalloc1(5, &indices));
27: indices[0] = rank + 1;
28: indices[1] = rank + 2;
29: indices[2] = rank + 3;
30: indices[3] = rank + 4;
31: indices[4] = rank + 5;
32: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, 5, indices, PETSC_COPY_VALUES, &is));
33: /*
34: Note that ISCreateGeneral() has made a copy of the indices
35: so we may (and generally should) free indices[]
36: */
37: PetscCall(PetscFree(indices));
39: /*
40: Print the index set to stdout
41: */
42: PetscCall(ISView(is, PETSC_VIEWER_STDOUT_SELF));
44: /*
45: Get the number of indices in the set
46: */
47: PetscCall(ISGetLocalSize(is, &n));
49: /*
50: Get the indices in the index set
51: */
52: PetscCall(ISGetIndices(is, &nindices));
53: /*
54: Now any code that needs access to the list of integers
55: has access to it here through indices[].
56: */
57: PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] First index %" PetscInt_FMT "\n", rank, nindices[0]));
59: /*
60: Once we no longer need access to the indices they should
61: returned to the system
62: */
63: PetscCall(ISRestoreIndices(is, &nindices));
65: /*
66: One should destroy any PETSc object once one is completely
67: done with it.
68: */
69: PetscCall(ISDestroy(&is));
70: PetscCall(PetscFinalize());
71: return 0;
72: }
74: /*TEST
76: test:
78: TEST*/