[petsc-users] Store type (Eigen::Vector2d) in a petsc vec

Jed Brown jed at jedbrown.org
Tue Feb 5 08:59:19 CST 2019


Andrew Parker <andy.john.parker at gmail.com> writes:

> Thanks, so you would suggest a flat vector storing u, v, w (or indeed x, y,
> z) or interleaved and then construct eigen types on the fly?  

Interleaved if you want to use Eigen types in the same memory, or if
your code (like most applications) benefits more from memory locality
than from vectorization.  You can cast because Matrix<double,2,1> has
the same storage representation as

struct {
  double values[2];
};

> Can I ask, is that because Vec cannot store user defined types (as in
> it's not templatetable?)

PETSc does not use templates, but you wouldn't want it to in this
circumstance.  Either your types have the same storage representation
and you can cast or they don't and templating would ruin what are now
contiguous operations.


More information about the petsc-users mailing list