[petsc-users] Memory corruption with two-dimensional array and PetscMemzero

Christophe Ortiz christophe.ortiz at ciemat.es
Wed May 14 06:29:55 CDT 2014


Hi all,

I am experiencing some problems of memory corruption with PetscMemzero().

I set the values of the Jacobian by blocks using MatSetValuesBlocked(). To
do so, I use some temporary two-dimensional arrays[dof][dof] that I must
reset at each loop.

Inside FormIJacobian, for instance, I declare the following two-dimensional
array:

   PetscScalar  diag[dof][dof];

and then, to zero the array diag[][] I do

   ierr = PetscMemzero(diag,dof*dof*sizeof(PetscScalar));

So far no problem. It works fine.

Now, what I want is to have diag[][] as a global array so all functions can
have access to it. Therefore, I declare it outside main().
Since outside the main() I still do not know dof, which is determined later
inside main(), I declare the two-dimensional array diag as follows:

PetscScalar  **diag;

Then, inside main(), once dof is determined, I allocate memory for diag as
follows:

 diag = (PetscScalar**)malloc(sizeof(PetscScalar*) * dof);

 for (k = 0; k < dof; k++){
  diag[k] = (PetscScalar*)malloc(sizeof(PetscScalar) * dof);
 }
That is, the classical way to allocate memory using the pointer notation.

Then, when it comes to zero the two-dimensional array diag[][] inside
FormIJacobian, I do as before:

   ierr = PetscMemzero(diag,dof*dof*sizeof(PetscScalar));

Compilation goes well but when I launch the executable, after few timesteps
I get the following memory corruption message:

      TSAdapt 'basic': step   0 accepted t=0          + 1.000e-16
wlte=8.5e-05 family='arkimex' scheme=0:'1bee' dt=1.100e-16
      TSAdapt 'basic': step   1 accepted t=1e-16      + 1.100e-16
wlte=4.07e-13 family='arkimex' scheme=0:'3' dt=1.210e-16
      TSAdapt 'basic': step   2 accepted t=2.1e-16    + 1.210e-16
wlte=1.15e-13 family='arkimex' scheme=0:'3' dt=1.331e-16
      TSAdapt 'basic': step   3 accepted t=3.31e-16   + 1.331e-16
wlte=1.14e-13 family='arkimex' scheme=0:'3' dt=1.464e-16
[0]PETSC ERROR: PetscMallocValidate: error detected at TSComputeIJacobian()
line 719 in src/ts/interface/ts.c
[0]PETSC ERROR: Memory [id=0(0)] at address 0x243c260 is corrupted
(probably write past end of array)
[0]PETSC ERROR: Memory originally allocated in (null)() line 0 in
src/mat/impls/aij/seq/(null)
[0]PETSC ERROR: --------------------- Error Message
------------------------------------
[0]PETSC ERROR: Memory corruption!
[0]PETSC ERROR:  !
[0]PETSC ERROR:
------------------------------------------------------------------------
[0]PETSC ERROR: Petsc Release Version 3.4.1, Jun, 10, 2013
[0]PETSC ERROR: See docs/changes/index.html for recent updates.
[0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.
[0]PETSC ERROR: See docs/index.html for manual pages.
[0]PETSC ERROR:
------------------------------------------------------------------------
[0]PETSC ERROR: ./diffusion on a icc-nompi-double-blas-debug named
mazinger.ciemat.es by u5751 Wed May 14 13:23:26 2014
[0]PETSC ERROR: Libraries linked from
/home/u5751/petsc-3.4.1/icc-nompi-double-blas-debug/lib
[0]PETSC ERROR: Configure run at Wed Apr  2 14:01:51 2014
[0]PETSC ERROR: Configure options --with-mpi=0 --with-cc=icc --with-cxx=icc
--with-clanguage=cxx --with-debugging=1 --with-scalar-type=real
--with-precision=double --download-f-blas-lapack
[0]PETSC ERROR:
------------------------------------------------------------------------
[0]PETSC ERROR: PetscMallocValidate() line 149 in src/sys/memory/mtr.c
[0]PETSC ERROR: TSComputeIJacobian() line 719 in src/ts/interface/ts.c
[0]PETSC ERROR: SNESTSFormJacobian_ARKIMEX() line 995 in
src/ts/impls/arkimex/arkimex.c
[0]PETSC ERROR: SNESTSFormJacobian() line 3397 in src/ts/interface/ts.c
[0]PETSC ERROR: SNESComputeJacobian() line 2152 in src/snes/interface/snes.c
[0]PETSC ERROR: SNESSolve_NEWTONLS() line 218 in src/snes/impls/ls/ls.c
[0]PETSC ERROR: SNESSolve() line 3636 in src/snes/interface/snes.c
[0]PETSC ERROR: TSStep_ARKIMEX() line 765 in src/ts/impls/arkimex/arkimex.c
[0]PETSC ERROR: TSStep() line 2458 in src/ts/interface/ts.c
[0]PETSC ERROR: TSSolve() line 2583 in src/ts/interface/ts.c
[0]PETSC ERROR: main() line 2690 in src/ts/examples/tutorials/diffusion.cxx
./compile_diffusion: line 25: 17061 Aborted                 ./diffusion
-ts_adapt_monitor -ts_adapt_basic_clip 0.01,1.10 -draw_pause -2
-ts_arkimex_type 3 -ts_max_snes_failures -1 -snes_type newtonls
-snes_linesearch_type basic -ksp_type gmres -pc_type ilu

Did I do something wrong ? Or is it due to the pointer notation to declare
the two-dimensional array that conflicts with PetscMemzero ?

Many thanks in advance for your help.

Christophe

--
Q
Por favor, piense en el medio ambiente antes de imprimir este mensaje.
Please consider the environment before printing this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140514/2f259965/attachment-0001.html>


More information about the petsc-users mailing list