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