Actual source code: ex31.c

  1: static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n";

  3: #include <petscmat.h>

  5: /* Note:  Most applications would not read and write the same matrix within
  6:   the same program.  This example is intended only to demonstrate
  7:   both input and output. */

  9: int main(int argc, char **args)
 10: {
 11:   Mat           C;
 12:   PetscScalar   v;
 13:   PetscInt      i, j, Ii, J, Istart, Iend, N, m = 4, n = 4;
 14:   PetscMPIInt   rank, size;
 15:   PetscViewer   viewer;
 16:   PetscLogEvent MATRIX_GENERATE, MATRIX_READ;

 18:   PetscFunctionBeginUser;
 19:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 20:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 21:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 22:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
 23:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 24:   N = m * n;

 26:   /* PART 1:  Generate matrix, then write it in binary format */

 28:   PetscCall(PetscLogEventRegister("Generate Matrix", 0, &MATRIX_GENERATE));
 29:   PetscCall(PetscLogEventBegin(MATRIX_GENERATE, 0, 0, 0, 0));

 31:   /* Generate matrix */
 32:   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
 33:   PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, N, N));
 34:   PetscCall(MatSetFromOptions(C));
 35:   PetscCall(MatSetUp(C));
 36:   PetscCall(MatGetOwnershipRange(C, &Istart, &Iend));
 37:   for (Ii = Istart; Ii < Iend; Ii++) {
 38:     v = -1.0;
 39:     i = Ii / n;
 40:     j = Ii - i * n;
 41:     if (i > 0) {
 42:       J = Ii - n;
 43:       PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
 44:     }
 45:     if (i < m - 1) {
 46:       J = Ii + n;
 47:       PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
 48:     }
 49:     if (j > 0) {
 50:       J = Ii - 1;
 51:       PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
 52:     }
 53:     if (j < n - 1) {
 54:       J = Ii + 1;
 55:       PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES));
 56:     }
 57:     v = 4.0;
 58:     PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, ADD_VALUES));
 59:   }
 60:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
 61:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
 62:   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));

 64:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing matrix in binary to matrix.dat ...\n"));
 65:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &viewer));
 66:   PetscCall(MatView(C, viewer));
 67:   PetscCall(PetscViewerDestroy(&viewer));
 68:   PetscCall(MatDestroy(&C));
 69:   PetscCall(PetscLogEventEnd(MATRIX_GENERATE, 0, 0, 0, 0));

 71:   /* PART 2:  Read in matrix in binary format */

 73:   /* All processors wait until test matrix has been dumped */
 74:   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));

 76:   PetscCall(PetscLogEventRegister("Read Matrix", 0, &MATRIX_READ));
 77:   PetscCall(PetscLogEventBegin(MATRIX_READ, 0, 0, 0, 0));
 78:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading matrix in binary from matrix.dat ...\n"));
 79:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_READ, &viewer));
 80:   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
 81:   PetscCall(MatLoad(C, viewer));
 82:   PetscCall(PetscViewerDestroy(&viewer));
 83:   PetscCall(PetscLogEventEnd(MATRIX_READ, 0, 0, 0, 0));
 84:   PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD));

 86:   /* Free data structures */
 87:   PetscCall(MatDestroy(&C));

 89:   PetscCall(PetscFinalize());
 90:   return 0;
 91: }

 93: /*TEST

 95:    test:
 96:       filter: grep -v " MPI process"

 98: TEST*/