MatGetArray()
is an advanced (should be developer) level routine that depends on the matrix format and is best avoided. It is only used with dense matrices within PETSc; there is no reason to use it otherwise. It's existence comes from users that stubbornly insist on having the function, not from meaningful use cases.
VecGetArray()
, on the other hand, is the recommended way to write nonlinear residuals and other functions that need direct access to vector entries. You can use a VecScatter
to obtain a "local" vector that contains ghost points needed during assembly, and to assemble contributions from element-partitioned subdomains (as with finite element methods). There are many examples of this in PETSc, many use DMDAVecGetArray()
to also provide convenient indexing.