1 2 static char help[] = "Tests binary I/O of matrices and illustrates user-defined event logging.\n\n"; 3 4 #include <petscmat.h> 5 6 /* Note: Most applications would not read and write the same matrix within 7 the same program. This example is intended only to demonstrate 8 both input and output. */ 9 10 int main(int argc, char **args) { 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 #if defined(PETSC_USE_LOG) 17 PetscLogEvent MATRIX_GENERATE, MATRIX_READ; 18 #endif 19 20 PetscFunctionBeginUser; 21 PetscCall(PetscInitialize(&argc, &args, (char *)0, help)); 22 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 23 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 24 PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL)); 25 PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL)); 26 N = m * n; 27 28 /* PART 1: Generate matrix, then write it in binary format */ 29 30 PetscCall(PetscLogEventRegister("Generate Matrix", 0, &MATRIX_GENERATE)); 31 PetscCall(PetscLogEventBegin(MATRIX_GENERATE, 0, 0, 0, 0)); 32 33 /* Generate matrix */ 34 PetscCall(MatCreate(PETSC_COMM_WORLD, &C)); 35 PetscCall(MatSetSizes(C, PETSC_DECIDE, PETSC_DECIDE, N, N)); 36 PetscCall(MatSetFromOptions(C)); 37 PetscCall(MatSetUp(C)); 38 PetscCall(MatGetOwnershipRange(C, &Istart, &Iend)); 39 for (Ii = Istart; Ii < Iend; Ii++) { 40 v = -1.0; 41 i = Ii / n; 42 j = Ii - i * n; 43 if (i > 0) { 44 J = Ii - n; 45 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES)); 46 } 47 if (i < m - 1) { 48 J = Ii + n; 49 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES)); 50 } 51 if (j > 0) { 52 J = Ii - 1; 53 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES)); 54 } 55 if (j < n - 1) { 56 J = Ii + 1; 57 PetscCall(MatSetValues(C, 1, &Ii, 1, &J, &v, ADD_VALUES)); 58 } 59 v = 4.0; 60 PetscCall(MatSetValues(C, 1, &Ii, 1, &Ii, &v, ADD_VALUES)); 61 } 62 PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY)); 63 PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY)); 64 PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD)); 65 66 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "writing matrix in binary to matrix.dat ...\n")); 67 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_WRITE, &viewer)); 68 PetscCall(MatView(C, viewer)); 69 PetscCall(PetscViewerDestroy(&viewer)); 70 PetscCall(MatDestroy(&C)); 71 PetscCall(PetscLogEventEnd(MATRIX_GENERATE, 0, 0, 0, 0)); 72 73 /* PART 2: Read in matrix in binary format */ 74 75 /* All processors wait until test matrix has been dumped */ 76 PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD)); 77 78 PetscCall(PetscLogEventRegister("Read Matrix", 0, &MATRIX_READ)); 79 PetscCall(PetscLogEventBegin(MATRIX_READ, 0, 0, 0, 0)); 80 PetscCall(PetscPrintf(PETSC_COMM_WORLD, "reading matrix in binary from matrix.dat ...\n")); 81 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, "matrix.dat", FILE_MODE_READ, &viewer)); 82 PetscCall(MatCreate(PETSC_COMM_WORLD, &C)); 83 PetscCall(MatLoad(C, viewer)); 84 PetscCall(PetscViewerDestroy(&viewer)); 85 PetscCall(PetscLogEventEnd(MATRIX_READ, 0, 0, 0, 0)); 86 PetscCall(MatView(C, PETSC_VIEWER_STDOUT_WORLD)); 87 88 /* Free data structures */ 89 PetscCall(MatDestroy(&C)); 90 91 PetscCall(PetscFinalize()); 92 return 0; 93 } 94 95 /*TEST 96 97 test: 98 filter: grep -v " MPI process" 99 100 TEST*/ 101