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