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