mi024

College project "Projet IAD" master 1
git clone https://esimon.eu/repos/mi024.git
Log | Files | Refs | README

nmlp_module_iostream.ipp (6245B)


      1 /**
      2  * @file
      3  * @brief nmlp modules iostream function definitions.
      4  */
      5 
      6 #include <cstddef>
      7 
      8 #include <boost/serialization/vector.hpp>
      9 #include <boost/serialization/shared_ptr.hpp>
     10 #include <nmlp/modules/TensorModule.h>
     11 
     12 #include "nmlp_base_iostream.hpp"
     13 #include "nmlp_module_iostream.hpp"
     14 #include "robber.hpp"
     15 
     16 struct TableModule_modules_robber_tag: Tag_base<TableModule_modules_robber_tag, std::vector<boost::shared_ptr<Module> > TableModule::*>{};
     17 template struct Rob<TableModule_modules_robber_tag, &TableModule::modules>;
     18 
     19 template<class Archive>
     20 void serialize(Archive &ar, TableModule &rhs, unsigned const){
     21 	boost::serialization::void_cast_register<TableModule, Module>();
     22 	ar & rhs.*get(TableModule_modules_robber_tag());
     23 }
     24 
     25 struct SequentialModule_modules_robber_tag: Tag_base<SequentialModule_modules_robber_tag, std::vector<boost::shared_ptr<Module> > SequentialModule::*>{};
     26 template struct Rob<SequentialModule_modules_robber_tag, &SequentialModule::modules>;
     27 
     28 template<class Archive>
     29 void serialize(Archive &ar, SequentialModule &rhs, unsigned const){
     30 	boost::serialization::void_cast_register<SequentialModule, Module>();
     31 	ar & rhs.*get(SequentialModule_modules_robber_tag());
     32 }
     33 
     34 struct CPUUnique_parameters_robber_tag: Tag_base<CPUUnique_parameters_robber_tag, boost::shared_ptr<CPUMatrix> CPUUnique::*>{};
     35 template struct Rob<CPUUnique_parameters_robber_tag, &CPUUnique::parameters>;
     36 template<class Archive>
     37 void save_construct_data(Archive &ar, CPUUnique const *rhs, unsigned const){
     38 	int nc=((const_cast<CPUUnique*>(rhs)->*get(CPUUnique_parameters_robber_tag()))->getNumberOfColumns());
     39 	ar << nc;
     40 }
     41 template<class Archive>
     42 void load_construct_data(Archive &ar, CPUUnique *rhs, unsigned const){
     43 	int c;
     44 	ar >> c;
     45 	new(rhs) CPUUnique(c);
     46 }
     47 template<class Archive>
     48 void serialize(Archive &ar, CPUUnique &rhs, unsigned const){
     49 	boost::serialization::void_cast_register<CPUUnique, Module>();
     50 	ar & (rhs.*get(CPUUnique_parameters_robber_tag()));
     51 }
     52 
     53 struct GPUUnique_parameters_robber_tag: Tag_base<GPUUnique_parameters_robber_tag, boost::shared_ptr<GPUMatrix> GPUUnique::*>{};
     54 template struct Rob<GPUUnique_parameters_robber_tag, &GPUUnique::parameters>;
     55 template<class Archive>
     56 void save_construct_data(Archive &ar, GPUUnique const *rhs, unsigned const){
     57 	int nc=((const_cast<GPUUnique*>(rhs)->*get(GPUUnique_parameters_robber_tag()))->getNumberOfColumns());
     58 	ar << nc;
     59 }
     60 template<class Archive>
     61 void load_construct_data(Archive &ar, GPUUnique *rhs, unsigned const){
     62 	int c;
     63 	ar >> c;
     64 	new(rhs) GPUUnique(c);
     65 }
     66 template<class Archive>
     67 void serialize(Archive &ar, GPUUnique &rhs, unsigned const){
     68 	boost::serialization::void_cast_register<GPUUnique, Module>();
     69 	ar & (rhs.*get(GPUUnique_parameters_robber_tag()));
     70 }
     71 
     72 struct CPUConcatenation_size1_robber_tag: Tag_base<CPUConcatenation_size1_robber_tag, int CPUConcatenation::*>{};
     73 template struct Rob<CPUConcatenation_size1_robber_tag, &CPUConcatenation::size1>;
     74 struct CPUConcatenation_size2_robber_tag: Tag_base<CPUConcatenation_size2_robber_tag, int CPUConcatenation::*>{};
     75 template struct Rob<CPUConcatenation_size2_robber_tag, &CPUConcatenation::size2>;
     76 
     77 template<class Archive>
     78 void save_construct_data(Archive &ar, CPUConcatenation const *rhs, unsigned const){
     79 	ar << (const_cast<CPUConcatenation*>(rhs)->*get(CPUConcatenation_size1_robber_tag())) << (const_cast<CPUConcatenation*>(rhs)->*get(CPUConcatenation_size2_robber_tag()));
     80 }
     81 
     82 template<class Archive>
     83 void load_construct_data(Archive &ar, CPUConcatenation *rhs, unsigned const){
     84 	int size1, size2;
     85 	ar >> size1 >> size2;
     86 	new(rhs) CPUConcatenation(size1, size2);
     87 }
     88 
     89 template<class Archive>
     90 void save(Archive&,CPUConcatenation const&,unsigned const){
     91 	boost::serialization::void_cast_register<CPUConcatenation, Module>();
     92 }
     93 template<class Archive>
     94 void load(Archive&,CPUConcatenation&,unsigned const){
     95 	boost::serialization::void_cast_register<CPUConcatenation, Module>();
     96 }
     97 
     98 struct TensorModule_parameters_robber_tag: Tag_base<TensorModule_parameters_robber_tag, boost::shared_ptr<Matrix> TensorModule::*>{};
     99 template struct Rob<TensorModule_parameters_robber_tag, &TensorModule::parameters>;
    100 
    101 #define DEFINE_FUNCTION_MODULE_LINEAR_IO( NAME ) \
    102 	template<class Archive> \
    103 	void save_construct_data(Archive &ar, NAME const *rhs, unsigned const){ \
    104 		boost::shared_ptr<Matrix> matrix=const_cast<NAME*>(rhs)->*get(TensorModule_parameters_robber_tag()); \
    105 		int row=matrix->getNumberOfRows(), column=matrix->getNumberOfColumns(); \
    106 		ar << row << column; \
    107 	} \
    108 	template<class Archive> \
    109 	void load_construct_data(Archive &ar, NAME *rhs, unsigned const){ \
    110 		int row, column; \
    111 		ar >> row >> column; \
    112 		new(rhs) NAME(row, column); \
    113 	} \
    114 	template<class Archive> \
    115 	void serialize(Archive &ar, NAME &rhs, unsigned const){ \
    116 		boost::serialization::void_cast_register<NAME, Module>(); \
    117 		boost::serialization::void_cast_register<NAME, TensorModule>(); \
    118 		ar & rhs.*get(TensorModule_parameters_robber_tag()); \
    119 	}
    120 
    121 DEFINE_FUNCTION_MODULE_LINEAR_IO(CPUSparseLinear)
    122 DEFINE_FUNCTION_MODULE_LINEAR_IO(CPULinear)
    123 DEFINE_FUNCTION_MODULE_LINEAR_IO(GPULinear)
    124 
    125 #undef DEFINE_FUNCTION_MODULE_LINEAR_IO
    126 
    127 #define DEFINE_FUNCTION_MODULE_IO( NAME ) \
    128 	struct NAME ## _size_robber_tag: Tag_base<NAME ## _size_robber_tag, int NAME::*>{}; \
    129 	template struct Rob<NAME ## _size_robber_tag, &NAME::size>; \
    130 	template<class Archive> \
    131 	void save_construct_data(Archive &ar, NAME const *rhs, unsigned const){ \
    132 		ar << const_cast<NAME*>(rhs)->*get(NAME ## _size_robber_tag()); \
    133 	} \
    134 	template<class Archive> \
    135 	void load_construct_data(Archive &ar, NAME *rhs, unsigned const){ \
    136 		int size; \
    137 		ar >> size; \
    138 		new(rhs) NAME(size); \
    139 	} \
    140 	template<class Archive> \
    141 	void save(Archive&,NAME const&,unsigned const){ \
    142 		boost::serialization::void_cast_register<NAME, Module>(); \
    143 	} \
    144 	template<class Archive> \
    145 	void load(Archive&,NAME&,unsigned const){ \
    146 		boost::serialization::void_cast_register<NAME, Module>(); \
    147 	}
    148 
    149 DEFINE_FUNCTION_MODULE_IO(CPUIdentity)
    150 DEFINE_FUNCTION_MODULE_IO(CPULogistic)
    151 DEFINE_FUNCTION_MODULE_IO(CPUPositiveShrink)
    152 DEFINE_FUNCTION_MODULE_IO(CPUTanH)
    153 DEFINE_FUNCTION_MODULE_IO(GPUIdentity)
    154 DEFINE_FUNCTION_MODULE_IO(GPUPositiveShrink)
    155 DEFINE_FUNCTION_MODULE_IO(GPUTanH)
    156 
    157 #undef DEFINE_FUNCTION_MODULE_IO
    158