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 output (skipping "required from" stuff):
error: ‘class arma::Col<double>::fixed<3ull>’ has no member named ‘serialize’; did you mean ‘set_size’?
t.serialize(ar, file_version);
Edit: This post seems to be related to my question and unfortunately it is unanswered.
According to @UKMonkey answer I wrote a working example. Actually for this case there is no need to split
serialize
tosave
andload
.Since arm::Col::fixed doesn't support serialisation itself, you can either write it in your S class, or write a class that wraps it and serialises it. I'd recommend the 2nd option since it will let you use arm::Col::fixed in anything that you want to serialise without repeating.
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!Take a look at
Mat_bones.hpp
andCol_bones.hpp
... you'll see something like this, inside of the class definitions ofMat
andCol
:It made me very happy when I found this, because now I can do something like define a file called
Mat_extra_bones.hpp
:and then
Mat_extra_meat.hpp
:Then, in your program, all you need to do is
and the
serialize()
function will be a member of theMat
class. You can easily adapt this solution for other Armadillo types.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