Hi,

 

In my code I create and destroy during one run a lot of MPIAIJ matrices and it seems that not all memory is freed when I destroy the matrices. Moreover, it looks like the amount of “disappearing” memory increases with the number of processes. I am not so sure if I am doing something wrong or if it is really a memory leak. I have attached a simple reproducer.

 

Best,

Marius

program memory_leak
#include <petsc/finclude/petsc.h>
  use petscmat
  implicit none

  integer :: ierr,n,m,nalloc,i,nlrow1,nlrow2,inode,nlcol1,nlcol2,np,ndiag,j
  integer, allocatable :: i_row(:),i_col(:)
  double precision ::  info(MAT_INFO_SIZE)
  PetscInt, allocatable :: d_nnz(:)
  PetscScalar, allocatable :: pp(:)
  Mat :: A
  PetscRandom :: rnd

  call PetscInitialize(PETSC_NULL_CHARACTER,ierr)


  call MPI_COMM_SIZE(MPI_COMM_WORLD, np,ierr)
  call MPI_Comm_rank( PETSC_COMM_WORLD, inode, ierr )

  n=1000
  m=1000


  nalloc=10000
!~   nalloc=1
  call PetscRandomCreate(PETSC_COMM_WORLD,rnd,ierr)


  call MatCreate(PETSC_COMM_WORLD,A,ierr)
  call MatSetType(A, MATMPIAIJ,ierr)
  call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,m,ierr)
  call MatMPIAIJSetPreallocation(A,0,PETSC_NULL_INTEGER,0,PETSC_NULL_INTEGER,ierr)
  call MatGetOwnershipRange(A,nlrow1,nlrow2,ierr)
  call MatGetOwnershipRangeColumn(A,nlcol1,nlcol2,ierr)


  ndiag=nlrow2-nlrow1
  allocate(d_nnz(ndiag),pp(ndiag),i_col(ndiag),i_row(1))
  d_nnz=ndiag
  call MatDestroy(A,ierr)

  j=0
  do i=nlrow1,nlrow2-1
    j=j+1
    i_col(j)=i
    pp(j)=1d0
  end do

  if (inode.eq.0) call system('free --mega > mem.out')
  do i=1,nalloc

    call MatCreate(PETSC_COMM_WORLD,A,ierr)
    call MatSetType(A, MATMPIAIJ,ierr)
    call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,m,ierr)

    call MatMPIAIJSetPreallocation(A,0,d_nnz,0,PETSC_NULL_INTEGER,ierr)

    do j=nlrow1,nlrow2-1
      i_row(1)=j
      call MatSetValues(A,1,i_row,ndiag,i_col,pp,INSERT_VALUES,ierr)
    end do


    call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
    call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)


!~     call MatGetOwnershipRange(A,nlrow1,nlrow2,ierr)
!~     call MatGetOwnershipRangeColumn(A,nlcol1,nlcol2,ierr)
!~     call MatGetInfo(A, MAT_LOCAL, info,ierr);
!~     write(6,fmt='(8i12)') inode,int(info(mat_info_nz_allocated)),int(info(mat_info_nz_used)),nlrow1,nlrow2,nlrow2-nlrow1,nlcol1,nlcol2

    call MatDestroy(A,ierr)

    if (inode.eq.0) then
      if (mod(i,nint(nalloc*0.1)).eq.0) write(6,*) i,nalloc
    end if

  end do
  if (inode.eq.0) call system('free --mega >> mem.out')
  call PetscRandomDestroy(rnd,ierr)
  call PetscFinalize(ierr)

end program memory_leak

Reply via email to