[petsc-dev] Which approach to modifying vectors from DMComplex is better?

Matthew Knepley knepley at gmail.com
Wed Sep 26 09:32:51 CDT 2012


On Wed, Sep 26, 2012 at 10:18 AM, Chris Eldred <chris.eldred at gmail.com>wrote:

> I have a vector (created from a PetscSection) that holds a set of
> values distributed over various points in my mesh. It represents
> multiple fields with different dofs. I was wondering which approach
> for modifying (or just getting) the values for a given field from the
> vector was preferred? The vector is created using
> DMCreateLocal/GlobalVector.
>
> Approach 1 (get the underlying array, find the points using
> PetscSection, do whatever, restore the array)
>

Yes, this is the way I work. However, it can be simpler, if you are making
more
structured changes, to use

  call DMComplexVecGet/SetClosure()


> call VecGetArrayF90( varvector , varptr , ierr)
> CHKERRQ(ierr)
> call DMComplexGetDepthStratum(model_mesh, vardepth , pStart, pEnd, ierr)
> CHKERRQ(ierr)
>

Here if you want the entire section its simpler to use

  call PetscSectionGetChart(varsection, pStart, pEnd, ierr)


> do p=pStart,pEnd-1
> call PetscSectionGetFieldOffset( varsection, p, varnum , poffset, ierr)
> CHKERRQ(ierr)
> call PetscSectionGetFieldDof( varsection, p, varnum , ndof, ierr)
> CHKERRQ(ierr)
> do something with the values (modify them, read them, etc.)
> enddo
> call VecRestoreArrayF90( varvector , varptr , ierr)
> CHKERRQ(ierr)
>
> Approach 2 (find the points using PetscSection, get the values using
> VecGetValues, do whatever, set the values using VecSetValues (if
> needed), call VecAssemblyBegin/End )
>

Definitely do not do this. Its cumbersome and adds nothing.


> call DMComplexGetDepthStratum(model_mesh, vardepth , pStart, pEnd, ierr)
> CHKERRQ(ierr)
> do p=pStart,pEnd-1
> call PetscSectionGetFieldOffset( varsection, p, varnum , poffset, ierr)
> CHKERRQ(ierr)
> call PetscSectionGetFieldDof( varsection, p, varnum , ndof, ierr)
> CHKERRQ(ierr)
> add points to a list (points are a contiguos list so its easy)- ie for
> each dof add poffset+dof to the list
> enddo
> call VecGetValues( varvector, npts, ptslist, vals, ierr)
> CHKERRQ(ierr)
> do something with the values (modify them, read them, etc.)
> call VecSetValues( varvector, npts, ptslist, vals, INSERT_MODE, ierr)
> !if need to
> CHKERRQ(ierr)
> call VecAssemblyBegin/End (if i set values)
> CHKERRQ(ierr)
>
> Instead of VecAssemblyBegin/End, should I be using the DMGlobaltoLocal
> / DMLocaltoGlobal routines?
>

With this setup there should never be a need for VecAssembly. Just use
GlobalToLocal or LocalToGlobal for communication.


> On a related note, should I be calling VecAssemblyBegin/End (or
> DMGlobaltoLocal, etc.) when I modify the values in the array using
> Approach 1?
>

No need, since anything you can modify in the array is process local.


> Is there another, third approach I should be using?
>
> -Chris
> --
> Chris Eldred
> DOE Computational Science Graduate Fellow
> Graduate Student, Atmospheric Science, Colorado State University
> B.S. Applied Computational Physics, Carnegie Mellon University, 2009
> chris.eldred at gmail.com
>



-- 
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-dev/attachments/20120926/a24958ba/attachment.html>


More information about the petsc-dev mailing list