[petsc-users] Basic vector calculation question

hong at aspiritech.org hong at aspiritech.org
Wed Jan 27 12:02:15 CST 2016

Use VecGetArray()/VecRestoreArray(). They access arrays, do not create
any temporary
sequential vectors.


> Suppose I have four vectors A,B, C and D  with the same number of
> components. I would like to do the following component wise vector
> operation:
> (A – B)  / (C + D)
> I could calculate A-B and C+D  and store the results in temporary PETSc
> vector temp_result1 and temp_result2 (I need to keep A,B, C and D
> unchanged) and then call
> VecPointwiseDivide(temp_result1,temp_result1,temp_result2) and get my
> result in temp_result1.
> On the other hand, to avoid creating two temporary PETSc vectors, I could
> call VecGetArray()/VecRestoreArray() on the four vectors, iterate over the
> local indices and perform the same operations at once and store the result
> in another vector. What is the best way and why? I think that VecGetArray()
> creates temporary sequential vectors as well. I’m not sure if this is what
> the above mentioned PETSc routines internally do.
> Thanks
> Miguel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20160127/b60c8e0d/attachment.html>

More information about the petsc-users mailing list