making a package of PETSc vectors

33 views
Skip to first unread message

ito...@tamu.edu

unread,
Mar 23, 2017, 9:37:07 PM3/23/17
to deal.II User Group
Dear everybody:

I don't have any particular problem, just trying to streamline a code. I am have a mess with the interfaces of my code, in particular with the functions and classes I have created, primarily because I have to handle a ''packet'' of vectors, say 

    PETScWrappers::MPI::Vector  Vect1;
    PETScWrappers::MPI::Vector  Vect2;
    PETScWrappers::MPI::Vector  Vect3;
    PETScWrappers::MPI::Vector  Vect4;
    …
    PETScWrappers::MPI::Vector  Vect7;

and use them either as input or output arguments of some functions, solvers, complementary computations, etc. This is quite cumbersome, and as solution I naively thought I could do the following

    std::vector<PETScWrappers::MPI::Vector >  VectorPacket ;
    VectorPacket.resize(7) ;
    for (int i=0; i<7 ; ++i) 
      VectorPacket[i].reinit (locally_owned_dofs, mpi_communicator ) ;

So, now VectorPacket is really a packet, rather than seven isolated vectors. It is almost needless to say that this is bad idea, std::vector will demand quite a few things from the class PETScWrappers::MPI::Vector which are just not there (in particular some operators). In reality the size of the packet of vector is not always seven, which means that I really need to use a proper container for which I can change it's size. Do you have any suggestion (a.k.a. creative approach) on how to deal with an issue like this one? Which container from the STL would be appropriate?

Many thanks.

Jean-Paul Pelteret

unread,
Mar 24, 2017, 2:50:06 AM3/24/17
to deal.II User Group
Dear Itomas,

I would recommend going the route suggested in this recent post, namely creating a vector of shared pointers of PETSc vectors, i.e. std::vector<std::shared<PETScWrappers::MPI::Vector> >. Using shared pointers means that you safeguard against memory leaks, and can be stored as entries in a vector. That would give you the flexibility that you require with a minimal interface change to your code (apart from how you create the vectors initially, you would need to dereference these vector entries with the "->" operator instead of the "." operator). 

I hope that this helps.
Best regards,
Jean-Paul

Ignacio Tomas

unread,
Mar 24, 2017, 7:48:47 PM3/24/17
to dea...@googlegroups.com
Thanks, I will definitely try it.

Ignacio.

--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/rFm-6ZMYoU4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ignacio Tomas

unread,
Mar 24, 2017, 9:01:18 PM3/24/17
to dea...@googlegroups.com
I wanted to do something like (in reality I would do this with a for loop)

std::vector< std::shared_ptr<PETScWrappers::MPI::Vector> > VectorPacket ;
VectorPacket.resize(3) ;
VectorPacket[0].reset(new PETScWrappers::MPI::Vector) ;
VectorPacket[1].reset(new PETScWrappers::MPI::Vector) ;
VectorPacket[2].reset(new PETScWrappers::MPI::Vector) ;

and later initialize all of them (with a for loop too). Yet this does not seem to work. Otherwise

std::vector< std::shared_ptr<PETScWrappers::MPI::Vector> > VectorPacket ;
VectorPacket.resize(3) ;
VectorPacket[0].reset(&Vect1) ;
VectorPacket[1].reset(&Vect2) ;
VectorPacket[2].reset(&Vect3) ;

with Vect1, Vect2 and Vect3 defined/declared/initialized a priori works fine. But it comes with the overhead of definining these vectors separately rather than with the the vector of shared pointers. 

On Fri, Mar 24, 2017 at 1:50 AM, Jean-Paul Pelteret <jppel...@gmail.com> wrote:

--

ito...@tamu.edu

unread,
Mar 24, 2017, 9:08:49 PM3/24/17
to deal.II User Group
No worried, 

VectorPacket[0].reset(new PETScWrappers::MPI::Vector(locally_owned_dofs, mpi_communicator) ) ;

now did the job.

Thanks.
Reply all
Reply to author
Forward
0 new messages