1 static char help[] = "Check the difference of the two matrices \n\
2 Reads PETSc matrix A and B, then check B=A-B \n\
3 Input parameters include\n\
4 -fA <input_file> -fB <input_file> \n\n";
5
6 #include <petscmat.h>
7
main(int argc,char ** args)8 int main(int argc, char **args)
9 {
10 Mat A, B;
11 PetscViewer fd;
12 char file[2][PETSC_MAX_PATH_LEN];
13 PetscBool flg;
14 PetscMPIInt size;
15 PetscInt ma, na, mb, nb;
16
17 PetscFunctionBeginUser;
18 PetscCall(PetscInitialize(&argc, &args, NULL, help));
19 PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
20 PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");
21
22 /* read the two matrices, A and B */
23 PetscCall(PetscOptionsGetString(NULL, NULL, "-fA", file[0], sizeof(file[0]), &flg));
24 PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -fA options");
25 PetscCall(PetscOptionsGetString(NULL, NULL, "-fB", file[1], sizeof(file[1]), &flg));
26 PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -fP options");
27
28 /* Load matrices */
29 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file[0], FILE_MODE_READ, &fd));
30 PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
31 PetscCall(MatLoad(A, fd));
32 PetscCall(PetscViewerDestroy(&fd));
33 printf("\n A:\n");
34 printf("----------------------\n");
35 PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
36 PetscCall(MatGetSize(A, &ma, &na));
37
38 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file[1], FILE_MODE_READ, &fd));
39 PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
40 PetscCall(MatLoad(B, fd));
41 PetscCall(PetscViewerDestroy(&fd));
42 printf("\n B:\n");
43 printf("----------------------\n");
44 PetscCall(MatView(B, PETSC_VIEWER_STDOUT_WORLD));
45 PetscCall(MatGetSize(B, &mb, &nb));
46
47 /* Compute B = -A + B */
48 PetscCheck(ma == mb && na == nb, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "nonconforming matrix size");
49 PetscCall(MatAXPY(B, -1.0, A, DIFFERENT_NONZERO_PATTERN));
50 printf("\n B - A:\n");
51 printf("----------------------\n");
52 PetscCall(MatView(B, PETSC_VIEWER_STDOUT_WORLD));
53
54 PetscCall(MatDestroy(&B));
55 PetscCall(MatDestroy(&A));
56 PetscCall(PetscFinalize());
57 return 0;
58 }
59