[petsc-users] DMDAVecGetArray vs DMDAVecRestoreArray

Barry Smith bsmith at mcs.anl.gov
Thu Aug 18 17:49:58 CDT 2011


On Aug 18, 2011, at 5:02 PM, Alan Wei wrote:

> Dear all,
>     I hope you're having a nice day.
>     I'm trying to get the x, y-value of a coordinate created by DMDACreate2d. Lines I wrote down are:
> 
>   DMDAGetCoordinateDA(da, &cda);
>   DMDAGetGhostedCoordinates(da, &gc);
>   DMDAVecGetArray(cda, gc, &coors);
> 
>   for (i = xs; i < xs+xm; i++) {
>     for (j = ys; j < ys+ym; j++) {
>       printf("%f  %f\n", coors[j][i].x, coors[j][i].y);
>     }
>   }
> 
>   DMDAVecRestoreArray(cda, gc, &coors);
> 
> I wonder why I need to use "DMDAVecRestoreArray(cda, gc, &coors);" I used it because the manual said I need. Actually, I tried to comment it out and there is no difference.
> 
> Thanks,
> Alan

   The practical reason is you might get memory bleeding if you don't.  The philosphical reason is that a call DMDAVecGetArray() or VecGetArray() or others is a request to access the internal data structure of an object, the object then gives you permission to access that data. the RestoreArray() is a statement by you that after this call you will NOT access that data directly (without another GetArray call). Imagine if you did

    Barry


     



More information about the petsc-users mailing list