Actual source code: ex169.c
2: static char help[] = "Test memory leak when duplicating a redundant matrix.\n\n";
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>
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;
24: PetscInitialize(&argc, &args, (char *)0, help);
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, sizeof(file), &flg);
31: PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd);
32: MPI_Comm_size(PETSC_COMM_WORLD, &size);
33: PetscPrintf(PETSC_COMM_WORLD, "Reading matrix with %d processors\n", size);
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 %" PetscInt_FMT " 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");
48: MatDuplicate(Ar, MAT_COPY_VALUES, &C);
49: MatAXPY(Ar, 0.1, C, DIFFERENT_NONZERO_PATTERN);
50: PetscSubcommDestroy(&subc);
52: /*
53: Free memory
54: */
55: MatDestroy(&A);
56: MatDestroy(&Ar);
57: MatDestroy(&C);
58: PetscFinalize();
59: return 0;
60: }
62: /*TEST
64: test:
65: nsize: 4
66: requires: !complex double !defined(PETSC_USE_64BIT_INDICES)
67: args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
69: TEST*/