-
Notifications
You must be signed in to change notification settings - Fork 0
README_cereal
- Motivation
- [For SHOGUN developers](#For SHOGUN developers)
- [Examples] (#Examples)
- [For serialization framework developers] (#For serialization framework developers)
- [Serialization interface] (#Serialization interface)
- [Serialization methods in
SGObject] (#Serialization methods inSGObject) - [Serialization methods in
Any] (#Serialization methods inAny) - [Serialization methods in
SGVector,SGMatirxandSGReferencedData] (#Serialization methods inSGVector,SGMatirxandSGReferencedData)
Cereal is a header-only C++11 serialization library that is fast, light-weight, and easy to extend.
The Cereal serialization framework in SHOGUN uses the new tag parameter framework, which allows the easy and readable archive of SGObject data.
-
Cerealserialization library is required for SHOGUN compilation. If noCerealis found, SHOGUN will automatically download the library tothird_party/. -
SHOGUN now supports the serialization of data into 3 formats: binary, XML, and JSON archives. The 3 pairs of save/load methods can be called by:
save_binary(filename);
save_json(filename);
save_xml(filename);
load_binary(filename);
load_json(filename);
load_xml(filename);
- All parameters saved in tag parameter list for one
SGObjectcan be saved and load by:
SGObject obj_save;
obj_save.save_json(filename);
SGObject obj_load;
obj_load.load_json(filename);
- Customized archives can be added as shown here
CCerealObject class defined in tests/unit/io/CerealObject.h is a SGObject-based class used for Cereal serialization unit tests.
We also use CCerealObject here to show how to serialize SGObject in SHOGUN.
In CCerealObject, we initialized a member SGVector<float64_t> m_vector and regisitered it to the parameter list in constructors:
#include <shogun/base/SGObject.h>
#include <shogun/lib/SGVector.h>
using namespace shogun;
class CCerealObject : public CSGObject
{
public:
// Construct CCerealObject from input SGVector
CCerealObject(SGVector<float64_t> vec) : CSGObject()
{
m_vector = vec;
init_params();
}
// Default constructor
CCerealObject() : CSGObject()
{
m_vector = SGVector<float64_t>(5);
m_vector.set_const(0);
init_params();
}
const char* get_name() const { return "CerealObject"; }
protected:
// Register m_vector to parameter list with name(tag) "test_vector"
void init_params()
{
register_param("test_vector", m_vector);
}
SGVector<float64_t> m_vector;
}
m_vector will be archived if we call serialization methods on CCerealObject instance.
#include "CerealObject.h"
#include <shogun/lib/SGVector.h>
using namespace shogun;
// Create a CCerealObject instance with assigned SGVector values
SGVector<float64_t> vec;
vec.range_fill(1.0);
CCerealObject obj_save(vec);
// Serialization
obj_save.save_json("serialization_test_json.cereal");
// Create another CCerealObject instance for data loading
CCerealObject obj_load();
obj_load.load_json("serialization_test_json.cereal");
// We can extract the loaded parameter:
SGVector<float64_t> vec_load;
vec_load = obj_load.get<SGVector<float64_t>>("test_vector");
The JSON file serialization_test_json.cereal will be:
{
"CerealObject": { // Class name
"test_vector": { // The tag of the parameter to be saved
"value0": 2, // Container type for internal use
"value1": 12, // Primitive type for internal use
"value2": { // Data to archive
"ReferencedData": { // Reference Data
"ref_counting": true,
"refcount number": 3
},
"length": 5, // Length of the vector
"value1": 0, // values of the vector
"value2": 1,
"value3": 2,
"value4": 3,
"value5": 4
}
}
}
}
The serialization framework has two components:
-
serialization interfaces implemented in
SGObejct, and -
serialization (load/save) methods implemented in
SGObjectand non-SGObjectbased data structrues.
- The
save_binary()method inSGObject.hgenerates ancereal::BinaryOutputArchiveobject and savesSGObjectto binary file by callingcereal_save()method inSGObject.load_binary()method generates ancereal::BinaryInputArchiveobject and loads the parameters from binary file back toSGObjectby callingcereal_load()method inSGObject. The ideas are the same for JSON and XML archives.
-
cereal_save()method iterates through the parameter list ofSGObjectregistered asself::map, archives thename value pair, with name asbasetag.name()and value by callingany.cereal_save(). -
cereal_load()method iterates through the parameter list and reset the parameter by callingany.cereal_load()
- Namespace
serialand objectserial::DataType m_datatypeinAny.hsave and convert the data type of the value of parameters inAnyconstructors intoEnum.
enum EnumContainerType
{
CT_UNDEFINED,
CT_PRIMITIVE,
CT_SGVECTOR,
CT_SGMATRIX
};
enum EnumPrimitiveType
{
PT_UNDEFINED,
PT_BOOL_TYPE,
PT_CHAR_TYPE,
PT_INT_8,
PT_UINT_8,
PT_INT_16,
PT_UINT_16,
PT_INT_32,
PT_UINT_32,
PT_INT_64,
PT_UINT_64,
PT_FLOAT_32,
PT_FLOAT_64,
PT_FLOAT_MAX,
PT_COMPLEX_128,
};
-
Cereal_save()together withcereal_save_helper()methods cast the objectstorageto its input type and archives the value. -
Cereal_load()together withcereal_load_helper()methods read the saved value back tostorageand reset thepolicybased on the data type.
Both SGVector and SGMatirx are derived from SGReferencedData class.
-
SGReferencedDataarchives whetherref_countingis on by savingtrue/false, and theref_countingvalue ifm_refcount != NULL, i.e.ref_countingis on. -
SGVectorandSGMatrixarchiveref_countingvalue by calling base class load/save methods:cereal::base_class<SGReferencedData>(this)(Introduction). ForSGVector, length and vector values are archived, while forSGMatrix, row number, column number, and matrix values inT* matrixare archived. Data ofcomplex128_ttype is casted tofloat64_ttype before archiving.
Welcome to the Shogun wiki!
-
[quick link GSoC 2016 projects](Google Summer of Code 2016 Projects)
-
Readmes:
-
Documents
-
[Roadmaps](Project roadmaps)
-
GSoC
- Getting involved
- Follow ups
- [2016 projects](Google Summer of Code 2016 Projects)
-
Credits