如何序列化犰狳的载体 [英] How to serialize armadillo's vector
问题描述
如何序列化arma::Col
?以下是MWE和错误输出.
How can I serialize arma::Col
? Below are a MWE and the error output.
MWE:
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
#include "armadillo"
namespace mpi = boost::mpi;
struct S
{
int i;
arma::Col<double>::fixed<3> cvector;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar& i;
ar& cvector;
}
};
int main()
{
mpi::environment env;
mpi::communicator world;
S s;
if (world.rank() == 0)
{
s.cvector[0] = 2;
s.cvector[1] = 2;
world.send(1, 0, s);
}
else
{
world.recv(0, 0, s);
std::cout << s.cvector[0] << std::endl;
std::cout << s.cvector[1] << std::endl;
}
return 0;
}
错误输出(跳过需要"内容)
error: ‘class arma::Col<double>::fixed<3ull>’ has no member named ‘serialize’; did you mean ‘set_size’?
t.serialize(ar, file_version);
error: ‘class arma::Col<double>::fixed<3ull>’ has no member named ‘serialize’; did you mean ‘set_size’?
t.serialize(ar, file_version);
此帖子似乎与我的问题有关,很遗憾,该问题没有得到答案.
This post seems to be related to my question and unfortunately it is unanswered.
推荐答案
问题的真正症结在于您想向各种Armadillo对象添加serialize()
成员函数,但这似乎并不是可能...除了在Armadillo中巧妙地使用了预处理器之外,确实如此!
The real crux of the issue here is that you want to add a serialize()
member function to the various Armadillo objects, but that doesn't appear to be possible... except that thanks to a clever use of the preprocessor in Armadillo, it is!
看看Mat_bones.hpp
和Col_bones.hpp
...,您会在Mat
和Col
的类定义内看到类似的内容:
Take a look at Mat_bones.hpp
and Col_bones.hpp
... you'll see something like this, inside of the class definitions of Mat
and Col
:
public:
#ifdef ARMA_EXTRA_COL_PROTO
#include ARMA_INCFILE_WRAP(ARMA_EXTRA_COL_PROTO)
#endif
当我发现它时,这让我感到非常高兴,因为现在我可以做一些定义为Mat_extra_bones.hpp
的文件了:
It made me very happy when I found this, because now I can do something like define a file called Mat_extra_bones.hpp
:
//! Add a serialization operator.
template<typename Archive>
void serialize(Archive& ar, const unsigned int version);
,然后Mat_extra_meat.hpp
:
// Add a serialization operator.
template<typename eT>
template<typename Archive>
void Mat<eT>::serialize(Archive& ar, const unsigned int /* version */)
{
using boost::serialization::make_nvp;
using boost::serialization::make_array;
const uword old_n_elem = n_elem;
// This is accurate from Armadillo 3.6.0 onwards.
// We can't use BOOST_SERIALIZATION_NVP() because of the access::rw() call.
ar & make_nvp("n_rows", access::rw(n_rows));
ar & make_nvp("n_cols", access::rw(n_cols));
ar & make_nvp("n_elem", access::rw(n_elem));
ar & make_nvp("vec_state", access::rw(vec_state));
// mem_state will always be 0 on load, so we don't need to save it.
if (Archive::is_loading::value)
{
// Don't free if local memory is being used.
if (mem_state == 0 && mem != NULL && old_n_elem > arma_config::mat_prealloc)
{
memory::release(access::rw(mem));
}
access::rw(mem_state) = 0;
// We also need to allocate the memory we're using.
init_cold();
}
ar & make_array(access::rwp(mem), n_elem);
}
然后,在您的程序中,您所需要做的就是
Then, in your program, all you need to do is
#define ARMA_EXTRA_MAT_PROTO mat_extra_bones.hpp
#define ARMA_EXTRA_MAT_MEAT mat_extra_meat.hpp
和serialize()
函数将是Mat
类的成员.您可以轻松地将此解决方案改编为其他犰狳类型.
and the serialize()
function will be a member of the Mat
class. You can easily adapt this solution for other Armadillo types.
实际上,这正是mlpack库的内容( http://www.mlpack.org/)确实如此,因此,如果您有兴趣,可以仔细看看我在此处实现的确切解决方案:
In fact this is exactly what the mlpack library (http://www.mlpack.org/) does, so if you are interested you can take a closer look at the exact solution I implemented there:
https://github.com/mlpack/mlpack/tree/master/src/mlpack/core/arma_extend
这篇关于如何序列化犰狳的载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!