00001
00008 #ifndef __BINARYSAVE_H_
00009 #define __BINARYSAVE_H_
00010
00011 #include <cstdio>
00012 #include <vector>
00013
00014 #include "tristate.h"
00015 #include "ind_types.h"
00016 #include "kernel.h"
00017
00018
00019
00020
00021
00022
00023
00037 class StateSaver {
00038 public:
00039 virtual ~StateSaver() { }
00040
00042 virtual int read( FILE *infile )=0;
00043
00045 virtual int write( FILE *outfile )=0;
00046
00048 virtual void display_as_text( FILE *infile )=0;
00049 };
00050
00051
00052
00053
00054
00055
00056
00062 #define NEURONS_PER_BUFFER 1
00063 #define parts_per_row ((lm.N)/NEURONS_PER_BUFFER)
00064
00071 #define LF_TOKEN_GRAIN_SIZE 4
00072
00073 #define LFBufferDataType_to_use i32
00074 #define LFBufferDataType_swap_endian i32_swap_endian
00075
00076
00077
00078
00079
00080
00081
00082
00087 class LISSOMBinaryStateSaver : public StateSaver {
00088 public:
00089 virtual ~LISSOMBinaryStateSaver() { }
00090
00091 LISSOMBinaryStateSaver(BasicLissomMap& m) :
00092 tokensWritten(0), tokensRead(0), status(0), errorsEncountered (0),
00093 bigendian(endianness()), lm(m) { }
00094
00096 static void init_hook( void ) { binarysave_init_hook(); }
00097
00098 virtual int read( FILE *infile ) { return binaryWeightsFileRead( infile ); }
00099 virtual int write( FILE *outfile ) { return binaryWeightsFileWrite( outfile ); }
00100 virtual void display_as_text( FILE *infile ) { printTokens( infile ); }
00101
00103 typedef LFBufferDataType_to_use LFBufferDataType;
00104
00105 private:
00106 typedef BasicLissomMap::a_weight a_weight;
00107 typedef BasicLissomMap::l_weight l_weight;
00108
00109
00110
00112 struct LFTokenType {
00113 i32 right;
00114 i32 left;
00115 };
00116
00118 struct LFBuffer {
00119 LFBuffer(const int initial_size=0) : data(initial_size) { }
00120 int start;
00121 int end;
00122 inline int length() const { return (end-start + 1); }
00123 inline void ensure_space_available(const unsigned additional_length) {
00124 const unsigned required_length = length()+additional_length;
00125 if (required_length >= data.size())
00126 data.resize(required_length);
00127 }
00128 std::vector<LFBufferDataType> data;
00129 };
00130
00131
00132 static Tristate save_afferent_weights_only;
00133 static Tristate save_all_lateral_weights;
00134 static Tristate load_afferent_weights_only;
00135
00136
00137 int tokensWritten;
00138 int tokensRead;
00140 LFBuffer binaryBuffer;
00141 int status;
00143 int errorsEncountered;
00144
00146 int bigendian;
00147
00148
00149
00150 static void binarysave_init_hook( void );
00151
00152 int binaryWeightsFileWrite( FILE *file );
00153 int binaryWeightsFileRead( FILE *file );
00154 void printTokens( FILE *file );
00155
00156
00157 LFTokenType makeToken(int name, int length, int infoa, int infob );
00158 int tokenName( LFTokenType token );
00159 int tokenLength( LFTokenType token );
00160 int tokenInfoa( LFTokenType token );
00161 int tokenInfob( LFTokenType token );
00162 void printToken( LFTokenType token, int offset );
00163 const char *tokenNameString(LFTokenType token);
00164
00165 int writeToken(FILE *file, LFTokenType token );
00166 LFTokenType peekToken(FILE *file );
00167 LFTokenType readToken(FILE *file );
00168 LFTokenType readTokenExpecting(int tokenNameExpected, FILE *file);
00169 int writeItem(FILE *file, void *itemptr);
00170 LFBufferDataType readItem(FILE* file);
00171 int writeFloat(FILE *file, f32 aFloat );
00172 f32 readFloat(FILE *file);
00173 int writeInteger(FILE *file, int anInteger );
00174 int readInteger(FILE *file);
00175 void printTokensInBuffer( LFBuffer *buffer );
00176
00177 int binaryWeightsFileReadInArbitraryOrder(FILE *file, LFBuffer *buffer);
00178 int binaryWeightsFileReadInFixedOrder(FILE *file, LFBuffer *buffer);
00179
00180 int binaryWeightsBufferFill( LFBuffer *buffer, int current_map_row,
00181 int current_local_row, int part_of_row );
00182
00183 int binaryWeightsBufferProcess( LFBuffer *buffer, int verify);
00184 int binaryWeightsBufferWriteLatWeights( LFBuffer *buffer, int token_name,
00185 int i, int j, int radius, int array_width,
00186 l_weight *weights);
00187 int binaryWeightsBufferReadLatWeights( LFBuffer *buffer, LFTokenType token,
00188 int i, int j, int radius, int array_width,
00189 l_weight *weights, int verify, const char *description);
00190
00191 void LFBufferGet( LFBuffer *buffer, int pe );
00192 void LFBufferPut( LFBuffer *buffer, int pe );
00193 void LFBufferClear( LFBuffer *buffer );
00194 int LFBufferLength( LFBuffer *buffer );
00195 int LFBufferWrite( LFBuffer *buffer, FILE *file );
00196 int LFBufferRead( LFBuffer *buffer, FILE *file, int length);
00197 int LFBufferWriteToken(LFBuffer *buffer, LFTokenType token );
00198 LFTokenType LFBufferReadToken(LFBuffer *buffer );
00199 int LFBufferWriteItem(LFBuffer *buffer, void *itemptr);
00200 LFBufferDataType LFBufferReadItem(LFBuffer *buffer);
00201 int LFBufferWriteFloat(LFBuffer *buffer, f32 aFloat );
00202 f32 LFBufferReadFloat(LFBuffer *buffer);
00203 int LFBufferWriteInteger(LFBuffer *buffer, int anInteger );
00204 int LFBufferReadInteger( LFBuffer *buffer);
00205 int compareParameter(int weight_file_parameter, int param_file_parameter, const char *description);
00206 int compareOrSetInt(int *destination, int value, int verify, const char *description);
00207
00208 #define compareOrSet_proto(type) \
00209 type compareOrSet_ ## type(type *destination, type value, int verify, const char *description)
00210
00211 compareOrSet_proto(f64);
00212 compareOrSet_proto(a_weight);
00213 compareOrSet_proto(l_weight);
00214
00215 BasicLissomMap& lm;
00216 };
00217
00218
00219
00220 #endif