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;

 20:   PetscInitialize(&argc, &argv, (char *)0, help);
 21:   comm = PETSC_COMM_WORLD;
 22:   MPI_Comm_rank(comm, &rank);
 23:   MPI_Comm_size(comm, &size);
 24:   PetscOptionsGetInt(NULL, NULL, "-n", &nLocal, NULL);
 25:   PetscOptionsGetBool(NULL, NULL, "-auto_offset", &auto_offset, NULL);
 26:   PetscOptionsGetViewer(comm, NULL, NULL, "-viewer", &viewer, &format, NULL);
 27:   PetscMalloc1(nLocal, &indices);
 28:   for (i = 0; i < nLocal; i++) indices[i] = i + rank;
 29:   nGlobal = size - 1 + nLocal;
 30:   if (viewer) {
 31:     PetscViewerPushFormat(viewer, format);
 32:     PetscViewerASCIIPrintf(viewer, "nGlobal: %" PetscInt_FMT "\n", nGlobal);
 33:   }

 35:   /* Create a local-to-global mapping using ISLocalToGlobalMappingCreate() */
 36:   {
 37:     ISLocalToGlobalMappingCreate(comm, 1, nLocal, indices, PETSC_USE_POINTER, &l2g0);
 38:     ISLocalToGlobalMappingSetFromOptions(l2g0);
 39:     if (viewer) {
 40:       PetscObjectSetName((PetscObject)l2g0, "l2g0");
 41:       ISLocalToGlobalMappingView(l2g0, viewer);
 42:     }
 43:   }

 45:   /* Create the same local-to-global mapping using ISLocalToGlobalMappingCreateSF() */
 46:   {
 47:     PetscSF     sf;
 48:     PetscLayout rootLayout;

 50:     PetscSFCreate(comm, &sf);
 51:     PetscLayoutCreateFromSizes(comm, PETSC_DECIDE, nGlobal, 1, &rootLayout);
 52:     PetscSFSetGraphLayout(sf, rootLayout, nLocal, NULL, PETSC_USE_POINTER, indices);
 53:     PetscSFSetFromOptions(sf);
 54:     ISLocalToGlobalMappingCreateSF(sf, auto_offset ? PETSC_DECIDE : rootLayout->rstart, &l2g1);
 55:     if (viewer) {
 56:       PetscObjectSetName((PetscObject)sf, "sf1");
 57:       PetscObjectSetName((PetscObject)l2g1, "l2g1");
 58:       PetscSFView(sf, viewer);
 59:       ISLocalToGlobalMappingView(l2g1, viewer);
 60:     }
 61:     /* Test PetscSFSetGraphLayout() / PetscSFGetGraphLayout() */
 62:     {
 63:       PetscLayout lt;
 64:       PetscInt   *ind;
 65:       PetscInt    nl;

 67:       PetscSFGetGraphLayout(sf, &lt, &nl, NULL, &ind);
 68:       PetscLayoutCompare(lt, rootLayout, &flg);
 70:       for (i = 0; i < nl; i++)
 72:       PetscLayoutDestroy(&lt);
 73:       PetscFree(ind);
 74:     }
 75:     PetscLayoutDestroy(&rootLayout);
 76:     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:     ISCreateStride(comm, nLocal, 0, 1, &input);
 84:     ISLocalToGlobalMappingApplyIS(l2g0, input, &output0);
 85:     ISLocalToGlobalMappingApplyIS(l2g1, input, &output1);
 86:     if (viewer) {
 87:       PetscObjectSetName((PetscObject)input, "input");
 88:       PetscObjectSetName((PetscObject)output0, "output0");
 89:       PetscObjectSetName((PetscObject)output1, "output1");
 90:       ISView(input, viewer);
 91:       ISView(output0, viewer);
 92:       ISView(output1, viewer);
 93:     }
 94:     ISEqual(output0, output1, &flg);
 96:     ISDestroy(&input);
 97:     ISDestroy(&output0);
 98:     ISDestroy(&output1);
 99:   }

101:   if (viewer) {
102:     PetscViewerPopFormat(viewer);
103:     PetscViewerDestroy(&viewer);
104:   }
105:   ISLocalToGlobalMappingDestroy(&l2g0);
106:   ISLocalToGlobalMappingDestroy(&l2g1);
107:   PetscFree(indices);
108:   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*/