xref: /petsc/src/mat/tests/ex165.c (revision 6a98f8dc3f2c9149905a87dc2e9d0fedaf64e09a)
1 
2 static char help[] = "Tests C=A^T*B via MatTranspose() and MatMatMult(). \n\
3                      Contributed by Alexander Grayver, Jan. 2012 \n\n";
4 /* Example:
5   mpiexec -n <np> ./ex165 -fA A.dat -fB B.dat -view_C
6  */
7 
8 #include <petscmat.h>
9 int main(int argc,char **args)
10 {
11   PetscErrorCode ierr;
12   Mat            A,AT,B,C;
13   PetscViewer    viewer;
14   PetscBool      flg;
15   char           file[PETSC_MAX_PATH_LEN];
16 
17   ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
18   ierr = PetscOptionsGetString(NULL,NULL,"-fA",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
19   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Input fileA not specified");
20   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
21   ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
22   ierr = MatSetType(A,MATAIJ);CHKERRQ(ierr);
23   ierr = MatLoad(A,viewer);CHKERRQ(ierr);
24   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
25 
26   ierr = PetscOptionsGetString(NULL,NULL,"-fB",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
27   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Input fileB not specified");
28   ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
29   ierr = MatCreate(PETSC_COMM_WORLD,&B);CHKERRQ(ierr);
30   ierr = MatSetType(B,MATDENSE);CHKERRQ(ierr);
31   ierr = MatLoad(B,viewer);CHKERRQ(ierr);
32   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
33 
34   ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&AT);CHKERRQ(ierr);
35   ierr = MatMatMult(AT,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&C);
36 
37   ierr = PetscOptionsHasName(NULL,NULL,"-view_C",&flg);CHKERRQ(ierr);
38   if (flg) {
39     ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"C.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
40     ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_NATIVE);CHKERRQ(ierr);
41     ierr = MatView(C,viewer);CHKERRQ(ierr);
42     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
43     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
44   }
45   ierr = MatDestroy(&A);CHKERRQ(ierr);
46   ierr = MatDestroy(&B);CHKERRQ(ierr);
47   ierr = MatDestroy(&AT);CHKERRQ(ierr);
48   ierr = MatDestroy(&C);CHKERRQ(ierr);
49   ierr = PetscFinalize();
50   return ierr;
51 }
52