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;

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

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

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

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

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

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