Refactor serialization and fix it.

This commit is contained in:
Ryan Curtin
2020-04-17 12:17:36 -04:00
parent fba38165a8
commit f5b283a6a0
4 changed files with 24 additions and 11 deletions
+2 -2
View File
@@ -69,8 +69,8 @@ function(append_serialization SERIALIZATION_FILE PROGRAM_NAME PROGRAM_MAIN_FILE)
"${SERIALIZATION_FILE}"
"serialize(stream::IO, model::${MODEL_SAFE_TYPE}Ptr) =\n"
" _Internal.${PROGRAM_NAME}_internal.serialize${MODEL_SAFE_TYPE}Ptr(stream, model)\n"
"deserialize!(model::${MODEL_SAFE_TYPE}Ptr, stream::IO) =\n"
" model = _Internal.${PROGRAM_NAME}_internal.deserialize${MODEL_SAFE_TYPE}Ptr(stream)\n"
"_deserialize_internal(model::${MODEL_SAFE_TYPE}Ptr, stream::IO) =\n"
" _Internal.${PROGRAM_NAME}_internal.deserialize${MODEL_SAFE_TYPE}Ptr(stream)\n"
"\n")
endif ()
endforeach ()
+10 -4
View File
@@ -62,7 +62,7 @@ void* CLI_GetParam${MODEL_SAFE_TYPE}Ptr(const char* paramName);
// Set the pointer to a ${MODEL_TYPE} parameter.
void CLI_SetParam${MODEL_SAFE_TYPE}Ptr(const char* paramName, void* ptr);
// Serialize a ${MODEL_TYPE} pointer.
const char* Serialize${MODEL_SAFE_TYPE}Ptr(void* ptr, size_t* length);
char* Serialize${MODEL_SAFE_TYPE}Ptr(void* ptr, size_t* length);
// Deserialize a ${MODEL_TYPE} pointer.
void* Deserialize${MODEL_SAFE_TYPE}Ptr(const char* buffer, const size_t length);
")
@@ -88,11 +88,16 @@ const char* Serialize${MODEL_SAFE_TYPE}Ptr(void* ptr, size_t* length)
std::ostringstream oss;
{
boost::archive::binary_oarchive oa(oss);
oa << ((${MODEL_TYPE}*) ptr);
oa << boost::serialization::make_nvp(\"${MODEL_SAFE_TYPE}\", ((${MODEL_TYPE}*) ptr);
}
*length = oss.str().length();
return oss.str().data();
// Copy the string buffer so we can return one that won't get deallocated when
// we exit this function. Julia will be responsible for freeing this.
char* buffer = new char[*length];
memcpy(buffer, oss.str().data(), *length);
return buffer;
}
// Deserialize a ${MODEL_TYPE} pointer.
@@ -103,9 +108,10 @@ void* Deserialize${MODEL_SAFE_TYPE}Ptr(const char* buffer, const size_t length)
std::istringstream iss(std::string(buffer, length));
{
boost::archive::binary_iarchive ia(iss);
ia >> boost::serialization::make_nvp(\"${MODEL_SAFE_TYPE}\", *t);
ia >> boost::serialization::make_nvp(\"${MODEL_SAFE_TYPE}\", t);
}
// Julia will be responsible for freeing this.
return (void*) t;
}
")
+4
View File
@@ -73,6 +73,10 @@ if (BUILD_JULIA_BINDINGS)
"${CMAKE_BINARY_DIR}/src/mlpack/bindings/julia/mlpack/src/serialization.jl"
"# This file imports all serialization and deserialization functions\n"
"# from internal modules."
"\n\n"
"# Deserialize an mlpack model type. This passes to other functions.\n"
"function deserialize(t::Type, stream::IO) = "
"_deserialize_internal(t(0), stream))"
"\n\n")
endif ()
@@ -69,8 +69,11 @@ void PrintParamDefn(
// end
//
// function serialize<Type>Ptr(stream::IO, model::<Type>Ptr)
// buf_len = UInt[0]
// buffer = ccall((:Serialize<Type>Ptr, <programName>Library),
// Vector{UInt8}, (Ptr{Nothing},), model.ptr)
// Vector{UInt8}, (Ptr{Nothing}, Ptr{UInt8}), model.ptr,
// Base.pointer(buf_len))
// buf = Base.unsafe_wrap(buf_ptr, buf_len[0]; own=true)
// write(stream, buf)
// end
//
@@ -112,11 +115,11 @@ void PrintParamDefn(
std::cout << "# Serialize a model to the given stream." << std::endl;
std::cout << "function serialize" << type << "Ptr(stream::IO, model::" << type
<< "Ptr)" << std::endl;
std::cout << " buf_len::UInt = 0" << std::endl;
std::cout << " buf_len = UInt[0]" << std::endl;
std::cout << " buf_ptr = ccall((:Serialize" << type << "Ptr, " << programName
<< "Library), Ptr{UInt8}, (Ptr{Nothing}, Ref{UInt}), model.ptr, "
<< "Ref(buf_len))" << std::endl;
std::cout << " buf = Base.unsafe_wrap(buf_ptr, buf_len; own=true)"
<< "Library), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, "
<< "Base.pointer(buf_len))" << std::endl;
std::cout << " buf = Base.unsafe_wrap(buf_ptr, buf_len[0]; own=true)"
<< std::endl;
std::cout << " write(stream, buf)" << std::endl;
std::cout << "end" << std::endl;