Skip to content

Commit

Permalink
fix memory leak at the end of a program
Browse files Browse the repository at this point in the history
These memory leaks didn't affect runtime.

- Some temporary ISs are not freed properly.
- Destroy temporary Vec and VecScatter in the class.
  • Loading branch information
piyueh committed Mar 14, 2018
1 parent 15e1359 commit 65e274e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/init.cpp
Expand Up @@ -304,6 +304,12 @@ PetscErrorCode AmgXSolver::finalize()
ierr = MPI_Comm_free(&gpuWorld); CHK;
}

// destroy PETSc objects
ierr = VecScatterDestroy(&scatterLhs); CHK;
ierr = VecScatterDestroy(&scatterRhs); CHK;
ierr = VecDestroy(&redistLhs); CHK;
ierr = VecDestroy(&redistRhs); CHK;

// re-set necessary variables in case users want to reuse
// the variable of this instance for a new instance
gpuProc = MPI_UNDEFINED;
Expand Down
10 changes: 8 additions & 2 deletions src/setA.cpp
Expand Up @@ -72,6 +72,9 @@ PetscErrorCode AmgXSolver::setA(const Mat &A)
}
ierr = MPI_Barrier(globalCpuWorld); CHK;

// destroy temporary PETSc objects
ierr = ISDestroy(&devIS); CHK;

PetscFunctionReturn(0);
}

Expand All @@ -82,19 +85,22 @@ PetscErrorCode AmgXSolver::getDevIS(const Mat &A, IS &devIS)
PetscFunctionBeginUser;

PetscErrorCode ierr;
IS tempIS;

// get index sets of A locally owned by each process
// note that devIS is now a serial IS on each process
ierr = MatGetOwnershipIS(A, &devIS, nullptr); CHK;

// concatenate index sets that belong to the same devWorld
// note that now devIS is a parallel IS of communicator devWorld
ierr = ISOnComm(devIS, devWorld, PETSC_USE_POINTER, &devIS); CHK;
ierr = ISOnComm(devIS, devWorld, PETSC_USE_POINTER, &tempIS); CHK;
ierr = ISDestroy(&devIS); CHK;

// all gather in order to have all indices belong to a devWorld on the
// leading rank of that devWorld. THIS IS NOT EFFICIENT!!
// note that now devIS is again a serial IS on each process
ierr = ISAllGather(devIS, &devIS); CHK;
ierr = ISAllGather(tempIS, &devIS); CHK;
ierr = ISDestroy(&tempIS); CHK;

// empty devIS on ranks other than the leading ranks in each devWorld
if (myDevWorldRank != 0)
Expand Down

0 comments on commit 65e274e

Please sign in to comment.