Actual source code: ex198.c

petsc-3.8.4 2018-03-24
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>

  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;

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

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

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

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

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

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

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

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

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