Broadcasting user types containing smart pointers

87 views
Skip to first unread message

Paras Kumar

unread,
Mar 5, 2024, 9:46:14 AM3/5/24
to deal.II User Group
Dear deal.II Community,

Within the context of an MPI parallel code, I need to broadcast the ConstitutiveParameters struct which contains std::unique pointer to the base class StrainEnergyFuncParameters; cf. MWE file.

Using the ideas  discussed in the post https://groups.google.com/g/dealii/c/bmqTbs4ofHQ/m/aCuDpUekBAAJ, the code works fine if the members are concrete derived classes since there the serialize function is enough. However, I get segmentation fault during the unpack() call since the deserialize() function is not called.

Running the attached MWE would explain the issue.

Could someone please help in understanding how I could solve this issue?

Thanks and best regards,
Paras Kumar
mwe-deserialization.cc

Wolfgang Bangerth

unread,
Mar 5, 2024, 11:40:47 AM3/5/24
to dea...@googlegroups.com
Yes, this can't work :-) In essence, you are packing up a pointer (=an address
into memory space) on machine A, and unpack it on machine B. But on machine B,
nothing useful is likely going to be stored at the location of the memory
address at which there was a useful object on machine A, and so when you
access the data stored at that location, you should not expect anything good
to happen.

You need to find a way to serialize/deserialize the data pointed to, not the
pointer itself.

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/


Paras Kumar

unread,
Mar 5, 2024, 11:59:45 AM3/5/24
to dea...@googlegroups.com
Dear Wolfgang,

Thank you for your response. That is exactly what I am trying to do via the deserialize function. I build an object of the desired derived type (based on the derived type data archived during the call to serialize()) and then assign it to the pointer at the receiving end. But the problem is that the deserialize function is never called by boost. I am not sure what hint it needs so as to call the deserialize function. Also, interestingly boost does not call both serialize() or deserialize() during the call to dealii::Utilities::unpack() but it does call serialize() during the call to dealii::Utilities::pack().

Any clues on how to achieve this would be really helpful.

Best regards,
Paras

--
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 the Google Groups "deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/e7423abb-b213-4575-b407-12d3a2d7d0f8%40colostate.edu.

Wolfgang Bangerth

unread,
Mar 5, 2024, 6:36:43 PM3/5/24
to dea...@googlegroups.com
On 3/5/24 09:59, Paras Kumar wrote:
>
> Thank you for your response. That is exactly what I am trying to do via the
> deserialize function. I build an object of the desired derived type (based on
> the derived type data archived during the call to serialize()) and then assign
> it to the pointer at the receiving end. But the problem is that the
> deserialize function is never called by boost. I am not sure what hint it
> needs so as to call the deserialize function. Also, interestingly boost does
> not call both serialize() or deserialize() during the call to
> dealii::Utilities::unpack() but it does call serialize() during the call to
> dealii::Utilities::pack().

You've got the wrong function names. You either have to provide a serialize()
function, or a pair of functions called save()/load(). In the latter case, you
also have to say BOOST_SERIALIZATION_SPLIT_MEMBER(). Take a look at classes
such as AlignedVector, for example. The BOOST documentation also has a lot to
say about this.

Paras Kumar

unread,
Mar 12, 2024, 10:22:02 AM3/12/24
to dea...@googlegroups.com
Thank you for the hints. It works now.

Best regards,
Paras

--
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 the Google Groups "deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dealii+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages