xref: /petsc/src/mat/tests/ex169.c (revision ffa8c5705e8ab2cf85ee1d14dbe507a6e2eb5283) !
1 
2 static char help[] = "Test memory leak when duplicating a redundant matrix.\n\n";
3 
4 /*
5   Include "petscmat.h" so that we can use matrices.
6   automatically includes:
7      petscsys.h    - base PETSc routines   petscvec.h    - vectors
8      petscmat.h    - matrices
9      petscis.h     - index sets            petscviewer.h - viewers
10 */
11 #include <petscmat.h>
12 
13 int main(int argc,char **args)
14 {
15   Mat            A,Ar,C;
16   PetscViewer    fd;                        /* viewer */
17   char           file[PETSC_MAX_PATH_LEN];  /* input file name */
18   PetscInt       ns=2;
19   PetscMPIInt    size;
20   PetscSubcomm   subc;
21   PetscBool      flg;
22 
23   PetscCall(PetscInitialize(&argc,&args,(char*)0,help));
24   /*
25      Determine files from which we read the two linear systems
26      (matrix and right-hand-side vector).
27   */
28   PetscCall(PetscOptionsGetString(NULL,NULL,"-f0",file,sizeof(file),&flg));
29   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f0 option");
30   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd));
31   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
32   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Reading matrix with %d processors\n",size));
33   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
34   PetscCall(MatLoad(A,fd));
35   PetscCall(PetscViewerDestroy(&fd));
36   /*
37      Determines amount of subcomunicators
38   */
39   PetscCall(PetscOptionsGetInt(NULL,NULL,"-nsub",&ns,NULL));
40   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Splitting in %" PetscInt_FMT " subcommunicators\n",ns));
41   PetscCall(PetscSubcommCreate(PetscObjectComm((PetscObject)A),&subc));
42   PetscCall(PetscSubcommSetNumber(subc,ns));
43   PetscCall(PetscSubcommSetType(subc,PETSC_SUBCOMM_CONTIGUOUS));
44   PetscCall(PetscSubcommSetFromOptions(subc));
45   PetscCall(MatCreateRedundantMatrix(A,0,PetscSubcommChild(subc),MAT_INITIAL_MATRIX,&Ar));
46   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Copying matrix\n"));
47   PetscCall(MatDuplicate(Ar,MAT_COPY_VALUES,&C));
48   PetscCall(MatAXPY(Ar,0.1,C,DIFFERENT_NONZERO_PATTERN));
49   PetscCall(PetscSubcommDestroy(&subc));
50 
51   /*
52      Free memory
53   */
54   PetscCall(MatDestroy(&A));
55   PetscCall(MatDestroy(&Ar));
56   PetscCall(MatDestroy(&C));
57   PetscCall(PetscFinalize());
58   return 0;
59 }
60 
61 /*TEST
62 
63    test:
64       nsize: 4
65       requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
66       args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
67 
68 TEST*/
69