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

Christophe Ortiz christophe.ortiz at ciemat.es
Tue May 20 08:24:31 CDT 2014


On Tue, May 20, 2014 at 3:17 PM, Jed Brown <jed at jedbrown.org> wrote:

> Matthew Knepley <knepley at gmail.com> writes:
> >>  array = (PetscScalar**)malloc(sizeof(PetscScalar*) * dof);
> >>  for (k = 0; k < dof; k++){
> >>    array[k] = (PetscScalar*)malloc(sizeof(PetscScalar) * dof);
> >>   }
> >>
> >> When I pass it to MatSetValuesBlocked() there is a problem. Either Petsc
> >> complains because I am not passing it the right way or when it accepts
> it,
> >> wrong data is passed because the solution is not correct. Maybe Petsc
> >> expect dof*dof values and only dof are passed ?
> >>
> >
> > You can only pass contiguous memory to MatSetValues().
>
> And, while perhaps atypical, VecGetArray2D will give you contiguous
> memory behind the scenes, so it would work in this case.  (Make a Vec of
> the right size using COMM_SELF instead of malloc.)
>
> With C99, you can use VLA pointers to get the "2D indexing" without
> setting up explicit pointers.
>


Since for some reasons I need global two-dimensional arrays, what I did is
the following.
I declared a PetscScalar **array outside main(), ie before dof is
determined.
Then, knowing dof I use malloc inside main() to allocate memory to the
array. I use then array in different functions and in order to pass it to
MatSetValues, I copy it to a local and classical two-dimensional
array[dof][dof] (contiguous memory) which is passed to MatSetValues. It
works.

But I'll try with VecGetArray2D.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20140520/53d59e55/attachment-0001.html>


More information about the petsc-users mailing list