[petsc-users] Setting Vec raw contiguous data pointer, VecPlaceArray
Mark Lohry
mlohry at princeton.edu
Fri May 13 12:33:35 CDT 2016
Sorry, disregard that. A few more minutes of looking seems like
VecCreateSeqWithArray is what I wanted and this should work:
void EigenToPetscVec(const Eigen::MatrixXd& emat,Vec& pvec){
VecCreateSeqWithArray(PESTC_COMM_SELF,1,emat.rows()*emat.cols(),emat.data,&pvec);
}
On 05/13/2016 01:13 PM, Mark Lohry wrote:
> I'm trying to interface to petsc with code using the Eigen C++ matrix
> library. I can get the raw data pointer for a petsc vec at have Eigen
> "map" that data without moving it using VecGetArray like so (correct
> me if anything is wrong here):
>
> void PetscVecToEigen(const Vec& pvec,unsigned int nrow,unsigned int
> ncol,Eigen::MatrixXd& emat){
> PetscScalar *pdata;
> // Returns a pointer to a contiguous array containing this
> processor's portion
> // of the vector data. For standard vectors this doesn't use any
> copies.
> // If the the petsc vector is not in a contiguous array then it will
> copy
> // it to a contiguous array.
> VecGetArray(pvec,&pdata);
> // Make the Eigen type a map to the data. Need to be mindful of
> anything that
> // changes the underlying data location like re-allocations.
> emat = Eigen::Map<Eigen::MatrixXd>(pdata,nrow,ncol);
> VecRestoreArray(pvec,&pdata);
> }
>
> and then I can use the Eigen::Matrix as usual, directly modifying the
> data in the petscvec.
>
> Is VecPlaceArray the inverse of this for setting the petsc vector data
> pointer? e.g. this function:
>
> void PetscEigenToVec(const Eigen::MatrixXd& emat,Vec& pvec){
> VecPlaceArray(pvec,emat.data());
> }
>
>
> How do I then set the corresponding petsc vec size? Would it just be
> VecSetSizes(pvec, emat.rows() * emat.cols(), PETSC_DECIDE)?
>
> Does VecSetSizes do any reallocations?
>
>
>
>
> Thanks in advance,
> Mark Lohry
More information about the petsc-users
mailing list