[petsc-users] Correct use of VecGetArray with std::vector
Jed Brown
Jed.Brown at Colorado.EDU
Fri Feb 28 14:14:03 CST 2020
Matthew Knepley <knepley at gmail.com> writes:
> On Fri, Feb 28, 2020 at 2:12 PM Zane Charles Jakobs <
> Zane.Jakobs at colorado.edu> wrote:
>
>> Hi PETSc devs,
>>
>> I'm writing some C++ code that calls PETSc, and I'd like to be able to
>> place the result of VecGetArray into an std::vector and then later call
>> VecRestoreArray on that data, or get the same effects. It seems like the
>> correct way to do this would be something like:
>>
>
> Why are you calling Get/SetValues() instead Get/SetArray()? Shouldn't you
> just get the pointer using GetArray() and stick it into
> your std::vector?
Can't do this because std::vector can't be wrapped around existing memory.
I would recommend not using std::vector. Dynamic resizing isn't a
feature you want in this context, and since you'd like to use existing
memory, you need to use a container that can accept existing memory.
More information about the petsc-users
mailing list