<div dir="ltr"><div>Hi PETSc devs,</div><div><br></div><div>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:</div><div><br></div><div>Vec x;</div><div>std::vector<PetscScalar> vals, idx;</div><div>int   num_vals, global_offset;<br></div><div>PetscErrorCode ierr;<br></div><div>...</div><div>/* do some stuff to x and compute num_vals  and global_offset*/</div><div>...</div><div>vals.resize(num_vals);</div><div>idx.resize(num_vals);</div><div>std::iota(idx.begin(), idx.end(), global_offset);<br></div><div>ierr = VecGetValues(x, num_vals, idx.data(), vals.data());CHKERRQ(ierr);</div><div>/* do stuff to vals */</div><div>...</div><div>ierr = VecSetValues(x, num_vals, idx.data(), vals.data(), [whatever insert mode]);CHKERRQ(ierr);</div><div>idx.clear();</div><div>vals.clear();<br></div><div><br></div><div>Is that correct (in the sense that it does what you'd expect if you replaced the vectors with pointers to indices/data and used VecGet/RestoreArray() instead of VecGet/SetValues, and it doesn't violate any of std::vector's invariants, e.g. by reallocating its memory)? If not, is there a "normal" way to do this?</div><div><br></div><div>Thanks!<br></div><div><br></div><div>-Zane Jakobs<br></div><div><br></div><div><br></div></div>