Skip to content

Commit

Permalink
Merge pull request #1704 from isuruf/cereal
Browse files Browse the repository at this point in the history
Serialization using cereal
  • Loading branch information
isuruf committed Nov 10, 2021
2 parents 7f5055e + a953ef7 commit 4b841d1
Show file tree
Hide file tree
Showing 64 changed files with 11,615 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
symengine/utilities/cereal/include/cereal/* linguist-vendored=true
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ symengine/tests/basic/test_subs
symengine/tests/basic/test_rational
symengine/tests/basic/test_as_numer_denom
symengine/tests/basic/test_parser
symengine/tests/basic/test_serialize-cereal
symengine/tests/basic/test_cse
symengine/tests/matrix/test_matrix
symengine/tests/ntheory/test_ntheory
Expand Down Expand Up @@ -168,9 +169,12 @@ symengine/symengine_config_cling.h
# IDE related
/.idea
.*cache/
**.dir-locals.el
**.gdb_history
compile_commands.json
.vscode/

# Generated Documentation
gen_docs/
/docs/Doxygen/tags/
*.ipynb
*.ipynb
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@ if ((CMAKE_CXX_COMPILER_ID MATCHES Clang) AND (CMAKE_BUILD_TYPE STREQUAL "Releas
endif()

# Cotire
if ((CMAKE_VERSION VERSION_GREATER 2.8.11) AND
(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU|MSVC"))
set(WITH_COTIRE yes CACHE BOOL "Use cotire to speed up build")
else()
set(WITH_COTIRE no CACHE BOOL "Use cotire to speed up build")
endif()
set(WITH_COTIRE no CACHE BOOL "Use cotire to speed up build")

set(INTEGER_CLASS "gmp"
CACHE STRING "Integer class for symengine. Either gmp, gmpxx, flint or piranha")
Expand Down Expand Up @@ -318,6 +313,13 @@ if (WITH_MPC)
set(PKGS ${PKGS} "MPC")
endif()

if (WITH_SYSTEM_CEREAL)
find_package(CEREAL REQUIRED)
include_directories(SYSTEM ${CEREAL_INCLUDE_DIRS})
else()
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/symengine/utilities/cereal/include)
endif()

# PTHREAD
set(WITH_PTHREAD no
CACHE BOOL "Build with PTHREAD")
Expand Down
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,33 @@ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
-------------------------------------------------------------------------------

All files in symengine/utilities/cereal were taken from the Cereal package
and are licensed under the BSD-3-Clause license:

Copyright (c) 2014, Randolph Voorhies, Shane Grant
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of cereal nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ their default values indicated below:
-DWITH_MPFR:BOOL=OFF \ # Install with MPFR library
-DWITH_MPC:BOOL=OFF \ # Install with MPC library
-DWITH_LLVM:BOOL=OFF \ # Build with LLVM libraries
-DWITH_SYSTEM_CEREAL:BOOL=OFF \ # Build with cereal headers from the system instead of
the vendored copy
-DBUILD_TESTS:BOOL=ON \ # Build with tests
-DBUILD_BENCHMARKS:BOOL=ON \ # Build with benchmarks
-DBUILD_BENCHMARKS_GOOGLE:BOOL=OFF \ # Build with Google Benchmark benchmarks
Expand Down
1 change: 1 addition & 0 deletions bin/test_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if [[ "${WITH_SANITIZE}" != "" ]]; then
pwd; \
cmake --build . -j 2; \
cmake --build . --target install
cp /tmp/llvm-project-llvmorg-${LLVM_ORG_VER}/libcxxabi/include/* /opt/libcxx-12-msan/include/
)
if [ ! -e /opt/libcxx-12-msan/lib/libc++abi.so ]; then >&2 echo "Failed to build libcxx++abi?"; exit 1; fi
export MSAN_OPTIONS=print_stacktrace=1,halt_on_error=1,external_symbolizer_path=/usr/lib/llvm-12/bin/llvm-symbolizer
Expand Down
10 changes: 10 additions & 0 deletions cmake/FindCEREAL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
include(LibFindMacros)

libfind_include(cereal/types/vector.hpp cereal)

set(CEREAL_INCLUDE_DIRS ${CEREAL_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(CEREAL DEFAULT_MSG CEREAL_INCLUDE_DIRS)

mark_as_advanced(CEREAL_INCLUDE_DIR)
1 change: 1 addition & 0 deletions symengine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ set(HEADERS
real_double.h
real_mpfr.h
rings.h
serialize-cereal.h
series_flint.h
series_generic.h
series.h
Expand Down
53 changes: 53 additions & 0 deletions symengine/basic.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
#include <symengine/printers.h>
#include <symengine/subs.h>
#include <symengine/serialize-cereal.h>
#include <array>

namespace SymEngine
{

std::string type_code_name(TypeID id)
{
#define STRINGIFY0(x) #x
#define STRINGIFY(x) STRINGIFY0(x)
const static std::array<std::string,
static_cast<int>(TypeID::TypeID_Count) + 1>
type_names{{
#define SYMENGINE_INCLUDE_ALL
#define SYMENGINE_ENUM(type, Class) STRINGIFY(Class),
#include "symengine/type_codes.inc"
#undef SYMENGINE_ENUM
"TypeID_Count"}};
#undef SYMENGINE_INCLUDE_ALL
#undef STRINGIFY0
#undef STRINGIFY

if ((id < 0) || (id > TypeID::TypeID_Count)) {
throw std::runtime_error("type_id out of range");
}
return type_names[id];
}

int Basic::__cmp__(const Basic &o) const
{
auto a = this->get_type_code();
Expand All @@ -24,6 +48,35 @@ std::string Basic::__str__() const
return str(*this);
}

std::string Basic::dumps() const
{
std::ostringstream oss;
unsigned short major = SYMENGINE_MAJOR_VERSION;
unsigned short minor = SYMENGINE_MINOR_VERSION;
cereal::PortableBinaryOutputArchive{oss}(major, minor,
this->rcp_from_this());
return oss.str();
}

RCP<const Basic> Basic::loads(const std::string &serialized)
{
unsigned short major, minor;
RCP<const Basic> obj;
std::istringstream iss(serialized);
cereal::PortableBinaryInputArchive iarchive{iss};
iarchive(major, minor);
if (major != SYMENGINE_MAJOR_VERSION or minor != SYMENGINE_MINOR_VERSION) {
throw SerializationError(StreamFmt()
<< "SymEngine-" << SYMENGINE_MAJOR_VERSION
<< "." << SYMENGINE_MINOR_VERSION
<< " was asked to deserialize an object "
<< "created using SymEngine-" << major << "."
<< minor << ".");
}
iarchive(obj);
return obj;
}

RCP<const Basic> Basic::subs(const map_basic_basic &subs_dict) const
{
return SymEngine::subs(this->rcp_from_this(), subs_dict);
Expand Down
8 changes: 8 additions & 0 deletions symengine/basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ enum TypeID {
TypeID_Count
};

std::string type_code_name(TypeID id);

#include "basic-methods.inc"

class Visitor;
Expand Down Expand Up @@ -179,6 +181,12 @@ class Basic : public EnableRCPFromThis<Basic>
*/
std::string __str__() const;

//! Returns a string of the instance serialized.
std::string dumps() const;

//! Creates an instance of a serialized string.
static RCP<const Basic> loads(const std::string &);

//! Substitutes 'subs_dict' into 'self'.
RCP<const Basic> subs(const map_basic_basic &subs_dict) const;

Expand Down
1 change: 1 addition & 0 deletions symengine/prime_sieve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <valarray>
#include <algorithm>
#include <vector>
#include <iterator>

namespace SymEngine
{
Expand Down

0 comments on commit 4b841d1

Please sign in to comment.