[petsc-users] valgrind errors.

Jed Brown jed at 59A2.org
Tue Jan 19 11:44:38 CST 2010


On Tue, 19 Jan 2010 12:24:07 -0500, "(Rebecca) Xuefei YUAN" <xy2102 at columbia.edu> wrote:
> Dear Jed,
> 
> I found the source of these uninitialised values.
> 
> In my FormFunction(), there is a user-defined structure called  
> FieldOther and I will use this as a temp values to build up my  
> residual function. The main loop is like:
> 
> 
> typedef struct {
> 	PetscReal	x1,x2,x3,x4;
> } FieldOther;
> 
> #undef __FUNCT__
> #define __FUNCT__ "FormFunction"
> PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void*dummg)
> {
> // ...
> 	FieldOther		*fieldother;
> // ...
> 	PetscFunctionBegin;
> // ...
> 	ierr =  
> PetscMalloc(sizeof(PetscReal)*(info.mx)*(info.my)*sizeof(FieldOther),  
> &fieldother);CHKERRQ(ierr);

This allocates more memory than you need (probably 8 times).

  PetscMalloc(info.mx*info.my*sizeof(PetscReal),&fieldother);

would give you what you want.  Note that you can also zero it with

  PetscMemzero(fieldother,info.mx*info.my*sizeof(PetscReal));

instead of the explicit loop.


You are now allowed to index fieldother[0+0*info.mx] up to
fieldother[info.mx*info.my-1].  What values are jFirst,jLast,iFirst, and
iLast?  In particular, they will not start at 0 in parallel, and perhaps
they include ghost values?.

The usual procedure when you need such temporary arrays is to clone a DA
of whatever size you need, but with different values of dof (and maybe
overlap), then use DAGetArray() or DAGetLocalVector()+DAVecGetArray() so
that you can index it as fieldother[j][i].x2 (even in parallel).

Jed


More information about the petsc-users mailing list