xref: /petsc/src/mat/tests/ex198.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static char help[] = "Test MatMatMatMult\n\
2 Reads PETSc matrix A B and C, then comput D=A*B*C \n\
3 Input parameters include\n\
4   -fA <input_file> -fB <input_file> -fC <input_file> \n\n";
5 
6 #include <petscmat.h>
7 
8 int main(int argc, char **args) {
9   Mat         A, B, C, D, BC, ABC;
10   PetscViewer fd;
11   char        file[3][PETSC_MAX_PATH_LEN];
12   PetscBool   flg;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
16   /* read matrices A, B and C */
17   PetscCall(PetscOptionsGetString(NULL, NULL, "-fA", file[0], sizeof(file[0]), &flg));
18   PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_USER, "Must indicate binary file with the -fA options");
19 
20   PetscCall(PetscOptionsGetString(NULL, NULL, "-fB", file[1], sizeof(file[1]), &flg));
21   PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_USER, "Must indicate binary file with the -fB options");
22 
23   PetscCall(PetscOptionsGetString(NULL, NULL, "-fC", file[2], sizeof(file[2]), &flg));
24   PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_USER, "Must indicate binary file with the -fC options");
25 
26   /* Load matrices */
27   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file[0], FILE_MODE_READ, &fd));
28   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
29   PetscCall(MatLoad(A, fd));
30   PetscCall(PetscViewerDestroy(&fd));
31 
32   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file[1], FILE_MODE_READ, &fd));
33   PetscCall(MatCreate(PETSC_COMM_WORLD, &B));
34   PetscCall(MatLoad(B, fd));
35   PetscCall(PetscViewerDestroy(&fd));
36 
37   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file[2], FILE_MODE_READ, &fd));
38   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
39   PetscCall(MatLoad(C, fd));
40   PetscCall(PetscViewerDestroy(&fd));
41 
42   /* Test MatMatMult() */
43   PetscCall(MatMatMult(B, C, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &BC));
44   PetscCall(MatMatMult(A, BC, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &ABC));
45 
46   PetscCall(MatMatMatMult(A, B, C, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &D));
47   PetscCall(MatMatMatMult(A, B, C, MAT_REUSE_MATRIX, PETSC_DEFAULT, &D));
48   /* PetscCall(MatView(D,PETSC_VIEWER_STDOUT_WORLD)); */
49 
50   PetscCall(MatEqual(ABC, D, &flg));
51   PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "ABC != D");
52 
53   PetscCall(MatDestroy(&ABC));
54   PetscCall(MatDestroy(&BC));
55   PetscCall(MatDestroy(&D));
56   PetscCall(MatDestroy(&C));
57   PetscCall(MatDestroy(&B));
58   PetscCall(MatDestroy(&A));
59   PetscCall(PetscFinalize());
60   return 0;
61 }
62 
63 /*TEST
64 
65    test:
66       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
67       args: -fA ${DATAFILESPATH}/matrices/matmatmatmult/A.bin -fB ${DATAFILESPATH}/matrices/matmatmatmult/B.bin -fC ${DATAFILESPATH}/matrices/matmatmatmult/C.bin
68       output_file: output/ex198.out
69 
70    test:
71       suffix: 2
72       nsize: 3
73       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
74       args: -fA ${DATAFILESPATH}/matrices/matmatmatmult/A.bin -fB ${DATAFILESPATH}/matrices/matmatmatmult/B.bin -fC ${DATAFILESPATH}/matrices/matmatmatmult/C.bin
75       output_file: output/ex198.out
76 
77 TEST*/
78