[petsc-users] accessing DMDA Vec ghost values

Matthew Knepley knepley at gmail.com
Thu May 12 10:55:53 CDT 2016


On Thu, May 12, 2016 at 10:50 AM, Sean Dettrick <
sdettrick at trialphaenergy.com> wrote:

>
>
> From: <petsc-users-bounces at mcs.anl.gov> on behalf of Dave May <
> dave.mayhem23 at gmail.com>
> Date: Thursday, May 12, 2016 at 2:48 AM
> To: Sean Dettrick <sdettrick at trialphaenergy.com>
> Cc: "petsc-users at mcs.anl.gov" <petsc-users at mcs.anl.gov>
> Subject: Re: [petsc-users] accessing DMDA Vec ghost values
>
>
>
> On 12 May 2016 at 10:42, Sean Dettrick <sdettrick at trialphaenergy.com>
> wrote:
>
>> Hi,
>>
>> When discussing DMDAVecGetArrayDOF etc in section 2.4.4,  the PETSc 3.7
>> manual says "The array is accessed using the usual global indexing on
>> the entire grid, but the user may only refer to the local and ghost
>> entries of this array as all other entries are undefined”.
>>
>> OK so far.  But how to access the ghost entries?
>>
>> With a 2D DMDA, I can do this OK:
>>
>>
>>         PetscInt    xs,xm,ys,ym;
>>
>>         ierr=DMDAGetCorners(da,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr);
>>
>>         PetscScalar ***es;
>>
>>         ierr=DMDAVecGetArrayDOF(da,Es,&es);CHKERRQ(ierr);
>>
>>
>>         for (int j=ys; j < ys+ym; j++) {
>>
>>             for (int i=xs; i < xs+xm;i++) {
>>
>> es[j][i][0]=1.;
>>
>>                 es[j][i][1]=1.;
>>
>>             }
>>
>>         }
>>
>>         ierr=DMDAVecRestoreArrayDOF(da,Es,&es);CHKERRQ(ierr);
>>
>> But if I replace DMDAGetCorners with DMDAGetGhostCorners, then the code
>> crashes with a seg fault, presumably due to out of bounds memory access.
>>
>> Is that supposed to happen?
>>
>
> If you created the vector Es using
> the function DM{Get,Create}GlobalVector(), then the answer is yes.
>
>
>> What’s the remedy?
>>
>
> If you want to access the ghost entries, you need to create the vector
> using the function DM{Get,Create}LocalVector().
>
>
>
>
> Thanks!  Somehow I missed DM{Get,Create}LocalVector().  BTW what is the
> difference between the Get and Create versions?  It is not obvious from the
> documentation.
>

Create gives back a vector that you own and need to VecDestroy(). Get gives
back a vector we own and you need to call Restore.


> Also, can you explain the difference between DMDAVecGetArrayDOF and
> DMDAVecGetArrayDOFRead?
>

One allows writing into the storage, whereas the other does not.

  Matt


> Thanks again,
> Sean
>
>
>
> Thanks,
>   Dave
>
>
>
>>
>> Thanks very much!
>>
>> Sean Dettrick
>>
>>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20160512/020a8dd3/attachment-0001.html>


More information about the petsc-users mailing list