Actual source code: ex124.c

  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: int main(int argc,char **args)
  9: {
 10:   Mat            A,B;
 11:   PetscViewer    fd;
 12:   char           file[2][PETSC_MAX_PATH_LEN];
 13:   PetscBool      flg;
 14:   PetscMPIInt    size;
 15:   PetscInt       ma,na,mb,nb;

 17:   PetscInitialize(&argc,&args,(char*)0,help);
 18:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 21:   /* read the two matrices, A and B */
 22:   PetscOptionsGetString(NULL,NULL,"-fA",file[0],sizeof(file[0]),&flg);
 24:   PetscOptionsGetString(NULL,NULL,"-fB",file[1],sizeof(file[1]),&flg);

 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);
 32:   printf("\n A:\n");
 33:   printf("----------------------\n");
 34:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 35:   MatGetSize(A,&ma,&na);

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

 46:   /* Compute B = -A + B */
 48:   MatAXPY(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
 49:   printf("\n B - A:\n");
 50:   printf("----------------------\n");
 51:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);

 53:   MatDestroy(&B);
 54:   MatDestroy(&A);
 55:   PetscFinalize();
 56:   return 0;
 57: }