[petsc-users] valgrind errors.

(Rebecca) Xuefei YUAN xy2102 at columbia.edu
Tue Jan 19 12:27:14 CST 2010


Dear Jed,

I switched the code as below and it is working now without valgrind errors.

I am using multilevel, will the following approach be fine? I will  
modify the code a little more to get sth like yours.

typedef struct {
	PetscReal	x1,x2,x3,x4;
} FieldOther;

#undef __FUNCT__
#define __FUNCT__ "FormFunction"
PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void*dummg)
{
// ...
	DA				dafieldother;
	FieldOther		**fieldother;
	Vec				FIELDOTHER;
// ...
	PetscFunctionBegin;
// ...
	ierr = DAGetLocalInfo((DA)dmmg->dm,&info);CHKERRQ(ierr);
// ...
	ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,  
info.mx, info.my, PETSC_DECIDE, PETSC_DECIDE, 4, 2, 0, 0,  
&dafieldother);CHKERRQ(ierr);
	ierr = DASetFieldName(dafieldother, 0, "x1");CHKERRQ(ierr);
	ierr = DASetFieldName(dafieldother, 1, "x2");CHKERRQ(ierr);
	ierr = DASetFieldName(dafieldother, 2, "x3");CHKERRQ(ierr);
	ierr = DASetFieldName(dafieldother, 3, "x4");CHKERRQ(ierr);

	ierr = DAGetLocalVector(dafieldother,&FIELDOTHER);CHKERRQ(ierr);
     ierr = DAVecGetArray(dafieldother,FIELDOTHER,&fieldother);CHKERRQ(ierr);

	for (j = jFirst; j <= jLast; j++){
         for (i = iFirst; i <= iLast; i++){
                 fieldother[j][i].x1 = 0;
                 fieldother[j][i].x2 = 0;
                 fieldother[j][i].x3 = 0;
                 fieldother[j][i].x4 = 0;
		}
	}
// ...
     for (j = jFirst; j <= jLast; j++){
         for (i = iFirst; i <= iLast; i++){
			ffield[j][i].x1 = fieldother[j][i].x1;
		}
	}
// ...
     ierr =  
DAVecRestoreArray(dafieldother,FIELDOTHER,&fieldother);CHKERRQ(ierr);
	ierr = DARestoreLocalVector(dafieldother,&FIELDOTHER);CHKERRQ(ierr);
	ierr = DADestroy(dafieldother);CHKERRQ(ierr);
	PetscFunctionReturn(0);

}


Thanks so much!

Rebecca



Quoting Jed Brown <jed at 59A2.org>:

> On Tue, 19 Jan 2010 12:56:30 -0500, "(Rebecca) Xuefei YUAN"   
> <xy2102 at columbia.edu> wrote:
>> Dear Jed,
>>
>> I have one more question, what is the difference or how could I know
>> whether I need DAGetArray() or DAGetLocalVector()+DAVecGetArray() to
>> get my array?
>
> You can use the former unless you need to use it when packed up in a
> vector.
>
> In a mulitlevel scheme, I would probably put something like this in my
> FormFunction:
>
>   DA da = (DA)dmmg->dm,eda;
>
>   PetscObjectQuery((PetscObject)da,"ExtraDA",(PetscObject*)&eda);
>   if (!eda) {
>     // create eda based on the layout of da
>     PetscObjectCompose((PetscObject)da,"ExtraDA",eda);
>     DADestroy(eda); // give away ownership
>   }
>   DAGetArray(eda,PETSC_TRUE,&extrafield); // or FALSE if you don't   
> need ghost values
>
>   ...
>
>   DARestoreArray(eda,PETSC_TRUE,&extrafield);
>
>
> This way the memory is only allocated once, it will work with multiple
> levels, and get cleaned up when you are done.
>
> Jed
>
>



-- 
(Rebecca) Xuefei YUAN
Department of Applied Physics and Applied Mathematics
Columbia University
Tel:917-399-8032
www.columbia.edu/~xy2102



More information about the petsc-users mailing list