Actual source code: ex113.c
petsc-3.7.3 2016-08-01
2: static char help[] = "Tests sequential and parallel MatMatMult() and MatAXPY(...,SUBSET_NONZERO_PATTERN) \n\
3: Input arguments are:\n\
4: -f <input_file> : file to load\n\n";
5: /* e.g., mpiexec -n 3 ./ex113 -f <file> */
7: #include <petscmat.h>
11: int main(int argc,char **args)
12: {
13: Mat A,A1,A2,Mtmp,dstMat;
14: PetscViewer viewer;
16: PetscReal fill=4.0;
17: char file[128];
18: PetscBool flg;
20: PetscInitialize(&argc,&args,(char*)0,help);
22: /* Load the matrix A */
23: PetscOptionsGetString(NULL,NULL,"-f",file,128,&flg);
24: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix A with the -f option.");
26: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&viewer);
27: MatCreate(PETSC_COMM_WORLD,&A);
28: MatLoad(A,viewer);
29: PetscViewerDestroy(&viewer);
31: MatDuplicate(A,MAT_COPY_VALUES,&A1);
32: MatDuplicate(A,MAT_COPY_VALUES,&A2);
34: /* dstMat = A*A1*A2 */
35: MatMatMult(A1,A2,MAT_INITIAL_MATRIX,fill,&Mtmp);
36: MatMatMult(A,Mtmp,MAT_INITIAL_MATRIX,fill,&dstMat);
37: MatDestroy(&Mtmp);
39: /* dstMat += A1*A2 */
40: MatMatMult(A1,A2,MAT_INITIAL_MATRIX,fill,&Mtmp);
41: MatAXPY(dstMat,1.0,Mtmp,SUBSET_NONZERO_PATTERN);
42: MatDestroy(&Mtmp);
44: /* dstMat += A*A1 */
45: MatMatMult(A,A1,MAT_INITIAL_MATRIX,fill,&Mtmp);
46: MatAXPY(dstMat, 1.0, Mtmp,SUBSET_NONZERO_PATTERN);
47: MatDestroy(&Mtmp);
49: /* dstMat += A */
50: MatAXPY(dstMat, 1.0, A,SUBSET_NONZERO_PATTERN);
52: MatDestroy(&A);
53: MatDestroy(&A1);
54: MatDestroy(&A2);
55: MatDestroy(&dstMat);
56: PetscFinalize();
57: return 0;
58: }