Actual source code: ex3.c
2: static char help[] = "Demonstrates creating a blocked index set.\n\n";
4: #include <petscis.h>
5: #include <petscviewer.h>
7: int main(int argc, char **argv)
8: {
9: PetscInt i, n = 4, inputindices[] = {0, 1, 3, 4}, bs = 3, issize;
10: const PetscInt *indices;
11: IS set;
12: PetscBool isblock;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
17: /*
18: Create a block index set. The index set has 4 blocks each of size 3.
19: The indices are {0,1,2,3,4,5,9,10,11,12,13,14}
20: Note each processor is generating its own index set
21: (in this case they are all identical)
22: */
23: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, n, inputindices, PETSC_COPY_VALUES, &set));
24: PetscCall(ISView(set, PETSC_VIEWER_STDOUT_SELF));
26: /*
27: Extract indices from set.
28: */
29: PetscCall(ISGetLocalSize(set, &issize));
30: PetscCall(ISGetIndices(set, &indices));
31: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Printing indices directly\n"));
32: for (i = 0; i < issize; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT "\n", indices[i]));
33: PetscCall(ISRestoreIndices(set, &indices));
35: /*
36: Extract the block indices. This returns one index per block.
37: */
38: PetscCall(ISBlockGetIndices(set, &indices));
39: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Printing block indices directly\n"));
40: for (i = 0; i < n; i++) PetscCall(PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT "\n", indices[i]));
41: PetscCall(ISBlockRestoreIndices(set, &indices));
43: /*
44: Check if this is really a block index set
45: */
46: PetscCall(PetscObjectTypeCompare((PetscObject)set, ISBLOCK, &isblock));
47: PetscCheck(isblock, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Index set is not blocked!");
49: /*
50: Determine the block size of the index set
51: */
52: PetscCall(ISGetBlockSize(set, &bs));
53: PetscCheck(bs == 3, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Block size is not 3!");
55: /*
56: Get the number of blocks
57: */
58: PetscCall(ISBlockGetLocalSize(set, &n));
59: PetscCheck(n == 4, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of blocks not 4!");
61: PetscCall(ISDestroy(&set));
62: PetscCall(PetscFinalize());
63: return 0;
64: }
66: /*TEST
68: test:
70: TEST*/