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: #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:   PetscTruth     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_ERR_SUP,"This is a uniprocessor example only!");

 25:   /* read the two matrices, A and B */
 26:   PetscOptionsGetString(PETSC_NULL,"-fA",file[0],PETSC_MAX_PATH_LEN-1,&flg);
 27:   if (!flg) SETERRQ(PETSC_ERR_USER,"Must indicate binary file with the -fA options");
 28:   PetscOptionsGetString(PETSC_NULL,"-fB",file[1],PETSC_MAX_PATH_LEN-1,&flg);
 29:   if (!flg) SETERRQ(PETSC_ERR_USER,"Must indicate binary file with the -fP options");
 30: 
 31:   /* Load matrices */
 32:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);
 33:   MatLoad(fd,MATAIJ,&A);
 34:   PetscViewerDestroy(fd);
 35:   printf("\n A:\n");
 36:   printf("----------------------\n");
 37:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 38:   MatGetSize(A,&ma,&na);
 39: 
 40: 
 41:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[1],FILE_MODE_READ,&fd);
 42:   MatLoad(fd,MATAIJ,&B);
 43:   PetscViewerDestroy(fd);
 44:   printf("\n B:\n");
 45:   printf("----------------------\n");
 46:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);
 47:   MatGetSize(B,&mb,&nb);
 48: 
 49:   /* Compute B = -A + B */
 50:   if (ma != mb || na != nb) SETERRQ(PETSC_ERR_ARG_SIZ,"nonconforming matrix size");
 51:   MatAXPY(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
 52:   printf("\n B - A:\n");
 53:   printf("----------------------\n");
 54:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);

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