PetaVision  Alpha
DataStore.hpp
1 /*
2  * DataStore.hpp
3  *
4  * Created on: Sep 10, 2008
5  * Author: Craig Rasmussen
6  */
7 
8 #ifndef DATASTORE_HPP_
9 #define DATASTORE_HPP_
10 
11 #include "include/pv_arch.h"
12 #include "include/pv_types.h"
13 #include "layers/PVLayerCube.hpp"
14 #include "structures/RingBuffer.hpp"
15 #include "structures/SparseList.hpp"
16 #include <cstdlib>
17 #include <cstring>
18 
19 namespace PV {
20 
21 class DataStore {
22  public:
23  DataStore(int numBuffers, int numItems, int numLevels, bool isSparse);
24 
25  virtual ~DataStore() {
26  delete mBuffer;
27  delete mLastUpdateTimes;
28  delete mNumActive;
29  delete mActiveIndices;
30  }
31 
32  int getNumLevels() const { return mNumLevels; }
33  int getNumBuffers() const { return mNumBuffers; }
34  void newLevelIndex() {
35  mBuffer->newLevel();
36  mLastUpdateTimes->newLevel();
37  if (isSparse()) {
38  mNumActive->newLevel();
39  mActiveIndices->newLevel();
40  }
41  mCurrentLevel = (mNumLevels + mCurrentLevel - 1) % mNumLevels;
42  }
43 
44  // Level (delay) spins slower than bufferId (batch element)
45 
46  float *buffer(int bufferId, int level) {
47  return mBuffer->getBuffer(level, bufferId * mNumItems);
48  }
49 
50  float *buffer(int bufferId) { return mBuffer->getBuffer(bufferId * mNumItems); }
51 
52  double getLastUpdateTime(int bufferId, int level) const {
53  return *mLastUpdateTimes->getBuffer(level, bufferId);
54  }
55 
56  double getLastUpdateTime(int bufferId) const { return *mLastUpdateTimes->getBuffer(bufferId); }
57 
58  void setLastUpdateTime(int bufferId, int level, double t) {
59  *mLastUpdateTimes->getBuffer(level, bufferId) = t;
60  }
61 
62  void setLastUpdateTime(int bufferId, double t) { *mLastUpdateTimes->getBuffer(bufferId) = t; }
63 
64  bool isSparse() const { return mSparseFlag; }
65 
66  SparseList<float>::Entry *activeIndicesBuffer(int bufferId, int level) {
67  return mActiveIndices->getBuffer(level, bufferId * mNumItems);
68  }
69 
70  SparseList<float>::Entry *activeIndicesBuffer(int bufferId) {
71  return mActiveIndices->getBuffer(bufferId * mNumItems);
72  }
73 
74  void setNumActive(int bufferId, long numActive) { *mNumActive->getBuffer(bufferId) = numActive; }
75 
76  long *numActiveBuffer(int bufferId, int level) { return mNumActive->getBuffer(level, bufferId); }
77 
78  long *numActiveBuffer(int bufferId) { return mNumActive->getBuffer(bufferId); }
79 
80  void markActiveIndicesOutOfSync(int bufferId, int level);
81 
82  void updateActiveIndices(int bufferId, int level);
83 
84  int getNumItems() const { return mNumItems; }
85 
91  PVLayerCube createCube(PVLayerLoc const &loc, int delay);
92 
93  private:
94  int mNumItems;
95  int mCurrentLevel;
96  int mNumLevels;
97  int mNumBuffers;
98  bool mSparseFlag;
99 
100  RingBuffer<float> *mBuffer = nullptr;
101  RingBuffer<double> *mLastUpdateTimes = nullptr;
102  RingBuffer<long> *mNumActive = nullptr;
103  RingBuffer<SparseList<float>::Entry> *mActiveIndices = nullptr;
104 };
105 
106 } // NAMESPACE
107 
108 #endif /* DATASTORE_HPP_ */
PVLayerCube createCube(PVLayerLoc const &loc, int delay)
Definition: DataStore.cpp:68