Actual source code: ex124.c
petsc-3.15.0 2021-03-30
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;
15: PetscMPIInt size;
16: PetscInt ma,na,mb,nb;
18: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This is a uniprocessor example only!");
22: /* read the two matrices, A and B */
23: PetscOptionsGetString(NULL,NULL,"-fA",file[0],sizeof(file[0]),&flg);
24: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fA options");
25: PetscOptionsGetString(NULL,NULL,"-fB",file[1],sizeof(file[1]),&flg);
26: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fP options");
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 */
48: if (ma != mb || na != nb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nonconforming matrix size");
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 ierr;
58: }