Actual source code: ex169.c
petsc-3.8.4 2018-03-24
2: static char help[] = "Test memory leak when duplicating a redundant matrix.\n\n";
3:
5: /*
6: Include "petscmat.h" so that we can use matrices.
7: automatically includes:
8: petscsys.h - base PETSc routines petscvec.h - vectors
9: petscmat.h - matrices
10: petscis.h - index sets petscviewer.h - viewers
11: */
12: #include <petscmat.h>
14: int main(int argc,char **args)
15: {
16: Mat A,Ar,C;
17: PetscViewer fd; /* viewer */
18: char file[PETSC_MAX_PATH_LEN]; /* input file name */
20: PetscInt ns=2,np;
21: PetscSubcomm subc;
22: PetscBool flg;
23:
24: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
25: /*
26: Determine files from which we read the two linear systems
27: (matrix and right-hand-side vector).
28: */
29: PetscOptionsGetString(NULL,NULL,"-f0",file,PETSC_MAX_PATH_LEN,&flg);
30: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");
31: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
32: MPI_Comm_size(PETSC_COMM_WORLD,&np);
33: PetscPrintf(PETSC_COMM_WORLD,"Reading matrix with %d processors\n",np);
34: MatCreate(PETSC_COMM_WORLD,&A);
35: MatLoad(A,fd);
36: PetscViewerDestroy(&fd);
37: /*
38: Determines amount of subcomunicators
39: */
40: PetscOptionsGetInt(NULL,NULL,"-nsub",&ns,NULL);
41: PetscPrintf(PETSC_COMM_WORLD,"Splitting in %d subcommunicators\n",ns);
42: PetscSubcommCreate(PetscObjectComm((PetscObject)A),&subc);
43: PetscSubcommSetNumber(subc,ns);
44: PetscSubcommSetType(subc,PETSC_SUBCOMM_CONTIGUOUS);
45: PetscSubcommSetFromOptions(subc);
46: MatCreateRedundantMatrix(A,0,PetscSubcommChild(subc),MAT_INITIAL_MATRIX,&Ar);
47: PetscPrintf(PETSC_COMM_WORLD,"Copying matrix\n",ns);
48: MatDuplicate(Ar,MAT_COPY_VALUES,&C);
49: PetscSubcommDestroy(&subc);
50:
51: /*
52: Free memory
53: */
54: MatDestroy(&A);
55: MatDestroy(&Ar);
56: MatDestroy(&C);
57: PetscFinalize();
58: return ierr;
59: }