Actual source code: ex2.c

  1: static char help[] = "Tests ISView() and ISLoad() \n\n";

  3: #include <petscis.h>

  5: int main(int argc, char **argv)
  6: {
  7:   PetscInt n           = 3, *izero, j, i;
  8:   PetscInt ix[3][3][3] = {
  9:     {{3, 5, 4}, {1, 7, 9}, {0, 2, 8}},
 10:     {{0, 2, 8}, {3, 5, 4}, {1, 7, 9}},
 11:     {{1, 7, 9}, {0, 2, 8}, {3, 5, 4}}
 12:   };
 13:   IS          isx[3], il;
 14:   PetscMPIInt size, rank;
 15:   PetscViewer vx, vl;
 16:   PetscBool   equal;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 20:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 21:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 22:   PetscCheck(size < 4, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Example only works with up to three processes");

 24:   PetscCall(PetscCalloc1(size * n, &izero));
 25:   for (i = 0; i < 3; i++) PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]));

 27:   for (j = 0; j < 3; j++) {
 28:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_WRITE, &vx));
 29:     PetscCall(ISView(isx[0], vx));
 30:     PetscCall(PetscViewerDestroy(&vx));

 32:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
 33:     PetscCall(ISCreate(PETSC_COMM_WORLD, &il));
 34:     PetscCall(ISLoad(il, vl));
 35:     PetscCall(ISEqual(il, isx[0], &equal));
 36:     PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set loaded from file does not match", j);
 37:     PetscCall(ISDestroy(&il));
 38:     PetscCall(PetscViewerDestroy(&vl));

 40:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_APPEND, &vx));
 41:     PetscCall(ISView(isx[1], vx));
 42:     PetscCall(ISView(isx[2], vx));
 43:     PetscCall(PetscViewerDestroy(&vx));

 45:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
 46:     for (i = 0; i < 3; i++) {
 47:       PetscCall(ISCreate(PETSC_COMM_WORLD, &il));
 48:       PetscCall(ISLoad(il, vl));
 49:       PetscCall(ISEqual(il, isx[i], &equal));
 50:       PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
 51:       PetscCall(ISDestroy(&il));
 52:     }
 53:     PetscCall(PetscViewerDestroy(&vl));

 55:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile", FILE_MODE_READ, &vl));
 56:     for (i = 0; i < 3; i++) {
 57:       PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il));
 58:       PetscCall(ISLoad(il, vl));
 59:       PetscCall(ISEqual(il, isx[i], &equal));
 60:       PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
 61:       PetscCall(ISDestroy(&il));
 62:     }
 63:     PetscCall(PetscViewerDestroy(&vl));
 64:   }

 66:   for (j = 0; j < 3; j++) {
 67:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_WRITE, &vx));
 68:     PetscCall(PetscViewerBinarySetSkipHeader(vx, PETSC_TRUE));
 69:     for (i = 0; i < 3; i++) PetscCall(ISView(isx[i], vx));
 70:     PetscCall(PetscViewerDestroy(&vx));

 72:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "testfile_noheader", FILE_MODE_READ, &vl));
 73:     PetscCall(PetscViewerBinarySetSkipHeader(vl, PETSC_TRUE));
 74:     for (i = 0; i < 3; i++) {
 75:       PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, n, izero, PETSC_COPY_VALUES, &il));
 76:       PetscCall(ISLoad(il, vl));
 77:       PetscCall(ISEqual(il, isx[i], &equal));
 78:       PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
 79:       PetscCall(ISDestroy(&il));
 80:     }
 81:     PetscCall(PetscViewerDestroy(&vl));
 82:   }

 84:   for (i = 0; i < 3; i++) PetscCall(ISDestroy(&isx[i]));

 86:   for (j = 0; j < 2; j++) {
 87:     const char *filename  = (j == 0) ? "testfile_isstride" : "testfile_isblock";
 88:     PetscInt    blocksize = (j == 0) ? 1 : size;
 89:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_WRITE, &vx));
 90:     for (i = 0; i < 3; i++) {
 91:       if (j == 0) {
 92:         PetscCall(ISCreateStride(PETSC_COMM_WORLD, n, rank, rank + 1, &isx[i]));
 93:       } else {
 94:         PetscCall(ISCreateBlock(PETSC_COMM_WORLD, blocksize, n, ix[i][rank], PETSC_COPY_VALUES, &isx[i]));
 95:       }
 96:       PetscCall(ISView(isx[i], vx));
 97:       PetscCall(ISToGeneral(isx[i]));
 98:     }
 99:     PetscCall(PetscViewerDestroy(&vx));
100:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_READ, &vl));
101:     for (i = 0; i < 3; i++) {
102:       PetscCall(ISCreateGeneral(PETSC_COMM_WORLD, blocksize * n, izero, PETSC_COPY_VALUES, &il));
103:       PetscCall(ISLoad(il, vl));
104:       PetscCall(ISEqual(il, isx[i], &equal));
105:       PetscCheck(equal, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Iteration %" PetscInt_FMT " - Index set %" PetscInt_FMT " loaded from file does not match", j, i);
106:       PetscCall(ISDestroy(&il));
107:     }
108:     PetscCall(PetscViewerDestroy(&vl));
109:     for (i = 0; i < 3; i++) PetscCall(ISDestroy(&isx[i]));
110:   }
111:   PetscCall(PetscFree(izero));

113:   PetscCall(PetscFinalize());
114:   return 0;
115: }

117: /*TEST

119:    testset:
120:       args: -viewer_binary_mpiio 0
121:       output_file: output/empty.out
122:       test:
123:         suffix: stdio_1
124:         nsize: 1
125:       test:
126:         suffix: stdio_2
127:         nsize: 2
128:       test:
129:         suffix: stdio_3
130:         nsize: 3

132:    testset:
133:       requires: mpiio
134:       args: -viewer_binary_mpiio 1
135:       output_file: output/empty.out
136:       test:
137:         suffix: mpiio_1
138:         nsize: 1
139:       test:
140:         suffix: mpiio_2
141:         nsize: 2
142:       test:
143:         suffix: mpiio_3
144:         nsize: 3

146: TEST*/