Actual source code: ex9.c
1: static char help[] = "Test ISLocalToGlobalMappingCreateSF(), PetscSFSetGraphLayout(), PetscSFGetGraphLayout().\n\n";
3: #include <petscis.h>
4: #include <petscsf.h>
5: #include <petscviewer.h>
7: int main(int argc, char **argv)
8: {
9: MPI_Comm comm;
10: PetscViewer viewer;
11: PetscViewerFormat format;
12: PetscMPIInt rank, size;
13: PetscInt i, nLocal = 3, nGlobal;
14: PetscInt *indices;
15: PetscBool flg, auto_offset = PETSC_FALSE;
16: ISLocalToGlobalMapping l2g0, l2g1;
18: PetscFunctionBeginUser;
19: PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
20: comm = PETSC_COMM_WORLD;
21: PetscCallMPI(MPI_Comm_rank(comm, &rank));
22: PetscCallMPI(MPI_Comm_size(comm, &size));
23: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &nLocal, NULL));
24: PetscCall(PetscOptionsGetBool(NULL, NULL, "-auto_offset", &auto_offset, NULL));
25: PetscCall(PetscOptionsGetViewer(comm, NULL, NULL, "-viewer", &viewer, &format, NULL));
26: PetscCall(PetscMalloc1(nLocal, &indices));
27: for (i = 0; i < nLocal; i++) indices[i] = i + rank;
28: nGlobal = size - 1 + nLocal;
29: if (viewer) {
30: PetscCall(PetscViewerPushFormat(viewer, format));
31: PetscCall(PetscViewerASCIIPrintf(viewer, "nGlobal: %" PetscInt_FMT "\n", nGlobal));
32: }
34: /* Create a local-to-global mapping using ISLocalToGlobalMappingCreate() */
35: {
36: PetscCall(ISLocalToGlobalMappingCreate(comm, 1, nLocal, indices, PETSC_USE_POINTER, &l2g0));
37: PetscCall(ISLocalToGlobalMappingSetFromOptions(l2g0));
38: if (viewer) {
39: PetscCall(PetscObjectSetName((PetscObject)l2g0, "l2g0"));
40: PetscCall(ISLocalToGlobalMappingView(l2g0, viewer));
41: }
42: }
44: /* Create the same local-to-global mapping using ISLocalToGlobalMappingCreateSF() */
45: {
46: PetscSF sf;
47: PetscLayout rootLayout;
49: PetscCall(PetscSFCreate(comm, &sf));
50: PetscCall(PetscLayoutCreateFromSizes(comm, PETSC_DECIDE, nGlobal, 1, &rootLayout));
51: PetscCall(PetscSFSetGraphLayout(sf, rootLayout, nLocal, NULL, PETSC_USE_POINTER, indices));
52: PetscCall(PetscSFSetFromOptions(sf));
53: PetscCall(ISLocalToGlobalMappingCreateSF(sf, auto_offset ? PETSC_DECIDE : rootLayout->rstart, &l2g1));
54: if (viewer) {
55: PetscCall(PetscObjectSetName((PetscObject)sf, "sf1"));
56: PetscCall(PetscObjectSetName((PetscObject)l2g1, "l2g1"));
57: PetscCall(PetscSFView(sf, viewer));
58: PetscCall(ISLocalToGlobalMappingView(l2g1, viewer));
59: }
60: /* Test PetscSFSetGraphLayout() / PetscSFGetGraphLayout() */
61: {
62: PetscLayout lt;
63: PetscInt *ind;
64: PetscInt nl;
66: PetscCall(PetscSFGetGraphLayout(sf, <, &nl, NULL, &ind));
67: PetscCall(PetscLayoutCompare(lt, rootLayout, &flg));
68: PetscCheck(flg, comm, PETSC_ERR_PLIB, "PetscSFGetGraphLayout() gives different layout than the one passed to PetscSFSetGraphLayout()");
69: for (i = 0; i < nl; i++)
70: PetscCheck(ind[i] == indices[i], PETSC_COMM_SELF, PETSC_ERR_PLIB, "PetscSFSetGraphLayout() gives global_roots[%" PetscInt_FMT "] = %" PetscInt_FMT " != %" PetscInt_FMT " = global_roots[%" PetscInt_FMT "] passed to PetscSFSetGraphLayout()", i, ind[i], indices[i], i);
71: PetscCall(PetscLayoutDestroy(<));
72: PetscCall(PetscFree(ind));
73: }
74: PetscCall(PetscLayoutDestroy(&rootLayout));
75: PetscCall(PetscSFDestroy(&sf));
76: }
78: /* Compare the two local-to-global mappings by comparing results of apply for the same input */
79: {
80: IS input, output0, output1;
82: PetscCall(ISCreateStride(comm, nLocal, 0, 1, &input));
83: PetscCall(ISLocalToGlobalMappingApplyIS(l2g0, input, &output0));
84: PetscCall(ISLocalToGlobalMappingApplyIS(l2g1, input, &output1));
85: if (viewer) {
86: PetscCall(PetscObjectSetName((PetscObject)input, "input"));
87: PetscCall(PetscObjectSetName((PetscObject)output0, "output0"));
88: PetscCall(PetscObjectSetName((PetscObject)output1, "output1"));
89: PetscCall(ISView(input, viewer));
90: PetscCall(ISView(output0, viewer));
91: PetscCall(ISView(output1, viewer));
92: }
93: PetscCall(ISEqual(output0, output1, &flg));
94: PetscCheck(flg, comm, PETSC_ERR_PLIB, "output0 != output1");
95: PetscCall(ISDestroy(&input));
96: PetscCall(ISDestroy(&output0));
97: PetscCall(ISDestroy(&output1));
98: }
100: if (viewer) {
101: PetscCall(PetscViewerPopFormat(viewer));
102: PetscCall(PetscViewerDestroy(&viewer));
103: }
104: PetscCall(ISLocalToGlobalMappingDestroy(&l2g0));
105: PetscCall(ISLocalToGlobalMappingDestroy(&l2g1));
106: PetscCall(PetscFree(indices));
107: PetscCall(PetscFinalize());
108: return 0;
109: }
111: /*TEST
113: test:
114: suffix: 1
115: nsize: {{1 2 3}separate output}
116: args: -auto_offset {{true false}} -viewer
118: test:
119: suffix: 2
120: nsize: {{1 2 3}}
121: args: -n 33 -auto_offset {{true false}}
123: TEST*/