Actual source code: ex171.c
1: static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n";
3: #include <petscmat.h>
5: int main(int argc, char **args)
6: {
7: Mat A;
8: Vec x;
9: PetscViewer fd; /* viewer */
10: char file[PETSC_MAX_PATH_LEN]; /* input file name */
11: PetscReal norm;
12: PetscBool flg;
14: PetscFunctionBeginUser;
15: PetscCall(PetscInitialize(&argc, &args, NULL, help));
16: /* Determine file from which we read the matrix A */
17: PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
18: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
20: /* Load matrix A */
21: PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
22: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
23: PetscCall(MatSetFromOptions(A));
24: PetscCall(MatLoad(A, fd));
25: PetscCall(PetscViewerDestroy(&fd));
26: PetscCall(MatCreateVecs(A, &x, NULL));
27: PetscCall(MatGetDiagonal(A, x));
28: PetscCall(VecScale(x, -1.0));
29: PetscCall(MatDiagonalSet(A, x, ADD_VALUES));
30: PetscCall(MatGetDiagonal(A, x));
31: PetscCall(VecNorm(x, NORM_2, &norm));
32: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Norm %g\n", (double)norm));
34: /* Free data structures */
35: PetscCall(MatDestroy(&A));
36: PetscCall(VecDestroy(&x));
37: PetscCall(PetscFinalize());
38: return 0;
39: }
41: /*TEST
43: testset:
44: nsize: 4
45: requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
46: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
47: output_file: output/ex171_1.out
49: test:
50: suffix: 1
51: args: -mat_type aij
53: test:
54: suffix: 2
55: requires: kokkos_kernels
56: args: -mat_type aijkokkos
58: TEST*/