[petsc-users] Release object

Sang pham van pvsang002 at gmail.com
Thu Sep 17 19:12:56 CDT 2015


Hi,

I have the below code section, every time it runs through this section,
used memory increases very much and not decreases then. Could you please
let me know what object did I forgot to release in the for-loop?

Many thanks.

PVS


  int mu_x[20]={0,1,0,0,2,0,0,1,0,1,3,0,0,2,2,1,0,1,0,1};
  int mu_y[20]={0,0,1,0,0,2,0,1,1,0,0,3,0,1,0,2,2,0,1,1};
  int mu_z[20]={0,0,0,1,0,0,2,0,1,1,0,0,3,0,1,0,1,2,2,1};
  for (int ib=0;ib<isur->BIPoints.N;ib++)
  {
    Mat W,V;
    int Mm = isur->iSolidCellsNearBIPoint[ib+1] -
isur->iSolidCellsNearBIPoint[ib];
    PetscScalar val=0;
    MatCreateSeqAIJ(PETSC_COMM_SELF,Mm,Mm,Mm,NULL,&W);
    MatCreateSeqAIJ(PETSC_COMM_SELF,Mm,L3D,L3D,NULL,&V);
    double xm, ym, zm, dm;
    for (int i=0;i<Mm;i++)
    {
      xm =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[0]
- isur->BIPoints.x[ib];
      ym =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[1]
- isur->BIPoints.y[ib];
      zm =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[2]
- isur->BIPoints.z[ib];
      dm = sqrt(xm*xm+ym*ym+zm*zm);
      val = WeightFunc(dm,isur->R_solid[ib]);
      MatSetValue(W,i,i,val,INSERT_VALUES);
    }
    MatAssemblyBegin(W,MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(W,MAT_FINAL_ASSEMBLY);

    for (int i=0;i<Mm;i++)
    for (int j=0;j<L3D;j++)
    {
      xm =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[0]
- isur->BIPoints.x[ib];
      ym =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[1]
- isur->BIPoints.y[ib];
      zm =
Mesh->Cells[isur->SolidCellsNearBIPoint[isur->iSolidCellsNearBIPoint[ib]+i]].Cen.xyz[2]
- isur->BIPoints.z[ib];
      val = power(xm,mu_x[j])*power(ym,mu_y[j])*power(zm,mu_z[j]);
      MatSetValue(V,i,j,val,INSERT_VALUES);
    }
    MatAssemblyBegin(V,MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(V,MAT_FINAL_ASSEMBLY);
    //if(ib==13) MatView(V,PETSC_VIEWER_STDOUT_SELF);

    /// Solve least square problem:
    Mat Amat,W2,VtW2,VtW2V,invVtW2V, VtW2V1Vt,I,invVtW2V_b;

    MatDuplicate(W,MAT_DO_NOT_COPY_VALUES,&W2);
    MatDuplicate(W2,MAT_DO_NOT_COPY_VALUES,&VtW2);
    MatDuplicate(W2,MAT_DO_NOT_COPY_VALUES,&VtW2V);

    MatMatMult(W,W,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&W2);

    MatTransposeMatMult(V,W2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2);

    MatMatMult(VtW2,V,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2V);

    MatCreateSeqDense(PETSC_COMM_SELF,L3D,L3D,NULL,&I);
    MatDuplicate(I,MAT_DO_NOT_COPY_VALUES,&invVtW2V);

    Vec ivec;
    VecCreateSeq(PETSC_COMM_SELF,L3D,&ivec);
    VecSet(ivec,1.0);
    MatDiagonalSet(I,ivec,INSERT_VALUES);

    IS rperm,cperm;
    MatFactorInfo info;
    MatGetOrdering(VtW2V,  MATORDERINGNATURAL,  &rperm,  &cperm);
    MatFactorInfoInitialize(&info);
    MatLUFactor(VtW2V, rperm, cperm, &info);

    MatMatSolve(VtW2V,I,invVtW2V);

    MatConvert(invVtW2V,MATSEQAIJ,MAT_INITIAL_MATRIX,&invVtW2V_b);

MatMatTransposeMult(invVtW2V_b,V,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&VtW2V1Vt);
    MatMatMult(VtW2V1Vt,W2,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&Amat);
    for (int i=0;i<L3D;i++)
    for (int j=0;j<Mm;j++)
      MatGetValues(Amat,1,&i,1,&j,&A[ib][i][j]);

    MatDestroy(&W2);
    MatDestroy(&Amat);
    MatDestroy(&VtW2);
    MatDestroy(&VtW2V);
    MatDestroy(&invVtW2V);
    MatDestroy(&invVtW2V_b);
    MatDestroy(&VtW2V1Vt);
    MatDestroy(&I);

    MatDestroy(&W);
    MatDestroy(&V);
    VecDestroy(&ivec);
  }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150918/7aa2f9a9/attachment.html>


More information about the petsc-users mailing list