Actual source code: ex198.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  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";

  6: #include <petscmat.h>

 10: int main(int argc,char **args)
 11: {
 12:   Mat            A,B,C,D,BC,ABC;
 13:   PetscViewer    fd;
 14:   char           file[3][PETSC_MAX_PATH_LEN];
 15:   PetscBool      flg;

 18:   PetscInitialize(&argc,&args,(char*)0,help);
 19:   /* read matrices A, B and C */
 20:   PetscOptionsGetString(NULL,NULL,"-fA",file[0],PETSC_MAX_PATH_LEN,&flg);
 21:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Must indicate binary file with the -fA options");

 23:   PetscOptionsGetString(NULL,NULL,"-fB",file[1],PETSC_MAX_PATH_LEN,&flg);
 24:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Must indicate binary file with the -fB options");

 26:   PetscOptionsGetString(NULL,NULL,"-fC",file[2],PETSC_MAX_PATH_LEN,&flg);
 27:   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Must indicate binary file with the -fC options");

 29:   /* Load matrices */
 30:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);
 31:   MatCreate(PETSC_COMM_WORLD,&A);
 32:   MatLoad(A,fd);
 33:   PetscViewerDestroy(&fd);

 35:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[1],FILE_MODE_READ,&fd);
 36:   MatCreate(PETSC_COMM_WORLD,&B);
 37:   MatLoad(B,fd);
 38:   PetscViewerDestroy(&fd);

 40:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[2],FILE_MODE_READ,&fd);
 41:   MatCreate(PETSC_COMM_WORLD,&C);
 42:   MatLoad(C,fd);
 43:   PetscViewerDestroy(&fd);

 45:   /* Test MatMatMult() */
 46:   MatMatMult(B,C,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BC);
 47:   MatMatMult(A,BC,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&ABC);

 49:   MatMatMatMult(A,B,C,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&D);
 50:   MatMatMatMult(A,B,C,MAT_REUSE_MATRIX,PETSC_DEFAULT,&D);
 51:   /* MatView(D,PETSC_VIEWER_STDOUT_WORLD); */

 53:   MatEqual(ABC,D,&flg);
 54:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"ABC != D");
 55: 
 56:   MatDestroy(&ABC);
 57:   MatDestroy(&BC);
 58:   MatDestroy(&D);
 59:   MatDestroy(&C);
 60:   MatDestroy(&B);
 61:   MatDestroy(&A);
 62:   PetscFinalize();
 63:   return 0;
 64: }