Actual source code: ex124.c
petsc-3.10.5 2019-03-28
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
9: PetscInt main(PetscInt argc,char **args)
10: {
11: Mat A,B;
12: PetscViewer fd;
13: char file[2][PETSC_MAX_PATH_LEN];
14: PetscBool flg;
16: PetscMPIInt size;
17: PetscInt ma,na,mb,nb;
19: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
20: MPI_Comm_size(PETSC_COMM_WORLD,&size);
21: if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!");
23: /* read the two matrices, A and B */
24: PetscOptionsGetString(NULL,NULL,"-fA",file[0],PETSC_MAX_PATH_LEN,&flg);
25: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fA options");
26: PetscOptionsGetString(NULL,NULL,"-fB",file[1],PETSC_MAX_PATH_LEN,&flg);
27: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fP options");
29: /* Load matrices */
30: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[0],FILE_MODE_READ,&fd);
31: MatCreate(PETSC_COMM_WORLD,&A);
32: MatLoad(A,fd);
33: PetscViewerDestroy(&fd);
34: printf("\n A:\n");
35: printf("----------------------\n");
36: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
37: MatGetSize(A,&ma,&na);
39: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[1],FILE_MODE_READ,&fd);
40: MatCreate(PETSC_COMM_WORLD,&B);
41: MatLoad(B,fd);
42: PetscViewerDestroy(&fd);
43: printf("\n B:\n");
44: printf("----------------------\n");
45: MatView(B,PETSC_VIEWER_STDOUT_WORLD);
46: MatGetSize(B,&mb,&nb);
48: /* Compute B = -A + B */
49: if (ma != mb || na != nb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nonconforming matrix size");
50: MatAXPY(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);
51: printf("\n B - A:\n");
52: printf("----------------------\n");
53: MatView(B,PETSC_VIEWER_STDOUT_WORLD);
55: MatDestroy(&B);
56: MatDestroy(&A);
57: PetscFinalize();
58: return ierr;
59: }