Actual source code: ex169.c
petsc-3.13.6 2020-09-29
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;
21: PetscMPIInt size;
22: PetscSubcomm subc;
23: PetscBool flg;
24:
25: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
26: /*
27: Determine files from which we read the two linear systems
28: (matrix and right-hand-side vector).
29: */
30: PetscOptionsGetString(NULL,NULL,"-f0",file,PETSC_MAX_PATH_LEN,&flg);
31: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f0 option");
32: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
33: MPI_Comm_size(PETSC_COMM_WORLD,&size);
34: PetscPrintf(PETSC_COMM_WORLD,"Reading matrix with %d processors\n",size);
35: MatCreate(PETSC_COMM_WORLD,&A);
36: MatLoad(A,fd);
37: PetscViewerDestroy(&fd);
38: /*
39: Determines amount of subcomunicators
40: */
41: PetscOptionsGetInt(NULL,NULL,"-nsub",&ns,NULL);
42: PetscPrintf(PETSC_COMM_WORLD,"Splitting in %d subcommunicators\n",ns);
43: PetscSubcommCreate(PetscObjectComm((PetscObject)A),&subc);
44: PetscSubcommSetNumber(subc,ns);
45: PetscSubcommSetType(subc,PETSC_SUBCOMM_CONTIGUOUS);
46: PetscSubcommSetFromOptions(subc);
47: MatCreateRedundantMatrix(A,0,PetscSubcommChild(subc),MAT_INITIAL_MATRIX,&Ar);
48: PetscPrintf(PETSC_COMM_WORLD,"Copying matrix\n",ns);
49: MatDuplicate(Ar,MAT_COPY_VALUES,&C);
50: PetscSubcommDestroy(&subc);
51:
52: /*
53: Free memory
54: */
55: MatDestroy(&A);
56: MatDestroy(&Ar);
57: MatDestroy(&C);
58: PetscFinalize();
59: return ierr;
60: }
63: /*TEST
65: test:
66: nsize: 4
67: requires: datafilespath !complex double !define(PETSC_USE_64BIT_INDICES)
68: args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
71: TEST*/