PetaVision  Alpha
BufferUtilsPvp.hpp
1 #ifndef __BUFFERUTILSPVP_HPP__
2 #define __BUFFERUTILSPVP_HPP__
3 
4 #include "io/FileStream.hpp"
5 #include "structures/Buffer.hpp"
6 #include "structures/SparseList.hpp"
7 
8 #include <string>
9 #include <vector>
10 
11 using std::vector;
12 using std::string;
13 
14 namespace PV {
15 
16 namespace BufferUtils {
17 
21 typedef enum HeaderDataTypeEnum {
22  // Values are hardcoded to ensure consistency between builds.
23  UNRECOGNIZED_DATATYPE = 0,
24  BYTE = 1,
25  INT = 2,
26  FLOAT = 3,
27  // datatype 4 is obsolete;
28  TAUS_UINT4 = 5,
29 } HeaderDataType;
30 
31 // This structure is used to avoid having to traverse
32 // a sparse pvp file from start to finish every time
33 // we want to load data from it.
35  vector<long> frameStartOffsets;
36  vector<long> frameLengths;
37  bool valuesIncluded;
38 };
39 
41  int headerSize, numParams, fileType, nx, ny, nf, numRecords, recordSize, dataSize, dataType,
42  nxProcs, nyProcs, nxExtended, nyExtended, kx0, ky0, nBatch, nBands;
43  double timestamp;
44 };
45 
46 struct WeightHeader {
47  ActivityHeader baseHeader;
48  int nxp, nyp, nfp;
49  float minVal, maxVal;
50  int numPatches;
51 };
52 
53 template <typename T>
54 void writeFrame(FileStream &fStream, Buffer<T> *buffer, double timeStamp);
55 
56 template <typename T>
57 double readFrame(FileStream &fStream, Buffer<T> *buffer);
58 
59 template <typename T>
60 double readFrameWindow(
61  FileStream &fStream,
62  Buffer<T> *buffer,
63  ActivityHeader const &header,
64  int xStart,
65  int yStart,
66  int fStart);
67 
68 template <typename T>
69 BufferUtils::HeaderDataType returnDataType();
70 
71 template <typename T>
72 ActivityHeader buildActivityHeader(int width, int height, int features, int numFrames);
73 
74 template <typename T>
75 ActivityHeader buildSparseActivityHeader(int width, int height, int features, int numFrames);
76 
77 template <typename T>
78 void writeToPvp(const char *fName, Buffer<T> *buffer, double timeStamp, bool verifyWrites = false);
79 
80 template <typename T>
81 void appendToPvp(
82  const char *fName,
83  Buffer<T> *buffer,
84  int frameWriteIndex,
85  double timeStamp,
86  bool verifyWrites = false);
87 
96 template <typename T>
97 double readActivityFromPvp(
98  char const *fName,
99  Buffer<T> *buffer,
100  int frameReadIndex,
101  BufferUtils::SparseFileTable *const sparseFileTable);
102 
107 template <typename T>
108 double readDenseFromPvp(const char *fName, Buffer<T> *buffer, int frameReadIndex);
109 
110 template <typename T>
111 void writeSparseFrame(FileStream &fStream, SparseList<T> *list, double timeStamp);
112 
113 template <typename T>
114 double readSparseFrame(FileStream &fStream, SparseList<T> *list);
115 
116 template <typename T>
117 double readSparseBinaryFrame(FileStream &fStream, SparseList<T> *list, T oneVal);
118 
119 template <typename T>
120 void writeSparseToPvp(
121  const char *fName,
122  SparseList<T> *list,
123  double timeStamp,
124  int width,
125  int height,
126  int features,
127  bool verifyWrites = false);
128 
129 template <typename T>
130 void appendSparseToPvp(
131  const char *fName,
132  SparseList<T> *list,
133  int frameWriteIndex,
134  double timeStamp,
135  bool verifyWrites = false);
136 
137 template <typename T>
138 double readSparseFromPvp(
139  const char *fName,
140  SparseList<T> *list,
141  int frameReadIndex,
142  SparseFileTable *cachedTable = nullptr);
143 
149 template <typename T>
150 double readDenseFromSparsePvp(
151  char const *fName,
152  Buffer<T> *buffer,
153  int frameReadIndex,
154  SparseFileTable *sparseFileTable);
155 
156 template <typename T>
157 double readSparseBinaryFromPvp(
158  const char *fName,
159  SparseList<T> *list,
160  int frameReadIndex,
161  T oneVal,
162  SparseFileTable *cachedTable = nullptr);
163 
169 template <typename T>
170 double readDenseFromSparseBinaryPvp(
171  char const *fName,
172  Buffer<T> *buffer,
173  int frameReadIndex,
174  SparseFileTable *sparseFileTable);
175 
176 static void writeActivityHeader(FileStream &fStream, ActivityHeader const &header);
177 static ActivityHeader readActivityHeader(FileStream &fStream);
178 static SparseFileTable buildSparseFileTable(FileStream &fStream, int upToIndex);
179 
180 template <typename T>
181 std::size_t weightPatchSize(int numWeightsInPatch);
182 
183 std::size_t weightPatchSize(int numWeightsInPatch, bool compressed);
184 
198 WeightHeader buildWeightHeader(
199  bool shared,
200  int preLayerNx,
201  int preLayerNy,
202  int preLayerNf,
203  int preLayerNxExt,
204  int preLayerNyExt,
205  int numArbors,
206  double timestamp,
207  int nxp,
208  int nyp,
209  int nfp,
210  bool compress,
211  float minVal,
212  float maxVal);
213 
233 WeightHeader buildSharedWeightHeader(
234  int nxp,
235  int nyp,
236  int nfp,
237  int numArbors,
238  int numPatchesX,
239  int numPatchesY,
240  int numPatchesF,
241  double timestamp,
242  bool compress,
243  float minVal,
244  float maxVal);
245 
267 WeightHeader buildNonsharedWeightHeader(
268  int nxp,
269  int nyp,
270  int nfp,
271  int numArbors,
272  bool extended,
273  double timestamp,
274  PVLayerLoc const *preLayerLoc,
275  PVLayerLoc const *postLayerLoc,
276  int numColumnProcesses,
277  int numRowProcesses,
278  float minVal,
279  float maxVal,
280  bool compress);
281 
282 void calcNumberOfPatches(
283  PVLayerLoc const *preLayerLoc,
284  PVLayerLoc const *postLayerLoc,
285  int numColumnProcesses,
286  int numRowProcesses,
287  bool extended,
288  int nxp,
289  int nyp,
290  int &numPatchesX,
291  int &numPatchesY,
292  int &numPatchesF,
293  int &numPatchesXExt,
294  int &numPatchesYExt);
295 } // end namespace BufferUtils
296 } // end namespace PV
297 
298 #include "BufferUtilsPvp.tpp"
299 
300 #endif