Skip to Content.
Sympa Menu

charm - Re: [charm] [ppl] Pupping a vector of pointers

charm AT lists.siebelschool.illinois.edu

Subject: Charm++ parallel programming system

List archive

Re: [charm] [ppl] Pupping a vector of pointers


Chronological Thread 
  • From: Orion Lawlor <lawlor AT alaska.edu>
  • To: Robert Steinke <rsteinke AT uwyo.edu>
  • Cc: Charm Mailing List <charm AT cs.illinois.edu>
  • Subject: Re: [charm] [ppl] Pupping a vector of pointers
  • Date: Thu, 5 Mar 2015 13:59:59 -0900
  • List-archive: <http://lists.cs.uiuc.edu/pipermail/charm/>
  • List-id: CHARM parallel programming system <charm.cs.uiuc.edu>

Hi, Bob!  For your use case, pup'ing a vector of pointers to derived classes, the big problem is this:
       pointer = new T;

When T is a base class, when unpacking you'll end up making an instance of the base class instead of making a derived class, resulting in a pup mismatch or silently replacing derived with base class behavior.

There's actually an old, undocumented, and fairly hairy solution to this called PUP::able, that registers classes using a "PUP_ID" so the other side knows which constructor to call:
     http://charm.cs.uiuc.edu/doxygen/charm/pup_8h-source.shtml#l00625

I only know about it because I wrote it over ten years ago, and I'm not even sure if it still works, much less how!

On Thu, Mar 5, 2015 at 1:10 PM, Robert Steinke <rsteinke AT uwyo.edu> wrote:
Something came up in my code that I think might be generally useful.  I want to pup a std::vector of pointers.  The objects that the pointers point to have pup routines.  The code in pup_stl.h, in the process of pupping the vector, tries to pup each element in the vector.  But the elements in the vector aren't the puppable objects.  They are pointers to those objects.  I came up with this solution:

// This is a general function to pup a puppable object via a pointer to it.
template <typename T> inline void operator|(PUP::er &p, T* &pointer)
{
  bool notNull = (NULL != pointer);

  p | notNull;

  if (notNull)
    {
      if (p.isUnpacking())
        {
          pointer = new T;
        }

      p | *pointer;
    }
  else
    {
      pointer = NULL;
    }
}

It seems like this could be generally useful for any situation where you want to pup a pointer to a puppable object.  Would you consider this as an addition to the Charm++ codebase?



BTW, the reason my code has a std::vector of pointers is that I want a vector of objects of various subclasses of a single base class. To do this I have a vector of pointers to the base class pointing to dynamically allocated objects of the various subclasses.

Bob Steinke

_______________________________________________
charm mailing list
charm AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/charm
_______________________________________________
ppl mailing list
ppl AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/ppl



--
Dr. Orion Sky Lawlor   lawlor AT alaska.edu
http://www.cs.uaf.edu/~olawlor/



Archive powered by MHonArc 2.6.16.

Top of Page