Skip to content

Commit

Permalink
Cereal rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
OXPHOS authored and vigsterkr committed Apr 17, 2018
1 parent 8da4063 commit 7bbf097
Show file tree
Hide file tree
Showing 15 changed files with 803 additions and 157 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Expand Up @@ -403,6 +403,14 @@ IF (GDB_FOUND)
SET(GDB_DEFAULT_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/src/.gdb)
ENDIF()

FIND_PACKAGE(Cereal)
IF(NOT CEREAL_FOUND)
include(external/Cereal)
LIST(APPEND INCLUDES ${CEREAL_INCLUDE_DIRS})
ELSE()
LIST(APPEND INCLUDES ${CEREAL_INCLUDE_DIRS})
ENDIF()

FIND_PACKAGE(Doxygen 1.8.6)
IF(DOXYGEN_FOUND)
SET(HAVE_DOXYGEN 1)
Expand Down
19 changes: 19 additions & 0 deletions cmake/FindCereal.cmake
@@ -0,0 +1,19 @@
# - Try to find Cereal Serialization Library
#
# This sets the following variables:
# CEREAL_FOUND - True if Cereal was found.
# CEREAL_INCLUDE_DIRS - Directories containing the Cereal include files.

find_path(CEREAL_INCLUDE_DIR cereal
HINTS "$ENV{CMAKE_SOURCE_DIR}/include" "/usr/include" "$ENV{CMAKE_BINARY_DIR}/cereal/include")

set(CEREAL_INCLUDE_DIRS ${CEREAL_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Cereal DEFAULT_MSG CEREAL_INCLUDE_DIR)

mark_as_advanced(CEREAL_INCLUDE_DIR)

if(CEREAL_FOUND)
MESSAGE(STATUS "Found Cereal: ${CEREAL_INCLUDE_DIRS}")
endif(CEREAL_FOUND)
14 changes: 14 additions & 0 deletions cmake/external/Cereal.cmake
@@ -0,0 +1,14 @@
include(ExternalProject)
ExternalProject_Add(
Cereal
PREFIX ${CMAKE_BINARY_DIR}/Cereal
DOWNLOAD_DIR ${THIRD_PARTY_DIR}/Cereal
URL https://github.com/USCiLab/cereal/archive/v1.2.0.tar.gz
URL_MD5 e372c9814696481dbdb7d500e1410d2b
CMAKE_ARGS -DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS}${CMAKE_DEFINITIONS}
-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}${CMAKE_DEFINITIONS}
INSTALL_COMMAND ""
)

SET(CEREAL_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/Cereal/src/Cereal/include)
LIST(APPEND SHOGUN_DEPENDS Cereal)
20 changes: 20 additions & 0 deletions src/shogun/base/AnyParameter.h
Expand Up @@ -60,6 +60,16 @@ namespace shogun
return m_gradient;
}

/** serialize the object using cereal
*
* @param ar Archive type
*/
template<class Archive>
void serialize(Archive& ar)
{
ar(m_model_selection, m_gradient);
}

private:
std::string m_description;
EModelSelectionAvailability m_model_selection;
Expand Down Expand Up @@ -112,6 +122,16 @@ namespace shogun
return !(*this == other);
}

/** serialize the object using cereal
*
* @param ar Archive type
*/
template<class Archive>
void serialize(Archive& ar)
{
ar(m_value, m_properties);
}

private:
Any m_value;
AnyParameterProperties m_properties;
Expand Down
77 changes: 77 additions & 0 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -32,6 +32,15 @@
#include <rxcpp/rx-lite.hpp>

#include <algorithm>

#include <cereal/types/map.hpp>
#include <cereal/types/unordered_map.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/archives/json.hpp>
#include <cereal/archives/xml.hpp>
#include <cereal/types/polymorphic.hpp>
#include <fstream>

#include <unordered_map>
#include <memory>

Expand Down Expand Up @@ -235,6 +244,74 @@ int32_t CSGObject::unref()
}
}

void CSGObject::save_binary(const char* filename) const
{
std::ofstream os(filename);
cereal::BinaryOutputArchive archive(os);
archive(cereal::make_nvp(this->get_name(), *this));
}

void CSGObject::save_json(const char* filename) const
{
std::ofstream os(filename);
cereal::JSONOutputArchive archive(os);
archive(cereal::make_nvp(this->get_name(), *this));
}

void CSGObject::save_xml(const char* filename) const
{
std::ofstream os(filename);
cereal::XMLOutputArchive archive(os);
archive(cereal::make_nvp(this->get_name(), *this));
}

void CSGObject::load_binary(const char* filename)
{
std::ifstream is(filename);
cereal::BinaryInputArchive archive(is);
archive(*this);
}

void CSGObject::load_json(const char* filename)
{
std::ifstream is(filename);
cereal::JSONInputArchive archive(is);
archive(*this);
}

void CSGObject::load_xml(const char* filename)
{
std::ifstream is(filename);
cereal::XMLInputArchive archive(is);
archive(*this);
}

template <class Archive>
void CSGObject::cereal_save(Archive& ar) const
{
for (const auto& it : self->map)
ar(cereal::make_nvp(it.first.name(), it.second));
}
template void CSGObject::cereal_save<cereal::BinaryOutputArchive>(
cereal::BinaryOutputArchive& ar) const;
template void CSGObject::cereal_save<cereal::JSONOutputArchive>(
cereal::JSONOutputArchive& ar) const;
template void CSGObject::cereal_save<cereal::XMLOutputArchive>(
cereal::XMLOutputArchive& ar) const;

template <class Archive>
void CSGObject::cereal_load(Archive& ar)
{
for (auto& it : self->map)
ar(it.second);
}
template void CSGObject::cereal_load<cereal::BinaryInputArchive>(
cereal::BinaryInputArchive& ar);
template void
CSGObject::cereal_load<cereal::JSONInputArchive>(cereal::JSONInputArchive& ar);
template void
CSGObject::cereal_load<cereal::XMLInputArchive>(cereal::XMLInputArchive& ar);

#ifdef TRACE_MEMORY_ALLOCS
#include <shogun/lib/Map.h>
extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;
Expand Down
52 changes: 52 additions & 0 deletions src/shogun/base/SGObject.h
Expand Up @@ -140,6 +140,58 @@ class CSGObject
/** destructor */
virtual ~CSGObject();

#ifndef SWIG // SWIG should skip this part
/** serializes the SGObject to a binary file
*
* @param filename Binary archive filename
*/
void save_binary(const char* filename) const;

/** serializes the SGObject to a JSON file
*
* @param filename JSON archive filename
*/
void save_json(const char* filename) const;

/** serializes the SGObject to a XML file
*
* @param filename XML archive filename
*/
void save_xml(const char* filename) const;

/** loads SGObject from a Binary file
*
* @param filename Binary archive filename
*/
void load_binary(const char* filename);

/** loads SGObject from a JSON file
*
* @param filename JSON archive filename
*/
void load_json(const char* filename);

/** loads SGObject from a XML file
*
* @param filename XML archive filename
*/
void load_xml(const char* filename);

/** serializes SGObject parameters to Archive with Cereal
*
* @param ar Archive
*/
template <class Archive>
void cereal_save(Archive& ar) const;

/** loads SGObject parameters from Archive with Cereal
*
* @param ar Archive
*/
template <class Archive>
void cereal_load(Archive& ar);
#endif // #ifndef SWIG // SWIG should skip this part

/** increase reference counter
*
* @return reference count
Expand Down

0 comments on commit 7bbf097

Please sign in to comment.