]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Improve serialization of SD Expressions
authorJean-Paul Pelteret <jppelteret@gmail.com>
Sun, 19 Apr 2020 13:11:39 +0000 (15:11 +0200)
committerJean-Paul Pelteret <jppelteret@gmail.com>
Mon, 4 May 2020 13:46:02 +0000 (15:46 +0200)
include/deal.II/differentiation/sd/symengine_number_types.h

index fc3578a3e153b692251c50dcf0354ead965a9229..246f8884d9b5469c6a9338087861d7b6a14b94aa 100644 (file)
@@ -51,6 +51,8 @@
 #  include <deal.II/differentiation/sd/symengine_number_traits.h>
 #  include <deal.II/differentiation/sd/symengine_types.h>
 
+#  include <boost/serialization/split_member.hpp>
+
 #  include <symengine/derivative.h>
 
 #  include <algorithm>
@@ -450,11 +452,39 @@ namespace Differentiation
       void
       load(std::istream &stream);
 
+      /**
+       * Write the data of this object to a stream for the purpose
+       * of serialization.
+       *
+       * This effectively saves the value stored in this object to the
+       * @p archive with the given @p version number.
+       */
+      template <class Archive>
+      void
+      save(Archive &archive, const unsigned int version) const;
+
+      /**
+       * Read the data of this object from a stream for the purpose
+       * of serialization.
+       *
+       * This effectively loads into this object the value stored in of the
+       * @p archive with the given @p version number.
+       * In doing so, the previous contents of this object are thrown away.
+       *
+       * @note When deserializing a symbolic expression, it is imperative that
+       * you first create or deserialize all of the symbolic variables used in
+       * the serialized expression.
+       */
+      template <class Archive>
+      void
+      load(Archive &archive, const unsigned int version);
+
+#  ifdef DOXYGEN
       /**
        * Write and read the data of this object from a stream for the purpose
        * of serialization.
        *
-       * This effecively saves or loads the value stored into/out of the
+       * This effectively saves or loads the value stored into/out of the
        * @p archive with the given @p version number into this object.
        * If deserializing data, then the previous contents of this object
        * are thrown away.
@@ -466,6 +496,11 @@ namespace Differentiation
       template <class Archive>
       void
       serialize(Archive &archive, const unsigned int version);
+#  else
+      // This macro defines the serialize() method that is compatible with
+      // the templated save() and load() method that have been implemented.
+      BOOST_SERIALIZATION_SPLIT_MEMBER()
+#  endif
 
       //@}
 
@@ -1178,41 +1213,22 @@ namespace Differentiation
 
     template <class Archive>
     void
-    Expression::serialize(Archive &ar, const unsigned int /*version*/)
+    Expression::save(Archive &ar, const unsigned int /*version*/) const
     {
-      // This is a bit tricky... The SymEngine expression class is not
-      // serializable, and we're not sure if we're writing out or
-      // reading in here. So what we'll do is try to synchronise the
-      // current data with the stream, predicting if we're likely trying
-      // to write out data. We do this by checking if we're currently working
-      // with an object that has a trivial state. If not, then we're likely
-      // to output this object's contents.
-      const Expression default_constructed;
-      const bool       likely_writing_out =
-        (get_RCP()->__eq__(*default_constructed.get_RCP()) == false);
-
       std::stringstream sstream;
-      std::string       expr = default_constructed.get_RCP()->__str__();
-
-      // Output
-      if (likely_writing_out)
-        {
-          sstream << *this;
-          expr = sstream.str();
-        }
-
-      // Serialise
-      ar &expr;
-
-      // Input
-      if (!likely_writing_out)
-        {
-          sstream.clear();
-          sstream << expr;
-          sstream >> *this;
-
-          parse(expr);
-        }
+      sstream << *this;
+      const std::string expr = sstream.str();
+      ar &              expr;
+    }
+
+
+    template <class Archive>
+    void
+    Expression::load(Archive &ar, const unsigned int /*version*/)
+    {
+      std::string expr;
+      ar &        expr;
+      parse(expr);
     }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.