Actual source code: ex124.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  1: static char help[] = "Check the difference of the two matrices \n\
  2: Reads PETSc matrix A and B, then check B=A-B \n\
  3: Input parameters include\n\
  4:   -fA <input_file> -fB <input_file> \n\n";

  6: #include <petscmat.h>

  8: #undef WRITEFILE
 11: PetscInt main(PetscInt argc,char **args)
 12: {
 13:   Mat            A,B;
 14:   PetscViewer    fd;
 15:   char           file[2][PETSC_MAX_PATH_LEN];
 16:   PetscBool      flg;
 18:   PetscMPIInt    size;
 19:   PetscInt       ma,na,mb,nb;

 21:   PetscInitialize(&argc,&args,(char*)0,help);
 22:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 23:   if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!");

 25:   /* read the two matrices, A and B */
 26:   PetscOptionsGetString(NULL,NULL,"-fA",file[0],PETSC_MAX_PATH_LEN,&flg);
 27:   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fA options");
 28:   PetscOptionsGetString(NULL,NULL,"-fB",file[1],PETSC_MAX_PATH_LEN,&flg);
 29:   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fP options");

 31:   /* Load matrices */
 32:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);
 33:   MatCreate(PETSC_COMM_WORLD,&A);
 34:   MatLoad(A,fd);
 35:   PetscViewerDestroy(&fd);
 36:   printf("\n A:\n");
 37:   printf("----------------------\n");
 38:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 39:   MatGetSize(A,&ma,&na);

 41:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[1],FILE_MODE_READ,&fd);
 42:   MatCreate(PETSC_COMM_WORLD,&B);
 43:   MatLoad(B,fd);
 44:   PetscViewerDestroy(&fd);
 45:   printf("\n B:\n");
 46:   printf("----------------------\n");
 47:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);
 48:   MatGetSize(B,&mb,&nb);

 50:   /* Compute B = -A + B */
 51:   if (ma != mb || na != nb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nonconforming matrix size");
 52:   MatAXPY(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
 53:   printf("\n B - A:\n");
 54:   printf("----------------------\n");
 55:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);

 57:   MatDestroy(&B);
 58:   MatDestroy(&A);
 59:   PetscFinalize();
 60:   return 0;
 61: }